-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgcode_processor.py
More file actions
504 lines (402 loc) · 21.3 KB
/
gcode_processor.py
File metadata and controls
504 lines (402 loc) · 21.3 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
import math
import sys
import os
import datetime
import shutil
import traceback
import numpy as np
# from clocks import radToDeg
def radToDeg(rad):
return rad * 180 / math.pi
def apply_wipefix(gcode_in, log):
'''
Find toolchanges:
;--------------------
; CP TOOLCHANGE START
; toolchange #1
; material : PETG -> PETG
;--------------------
and make sure that the nozzle has moved to the wipe tower before M600 is called
need to move:
M600
T1
M900 K0.08 ; Filament gcode LA 1.5
M900 K45 ; Filament gcode LA 1.0
latest idea - copy the first G1 line to before M600
'''
gcode_out = []
toolchanges_left = True
#{index: {"start", "m600", "end"}}
toolchanges = {}
max_tool_change = -1
for i, line in enumerate(gcode_in):
if line.strip() =="; CP TOOLCHANGE START" and "; toolchange #" in gcode_in[i + 1]:
toolchange_id_line = gcode_in[i + 1].strip()
toolchange_id = int(toolchange_id_line.split("#")[1])
if toolchange_id > max_tool_change:
max_tool_change = toolchange_id
# log.write("Found toolchange #{} at line {}\n".format(toolchange_id,i))
start_line = i-1
end_line = -1
m600_line = -1
first_g_after_m600 = "notfound"
for j, line2 in enumerate(gcode_in[i:]):
if line2.strip() == "M600":
m600_line = j + i
if m600_line > 0 and line2.startswith("G1") and first_g_after_m600 == "notfound":
first_g_after_m600 = line2
if line2.strip() == "; CP TOOLCHANGE END":
end_line = j+1 + i
break
toolchanges[toolchange_id] = {"start":start_line, "m600":m600_line, "end":end_line, "gline": first_g_after_m600}
#log.write("{}\n".format(toolchanges))
for toolchange_id in toolchanges:
log.write("processing toolchange {}\n".format(toolchanges[toolchange_id]))
start_line = 0
if toolchange_id > 1:
start_line = toolchanges[toolchange_id-1]["end"]
#copy everything up to the start of the tool change
gcode_out.extend(gcode_in[start_line:toolchanges[toolchange_id]["m600"]])
#duplicate the first G1 command that would come after M600
gcode_out.append(toolchanges[toolchange_id]["gline"])
#copy up to the end of the tool change
gcode_out.extend(gcode_in[toolchanges[toolchange_id]["m600"]: toolchanges[toolchange_id]["end"]])
if toolchange_id == max_tool_change:
#copy the rest
gcode_out.extend(gcode_in[toolchanges[toolchange_id]["end"]:])
return gcode_out
def get_position(line, log=None):
'''
returns None if no position in line of gcode
'''
if line.startswith("G1") and "X" in line and "Y" in line and "E" in line:
# do we need to care if we're extruding?
parts = line.strip().split(" ")
x = 0
y = 0
for part in parts:
if part.startswith("X"):
x += float(part[1:])
if part.startswith("Y"):
y += float(part[1:])
if x == 0:
log.write("x==0 from line: {}\n".format(line))
if y == 0:
log.write("y==0 from line: {}\n".format(line))
return (x,y)
return None
def get_average_position(gcode, log=None):
x=0
y=0
found=0
for line in gcode:
# log.write(line)
pos = get_position(line, log=log)
# log.write("pos: {}\n".format(pos))
if pos is not None:
x += pos[0]
y += pos[1]
found += 1
# if line.startswith("G1") and "X" in line and "Y" in line and "E" in line:
# #do we need to care if we're extruding?
# parts = line.strip().split(" ")
# for part in parts:
# if part.startswith("X"):
# x += float(part[1:])
# if part.startswith("Y"):
# y += float(part[1:])
# found +=1
average = (x/found, y/found)
# log.write("average: {}\n".format(average))
return average
def get_shape_type(gcode):
'''
get the type of part being printed - should only see Perimeter, External perimeter and Solid infill
'''
current_shape_type = None
for line in gcode:
if line.startswith(";TYPE:"):
# last_shape_type = current_shape_type
current_shape_type = line.strip().split(":")[1]
return current_shape_type
def re_arrange_shapes(shapes, log, start_pos = None):
'''
give a list of lists of gcode, re-order the first list to reduce travelling
could do this with a general purpose proper minimum-distance-travelled algorithm
or i could cheat and use the fact I know it's a dial to simply use polar coordinates to ensure we print everything anticlockwise
'''
if start_pos is None:
start_pos = (-1,0)
shapes_processed = []
all_shape_gcode = []
for shape_gcode in shapes:
all_shape_gcode.extend(shape_gcode)
#assume this is the centre of the dial
centre = get_average_position(all_shape_gcode, log=log)
log.write("Assuming centre of dial is: {}\n".format(centre))
#we want to start the dial shapes with as little travel as possible, find the angle of the last known position of the nozzle
start_angle = math.atan2(start_pos[1] - centre[1], start_pos[0] - centre[0])
log.write("assuming attempting to start at angle {:.1f}deg\n".format(radToDeg(start_angle)))
for shape_gcode in shapes:
shape_centre = get_average_position(shape_gcode, log=log)
diff = (shape_centre[0] - centre[0], shape_centre[1] - centre[1])
angle = math.atan2(diff[1], diff[0])
distance = np.linalg.norm(diff)
#want the most -ve angle to be the one nearest the start angle so the sort will put that first
type = get_shape_type(shape_gcode)
if type == "External perimeter":
#ever so slightly prioritise so we don't do the infil of numbers with inner circles before the outline
angle-=1*2*math.pi/360
if angle < start_angle:
angle += math.pi*2
# if angle < 0:
# angle += math.pi*2
shape_info = {"gcode":shape_gcode, "angle":angle, "type": type, "centre": shape_centre, "distance": distance}
shapes_processed.append(shape_info)
shapes_processed.sort(key=lambda x: x["angle"])
gcode_out = []
for shape_info in shapes_processed:
gcode_out.extend(shape_info["gcode"])
log.write("adding shape with angle: {:.2f}={:.1f}deg, {} centre: {} distance: {}\n".format(shape_info["angle"], radToDeg(shape_info["angle"]), shape_info['type'], shape_info['centre'], shape_info['distance']))
return gcode_out
def apply_dialfix(gcode_in, log, printer_version=None, layers_to_fix=2, extruder_to_fix=1, detail_printed_first=False, increase_z_hop=False):
'''
for small objects on the dial, attempt to re-order to reduce total distance travelled (and thus stringing)
'''
gcode_out = []
current_z = 0
# current_z_100 = 0
layer = -1
current_tool = -1
inside_a_shape = False
current_shape = []
current_shape_type = None
last_shape_type = None
changing_tool = False
if detail_printed_first:
#bodgetastic time
current_tool = extruder_to_fix
last_known_pos = None
relevant_shape_types = ["External perimeter", "Perimeter", "Solid infill"]
relevant_shapes_in_layer = []
for i,line in enumerate(gcode_in):
if line.strip() == ";LAYER_CHANGE":
current_z = float(gcode_in[i+1].strip().split(":")[1])
# current_z_100 = int(current_z*100)
# log.write("Processing layer {}\n".format(current_z))
layer+=1
# log.write("previous relevant_shapes_in_layer: {}\n".format(len(relevant_shapes_in_layer)))
# log.write("extending gcode out starting at line {}\n".format(len(gcode_out)))
# gcode_out.extend(re_arrange_shapes(relevant_shapes_in_layer))
relevant_shapes_in_layer = []
current_shape_type = None
already_combined_with_previous_shape = False
last_shape_type = None
if line.strip() == "M600":
if gcode_in[i+1].startswith("T"):
current_tool = int(gcode_in[i + 1].strip()[1:])
log.write("Start changing to tool: T{} line {}\n".format(current_tool, i))
changing_tool = True
#if it doesn't say T[int] then it's probably a manual layer filament change
if line.startswith("; printing object") and changing_tool:
#assume this means we've dealt with all the toolchange and wipe tower and are back to printing proper
#THIS DOESN'T HAPPEN with prusaslicer 2.7.2 targetting the mk4
changing_tool = False
log.write("Finished changing to tool: T{} line {}\n".format(current_tool, i))
if printer_version == "MK4" and line.startswith(";TYPE:") and changing_tool:
#we don't seem to get the nice "printing object [blah]" for the mk4 :(
#so instead stop when we see we're printing something proper
current_shape_type = line.strip().split(":")[1]
if current_shape_type in relevant_shape_types:
log.write("Finished changing to tool: T{} line {}\n".format(current_tool, i))
changing_tool = False
if layer < layers_to_fix and current_tool == extruder_to_fix and not changing_tool:
'''
this is the layer and tool we want to tinker with
on the mk3 we raise after an object and lower just before printing with G1 Z.
on the mk4 we raise as part of a move after the last object, so the line won't start G1 Z.
it will be a standard move with a Z as well like:
G1 X203.226 Y79.155 Z.359 F18000 <- finish last object
G1 Z.2 F720 <- lower to start next
'''
if line.startswith("G1") and " Z." in line:
#are we raising up or lowering down?
#TODO last shape in layer doesn't end with raising the nozzle - check for ";stop printing object" ?
#also TODO seeing 73 unique shapes on a layer - since we've missed the last shape we've seen two too many. how?
#I think the two bonus shapes are the inside circles in the 9 and 6, so if we fix the last shape everything's accounted for!
#eg G1 Z.45 F720
#want the "0.45" bit THIS DOESN'T WORK ON THE MK4, which doesn't appear to raise and lower the head for printing
#z = float(line.strip().split(" ")[1][1:])
axes = line.split(" ")
for axis in axes:
if axis.startswith("Z."):
z = float(axis[1:])
starting_shape = abs(z - current_z) < 0.01
if starting_shape and not inside_a_shape:
inside_a_shape = True
#keep the previous line which was to move to the start position
current_shape = [gcode_in[i-1].strip()]
#scratch previous line from gcode
gcode_out = gcode_out[:-1]
if inside_a_shape and not starting_shape:
#finished a shape
actually_same_shape = False
inside_a_shape = False
if increase_z_hop:
line_with_z_hop = ""
for axis in line.split(" "):
if axis.startswith("Z."):
z_string = "{:.1f}".format(z+0.2)
if z_string.startswith("0"):
z_string = z_string[1:]
line_with_z_hop += "Z{} ".format(z_string)
else:
line_with_z_hop+= axis+" "
log.write("increasing z hop. Line {} was \"{}\" now \"{}\"".format(i, line, line_with_z_hop))
current_shape.append(line_with_z_hop)
else:
current_shape.append(line)
if not already_combined_with_previous_shape:
if ((current_shape_type == "Solid infill" and last_shape_type == "External perimeter") or
(current_shape_type == "External perimeter" and last_shape_type == "Perimeter") or
(current_shape_type == "Solid infill" and last_shape_type == "Solid infill")):
#this is actually part of the same shape
log.write("actually same shape. current: {}, previous: {}\n".format(current_shape_type, last_shape_type))
actually_same_shape = True
# log.write("current {}, last {}, assuming same shape line: {}\n".format(current_shape_type, last_shape_type, i))
#mega bodge, need to figure this out
if current_z > 0 and current_shape_type is None:
current_shape_type = last_shape_type
if current_shape_type not in relevant_shape_types:
#"Skirt/Brim" or something else entirely, sent directly to gcode_out without re-arranging
gcode_out.extend(current_shape)
log.write("current shape not being processed: {} line in {} line out {} \n{}\n\n".format(current_shape_type, i,len(gcode_out), current_shape))
continue
if actually_same_shape:
if len(relevant_shapes_in_layer) == 0:
log.write("somehow actually_same_shape but there is no previous shape. current_shape_type= {} last_shape_type = {}\n".format(current_shape_type,last_shape_type))
continue
relevant_shapes_in_layer[-1].extend(current_shape)
else:
relevant_shapes_in_layer.append(current_shape)
log.write("finished processing shape type {} line: {}\n".format(current_shape_type, i))
last_shape_type = current_shape_type
current_shape_type = None
already_combined_with_previous_shape = False
#don't add this line to gcode out
continue
if line.startswith("; stop printing object"):
current_shape.append(line)
if increase_z_hop:
z_string = "{:.1f}".format(z + 0.2)
if z_string.startswith("0"):
z_string = z_string[1:]
#G1 Z{z_offset+min(max_layer_z+1, max_print_height)} F720 ;
z_hop_line = "G1 Z{} F720 ;".format(z_string)
current_shape.append(z_hop_line)
relevant_shapes_in_layer.append(current_shape)
inside_a_shape = False
log.write("finished processing shape LAST IN LAYER type {} line: {}\n".format(current_shape_type, i))
log.write("previous relevant_shapes_in_layer: {}\n".format(len(relevant_shapes_in_layer)))
log.write("extending gcode out starting at line {}\n".format(len(gcode_out)))
if len(relevant_shapes_in_layer) > 1:
re_arranged_lines = re_arrange_shapes(relevant_shapes_in_layer, log, last_known_pos)
for line in re_arranged_lines:
if line.startswith(" "):
log.write("re-arranged line starts with space, line: {}\n".format(line))
gcode_out.extend(re_arranged_lines)
else:
print("insufficient relevant_shapes_in_layer, only found: {}".format(len(relevant_shapes_in_layer)))
relevant_shapes_in_layer = []
# if already_combined_with_previous_shape:
current_shape_type = None
#seem to be some situations (second layer, larger objects) where we do raise the nozzle between perimeter and solid infill, but without declaring it's infil
last_shape_type = None
already_combined_with_previous_shape = False
if inside_a_shape:
current_shape.append(line)
if line.startswith(";TYPE:"):
#it is possible to print multiple of these without raising the nozzle (seen with little circles) - at the moment we'll end up just choosing the LAST shape type
#which may accidentally be merged wit hthe previous shape
# last_shape_type = current_shape_type
shape_found = line.strip().split(":")[1]
if shape_found in relevant_shape_types and current_shape_type is not None and current_shape_type in relevant_shape_types:
log.write("currently processing shape type {}, no sign of having raised nozzle so assume {} is same shape\n".format(current_shape_type, shape_found))
already_combined_with_previous_shape = True
current_shape_type = shape_found
else:
gcode_out.append(line)
pos = get_position(line)
if pos is not None:
last_known_pos = pos
else:
gcode_out.append(line)
return gcode_out
if __name__ == "__main__":
'''
expects arguments: prusaslicer_version [optional] gcode/file/path
optional:
- firstm600 - remove the first M600 so we don't change filament immediately before the print
- dialfix - re-arrange the dial detail elements so there's less travel
- wipefix - re-arrange so the nozzle moves to the wipe tower BEFORE M600
- detailfirst - the dial is printed with the detail first (default is detail second)
C:/Python311_2023/python.exe C:/Users/Luke/Documents/Clocks/3DPrintedClocks/gcode_processor.py 2.7.2 firstm600 wipefix
'''
logfile = "C:/Users/Luke/Documents/Clocks/3DPrintedClocks/postprocesslog.txt"
backup = "C:/Users/Luke/Documents/Clocks/3DPrintedClocks/postprocessbackup.gcode"
#multicolour print will start with the right colour loaded manually, don't need the initial tool change
remove_first_m600 = False
#can I re-order the dial detail so it doesn't string so horrendously?
need_dialfix = False
#prusaslicer > 2.5 changes tool before moving to the wipe tower, leaving unsightly blobs
need_wipefix = False
if len(sys.argv) < 3:
raise ValueError("Need more arguments - prusaslicer versaion and the path to the gcode file")
#haven't ended up actually using this...
prusaslicer_version = sys.argv[1].upper()
relevant_args = sys.argv[2:-1]
remove_first_m600 = "firstm600" in relevant_args
need_dialfix = "dialfix" in relevant_args
need_wipefix = "wipefix" in relevant_args
detail_printed_first = "detailfirst" in relevant_args
#this... does not work as intended. the nozzel seems to raise,then immediately lower before moving, making it even worse
#think I've found it, we're accidentally putting a space before the next line so it gets ignored and the next thing it does process is lowering the nozzle!!
increase_z_hop = "zhop" in relevant_args
#file prusaslicer expects to be edited in situ
gcode_temp_file = sys.argv[-1]
gcode_in = []
found_first_m600 = False
shutil.copyfile(gcode_temp_file, backup)
with open(gcode_temp_file, 'r') as input_file:
for line in input_file:
gcode_in.append(line)
print(gcode_in)
printer_version = None
gcode_out = []
with open(logfile, "a+") as log:
try:
log.write("\n=======Post processing {} on {}===========\n".format(gcode_temp_file, datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S")))
log.write("remove_first_m600: {}, rearrange_dial_bits:{}, need_wipefix:{} Prusaslicer {}\n".format(remove_first_m600, need_dialfix, need_wipefix, prusaslicer_version))
line_number = 1
for line in gcode_in:
keepline = True
if "M600" in line and remove_first_m600 and not found_first_m600:
keepline = False
found_first_m600 = True
log.write("removed first M600 from line {}\n".format(line_number))
if line.startswith("M862.3 P ") and printer_version is None:
printer_version = line.split("\"")[1]
log.write("Found printer: {}\n".format(printer_version))
if keepline:
gcode_out.append(line)
line_number+=1
if need_wipefix:
gcode_out = apply_wipefix(gcode_out, log)
if need_dialfix:
gcode_out = apply_dialfix(gcode_out, log, printer_version, detail_printed_first=detail_printed_first, increase_z_hop=increase_z_hop)
except Exception as error:
log.write("Exception: {}\n{}".format(error, traceback.format_exc()))
#not sure where this is creeping in, but let's try belt-and-braces
gcode_out_no_spaces = [line.strip()+"\n" for line in gcode_out]
with open(gcode_temp_file, 'w') as out_file:
out_file.writelines(gcode_out_no_spaces)