-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcode2png
executable file
·353 lines (311 loc) · 10.8 KB
/
gcode2png
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/perl
# == Gcode2PNG, written by Rene K. Mueller <[email protected]>
#
# License: LGPLv3
#
# USAGE: gcode2png test.gcode
#
# Description:
# Convert G-code to PNG (render with Povray)
#
# Supported slicers:
# - Slic3r
# - Prusa-Slicer
# - Cura
#
# History:
# 2022/02/27: 0.1.2: supporting --even or --odd for changing layer color, must have G-code "LAYER: \d+" present
# 2022/01/28: 0.1.1: supporting G2/G3 arc extrusions
# 2021/03/27: 0.1.0: povray timeouts after 90s, --timeout=120 changes it
# 2021/03/17: 0.0.6: better parsing of extrusion (in-place purging)
# 2021/03/16: 0.0.5: --complete=0..1 or 0%..100% added for partial G-code view
# 2021/03/16: 0.0.3: opts and files pre-processed, temp file removed (was disabled)
# 2021/03/15: 0.0.1: based on gcode2scad
use strict;
use Math::Trig;
my $APPNAME = 'gcode2png';
my $VERSION = '0.1.2';
my $conf = {
autolevel => 0,
nozzle => 0.4,
size => '512x512',
grid_size => 10,
grid => 1,
color => '.1,.8,.1',
rotate => '30,0,-20',
dist => 1.0,
timeout => 90
};
help() if(@ARGV==0);
my(@fs);
foreach(@ARGV) { # -- preprocess all arguments first
$conf->{$1}++, next if(/^--(\w+)$/);
$conf->{$1} = $2, next if(/^--(\w+)=(.*)$/);
push(@fs,$_);
}
$conf->{complete} = $conf->{complete} * 1 / 100.0 if(defined $conf->{complete} && $conf->{complete}=~/%/);
foreach my $fn (@fs) {
process($fn);
}
print "$APPNAME $VERSION\n" if($conf->{version});
sub process {
my($fn) = @_;
if(!-e $fn) {
print "$APPNAME ERROR: $fn doesn't exist.\n";
exit -1;
}
my($lx,$ly,$lz,$le,$la,$lb) = (0,0,0,0,0,0);
my($lxe,$lye,$lze) = (0,0,0);
my($x,$y,$z,$e,$a,$b) = (0,0,0,0,0);
my($i,$j) = (0,0);
my($cx,$cy) = (0,0);
my($rx,$ry,$rz) = split(/,/,$conf->{rotate});
my($w,$h) = split(/x/,$conf->{size});
$w = 512 unless($w);
$h = $w unless($h);
my $ds = $w>$h ? $w/$h : $h/$w;
my $gs = $conf->{grid_size} || 10;
my $tot_ln = 0;
my $ln = 0; # -- gcode line
my $lyn = 0; # -- layer no
$ds *= $conf->{dist};
my($zmin,$zmax) = (1e38,-1e38); # -- pre-read G-code to find zmin
if(defined $conf->{complete} || $conf->{autolevel}) {
open(my $fh,"<",$fn);
while(<$fh>) {
#next if(/^\s*;/);
$tot_ln++;
if(/ Z([\-\d\.]+)/) {
my $z = $1;
$zmin = $z if($zmin>$z);
$zmax = $z if($zmax<$z);
}
}
close $fh;
}
if($conf->{autolevel} && $zmin==1e38) {
print STDERR "$APPNAME ERROR: zmin not determined, likely empty or non-existing gcode\n";
exit -1;
}
my $tmp = "/tmp/gcode2png-$$.pov";
my $out = $fn;
$out =~ s/\.gcode$/.png/i;
$out = $conf->{output} if(defined $conf->{output});
open(my $fho,">",$tmp);
open(my $fh,"<",$fn);
print $fho "// created by $APPNAME $VERSION, ".scalar localtime()." from $fn\n";
print $fho "#include \"math.inc\"
#include \"finish.inc\"
#include \"transforms.inc\"
#include \"functions.inc\"
";
my $ra = $conf->{nozzle} / 2;
print $fho "#declare mything = union {\n";
while(<$fh>) {
#next if(/^\s*;/);
last if(defined $conf->{complete} && $ln > $conf->{complete}*$tot_ln);
last if(defined $conf->{complete_layer} && $lyn > $conf->{complete_layer});
$ln++;
if(/^G[01] /) {
$x = $1 - $cx if(/ X([\-\d\.]+)/);
$y = $1 - $cy if(/ Y([\-\d\.]+)/);
$z = $1 - ($conf->{autolevel} ? $zmin : 0) if(/ Z([\-\d\.]+)/);
$a = $1 if(1 && / A([\-\d\.]+)/);
$b = $1 if(1 && / B([\-\d\.]+)/);
my($d) = sqrt(($x-$lx)*($x-$lx)+($y-$ly)*($y-$ly)+($z-$lz)*($z-$lz));
if(/ E([\-\d\.]+)/) { # -- extrusion?
$e = $1;
if($e > $le && $d > 0) { # -- positive extrusion and actual X/Y motion (not just purging in-place)
print $fho "union { sphere {<$lx,$ly,$lz>,$ra} ";
print $fho "cylinder{<$lx,$ly,$lz>,<$x,$y,$z>,$ra} " if($lx!=$x||$ly!=$y||$lz!=$z);
if(($conf->{even}||$conf->{odd})&&($lyn%2)==0) {
print $fho "texture { pigment { color <$conf->{color}>*.8 } } ";
}
print $fho "}\n";
$lxe = $x;
$lye = $y;
$lze = $z;
}
}
$lx = $x;
$ly = $y;
$lz = $z;
$le = $e;
$la = $a;
$lb = $b;
} elsif(/^G([23]) /) {
my $dir = $1 eq '2' ? -1 : 1; # -- angle-wise
$x = $1 - $cx if(/ X([\-\d\.]+)/);
$y = $1 - $cy if(/ Y([\-\d\.]+)/);
$z = $1 - ($conf->{autolevel} ? $zmin : 0) if(/ Z([\-\d\.]+)/);
$a = $1 if(1 && / A([\-\d\.]+)/);
$b = $1 if(1 && / B([\-\d\.]+)/);
$i = $1 if(/ I([\-\d\.]+)/);
$j = $1 if(/ J([\-\d\.]+)/);
my $xp = $lx + $i;
my $yp = $ly + $j;
my $x_ = $x;
my $y_ = $y;
#my($d) = sqrt(($x-$lx)*($x-$lx)+($y-$ly)*($y-$ly)+($z-$lz)*($z-$lz));
if(/ E([\-\d\.]+)/) { # -- extrusion?
$e = $1;
if($e > $le) { # -- positive extrusion and actual X/Y motion (not just purging in-place)
my $as = atan2(-$j,-$i);
my $ae = atan2($y-$yp,$x-$xp);
my $da = sqrt($i**2 + $j**2);
my $s = .5; # -- segment length
my $al;
if($dir > 0) {
# -- ccw (a++)
$as = $as > $ae ? $as - 2*pi() : $as;
$al = abs($ae - $as) * $dir;
} else {
# -- cw (a--)
$as = $as < $ae ? $as + 2*pi() : $as;
$al = abs($as - $ae) * $dir;
}
my $n = int(abs($al)*$da/$s);
#print("$lx,$ly,$lz -> $x,$y,$z ($i,$j), d=$da, n=$n ($dir), al=$al\n");
#print $fho "sphere {<$lx,$ly,$lz>,$ra}\n";
for($i=1; $i<=$n; $i++) {
my $f = $i/$n;
my $a = $as + $al*$f;
$x = $xp + cos($a) * $da;
$y = $yp + sin($a) * $da;
$z = $lz;
#print(" $x,$y,$z ($i of $n)\n");
#print "$lx,$ly,$lz -> $x_,$y_,$z_ ($ra)\n";
print $fho "sphere {<$lx,$ly,$lz>,$ra}\n";
print $fho "cylinder{<$lx,$ly,$lz>,<$x,$y,$z>,$ra}\n" if($lx!=$x||$ly!=$y||$lz!=$z);
$lx = $x;
$ly = $y;
$lz = $z;
}
$lx = $x;
$ly = $y;
$lz = $z;
#$lx = $x_;
#$ly = $y_;
#print($lx-$x_," ",$ly-$y_,"\n");
#print sprintf("\tERROR: x=%.2f%% y=%.2f%%\n",100-100/$lx*$x_,100-100/$ly*$y_) if(abs($lx-$x_)+abs($ly-$y_)>0.1);
#print $fho "sphere {<$lx,$ly,$lz>,$ra}\n";
$lxe = $lx;
$lye = $ly;
$lze = $lz;
}
}
$lx = $x;
$ly = $y;
$lz = $z;
$le = $e;
$la = $a;
$lb = $b;
} elsif(/^G92 E0/) { # -- track reset of extrusion measurement
$le = 0;
} elsif(/^;\s*LAYER:\s*(\d+)/) {
$lyn = $1*1;
}
}
print $fho "sphere {<$lxe,$lye,$lze>,$ra}\n";
print $fho "}\n";
print $fho "light_source {
<-500,-500,400>
rgb 1
//shadowless
parallel
//area_light <5,0,0> <0,0,5> 40,40 adaptive 0 jitter
}
light_source {
<-100,500,0>
rgb 1
//shadowless
parallel
}
global_settings {
assumed_gamma 2
}
#declare Min = min_extent(mything);
#declare Max = max_extent(mything);
#declare bottom_diag = sqrt(pow(Max.y - Min.y, 2) + pow(Max.x - Min.x, 2));
#debug concat(\"bottom_diag:\", str(bottom_diag, 5, 0))
#declare box_diag = sqrt(pow(bottom_diag, 2) + pow(Max.z - Min.z, 2));
#debug concat(\"box_diag:\", str(box_diag, 5, 0))
camera {
location <0,box_diag*2*$ds,0>
rotate <$rx,$ry,$rz+180>
//rotate <0,0,180>
up z
angle 30
look_at <0,0,0>
right x*$w/$h
}
object {
mything
Center_Trans(mything, x+y+z)
//rotate <$rx,$ry,$rz> // rather rotate camera
texture {
pigment { color <$conf->{color}> }
finish { phong 0.8 diffuse 0.8 ambient 0.25 reflection {0.003, 0.005} specular 0.2 }
}
no_shadow
}";
print $fho "#macro Raster(RScale, RLine)
pigment{
gradient x scale RScale
color_map{
[0.000 color rgb<1,1,1>*.5]
[0+RLine color rgb<1,1,1>*.5]
[0+RLine color rgbt<1,1,1,1>]
[1-RLine color rgbt<1,1,1,1>]
[1-RLine color rgb<1,1,1>*.5]
[1.000 color rgb<1,1,1>*.5]
}
} // end of pigment
#end
#macro Grid(RasterScale, RasterHalfLine, Background_pigment)
plane{<0,1,0>, -(Max.z-Min.z)/2
texture{ Background_pigment } // base color
texture{ Raster(RasterScale, RasterHalfLine) } // 2nd layer
texture{ Raster(RasterScale, RasterHalfLine) rotate<0,90,0> } // 3rd layer
texture{ Raster(RasterScale*0.1, RasterHalfLine*2) } // fine-grained 4st layer
texture{ Raster(RasterScale*0.1, RasterHalfLine*2) rotate<0,90,0> } // fine-grained 5th layer
} // end of plane
#end
object { Grid($gs,0.01,pigment{color rgb<1,1,1,1>*2}) rotate <90,0,0> }
" if($conf->{grid});
close $fh;
if(fork()==0) {
close STDOUT;
close STDERR;
exec("timeout",$conf->{timeout},"povray","-d","-v","-i$tmp","+FN","+W$w","+H$h","-o\"$out\"","+Q11","+AM1","+A","+UA","+R5");
}
wait;
my $err = $? >> 8;
print "$APPNAME: ERROR: $err in povray, abort\n" if($err);
unlink $tmp unless($conf->{keep});
}
sub help() {
print "USAGE $APPNAME $VERSION: [<opts>] <file.gcode> ...
options:
--version print version and exit
--autolevel level Z minimum to 0 (default: off)
--output=<fn> override .gcode -> .png conversion
--size=<w>x<h> set size of image (default: $conf->{size})
--rotate=<x>,<y>,<z> rotate model (default: $conf->{rotate})
--dist=<d> set distance multiplier (default: $conf->{dist})
--color=<r>,<g>,<b> set color (default: $conf->{color})
--grid=0 or 1 set grid (default: $conf->{grid})
--grid_size=<s> set grid size (default: $conf->{grid_size})
--nozzle=<d> set nozzle diameter (default: $conf->{nozzle})
--complete=<i> set completeness: 0..1 or 0%..100%
--complete_layer=<n> set last complete layer: 0..n
--even or --odd set alternate color to distinct layers
--timeout=<s> set timeout of povray (default: $conf->{timeout})
examples:
gcode2png gcube.gcode
gcode2png --output=cube-normal.png cube.gcode
gcode2png --color=1,0,0 3DBenchy.gcode
gcode2png --complete=50% 3DBenchy.gcode
";
exit 0;
}