June 03, 2004

HPGL to G Translator Source


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>HPGL to G-Code Translator</title>
<style type="text/css" media="all">
    @import "style1.css";
    span.g0 { color: #00b; }
    span.g1 { color: #070; }
    span.m2 { color: #d00; }
    div.text{ width: 6in; }
    h1 { font-size: 140%; }
</style> 
</head>
<body>
<?php 
$uri = $_SERVER["SCRIPT_URI"];           // where am I?
$self = $_SERVER["PHP_SELF"];            // who am I?
$dec   = $_POST['dec'];                  // how many decimals?
$scale = $_POST['scale'];                // what resolution?
if ($dec < .1 || $dec > 16) {$dec = 5;}  // 5 places if none given, 12 max
if ($scale < 1) {$scale = 1016; }        // standard resolution for HPGL, maybe

$uploaded = $_FILES['my_text']['name'];
if (!$uploaded){
echo '
<h1>HPGL to G-Code Translator</h1>
<div class="text">
This script will translate simple HPGL-1 (as exported via CorelDraw) into basic RS274NGC (G-Code). Only pen-up (PU) and pen-down (PD) commands with two numbers separated by a space and then a line break are translated.
<br /><br />
For example:<br /><br />
PU0 0;<br />
PD0 22;<br />
PD-1 44;<br />
PD-3 65; and so on. 
<br /><br /></div>
<form enctype="multipart/form-data" action="' . $self . '" method="post">
<div>
input resolution: &nbsp;&nbsp;&nbsp;<input type="text" maxlength="5" size="5" name="scale" value="' . $scale . '" />
<br />
number of decimals: <input type="text" maxlength="2" size="3" name="dec" value="' . $dec . '" />
<br /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="40000" />
<input name="my_text" type="file" /> 
<br />
<input type="submit" value="Translate" /> 
</div>
</form>';
}
else
{
echo "<div>( Simple RS274NGC translated from HPGL by $uri )
<br />
( Direct any complaints and suggestions to Hal Eckhart at gff5o6j02@sneakemail.com )
<pre>
";
$dec = "%01." . $dec . "f";                     // set decimal formatting string
$my_text = @file_get_contents($_FILES['my_text']['tmp_name']) ; // grab the file
$my_text = str_replace (                     // add possible missing line breaks
array ("p", "P"), 
array ("\np", "\nP"), $my_text );
$my_text2 = explode ("\n", $my_text);          // break it into bite-size chunks

// Parse, replace HPGL with G, fix resolution, add syntax coloring
while (!is_null($key = key($my_text2) ) ) {
if (preg_match ("/pu *(-?\d+)[ ,]+(-?\d+)/i", $my_text2[$key], $match)) {
echo "\n<span class=\"g0\">G00 x".sprintf("$dec", $match[1]/$scale)." y".sprintf("$dec", $match[2]/$scale)."</span>\n";
}
elseif (preg_match ("/pd *(-?\d+)[ ,]+(-?\d+)/i", $my_text2[$key], $match)) {
echo "<span class=\"g1\">G01 x".sprintf("$dec", $match[1]/$scale)." y".sprintf("$dec", $match[2]/$scale)."</span>\n";
}
next($my_text2);
}
echo "\n\n<span class=\"m2\">M02</span> </pre></div>";
}
?>
</body>
</html>
Posted by Hal Eckhart at June 3, 2004 06:02 PM | TrackBack
Comments
Post a comment
spam and annoying comments will be deleted







Remember personal info?




enter this number security code in the box below (prevents comment spam)