| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # display a GcodeXY generated gcode file on a Tk canvas | ||
| 4 | |||
| 5 | use Tk; | ||
| 6 | use Tk::Button; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | # configurable parameters: | ||
| 11 | my $xres = 1920; # screen resolution | ||
| 12 | my $yres = 1080; | ||
| 13 | my $xsize = 1840; # screen size in pt | ||
| 14 | my $ysize = 956; | ||
| 15 | my $m2i = 1.0; # mm to inch scale factor - 1 inch = 25 mm | ||
| 16 | # set it to 25.0 for gcode that uses mm | ||
| 17 | # we are using inches internally as default | ||
| 18 | my $pdcmd1 = "G00 Z 0.2"; # pen down command - may need tweaking - inch version | ||
| 19 | my $pdcmd2 = "G00 Z 5.0"; # pen down command - may need tweaking - mm version | ||
| 20 | # end configurable parameters | ||
| 21 | |||
| 22 | |||
| 23 | my $pucmd1 = "G00 Z 0"; | ||
| 24 | my $pucmd2 = "G00 Z 0.0"; | ||
| 25 | my $inchcmd = "G20"; # inches used | ||
| 26 | my $mmcmd = "G21"; # mm used | ||
| 27 | my $i2p = 72.0; # inches to points (for canvas translation), normally 72.0 | ||
| 28 | my $geom = $xres . 'x' . $yres; # window size | ||
| 29 | |||
| 30 | # opcodes | ||
| 31 | my $PU = 1; # penup line | ||
| 32 | my $PD = 2; # pendown line | ||
| 33 | my $G00 = 3; # G00 line | ||
| 34 | my $G01 = 4; # G01 line | ||
| 35 | my $NOOP = 5; # ignore this line | ||
| 36 | |||
| 37 | # current line | ||
| 38 | my $op = ""; # current op | ||
| 39 | my $xn = ""; # current x coord | ||
| 40 | my $yn = ""; # current y coord | ||
| 41 | |||
| 42 | # counters | ||
| 43 | my $linecount = 0; # gcode input line count | ||
| 44 | my $emitcount = 0; # ditto for output | ||
| 45 | |||
| 46 | # bounding box | ||
| 47 | my $maxx = 0.0; | ||
| 48 | my $maxy = 0.0; | ||
| 49 | my $minx = 100.0; | ||
| 50 | my $miny = 100.0; | ||
| 51 | |||
| 52 | my ($x, $y); | ||
| 53 | |||
| 54 | # start of main - open files | ||
| 55 | my $gcin = shift || die "input file missing"; | ||
| 56 | open (my $in , '<', $gcin) or die "cannot open input file $gcin"; | ||
| 57 | |||
| 58 | doheader($in); | ||
| 59 | |||
| 60 | # now the GUI | ||
| 61 | my $top = MainWindow->new(); | ||
| 62 | $top->geometry ($geom); | ||
| 63 | my $lf = $top->Frame( -width => 20, -borderwidth => '1', -relief => 'solid')->pack(-side => 'left'); | ||
| 64 | $lf->Button(-text => "quit", -command => sub{exit})->pack; | ||
| 65 | my $canvas = $top->Scrolled('Canvas',width => $xres, height => $yres)->pack(-expand => 1, -fill => 'both'); | ||
| 66 | #my @coords = (0, 0); | ||
| 67 | my @coords = ($xsize, $ysize); # start at the origin | ||
| 68 | |||
| 69 | # now the file | ||
| 70 | while (<$in>) { | ||
| 71 | $linecount++; | ||
| 72 | chomp; | ||
| 73 | ($op, $xn, $yn) = parse($_); # xn and yn in inches | ||
| 74 | # create the lines | ||
| 75 | if ($op == $PU) { | ||
| 76 | @coords = (); | ||
| 77 | } | ||
| 78 | if ($op == $G00) { | ||
| 79 | push @coords, ($xsize-$yn*$i2p), ($ysize-$xn*$i2p); | ||
| 80 | } | ||
| 81 | if ($op == $G01) { | ||
| 82 | if (scalar @coords > 2) { | ||
| 83 | shift @coords; | ||
| 84 | shift @coords; | ||
| 85 | } | ||
| 86 | push @coords, ($xsize-$yn*$i2p), ($ysize-$xn*$i2p); | ||
| 87 | $canvas->createLine(@coords); | ||
| 88 | #printline($_); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | $top->update(); | ||
| 93 | MainLoop(); | ||
| 94 | |||
| 95 | exit; | ||
| 96 | |||
| 97 | ################### end main ################################################ | ||
| 98 | |||
| 99 | #debugging | ||
| 100 | sub printcoords { | ||
| 101 | print "coords at $linecount " . shift . ": "; | ||
| 102 | print join " ", @coords; | ||
| 103 | print "\n"; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub printline { | ||
| 107 | print "canvas line at $linecount " . shift . ": "; | ||
| 108 | print "from (" . $coords[0] . "," . $coords[1] . ") to (" . $coords[2] . "," . $coords[3] . ")\n"; | ||
| 109 | print "\n"; | ||
| 110 | } | ||
| 111 | |||
| 112 | # ignore the header, switch to mm if necessary | ||
| 113 | sub doheader { | ||
| 114 | my $in = shift; | ||
| 115 | # ignore the header, except for the mm/inch part | ||
| 116 | while (<$in>) { | ||
| 117 | $linecount++; | ||
| 118 | #print "header $linecount: $_"; | ||
| 119 | chomp; | ||
| 120 | if ($_ =~ /^G21/) {$m2i = 25.0} # we are using mm | ||
| 121 | last if ($_ eq $pucmd1 || $_ eq $pucmd2); # better make sure it's the last one! | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | # quit with error message and line number | ||
| 126 | sub quit { | ||
| 127 | die shift . " at line $linecount"; | ||
| 128 | } | ||
| 129 | |||
| 130 | # parsing of new instruction | ||
| 131 | sub parse { | ||
| 132 | my ($ss, $opp, $x, $xcoord, $y, $ycoord); | ||
| 133 | $ss = shift; | ||
| 134 | |||
| 135 | # some lines can be ignored | ||
| 136 | if ($ss =~ /^\s*$/) {return ($NOOP, 0,0)} # ignore empty line | ||
| 137 | if ($ss =~ /^\s*\(/) {return ($NOOP, 0,0)} # ignore comment line | ||
| 138 | |||
| 139 | # do some standardization, different tools have different gcode formats | ||
| 140 | $ss =~ s/X/X /; # in case there is no space after X or Y or Z | ||
| 141 | $ss =~ s/Y/Y /; | ||
| 142 | $ss =~ s/Z0/Z 0/; | ||
| 143 | $ss =~ s/G0 /G00 /; # G0 equivalent to G00 | ||
| 144 | $ss =~ s/G1 /G01 /; # G1 equivalent to G01 | ||
| 145 | $ss =~ s/ \.000/ 0\.000/g; # turns .0 into 0.0 | ||
| 146 | |||
| 147 | ($opp, $x, $xcoord, $y, $ycoord) = split(/ +/, $ss); # split on multiple spaces | ||
| 148 | |||
| 149 | if ($ss eq $pucmd1) { | ||
| 150 | return ($PU, "0", "0"); | ||
| 151 | } | ||
| 152 | if ($ss eq $pucmd2) { | ||
| 153 | return ($PU, "0", "0"); | ||
| 154 | } | ||
| 155 | if ($ss =~ /G00 Z / || $ss =~ /G01 Z /) { | ||
| 156 | return ($PD, "0", "0"); | ||
| 157 | } | ||
| 158 | if ($opp eq "G00") { | ||
| 159 | return ($G00, $xcoord/$m2i, $ycoord/$m2i); | ||
| 160 | } | ||
| 161 | if ($opp eq "G01") { | ||
| 162 | return ($G01, $xcoord/$m2i, $ycoord/$m2i); | ||
| 163 | } | ||
| 164 | quit("parse: unknown instruction \"$ss\""); | ||
| 165 | } |