Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Created April 18, 2018 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnapoli/746ee35d38746874e0f39efdc64a0ea6 to your computer and use it in GitHub Desktop.
Save mnapoli/746ee35d38746874e0f39efdc64a0ea6 to your computer and use it in GitHub Desktop.

Usage:

php script.php test.plt

Sample output:

G01 X-7626 Y-392 Z0
G01 X-7642 Y-436 Z0
G01 X-7652 Y-481 Z0
G01 X-7655 Y-526 Z0
G01 X-7652 Y-570 Z0
G01 X-7642 Y-615 Z0
G01 X-7626 Y-659 Z0
G01 X-7604 Y-704 Z0
G01 X-7635 Y-704 Z0
G01 Z1000
<?php
/**
* Invoke the script with a filename to parse:
*
* php script.php test.plt
*/
if (!isset($argv[1])) {
echo 'Please provide a file name an argument, for example:' . PHP_EOL;
echo 'php script.php test.plt' . PHP_EOL;
exit(1);
}
$filename = $argv[1];
if (!file_exists($filename)) {
echo "The file $filename does not exist" . PHP_EOL;
exit(1);
}
// Read the file
$fileContent = file_get_contents($filename);
// Split on the ';' separator
$instructions = explode(';', $fileContent);
foreach ($instructions as $instruction) {
if (empty($instruction) || ($instruction == 'IN')) {
// Ignore empty lines and IN
continue;
}
// 2 first characters
$penAction = substr($instruction, 0, 2);
if ($penAction == 'PU') {
// Pen UP
$penAction = 'Z1000';
} else {
// Pen DOWN
$penAction = 'Z0';
}
$coordinates = substr($instruction, 2);
if (empty($coordinates)) {
// No coordinates?
echo 'G01 ' . $penAction . PHP_EOL;
continue;
}
$coordinates = explode(',', $coordinates);
$x = $coordinates[0];
$y = $coordinates[1];
echo 'G01 X-' . $x . ' Y-' . $y . ' ' . $penAction . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment