-
Notifications
You must be signed in to change notification settings - Fork 12
/
wipe_towers_v02.pl
executable file
·822 lines (732 loc) · 20.1 KB
/
wipe_towers_v02.pl
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#!/usr/bin/perl -i
use strict;
use warnings;
use Math::Round;
use POSIX qw[ceil floor];
use List::Util qw[min max];
use constant PI => 4 * atan2(1, 1);
# printer parameters with default values
my $nozzleDiameter=0.4;
my $filamentDiameter=1.75;
my $extrusionMultiplier=1.0;
my $firstLayerExtrusionMultiplier=4.0;
my $extrusionWidth=$nozzleDiameter;
my $layerHeight=0.2;
my $firstLayerHeight=0.1;
my $retractionLength=5;
my $toolChangeRetractionLength=5;
my $bedWidth=160;
my $bedDepth=165;
my $extruders=2;
# other params
my $travelLift=1;
# wipe tower parameters with default values
my $wipeTowerX=80;
my $wipeTowerY=155;
my $wipeTowerW=10;
my $wipeTowerH=10;
my $wipeTowerSpacing=20;
my $wipeTowerLoops=5;
my $wipeTowerBrimLoops=7;
my $forceToolChanges=1;
# wipe parameters with default values
my $wipeOffset=2;
my $purgeOffset=2;
my $wipeLift=5;
my $purgeAmount=3;
# printing parameters with default parameters, feedrates are multiplied by 60 on import for converting them from mm/s to mm/min
my $retractionFeedrate=75*60;
my $travelFeedrate=150*60;
my $printFeedrate=30*60;
my $extrusionFeedrate=25*60;
# state variables, keeping track of whats happening inside the G-code
my @extruderUsed=(0,0,0,0); # counts how often an extruder is used
my $gcodeF=4500;
my $gcodeActiveExtruder=0;
my @gcodeX=();
my @gcodeY=();
my $gcodeZ=0;
my $lastGcodeZ=0;
my @gcodeE=();
my @gcodeRetraction=();
my $gcodeAbsolutePositioning=0;
# state variables, keeping track of what we're doing
my $scriptActiveExtruder=0;
my $currentF=4500;
my $currentE=0;
my $currentX=0;
my $currentY=0;
my $absolutePositioning=1;
my $absoluteExtrusion=0;
my $line=1;
my $towerLayer=0;
my @scriptRetraction=();
# processing variables
my $layer = 0;
my $start = 0;
my $end = 0;
my @linesByExtruder=();
my @endOfLayerLines=();
initializeBuffer();
##########
# MAIN LOOP
##########
while (<>) {
if($start==0){
readParams($_);
evaluateLine($_);
print;
}elsif($end==1){
print; # just print out everything after the end code marker
}elsif (/^T(\d)/){
evaluateLine($_);
}elsif(/^; next layer/){
# do nothing, strips line by not printing it back
}elsif(/^; tool change/){
# do nothing, strips line by not printing it back
}elsif(/^M204/){
push(@{$linesByExtruder[$gcodeActiveExtruder]},$_); # acceleration changes are sorted into extruder arrays
}elsif(/^G[01]( X(-?\d*\.?\d*))?( Y(-?\d*\.?\d*))?( Z(-?\d*\.?\d*))?( E(-?\d*\.?\d*))?/){ # regular move
if($6){ # move contains z-move, interpreted as layer change if not happening before the start code marker
insertSortedLayer(); # inserts all moves of the current layer, this also inserts the wipe towers on tool chage
evaluateLine($_); # keeps the g-code tracker aligned
print; # copying z-move
print("; next layer\n"); # insert next layer marker because having stripped it before
$layer++; # count layer
}else{
push(@{$linesByExtruder[$gcodeActiveExtruder]},$_); # moves that do not contain z-moves are sorted into the extruder arrays
}
}elsif(/^; end of g-code/){
$end=1;
insertSortedLayer(); # the last layer is not followed by a layer change, thats why we have to insert it here
print;
}else{ # all the other gcodes, such as temperature changes, fan on/off, the config summary, etc.. are shoved to the end of a layer
push(@endOfLayerLines,$_);
}
}
##########
# PRINT TOWER
##########
sub squareTowerEPL{ # returns the gcode for printing a wipe tower
my $e=$_[0];
my $p=$_[1];
my $l=$_[2];
my $x=$wipeTowerX+$e*$wipeTowerSpacing;
my $y=$wipeTowerY;
my $gcode="";
$gcode.=comment("printing square tower with layer height $l");
my $travelPoints=generatePreTravelPointsEN($p,$layer);
$gcode.=lift($travelLift);
$gcode.=travelToXYF($travelPoints->[0]->[0],$travelPoints->[0]->[1],$travelFeedrate);
$gcode.=lower($travelLift);
$gcode.=travelToXYF($travelPoints->[1]->[0],$travelPoints->[1]->[1],$travelFeedrate);
if($layer==0){
$gcode.=comment("printing brim");
for(my $loop=0;$loop<$wipeTowerBrimLoops;$loop++){
my $brimPoints=baseCornerBrimPointsELN($p,$loop,$layer);
$gcode.=travelToXYF($brimPoints->[0]->[0],$brimPoints->[0]->[1],$travelFeedrate);
if($loop==0){
$gcode.=extrudeEF(-$gcodeRetraction[$e], $retractionFeedrate); #$retractionLength
}
for(my $b=1;$b<5;$b++){
$gcode.=extrudeToXYFL($brimPoints->[$b]->[0],$brimPoints->[$b]->[1],$printFeedrate,$l);
}
if($loop==$wipeTowerBrimLoops-1){
$gcode.=extrudeEF($gcodeRetraction[$e], $retractionFeedrate); #-$retractionLength
}
}
}
$gcode.=comment("printing loops");
for(my $loop=0;$loop<$wipeTowerLoops;$loop++){
my $printPoints=baseCornerPointsELN($p,$loop,$layer);
$gcode.=travelToXYF($printPoints->[0]->[0],$printPoints->[0]->[1],$travelFeedrate);
if($loop==0){
$gcode.=extrudeEF(-$gcodeRetraction[$e], $retractionFeedrate); #$retractionLength
}
for(my $p=1;$p<5;$p++){
$gcode.=extrudeToXYFL($printPoints->[$p]->[0],$printPoints->[$p]->[1],$printFeedrate,$l);
}
if($loop==$wipeTowerLoops-1){
$gcode.=extrudeEF($gcodeRetraction[$e], $retractionFeedrate); #-$retractionLength
}
}
return $gcode;
}
##########
# MATH
##########
sub digitize { # cut floats to size
my $num=$_[0];
my $digits=$_[1];
my $factor=10**$digits;
return (round($num*$factor))/$factor;
}
sub dist{ # calculate distances between 2d points
my $x1=$_[0];
my $y1=$_[1];
my $x2=$_[2];
my $y2=$_[3];
return sqrt(($x2-$x1)**2+($y2-$y1)**2);
}
sub extrusionXYXY{ # calculate the extrusion length for a move from (x1,y1) to (x2,y2)
my $x1=$_[0];
my $y1=$_[1];
my $x2=$_[2];
my $y2=$_[3];
my $filamentArea=$filamentDiameter*$filamentDiameter/4*PI;
my $lineLength=dist($x1,$y1,$x2,$y2);
my $eDist=$lineLength*$extrusionWidth/$filamentArea;
if($layer==0){
$eDist*=$firstLayerHeight;
$eDist*=$firstLayerExtrusionMultiplier;
}else{
$eDist*=$layerHeight;
$eDist*=$extrusionMultiplier;
}
return digitize($eDist,4);
}
sub extrusionXYXYL{ # calculate the extrusion length for a move from (x1,y1) to (x2,y2)
my $x1=$_[0];
my $y1=$_[1];
my $x2=$_[2];
my $y2=$_[3];
my $l=$_[4];
my $filamentArea=$filamentDiameter*$filamentDiameter/4*PI;
my $lineLength=dist($x1,$y1,$x2,$y2);
my $eDist=$lineLength*$extrusionWidth/$filamentArea;
$eDist*=$l;
if($layer==0){
$eDist*=$firstLayerExtrusionMultiplier;
}else{
$eDist*=$extrusionMultiplier;
}
return digitize($eDist,4);
}
sub extrusionXY { # calculate the extrusion length for a move from the current extruder position to (x,y)
my $x=$_[0];
my $y=$_[1];
if($absolutePositioning){
return extrusionXYXY($currentX, $currentY, $x, $y);
}else{
return extrusionXYXY(0, 0, $x, $y);
}
}
sub extrusionXYL { # calculate the extrusion length for a move from the current extruder position to (x,y) taking a layer height
my $x=$_[0];
my $y=$_[1];
my $l=$_[2];
if($absolutePositioning){
return extrusionXYXYL($currentX, $currentY, $x, $y, $l);
}else{
return extrusionXYXYL(0, 0, $x, $y, $l);
}
}
sub baseCornerPointsELN{ # calculates the corner points of the wipe tower
my $e=$_[0];
my $l=$_[1];
my $n=$_[2];
my $x=$wipeTowerX+$e*$wipeTowerSpacing;
my $y=$wipeTowerY;
my $extrusionWidthOffset=$l*$extrusionWidth;
my $points=[
[$x-$wipeTowerW/2+$extrusionWidthOffset,$y-$wipeTowerH/2+$extrusionWidthOffset],
[$x-$wipeTowerW/2+$extrusionWidthOffset,$y+$wipeTowerH/2-$extrusionWidthOffset],
[$x+$wipeTowerW/2-$extrusionWidthOffset,$y+$wipeTowerH/2-$extrusionWidthOffset],
[$x+$wipeTowerW/2-$extrusionWidthOffset,$y-$wipeTowerH/2+$extrusionWidthOffset]
];
my $result=[
$points->[$n%4],
$points->[($n+1)%4],
$points->[($n+2)%4],
$points->[($n+3)%4],
$points->[$n%4]
];
return $result;
}
sub baseCornerBrimPointsELN{ # calculates the corner points of the wipe tower
my $e=$_[0]; #extruder
my $l=$_[1]; #loops
my $n=$_[2]; #layer n
my $x=$wipeTowerX+$e*$wipeTowerSpacing;
my $y=$wipeTowerY;
my $extrusionWidthOffset=$l*$extrusionWidth-$wipeTowerBrimLoops*$extrusionWidth;
my $points=[
[$x-$wipeTowerW/2+$extrusionWidthOffset,$y-$wipeTowerH/2+$extrusionWidthOffset],
[$x-$wipeTowerW/2+$extrusionWidthOffset,$y+$wipeTowerH/2-$extrusionWidthOffset],
[$x+$wipeTowerW/2-$extrusionWidthOffset,$y+$wipeTowerH/2-$extrusionWidthOffset],
[$x+$wipeTowerW/2-$extrusionWidthOffset,$y-$wipeTowerH/2+$extrusionWidthOffset]
];
my $result=[
$points->[$n%4],
$points->[($n+1)%4],
$points->[($n+2)%4],
$points->[($n+3)%4],
$points->[$n%4]
];
return $result;
}
sub generatePreTravelPointsEN{ # calculates the travel points for approaching a wipe tower
my $e=$_[0];
my $n=$_[1];
my $x=$wipeTowerX+$e*$wipeTowerSpacing;
my $y=$wipeTowerY;
my $points=[
[
[$x-$wipeTowerW/2-$wipeOffset,$y-$wipeTowerH/2-$wipeOffset],
[$x-$wipeTowerW/2-$wipeOffset,$y-$wipeTowerH/2-$wipeOffset] # duplicate
],
[
[$x-$wipeTowerW/2-$wipeOffset,$y-$wipeTowerH/2-$wipeOffset],
[$x-$wipeTowerW/2-$wipeOffset,$y+$wipeTowerH/2+$wipeOffset]
],
[
[$x+$wipeTowerW/2+$wipeOffset,$y-$wipeTowerH/2-$wipeOffset],
[$x+$wipeTowerW/2+$wipeOffset,$y+$wipeTowerH/2+$wipeOffset]
],
[
[$x+$wipeTowerW/2+$wipeOffset,$y-$wipeTowerH/2-$wipeOffset],
[$x+$wipeTowerW/2+$wipeOffset,$y-$wipeTowerH/2-$wipeOffset] # duplicate
]
];
return $points->[$n%4];
}
sub generatePostTravelPoints{ # calculates the travel points for leaving a wipe tower
my $x=$_[0];
my $y=$_[1];
my $n=$_[2];
my $points=[
[
[$x+$wipeTowerW/2+$wipeOffset,$y+$wipeTowerH/2+$wipeOffset],
[$x+$wipeTowerW/2+$wipeOffset,$y-$wipeTowerH/2-$wipeOffset]
],
[
[$x+$wipeTowerW/2+$wipeOffset,$y-$wipeTowerH/2-$wipeOffset],
[$x+$wipeTowerW/2+$wipeOffset,$y-$wipeTowerH/2-$wipeOffset] # duplicate
],
[
[$x-$wipeTowerW/2-$wipeOffset,$y-$wipeTowerH/2-$wipeOffset],
[$x-$wipeTowerW/2-$wipeOffset,$y-$wipeTowerH/2-$wipeOffset] # duplicate
],
[
[$x-$wipeTowerW/2-$wipeOffset,$y+$wipeTowerH/2+$wipeOffset],
[$x-$wipeTowerW/2-$wipeOffset,$y-$wipeTowerH/2-$wipeOffset]
],
];
return $points->[$n%4];
}
sub generatePurgePosition{
my $x=$_[0];
my $y=$_[1];
my $n=$_[2];
my $positions=[
[$x-$purgeOffset,$y-$purgeOffset],
[$x-$purgeOffset,$y+$purgeOffset],
[$x+$purgeOffset,$y+$purgeOffset],
[$x+$purgeOffset,$y-$purgeOffset]
];
return $positions->[$n%4];
}
##########
# TRAVEL
##########
sub travelToZ{ # appends a trave move
my $z=$_[0];
return "G1 Z".digitize($z,4)."\n";
}
sub travelToXYF{ # appends a trave move
my $x=$_[0];
my $y=$_[1];
my $f=$_[2];
if($absolutePositioning){
$currentX=$x;
$currentY=$y;
}else{
$currentX+=$x;
$currentY+=$y;
}
$currentF=$f;
return "G1 X".digitize($x,4)." Y".digitize($y,4)." F".$f."\n";
}
sub travelToXY{ # appends a trave move
my $x=$_[0];
my $y=$_[1];
if($absolutePositioning){
$currentX=$x;
$currentY=$y;
}else{
$currentX+=$x;
$currentY+=$y;
}
return "G1 X".digitize($x,4)." Y".digitize($y,4)."\n";
}
sub lift{
my $gcode="";
$gcode.=relativePositioning();
$gcode.=travelToZ($_[0]);
$gcode.=absolutePositioning();
return $gcode;
}
sub lower{
my $gcode="";
$gcode.=relativePositioning();
$gcode.=travelToZ(-$_[0]);
$gcode.=absolutePositioning();
return $gcode;
}
##########
# EXTRUDE
##########
sub logScriptExtrusion{ # ($tool,$extrusion) or ($extrusion) for $gcodeActiveExtruder
my $t=$gcodeActiveExtruder;
my $e=0;
if($#_==1){
$e=$_[0];
}elsif($#_==2){
$t=$_[0];
$e=$_[1];
}
$currentE+=$e;
$scriptRetraction[$t]+=$e;
if($scriptRetraction[$t]>0){
$scriptRetraction[$t]=0;
}
}
sub extrudeEF{ # appends an extrusion (=printing) move
my $e=$_[0];
my $f=$_[1];
$currentE+=$e;
if($absoluteExtrusion){
return "G1 E".digitize($currentE,4)." F".digitize($f,4)."\n";
}else{
return "G1 E".digitize($e,4)." F".digitize($f,4)."\n";
}
}
sub extrudeE{ # appends an extrusion (=printing) move
my $e=$_[0];
$currentE+=$e;
if($absoluteExtrusion){
return "G1 E".digitize($currentE,4)."\n";
}else{
return "G1 E".digitize($e,4)."\n";
}
}
sub extrudeToXYF{
my $x=$_[0];
my $y=$_[1];
my $f=$_[2];
my $extrusionLength=extrusionXY($x,$y);
$currentE+=$extrusionLength;
if($absolutePositioning){
$currentX=$x;
$currentY=$y;
}else{
$currentX+=$x;
$currentY+=$y;
}
$currentF=$f;
if($absoluteExtrusion){
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($currentE,4)." F".digitize($f,4)."\n";
}else{
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($extrusionLength,4)." F".digitize($f,4)."\n";
}
}
sub extrudeToXYFL{
my $x=$_[0];
my $y=$_[1];
my $f=$_[2];
my $l=$_[3];
my $extrusionLength=extrusionXYL($x,$y,$l);
$currentE+=$extrusionLength;
if($absolutePositioning){
$currentX=$x;
$currentY=$y;
}else{
$currentX+=$x;
$currentY+=$y;
}
$currentF=$f;
if($absoluteExtrusion){
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($currentE,4)." F".digitize($f,4)."\n";
}else{
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($extrusionLength,4)." F".digitize($f,4)."\n";
}
}
sub extrudeToXY{ # appends an extrusion (=printing) move
my $x=$_[0];
my $y=$_[1];
my $extrusionLength=extrusionXY($x,$y);
$currentE+=$extrusionLength;
if($absolutePositioning){
$currentX=$x;
$currentY=$y;
}else{
$currentX+=$x;
$currentY+=$y;
}
if($absoluteExtrusion){
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($currentE,4)."\n";
}else{
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($extrusionLength,4)."\n";
}
}
sub extrudeToXYL{ # appends an extrusion (=printing) move, respecting the layer height
my $x=$_[0];
my $y=$_[1];
my $l=$_[2];
my $extrusionLength=extrusionXYL($x,$y,$l);
$currentE+=$extrusionLength;
if($absolutePositioning){
$currentX=$x;
$currentY=$y;
}else{
$currentX+=$x;
$currentY+=$y;
}
if($absoluteExtrusion){
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($currentE,4)."\n";
}else{
return "G1 X".digitize($x,4)." Y".digitize($y,4)." E".digitize($extrusionLength,4)."\n";
}
}
##########
# OTHER GCODES
##########
sub absolutePositioning{ # changes coordinate mode and appends the necessary G-code
$absolutePositioning=1;
return "G90 ; set absolute positioning\n";
}
sub relativePositioning{ # changes coordinate mode and appends the necessary G-code
$absolutePositioning=0;
return "G91 ; set relative positioning\n";
}
sub absoluteExtrusion{ # changes extrusion mode and appends the necessary G-code
$absoluteExtrusion=1;
return "M82 ; set extruder to absolute mode\n";
}
sub relativeExtrusion{ # changes extrusion mode and appends the necessary G-code
$absoluteExtrusion=0;
return "M83 ; set extruder to relative mode\n";
}
sub selectExtruder{ # switches the used extruder and appends the necessary G-code, does NOT change $activeExtruder since we want to switch back to $activeExtruder
return "T".$_[0]."\n";
}
sub dwell{ # appends a dwelling G-code with the argument as seconds
return "G4 S".$_[0]."\n";
}
sub comment{ # appends the argument to the currently read G-code line and comments it out with a "; "
return "; ".$_[0]."\n";
}
##########
# PROCESSING
##########
sub evaluateLine{
my $e=$gcodeActiveExtruder;
if($#_==1){
$e=$_[1];
}
if ($_[0]=~/^T(\d)/){
$gcodeActiveExtruder=$1;
}elsif(/^; next layer/){
$start=1;
}elsif(/^G90/){
$gcodeAbsolutePositioning=1;
}elsif(/^G91/){
$gcodeAbsolutePositioning=0;
}elsif(/^G28/){
$lastGcodeZ=0;
$gcodeZ=0;
}elsif(/^G29/){
$lastGcodeZ=0;
$gcodeZ=0;
}elsif($_[0]=~/^G[01]( X(-?\d*\.?\d*))?( Y(-?\d*\.?\d*))?( Z(-?\d*\.?\d*))?( E(-?\d*\.?\d*))?/){
if($2){
if($gcodeAbsolutePositioning){
$gcodeX[$e]=$2;
}else{
$gcodeX[$e]+=$2;
}
}
if($4){
if($gcodeAbsolutePositioning){
$gcodeY[$e]=$4;
}else{
$gcodeY[$e]+=$4;
}
}
if($6){
if($start){
$lastGcodeZ=$gcodeZ;
}
if($gcodeAbsolutePositioning){
$gcodeZ=$6;
}else{
$gcodeZ+=$6;
}
}
if($8){ # keeps track of printing (and retraction) moves for each extruder (only relative extrusion mode)
#$gcodeRetraction[$e]+=$8;
#if($gcodeRetraction[$e]>0){
# $gcodeRetraction[$e]=0;
#}
#VERY BAD CODE MUST BE IGNORED BY READER THANK YOU
if(!$2 && !$4 && !$6){ # must be a retraction move
if($8<0){
$gcodeRetraction[$e]+=$8;
}elsif($8>0){
if($8>-$gcodeRetraction[$e]){
$gcodeRetraction[$e]=0;
}else{
$gcodeRetraction[$e]+=$8;
}
}
}
}
}
}
sub insertSortedLayer{
for(my $e=0;$e<$extruders;$e++){
if($forceToolChanges || $#{$linesByExtruder[$e]}>-1){ # only change the tool to print the tower if the tool is used, otherwise continue with current extruder
# this should be a tool change sub
print("; tool change\n");
print "T".$e."\n";
$scriptActiveExtruder=$e;
}else{
print("; omitted tool change\n"); # printing wipe tower with current extruder, this makes sense if one of the extruders is not used for a while
}
insertWipeTowerEP($scriptActiveExtruder,$e);
for(my $i=0; $i<=$#{$linesByExtruder[$e]};$i++){
evaluateLine($linesByExtruder[$e][$i],$e);
print($linesByExtruder[$e][$i]);
}
@{$linesByExtruder[$e]}=();
}
if($#endOfLayerLines>-1){
print("; end of layer lines\n");
for(my $i=0; $i<=$#endOfLayerLines;$i++){
print($endOfLayerLines[$i]);
}
@endOfLayerLines=();
}
}
sub insertWipeTowerEP{
my $e=$_[0];
my $p=$_[1];
my $l=$gcodeZ-$lastGcodeZ;
if($l>0){
print squareTowerEPL($e,$p,$l);
print lift($travelLift);
print travelToXYF($gcodeX[$e],$gcodeY[$e],$travelFeedrate);
print lower($travelLift);
}else{
print "; omitting wipe tower due to zero layer height\n";
}
}
sub initializeBuffer{
for(my $i=0;$i<$extruders;$i++){
my $travelPoints=generatePreTravelPointsEN($i,$layer);
$linesByExtruder[$i]=();
$gcodeX[$i]=$travelPoints->[0]->[0];
$gcodeY[$i]=$travelPoints->[0]->[1];
$gcodeE[$i]=0;
$gcodeRetraction[$i]=0;
$scriptRetraction[$i]=0;
}
}
sub readParams{ # collecting params
if($_[0]=~/nozzleDiameter=(\d*\.?\d*)/){
$nozzleDiameter=$1*1.0;
}
if($_[0]=~/filamentDiameter=(\d*\.?\d*)/){
$filamentDiameter=$1*1.0;
}
if($_[0]=~/extrusionWidth=(\d*\.?\d*)/){
# Use the specified extrusion width, unless it is set to the "automatic" value of 0 (Slic3r default),
# in which case the pre-initialized value of $nozzleDiameter will be used
unless ( $1 eq "0" ) {
$extrusionWidth=$1*1.0;
}
}
if($_[0]=~/extrusionMultiplier=(\d*\.?\d*)/){
$extrusionMultiplier=$1*1.0;
}
if($_[0]=~/firstLayerExtrusionMultiplier=(\d*\.?\d*)/){
$firstLayerExtrusionMultiplier=$1*1.0;
}
if($_[0]=~/layerHeight=(\d*\.?\d*)/){
$layerHeight=$1*1.0;
}
if($_[0]=~/firstLayerHeight=(\d*\.?\d*)/){
$firstLayerHeight=$1*1.0;
}
if($_[0]=~/retractionLength=(\d*\.?\d*)/){
$retractionLength=$1*1.0;
}
if($_[0]=~/toolChangeRetractionLength=(\d*\.?\d*)/){
$toolChangeRetractionLength=$1*1.0;
}
if($_[0]=~/bedWidth=(\d*\.?\d*)/){
$bedWidth=$1*1.0;
}
if($_[0]=~/bedDepth=(\d*\.?\d*)/){
$bedDepth=$1*1.0;
}
if($_[0]=~/extruders=(\d*\.?\d*)/){
$extruders=$1*1.0;
initializeBuffer();
}
if($_[0]=~/wipeTowerX=(\d*\.?\d*)/){
$wipeTowerX=$1*1.0;
}
if($_[0]=~/wipeTowerY=(\d*\.?\d*)/){
$wipeTowerY=$1*1.0;
}
if($_[0]=~/wipeTowerW=(\d*\.?\d*)/){
$wipeTowerW=$1*1.0;
}
if($_[0]=~/wipeTowerH=(\d*\.?\d*)/){
$wipeTowerH=$1*1.0;
}
if($_[0]=~/wipeTowerSpacing=(\d*\.?\d*)/){
$wipeTowerSpacing=$1*1.0;
}
if($_[0]=~/wipeTowerLoops=(\d*\.?\d*)/){
$wipeTowerLoops=$1*1.0;
}
if($_[0]=~/wipeTowerBrimLoops=(\d*\.?\d*)/){
$wipeTowerBrimLoops=$1*1.0;
}
if($_[0]=~/wipeOffset=(\d*\.?\d*)/){
$wipeOffset=$1*1.0;
}
if($_[0]=~/purgeOffset=(\d*\.?\d*)/){
$purgeOffset=$1*1.0;
}
if($_[0]=~/wipeLift=(\d*\.?\d*)/){
$wipeLift=$1*1.0;
}
if($_[0]=~/travelLift=(\d*\.?\d*)/){
$travelLift=$1*1.0;
}
if($_[0]=~/purgeAmount=(\d*\.?\d*)/){
$purgeAmount=$1*1.0;
}
if($_[0]=~/retractionFeedrate=(\d*\.?\d*)/){
$retractionFeedrate=$1*60.0;
}
if($_[0]=~/travelFeedrate=(\d*\.?\d*)/){
$travelFeedrate=$1*60.0;
}
if($_[0]=~/printFeedrate=(\d*\.?\d*)/){
$printFeedrate=$1*60.0;
}
if($_[0]=~/extrusionFeedrate=(\d*\.?\d*)/){
$extrusionFeedrate=$1*60.0;
}
if($_[0]=~/forceToolChanges=(true|false)/){
if($1 eq "true"){
$forceToolChanges=1;
}elsif($1 eq "false"){
$forceToolChanges=0;
}
}
}