-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoders.coffee
More file actions
1202 lines (769 loc) · 42.4 KB
/
coders.coffee
File metadata and controls
1202 lines (769 loc) · 42.4 KB
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# G-code generation methods for Polyslice.
polyconvert = require('@jgphilpott/polyconvert')
module.exports =
# Helper method to format a numeric value to specified decimal precision.
# Removes trailing zeros and unnecessary decimal points.
formatPrecision: (value, decimals) ->
if typeof value isnt "number" or isNaN(value)
return value
# Round to specified decimal places.
factor = Math.pow(10, decimals)
rounded = Math.round(value * factor) / factor
# Convert to string with fixed decimals, then remove trailing zeros.
formatted = rounded.toFixed(decimals)
# Remove trailing zeros after decimal point, but keep at least "0" for zero values.
if formatted.includes('.')
formatted = formatted.replace(/\.?0+$/, '')
# Handle edge case where formatted becomes empty string (e.g., 0.0 with 0 decimals).
if formatted is '' or formatted is '-'
formatted = '0'
return formatted
# Calculate estimated print time from G-code.
# Parses G-code commands to estimate total print time based on movement distances and feedrates.
calculatePrintTime: (slicer) ->
totalTime = 0 # Total time in seconds.
lines = slicer.gcode.split(slicer.newline)
currentX = 0
currentY = 0
currentZ = 0
currentFeedrate = null # Feedrate in mm/min.
# Track positioning mode (true = absolute G90, false = relative G91)
isAbsolutePositioning = true
for line in lines
# Skip empty lines and comments.
line = line.trim()
continue if line is '' or line.startsWith(';')
# Extract command and parameters.
parts = line.split(/\s+/)
command = parts[0]
# Track positioning mode changes
if command is 'G90'
isAbsolutePositioning = true
continue
else if command is 'G91'
isAbsolutePositioning = false
continue
# Process movement commands (G0, G1, G2, G3).
if command in ['G0', 'G1', 'G2', 'G3']
# Extract coordinates and feedrate from the command.
newX = currentX
newY = currentY
newZ = currentZ
newFeedrate = currentFeedrate
# Arc parameters (for G2/G3)
arcI = null
arcJ = null
arcR = null
# Track if coordinates were specified
hasX = false
hasY = false
hasZ = false
xValue = 0
yValue = 0
zValue = 0
for part in parts[1..]
if part.startsWith('X')
hasX = true
xValue = parseFloat(part.substring(1))
else if part.startsWith('Y')
hasY = true
yValue = parseFloat(part.substring(1))
else if part.startsWith('Z')
hasZ = true
zValue = parseFloat(part.substring(1))
else if part.startsWith('F')
newFeedrate = parseFloat(part.substring(1))
else if part.startsWith('I')
arcI = parseFloat(part.substring(1))
else if part.startsWith('J')
arcJ = parseFloat(part.substring(1))
else if part.startsWith('R')
arcR = parseFloat(part.substring(1))
# Apply positioning mode to coordinates
if isAbsolutePositioning
# Absolute mode: coordinates are absolute positions
newX = xValue if hasX
newY = yValue if hasY
newZ = zValue if hasZ
else
# Relative mode: coordinates are offsets from current position
newX = currentX + xValue if hasX
newY = currentY + yValue if hasY
newZ = currentZ + zValue if hasZ
# Calculate distance moved.
distance = 0
# For arc movements (G2/G3), calculate arc length
if (command is 'G2' or command is 'G3') and (arcI? or arcJ? or arcR?)
# Calculate arc length using the arc parameters
if arcR?
# R format: radius is given directly
radius = Math.abs(arcR)
else if arcI? or arcJ?
# I/J format: center offset from start point
i = arcI ? 0
j = arcJ ? 0
radius = Math.sqrt(i * i + j * j)
if radius > 0
# Calculate the chord length (straight line distance)
dx = newX - currentX
dy = newY - currentY
dz = newZ - currentZ
chordLength = Math.sqrt(dx * dx + dy * dy)
# Calculate the angle subtended by the arc
# Using the formula: angle = 2 * arcsin(chord / (2 * radius))
if chordLength < 2 * radius
angle = 2 * Math.asin(chordLength / (2 * radius))
# Arc length = radius * angle
arcLength = radius * angle
# Include Z component if present (helical arc)
if dz isnt 0
distance = Math.sqrt(arcLength * arcLength + dz * dz)
else
distance = arcLength
else
# Chord is too long for the radius (degenerate case), use straight line
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
else
# Invalid radius, fall back to straight line
dx = newX - currentX
dy = newY - currentY
dz = newZ - currentZ
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
else
# Linear movement (G0/G1) or arc without parameters
dx = newX - currentX
dy = newY - currentY
dz = newZ - currentZ
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
# Calculate time for this move if we have a feedrate.
if distance > 0 and newFeedrate isnt null and newFeedrate > 0
# Feedrate is in mm/min, convert to mm/s then calculate time.
speedMmPerSec = newFeedrate / 60
moveTime = distance / speedMmPerSec
totalTime += moveTime
# Update current position and feedrate.
currentX = newX
currentY = newY
currentZ = newZ
currentFeedrate = newFeedrate
# Process dwell commands (G4).
else if command is 'G4'
for part in parts[1..]
if part.startsWith('P')
# P parameter is in milliseconds.
dwellTime = parseFloat(part.substring(1)) / 1000
totalTime += dwellTime
else if part.startsWith('S')
# S parameter is in seconds.
dwellTime = parseFloat(part.substring(1))
totalTime += dwellTime
# Process heating commands (M109, M190) - add estimated heating time.
else if command in ['M109', 'M190']
# Estimate heating time: ~30 seconds for nozzle, ~60 seconds for bed.
if command is 'M109'
totalTime += 30
else if command is 'M190'
totalTime += 60
return totalTime
# Format time in seconds to HH:MM:SS format.
formatTime: (timeInSeconds) ->
# Handle negative or invalid time values.
if typeof timeInSeconds isnt 'number' or isNaN(timeInSeconds) or timeInSeconds < 0
return "00:00:00"
hours = Math.floor(timeInSeconds / 3600)
minutes = Math.floor((timeInSeconds % 3600) / 60)
seconds = Math.floor(timeInSeconds % 60)
# Pad with zeros for consistent formatting.
hoursStr = String(hours).padStart(2, '0')
minutesStr = String(minutes).padStart(2, '0')
secondsStr = String(seconds).padStart(2, '0')
return "#{hoursStr}:#{minutesStr}:#{secondsStr}"
# https://marlinfw.org/docs/gcode/G028.html
# Generate autohome G-code command.
codeAutohome: (slicer, x = null, y = null, z = null, skip = null, raise = null, leveling = null) ->
gcode = "G28"
if x then gcode += " X"
if y then gcode += " Y"
if z then gcode += " Z"
if skip then gcode += " O"
if leveling then gcode += " L"
if typeof raise is "number" then gcode += " R" + raise
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/G017-G019.html
# Set workspace plane for coordinate system interpretation.
codeWorkspacePlane: (slicer, plane = null) ->
if plane is null
plane = slicer.getWorkspacePlane()
if plane is "XY"
return "G17" + slicer.newline
if plane is "XZ"
return "G18" + slicer.newline
if plane is "YZ"
return "G19" + slicer.newline
# https://marlinfw.org/docs/gcode/G020-G021.html
# Set length units for coordinate measurements.
codeLengthUnit: (slicer, unit = null) ->
if unit is null
unit = slicer.getLengthUnit()
if unit is "millimeters"
return "G21" + slicer.newline
if unit is "inches"
return "G20" + slicer.newline
# https://marlinfw.org/docs/gcode/M149.html
# Set temperature units for thermal measurements.
codeTemperatureUnit: (slicer, unit = null) ->
if unit is null
unit = slicer.getTemperatureUnit()
if unit is "celsius"
return "M149 C" + slicer.newline
if unit is "fahrenheit"
return "M149 F" + slicer.newline
if unit is "kelvin"
return "M149 K" + slicer.newline
# Helper method to build movement parameter strings.
codeMovement: (slicer, x = null, y = null, z = null, extrude = null, feedrate = null, power = null) ->
gcode = ""
# Get precision settings from slicer.
coordPrecision = slicer.getCoordinatePrecision()
extrusionPrecision = slicer.getExtrusionPrecision()
feedratePrecision = slicer.getFeedratePrecision()
if typeof x is "number"
gcode += " X" + module.exports.formatPrecision(x, coordPrecision)
if typeof y is "number"
gcode += " Y" + module.exports.formatPrecision(y, coordPrecision)
if typeof z is "number"
gcode += " Z" + module.exports.formatPrecision(z, coordPrecision)
if typeof extrude is "number"
gcode += " E" + module.exports.formatPrecision(extrude, extrusionPrecision)
if typeof feedrate is "number"
gcode += " F" + module.exports.formatPrecision(feedrate, feedratePrecision)
if typeof power is "number"
gcode += " S" + power
return gcode
# https://marlinfw.org/docs/gcode/G090-G091.html
# Set positioning mode (absolute or relative).
codePositioningMode: (slicer, absolute = true) ->
if absolute
return "G90" + slicer.newline
else
return "G91" + slicer.newline
# https://marlinfw.org/docs/gcode/M082-M083.html
# Set extruder mode (absolute or relative).
codeExtruderMode: (slicer, absolute = true) ->
if absolute
return "M82" + slicer.newline
else
return "M83" + slicer.newline
# https://marlinfw.org/docs/gcode/G092.html
# Set position of axes or extruder.
codeSetPosition: (slicer, x = null, y = null, z = null, extrude = null) ->
gcode = "G92"
if typeof x is "number"
gcode += " X" + x
if typeof y is "number"
gcode += " Y" + y
if typeof z is "number"
gcode += " Z" + z
if typeof extrude is "number"
gcode += " E" + extrude
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M084.html
# Disable steppers.
codeDisableSteppers: (slicer, x = false, y = false, z = false, e = false) ->
gcode = "M84"
if x then gcode += " X"
if y then gcode += " Y"
if z then gcode += " Z"
if e then gcode += " E"
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/G000-G001.html
# Generate linear movement G-code command.
# NOTE: feedrate parameter should be in mm/min (G-code format), not mm/s.
codeLinearMovement: (slicer, x = null, y = null, z = null, extrude = null, feedrate = null, power = null) ->
if not extrude then gcode = "G0" else gcode = "G1"
gcode += module.exports.codeMovement slicer, x, y, z, extrude, feedrate, power
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/G002-G003.html
# Generate arc movement G-code command.
codeArcMovement: (slicer, direction = "clockwise", x = null, y = null, z = null, extrude = null, feedrate = null, power = null, xOffset = null, yOffset = null, radius = null, circles = null) ->
if direction is "clockwise"
gcode = "G2"
else if direction is "counterclockwise"
gcode = "G3"
else
console.error "Invalid direction: #{direction}"
return ""
gcode += module.exports.codeMovement slicer, x, y, z, extrude, feedrate, power
# Get coordinate precision for arc offsets.
coordPrecision = slicer.getCoordinatePrecision()
if typeof xOffset is "number"
gcode += " I" + module.exports.formatPrecision(xOffset, coordPrecision)
if typeof yOffset is "number"
gcode += " J" + module.exports.formatPrecision(yOffset, coordPrecision)
if typeof radius is "number"
gcode += " R" + module.exports.formatPrecision(radius, coordPrecision)
if typeof circles is "number"
gcode += " P" + circles
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/G005.html
# Generate Bézier curve movement G-code commands.
codeBézierMovement: (slicer, controlPoints = []) ->
gcode = ""
for controlPoint, index in controlPoints
if typeof controlPoint.xOffsetEnd is "number" and typeof controlPoint.yOffsetEnd is "number"
if index is 0 and (typeof controlPoint.xOffsetStart isnt "number" or typeof controlPoint.yOffsetStart isnt "number")
console.error "First Bézier control point must have start offsets"
else
gcode += "G5"
if index is 0
gcode += " I" + controlPoint.xOffsetStart
gcode += " J" + controlPoint.yOffsetStart
if typeof controlPoint.xOffsetStart is "number" and typeof controlPoint.yOffsetStart is "number"
gcode += " I" + controlPoint.xOffsetStart
gcode += " J" + controlPoint.yOffsetStart
gcode += " P" + controlPoint.xOffsetEnd
gcode += " Q" + controlPoint.yOffsetEnd
gcode += slicer.newline
else
console.error "Invalid Bézier Movement Parameters"
return gcode
# https://marlinfw.org/docs/gcode/M114.html
# https://marlinfw.org/docs/gcode/M154.html
# Generate position reporting G-code commands.
codePositionReport: (slicer, auto = true, interval = 1, real = false, detail = false, extruder = false) ->
if auto
gcode = "M154"
if typeof interval is "number" and interval >= 0
if slicer.getTimeUnit() is "milliseconds"
interval /= 1000
gcode += " S" + interval
else
gcode = "M114"
if real then gcode += " R"
if detail then gcode += " D"
if extruder then gcode += " E"
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M104.html
# https://marlinfw.org/docs/gcode/M109.html
# Generate nozzle temperature control G-code commands.
codeNozzleTemperature: (slicer, temp = null, wait = true, index = null) ->
if temp is null
temp = slicer.getNozzleTemperature()
if wait
gcode = "M109"
if typeof temp is "number" and temp >= 0
gcode += " R" + temp
if typeof index is "number"
gcode += " T" + index
else
gcode = "M104"
if typeof temp is "number" and temp >= 0
gcode += " S" + temp
if typeof index is "number"
gcode += " T" + index
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M140.html
# https://marlinfw.org/docs/gcode/M190.html
# Generate bed temperature control G-code commands.
codeBedTemperature: (slicer, temp = null, wait = true, time = null) ->
if temp is null
temp = slicer.getBedTemperature()
if wait
gcode = "M190"
if typeof temp is "number" and temp >= 0
gcode += " R" + temp
if typeof time is "number" and time > 0
gcode += " T" + time
else
gcode = "M140"
if typeof temp is "number" and temp >= 0
gcode += " S" + temp
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M105.html
# https://marlinfw.org/docs/gcode/M155.html
# Generate temperature reporting G-code commands.
codeTemperatureReport: (slicer, auto = true, interval = 1, index = null, sensor = null) ->
if auto
gcode = "M155"
if typeof interval is "number" and interval >= 0
if slicer.getTimeUnit() is "milliseconds"
interval /= 1000
gcode += " S" + interval
else
gcode = "M105"
if typeof index is "number"
gcode += " T" + index
if typeof sensor is "number"
gcode += " R" + sensor
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M106.html
# https://marlinfw.org/docs/gcode/M107.html
# Generate fan speed control G-code commands.
codeFanSpeed: (slicer, speed = null, index = null) ->
if speed is null
speed = slicer.getFanSpeed()
if speed > 0
gcode = "M106" + " S" + Math.round(speed * 2.55)
else
gcode = "M107"
if typeof index is "number"
gcode += " P" + index
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M123.html
# Generate fan status reporting G-code commands.
codeFanReport: (slicer, auto = true, interval = 1) ->
gcode = "M123"
if auto and typeof interval is "number" and interval >= 0
if slicer.getTimeUnit() is "milliseconds"
interval /= 1000
gcode += " S" + interval
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/G004.html
# https://marlinfw.org/docs/gcode/M000-M001.html
# Generate pause/dwell G-code commands.
codeDwell: (slicer, time = null, interruptible = true, message = "") ->
if interruptible then gcode = "M0" else gcode = "G4"
if typeof time is "number" and time > 0
if slicer.getTimeUnit() is "milliseconds" then gcode += " P" + time
if slicer.getTimeUnit() is "seconds" then gcode += " S" + time
if message and typeof message is "string"
gcode += " " + message
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M108.html
# Generate emergency interrupt G-code command.
codeInterrupt: (slicer) ->
return "M108" + slicer.newline
# https://marlinfw.org/docs/gcode/M400.html
# Generate wait for moves completion G-code command.
codeWait: (slicer) ->
return "M400" + slicer.newline
# https://marlinfw.org/docs/gcode/M300.html
# Generate buzzer/tone G-code command.
codeTone: (slicer, duration = 1, frequency = 500) ->
gcode = "M300"
if typeof duration is "number" and duration > 0
if slicer.getTimeUnit() is "milliseconds"
gcode += " P" + duration
if slicer.getTimeUnit() is "seconds"
gcode += " P" + (duration * 1000)
if typeof frequency is "number" and frequency > 0
gcode += " S" + frequency
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M117.html
# https://marlinfw.org/docs/gcode/M118.html
# Generate display message G-code command.
codeMessage: (slicer, message = "") ->
return "M117 " + message + slicer.newline
# https://marlinfw.org/docs/gcode/M112.html
# Generate emergency shutdown G-code command.
codeShutdown: (slicer) ->
return "M112" + slicer.newline
# https://marlinfw.org/docs/gcode/M115.html
# Generate firmware info request G-code command.
codeFirmwareReport: (slicer) ->
return "M115" + slicer.newline
# https://marlinfw.org/docs/gcode/M027.html
# Generate SD card status reporting G-code commands.
codeSDReport: (slicer, auto = true, interval = 1, name = false) ->
gcode = "M27"
if name then gcode += " C"
if auto and typeof interval is "number" and interval >= 0
if slicer.getTimeUnit() is "milliseconds"
interval /= 1000
gcode += " S" + interval
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/M073.html
# Generate print progress reporting G-code commands.
codeProgressReport: (slicer, percent = null, time = null) ->
gcode = "M73"
if typeof percent is "number" and percent >= 0
gcode += " P" + percent
if typeof time is "number" and time >= 0
if slicer.getTimeUnit() is "milliseconds"
time /= 60000
else if slicer.getTimeUnit() is "seconds"
time /= 60
gcode += " R" + time
return gcode + slicer.newline
# https://marlinfw.org/docs/gcode/G010-G011.html
# Generate retraction G-code using the configured retraction settings.
codeRetract: (slicer, distance = null, speed = null) ->
retractDistance = if distance isnt null then distance else slicer.retractionDistance # Use internal storage (mm)
retractSpeed = if speed isnt null then speed else slicer.retractionSpeed # Use internal storage (mm/s)
if retractDistance <= 0
return "" # No retraction needed
gcode = "G1"
gcode += " E-" + retractDistance # E is always in mm for G-code
if retractSpeed > 0
# Convert mm/s to mm/min for F parameter using polyconvert
feedratePerMin = Math.round(polyconvert.speed.millimeterSecond.millimeterMinute(retractSpeed))
gcode += " F" + feedratePerMin
return gcode + slicer.newline
# Generate unretract/prime G-code using configured settings.
codeUnretract: (slicer, distance = null, speed = null) ->
retractDistance = if distance isnt null then distance else slicer.retractionDistance # Use internal storage (mm)
retractSpeed = if speed isnt null then speed else slicer.retractionSpeed # Use internal storage (mm/s)
if retractDistance <= 0
return "" # No unretraction needed
gcode = "G1"
gcode += " E" + retractDistance # E is always in mm for G-code
if retractSpeed > 0
# Convert mm/s to mm/min for F parameter using polyconvert
feedratePerMin = Math.round(polyconvert.speed.millimeterSecond.millimeterMinute(retractSpeed))
gcode += " F" + feedratePerMin
return gcode + slicer.newline
# Update metadata in G-code with print statistics.
# This should be called after slicing is complete to add statistics to the metadata header.
updateMetadataWithStats: (slicer) ->
# Only update if metadata is enabled.
return unless slicer.getMetadata()
# Find the end of the metadata block (marked by blank line after metadata).
lines = slicer.gcode.split(slicer.newline)
metadataEndIndex = -1
inMetadata = false
for line, index in lines
# Metadata starts with "; Generated by Polyslice"
if line.startsWith('; Generated by Polyslice')
inMetadata = true
# Metadata ends with a blank line
else if inMetadata and line.trim() is ''
metadataEndIndex = index
break
# If we found the metadata block, insert statistics before the blank line.
if metadataEndIndex > 0
statsLines = []
# Add print statistics.
if slicer.getMetadataTotalLayers() and slicer.totalLayers?
statsLines.push("; Total Layers: " + slicer.totalLayers)
if slicer.totalFilamentLength?
# Filament length in mm.
filamentLengthMm = slicer.totalFilamentLength
if slicer.getMetadataFilamentLength()
statsLines.push("; Filament Length: " + module.exports.formatPrecision(filamentLengthMm, 2) + "mm")
# Calculate material volume in mm³.
# Use internal units (filamentDiameter is already in mm).
filamentRadius = slicer.filamentDiameter / 2
volumeMm3 = Math.PI * filamentRadius * filamentRadius * filamentLengthMm
if slicer.getMetadataMaterialVolume()
statsLines.push("; Material Volume: " + module.exports.formatPrecision(volumeMm3, 2) + "mm³")
# Calculate material weight in grams if filament density is available.
if slicer.getMetadataMaterialWeight() and slicer.filament and slicer.filament.getDensity()
densityGPerCm3 = slicer.filament.getDensity()
volumeCm3 = volumeMm3 / 1000 # Convert mm³ to cm³.
weightGrams = volumeCm3 * densityGPerCm3
statsLines.push("; Material Weight: " + module.exports.formatPrecision(weightGrams, 2) + "g")
# Calculate estimated print time.
if slicer.getMetadataPrintTime()
printTimeSeconds = module.exports.calculatePrintTime(slicer)
formattedTime = module.exports.formatTime(printTimeSeconds)
statsLines.push("; Estimated Print Time: " + formattedTime)
# Insert statistics before the blank line that ends the metadata block.
lines.splice(metadataEndIndex, 0, statsLines...)
# Reconstruct the G-code.
slicer.gcode = lines.join(slicer.newline)
return
# Generate metadata header comment for G-code.
codeMetadata: (slicer) ->
gcode = ""
now = new Date() # Get current timestamp.
timestamp = now.toISOString()
try # Get package version (this will be available in the compiled context).
pkg = require('../../../package.json')
version = pkg.version
catch
version = "Unknown"
# Generate metadata header.
# Always include the title as a marker for the metadata block.
gcode += "; Generated by Polyslice" + slicer.newline
if slicer.getMetadataVersion()
gcode += "; Version: " + version + slicer.newline
if slicer.getMetadataTimestamp()
gcode += "; Timestamp: " + timestamp + slicer.newline
if slicer.getMetadataRepository()
gcode += "; Repository: https://github.com/jgphilpott/polyslice" + slicer.newline
# G-code flavor/firmware compatibility
if slicer.getMetadataFlavor()
gcode += "; Flavor: Marlin" + slicer.newline
if slicer.getMetadataPrinter() and slicer.printer # Add printer information if available.
gcode += "; Printer: " + slicer.printer.model + slicer.newline
if slicer.getMetadataFilament() and slicer.filament # Add filament information if available.
gcode += "; Filament: " + slicer.filament.name + " (" + slicer.filament.type + ")" + slicer.newline
# Add key print settings.
if slicer.getMetadataNozzleTemp()
gcode += "; Nozzle Temp: " + slicer.nozzleTemperature + "°C" + slicer.newline
if slicer.getMetadataBedTemp()
gcode += "; Bed Temp: " + slicer.bedTemperature + "°C" + slicer.newline
if slicer.getMetadataLayerHeight()
gcode += "; Layer Height: " + slicer.getLayerHeight() + "mm" + slicer.newline
# Add additional critical print parameters
if slicer.getMetadataInfillDensity()
gcode += "; Infill Density: " + slicer.infillDensity + "%" + slicer.newline
if slicer.getMetadataInfillPattern()
gcode += "; Infill Pattern: " + slicer.infillPattern + slicer.newline
# Calculate wall count
if slicer.getMetadataWallCount()
wallCount = Math.max(1, Math.floor((slicer.shellWallThickness / slicer.nozzleDiameter) + 0.0001))
gcode += "; Wall Count: " + wallCount + slicer.newline
# Support and adhesion information
if slicer.getMetadataSupport()
gcode += "; Support: " + (if slicer.supportEnabled then "Yes" else "No") + slicer.newline
if slicer.getMetadataAdhesion()
if slicer.adhesionEnabled
gcode += "; Adhesion: " + slicer.adhesionType + slicer.newline
else
gcode += "; Adhesion: None" + slicer.newline
# Print speed settings
if slicer.getMetadataSpeeds()
gcode += "; Perimeter Speed: " + slicer.getPerimeterSpeed() + "mm/s" + slicer.newline
gcode += "; Infill Speed: " + slicer.getInfillSpeed() + "mm/s" + slicer.newline
gcode += "; Travel Speed: " + slicer.getTravelSpeed() + "mm/s" + slicer.newline
# Bounding box (will be filled in after slicing if available)
if slicer.getMetadataBoundingBox() and slicer.meshBounds?
minX = module.exports.formatPrecision(slicer.meshBounds.minX, 2)
minY = module.exports.formatPrecision(slicer.meshBounds.minY, 2)
minZ = module.exports.formatPrecision(slicer.meshBounds.minZ, 2)
maxX = module.exports.formatPrecision(slicer.meshBounds.maxX, 2)
maxY = module.exports.formatPrecision(slicer.meshBounds.maxY, 2)
maxZ = module.exports.formatPrecision(slicer.meshBounds.maxZ, 2)
# Emit Cura-style per-axis bounding box metadata for consistent parsing.
gcode += "; MINX: " + minX + slicer.newline
gcode += "; MAXX: " + maxX + slicer.newline
gcode += "; MINY: " + minY + slicer.newline
gcode += "; MAXY: " + maxY + slicer.newline
gcode += "; MINZ: " + minZ + slicer.newline
gcode += "; MAXZ: " + maxZ + slicer.newline
gcode += slicer.newline
return gcode
# Generate test strip G-code to verify extrusion before print.
codeTestStrip: (slicer, length = null, width = 0.4, height = 0.25) ->