-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMultiBrim.py
439 lines (359 loc) · 16.2 KB
/
MultiBrim.py
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
#------------------------------------------------------------------------------------------------------------------------------------
#
# Cura PostProcessing Script
# Author: 5axes
# Date: November 10, 2021
#
# Description: MultiBrim
#
#------------------------------------------------------------------------------------------------------------------------------------
#
# Version 1.0 10/11/2021 first prototype right now must be use with the relative extrusion activated and no Zhop
# Version 1.1 11/11/2021 first prototype tested on Ender3
# Version 1.2 12/11/2021 Adding Speed value for the subsequent brim print, Zhop are still not managed
# Version 1.3 12/11/2021 ZHop management
# Version 1.4 12/11/2021 Retract management
# Version 1.5 13/11/2021 Management of Print Sequence 'One at a Time'
# Version 1.6 13/11/2021 Management Relative extruder M83
# Version 1.6 17/07/2022 Change for 5.0
#
#------------------------------------------------------------------------------------------------------------------------------------
from ..Script import Script
from UM.Logger import Logger
from UM.Application import Application
import re #To perform the search
from enum import Enum
from collections import namedtuple
from typing import List, Tuple
__version__ = '1.7'
Point2D = namedtuple('Point2D', 'x y')
class Section(Enum):
"""Enum for section type."""
NOTHING = 0
SKIRT = 1
BRIM = 2
INNER_WALL = 3
OUTER_WALL = 4
INFILL = 5
SKIN = 6
SKIN2 = 7
def is_begin_layer_line(line: str) -> bool:
"""Check if current line is the start of a layer section.
Args:
line (str): Gcode line
Returns:
bool: True if the line is the start of a layer section
"""
return line.startswith(";LAYER:")
def is_begin_skirt_line(line: str) -> bool:
"""Check if current line is the start of a SKIRT section.
Args:
line (str): Gcode line
Returns:
bool: True if the line is the start of a SKIRT section
"""
return line.startswith(";TYPE:SKIRT")
def is_begin_type_line(line: str) -> bool:
"""Check if current line is the start of a type section.
Args:
line (str): Gcode line
Returns:
bool: True if the line is the start of a type section
"""
return line.startswith(";TYPE")
def is_begin_mesh_line(line: str) -> bool:
"""Check if current line is the start of a new MESH.
Args:
line (str): Gcode line
Returns:
bool: True if the line is the start of a new MESH
"""
return line.startswith(";MESH:")
def is_z_line(line: str) -> bool:
"""Check if current line is a Z line
Args:
line (str): Gcode line
Returns:
bool: True if the line is a Z line segment
"""
return "G0" in line and "Z" in line and not "E" in line
def is_e_line(line: str) -> bool:
"""Check if current line is a an Extruder line
Args:
line (str): Gcode line
Returns:
bool: True if the line is an Extruder line segment
"""
return "G1" in line and "E" in line
def is_relative_extrusion_line(line: str) -> bool:
"""Check if current line is a relative extrusion line
Args:
line (str): Gcode line
Returns:
bool: True if the line is a relative extrusion line
"""
return "M83" in line
def is_absolute_extrusion_line(line: str) -> bool:
"""Check if current line is an absolute extrusion line
Args:
line (str): Gcode line
Returns:
bool: True if the line is an absolute extrusion line
"""
return "M82" in line
def is_only_extrusion_line(line: str) -> bool:
"""Check if current line is a pure extrusion command.
Args:
line (str): Gcode line
Returns:
bool: True if the line is a pure extrusion command
"""
return "G1" in line and not "X" in line and not "Y" in line and "E" in line
def getXY(currentLine: str) -> Point2D:
"""Create a ``Point2D`` object from a gcode line.
Args:
currentLine (str): gcode line
Raises:
SyntaxError: when the regular expressions cannot find the relevant coordinates in the gcode
Returns:
Point2D: the parsed coordinates
"""
searchX = re.search(r"X(\d*\.?\d*)", currentLine)
searchY = re.search(r"Y(\d*\.?\d*)", currentLine)
if searchX and searchY:
elementX = searchX.group(1)
elementY = searchY.group(1)
else:
raise SyntaxError('Gcode file parsing error for line {currentLine}')
return Point2D(float(elementX), float(elementY))
class MultiBrim(Script):
def __init__(self):
super().__init__()
def getSettingDataString(self):
return """{
"name": "MultiBrim",
"key": "MultiBrim",
"metadata": {},
"version": 2,
"settings":
{
"multiply":
{
"label": "Brim addition",
"description": "Number of brim to add to the existing one.",
"type": "int",
"default_value": 1,
"minimum_value": 1,
"maximum_value_warning": 3,
"maximum_value": 5
},
"speed":
{
"label": "Brim speed",
"description": "Speed for the subsequent brim.",
"type": "float",
"unit": "mm/s",
"default_value": 30,
"minimum_value": 0,
"maximum_value_warning": 50,
"maximum_value": 100
}
}
}"""
def execute(self, data):
BrimMultiply = int(self.getSettingValueByKey("multiply"))
BrimSpeed = int(self.getSettingValueByKey("speed"))*60
BrimReplaceSpeeed = "F" + str(BrimSpeed)
idl=0
lines_brim =[]
StartLine=''
BrimF='F0'
FirstZToReplace=''
InitialE=''
StartZ=0
BrimZ=0
xyline=''
nb_line=0
currentlayer=0
CurrentE=0
RetractE=0
ResetE=0
lastE='G92 E0'
RetractF=3000
RelativeExtruder = False
for layer in data:
layer_index = data.index(layer)
lines = layer.split("\n")
for line in lines:
if is_relative_extrusion_line(line):
RelativeExtruder = True
if is_absolute_extrusion_line(line):
RelativeExtruder = False
if line.startswith(";LAYER_COUNT:"):
# Logger.log("w", "found LAYER_COUNT %s", line[13:])
layercount=int(line[13:])
if is_begin_layer_line(line):
line_index = lines.index(line)
# Logger.log('d', 'layer_lines : {}'.format(line))
currentlayer=int(line[7:])
# Logger.log('d', 'currentlayer : {:d}'.format(currentlayer))
if line.startswith(";LAYER:0"):
idl=1
# Copy the Original Brim
elif currentlayer <= BrimMultiply :
# Logger.log('d', 'Insert Here : {:d}'.format(currentlayer))
# Logger.log('d', 'First Z : {}'.format(FirstZToReplace))
line_index = lines.index(line)
xyline=lines[line_index-3]
#----------------------------
# Begin of modification
#----------------------------
nb_line = 1
lines.insert(line_index + nb_line, ";BEGIN_OF_MODIFICATION")
# Logger.log('d', 'xyline : {}'.format(xyline))
# Reset the Extruder position
if RetractE >0 and RelativeExtruder == False :
nb_line+=1
lines.insert(line_index + nb_line, "G1 F" + str(RetractF) + " E" + str(ResetE) )
if RelativeExtruder == False :
nb_line+=1
lines.insert(line_index + nb_line, InitialE)
# Set Z position of the Brim
searchZ = re.search(r"Z(\d*\.?\d*)", StartLine)
if searchZ:
FirstZToReplace="Z"+searchZ.group(1)
ModiZ="Z"+str(round((float(searchZ.group(1) )+BrimZ),5))
BeginLine=StartLine.replace(FirstZToReplace, ModiZ)
nb_line+=1
lines.insert(line_index + nb_line, BeginLine)
for aline in lines_brim:
nb_line+=1
searchZ = re.search(r"Z(\d*\.?\d*)", aline)
if searchZ:
Cz="Z"+searchZ.group(1)
ModiZ="Z"+str(round((float(searchZ.group(1) )+BrimZ),5))
# Logger.log('d', 'Current Z : {}'.format(Cz))
# Logger.log('d', 'Modi Z : {}'.format(ModiZ))
InsertLine=aline.replace(Cz, ModiZ)
else:
InsertLine=aline
lines.insert(line_index + nb_line, InsertLine)
nb_line+=1
lines.insert(line_index + nb_line, xyline)
nb_line+=1
lines.insert(line_index + nb_line, "G1 Z"+str(currentz))
# Reset Etruder position
if RelativeExtruder == False:
nb_line+=1
lines.insert(line_index + nb_line, lastE)
#----------------------------
# End of modification
#----------------------------
nb_line+=1
lines.insert(line_index + nb_line, ";END_OF_MODIFICATION")
BrimZ += StartZ
if idl == 2 and is_begin_type_line(line):
idl = 0
if idl == 2 and is_begin_mesh_line(line) :
idl = 0
#---------------------------------------
# Add the Brim line to the brim path
#---------------------------------------
if idl == 2 :
# if not is_only_extrusion_line(line):
if BrimSpeed >0 :
cline = line.replace(BrimF,BrimReplaceSpeeed)
else :
cline = line
lines_brim.append(cline)
#---------------------------------------
# Init copy of the BRIM extruding path
#---------------------------------------
# G0 F6000 X106.445 Y116.579 Z0.2 -> StartLine
# ;TYPE:SKIRT
# G1 F3000 E0 -> ZHopLine/ELine
# G1 F1200 X106.693 Y116.356 E0.011 -> SpeedLine
# or
# G0 F6000 X51.318 Y121.726 Z0.4 -> StartLine
# ;TYPE:SKIRT
# G1 F300 Z0.2 -> ZHopLine
# G1 F3000 E0 -> ELine
# G1 F1080 X51.568 Y121.624 E0.0089 -> SpeedLine
# or Cura 5.0 & 5.1
# G0 F6000 X105.446 Y125.313 Z0.2 -> PreviousStartLine
# G0 X104.5 Y127.5 -> StartLine
# ;TYPE:SKIRT
# G1 F3000 E0 -> ZHopLine/ELine
# G1 F1200 X104.54 Y127.012 E0.01629 -> SpeedLine
# Modification for Cura 5.0 StartLine += " " + Z0.2
# Relative mode
# G0 F6000 X109.982 Y102.608 Z0.2
# ;TYPE:SKIRT
# G1 F3000 E5
# G1 F1080 X110.147 Y102.432 E0.00795
if idl == 1 and is_begin_skirt_line(line):
idl=2
InitialE=''
# StartLine get the Z height
line_index = lines.index(line)-1
StartLine=lines[line_index]
searchZ = re.search(r"Z(\d*\.?\d*)", StartLine)
if searchZ:
StartZ=float(searchZ.group(1))
FirstZToReplace="Z"+searchZ.group(1)
else :
Logger.log('d', 'Format 5.0 : {}'.format(StartLine))
PreviousStartLine=lines[line_index-1]
Logger.log('d', 'Format 5.0 PreviousStartLine : {}'.format(PreviousStartLine))
searchZ = re.search(r"Z(\d*\.?\d*)", PreviousStartLine)
if searchZ:
StartZ=float(searchZ.group(1))
FirstZToReplace="Z"+searchZ.group(1)
StartLine += " "
StartLine += FirstZToReplace
Logger.log('d', 'Format 5.0 StartLine : {}'.format(StartLine))
# Test for Z hop case
ZHopLine=lines[line_index+2]
searchZ = re.search(r"Z(\d*\.?\d*)", ZHopLine)
if searchZ:
StartZ=float(searchZ.group(1))
FirstZToReplace="Z"+searchZ.group(1)
BrimZ = StartZ
# Logger.log('d', 'BrimZ : {:f}'.format(BrimZ))
# Logger.log('d', 'ZHopLine : {}'.format(ZHopLine))
searchE = re.search(r"E([-+]?\d*\.?\d*)", ZHopLine)
if searchE:
InitialE="G92 E"+str(searchE.group(1))
nb_line=3
else:
ZHopLine=lines[line_index+3]
searchE = re.search(r"E([-+]?\d*\.?\d*)", ZHopLine)
if searchE and InitialE=='' :
InitialE="G92 E"+str(searchE.group(1))
nb_line=4
SpeedLine=lines[line_index+nb_line]
# Logger.log('d', 'SpeedLine : {}'.format(SpeedLine))
searchF = re.search(r"F(\d*\.?\d*)", SpeedLine)
if searchF:
BrimF="F"+searchF.group(1)
# Logger.log('d', 'BrimF : {}'.format(BrimF))
lines_brim =[]
startlayer=currentlayer
lines_brim.append(line)
if currentlayer <= BrimMultiply and is_z_line(line):
searchZ = re.search(r"Z(\d*\.?\d*)", line)
if searchZ:
currentz=float(searchZ.group(1))
if currentlayer <= BrimMultiply and is_e_line(line):
searchE = re.search(r"E([-+]?\d*\.?\d*)", line)
if searchE:
lastE="G92 E"+searchE.group(1)
RetractE=CurrentE-float(searchE.group(1))
ResetE=CurrentE
CurrentE=float(searchE.group(1))
searchF = re.search(r"F(\d*\.?\d*)", line)
if searchF:
RetractF=float(searchF.group(1))
result = "\n".join(lines)
data[layer_index] = result
return data