forked from fieldOfView/Cura-SimpleShapes
-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
RetractTower.py
356 lines (304 loc) · 15.1 KB
/
RetractTower.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
#------------------------------------------------------------------------------------------------------------------------------------
#
# Cura PostProcessing Script
# Author: 5axes
# Date: November 29 2020
#
# Description: postprocessing script to easily define a Retract Tower
#
#------------------------------------------------------------------------------------------------------------------------------------
#
# Version 1.0 29/11/2020
# Version 1.1 29/01/2021
# Version 1.2 19/02/2021 : First instruction output
# Version 1.3 18/04/2021 : ChangeLayerOffset += 2
# Version 1.4 01/06/2021 : Detect G91/G90 M82/M83 in G-Code
# https://github.com/5axes/Calibration-Shapes/issues/28
# Version 1.5 02/06/2021 : Detect G92 E0 in G-Code
# Version 1.6 04/07/2021 : Bug in Extrusion mode on the first call we should have the retraction value equal to the first test value.
# Otherwise we could have an issue IE : -5 -> 1.000
#
# G1 F2700 E-5
# ....
# ;LAYER:4
# M117 START distance (1.0/1.0)
# ....
# G1 F2700 E1.00000
#
# Version 1.7 15/02/2022 Change Int for Layeroffset & changelayer
# Version 1.8 11/08/2022 Init on G92 E0
# Version 1.9 13/09/2022 Do not detect M83 in comment line
# Version 1.10 24/10/2022 Do not detect Extruder value in comment line : https://github.com/5axes/Calibration-Shapes/issues/120
# Version 1.11 16/02/2024 Case of G92 not equal to 0 : https://github.com/5axes/Calibration-Shapes/issues/147
#------------------------------------------------------------------------------------------------------------------------------------
from ..Script import Script
from UM.Logger import Logger
from UM.Application import Application
import re #To perform the search
from enum import Enum
__version__ = '1.11'
class Section(Enum):
"""Enum for section type."""
NOTHING = 0
SKIRT = 1
INNER_WALL = 2
OUTER_WALL = 3
INFILL = 4
SKIN = 5
SKIN2 = 6
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_retract_line(line: str) -> bool:
"""Check if current line is a retract segment.
Args:
line (str): Gcode line
Returns:
bool: True if the line is a retract segment
"""
return "G1" in line and "F" in line and "E" in line and not "X" in line and not "Y" in line and not "Z" in line
def is_extrusion_line(line: str) -> bool:
"""Check if current line is a standard printing segment.
Args:
line (str): Gcode line
Returns:
bool: True if the line is a standard printing segment
"""
return "G1" in line and "X" in line and "Y" in line and "E" in line
def is_not_extrusion_line(line: str) -> bool:
"""Check if current line is a rapid movement segment.
Args:
line (str): Gcode line
Returns:
bool: True if the line is a standard printing segment
"""
return "G0" in line and "X" in line and "Y" in line and not "E" in line
def is_relative_instruction_line(line: str) -> bool:
"""Check if current line contain a M83 / G91 instruction
Args:
line (str): Gcode line
Returns:
bool: True contain a M83 / G91 instruction
"""
return "G91" in line or "M83" in line
def is_not_relative_instruction_line(line: str) -> bool:
"""Check if current line contain a M82 / G90 instruction
Args:
line (str): Gcode line
Returns:
bool: True contain a M82 / G90 instruction
"""
return "G90" in line or "M82" in line
def is_reset_extruder_line(line: str) -> bool:
"""Check if current line contain a G92 E0
Args:
line (str): Gcode line
Returns:
bool: True contain a G92 E0 instruction
"""
return "G92" in line and "E0" in line
def is_set_extruder_line(line: str) -> bool:
"""Check if current line contain a G92
Args:
line (str): Gcode line
Returns:
bool: True contain a G92 instruction
"""
return "G92" in line and not "E0" in line
class RetractTower(Script):
def __init__(self):
super().__init__()
def getSettingDataString(self):
return """{
"name": "RetractTower",
"key": "RetractTower",
"metadata": {},
"version": 2,
"settings":
{
"command": {
"label": "Command",
"description": "GCode Commande",
"type": "enum",
"options": {
"speed": "Speed",
"distance": "Distance"
},
"default_value": "speed"
},
"startValue":
{
"label": "Starting value",
"description": "The starting value of the Tower (speed/distance).",
"type": "float",
"default_value": 10
},
"valueChange":
{
"label": "Value Increment",
"description": "The value change of each block, can be positive or negative (speed/distance).",
"type": "float",
"default_value": 10
},
"changelayer":
{
"label": "Change Layer",
"description": "How many layers needs to be printed before the value should be changed.",
"type": "int",
"default_value": 38,
"minimum_value": 1
},
"changelayeroffset":
{
"label": "Change Layer Offset",
"description": "If the print has a base, indicate the number of layers from which to start the changes.",
"type": "int",
"default_value": 5,
"minimum_value": 0
},
"lcdfeedback":
{
"label": "Display details on LCD",
"description": "This setting will insert M117 gcode instructions, to display current modification in the G-Code is being used.",
"type": "bool",
"default_value": true
}
}
}"""
## -----------------------------------------------------------------------------
#
# Main Prog
#
## -----------------------------------------------------------------------------
def execute(self, data):
# Deprecation function
# extrud = list(Application.getInstance().getGlobalContainerStack().extruders.values())
extrud = Application.getInstance().getGlobalContainerStack().extruderList
relative_extrusion = bool(extrud[0].getProperty("relative_extrusion", "value"))
# Logger.log('d', 'Relative_extrusion : {}'.format(relative_extrusion))
UseLcd = self.getSettingValueByKey("lcdfeedback")
Instruction = self.getSettingValueByKey("command")
StartValue = self.getSettingValueByKey("startValue")
ValueChange = self.getSettingValueByKey("valueChange")
ChangeLayer = int(self.getSettingValueByKey("changelayer"))
ChangeLayerOffset = int(self.getSettingValueByKey("changelayeroffset"))
ChangeLayerOffset += 2 # Modification to take into account the numbering offset in Gcode
# layer_index = 0 for initial Block 1= Start Gcode normaly first layer = 0
CurrentValue = -1
save_e = -1
Command=""
# Logger.log('d', 'Change Layer Offset : {}'.format(ChangeLayerOffset))
lcd_gcode = "M117 {:s} ({:.1f}/{:.1f})".format(Instruction,StartValue,ValueChange)
if (Instruction=='speed'):
StartValue = StartValue*60
ValueChange = ValueChange*60
idl=0
current_e = 0
addition_e = 0
first_code = False
for layer in data:
layer_index = data.index(layer)
# Logger.log('d', 'layer_index : {}'.format(layer_index))
lines = layer.split("\n")
for line in lines:
line_index = lines.index(line)
# Check if the line start with the Comment Char
if is_relative_instruction_line(line) and line[0] != ";" :
relative_extrusion = True
# Logger.log('d', 'Relative_extrusion founded : {}'.format(line))
# Check if the line start with the Comment Char
if is_not_relative_instruction_line(line) and line[0] != ";" :
relative_extrusion = False
# Manage https://github.com/5axes/Calibration-Shapes/issues/147
if is_set_extruder_line(line) and line[0] != ";" :
# Logger.log('d', 'Set_extruder current_e :' + str(current_e))
# Logger.log('d', 'Set_extruder save_e :' + str(save_e))
searchE = re.search(r"E([-+]?\d*\.?\d*)", line)
if searchE:
current_e=float(searchE.group(1))
save_e = current_e
addition_e = current_e
if is_reset_extruder_line(line) and line[0] != ";" :
# Logger.log('d', 'Reset_extruder :' + str(current_e))
current_e = 0
save_e = 0
# If we have define a value
if CurrentValue>=0:
if is_retract_line(line):
# Logger.log('d', 'Retract_line : {}'.format(line))
searchF = re.search(r"F(\d*)", line)
if searchF:
current_f=float(searchF.group(1))
# Logger.log('d', 'CurF :' + str(current_f))
searchE = re.search(r"E([-+]?\d*\.?\d*)", line)
if searchE:
current_e=float(searchE.group(1))
# Logger.log('d', 'CurE :' + str(current_e))
if relative_extrusion:
if current_e<0:
# Logger.log('d', 'Mode retract')
if (Instruction=='speed'):
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(CurrentValue), current_e)
lcd_gcode = "M117 speed F{:d}".format(int(CurrentValue))
if (Instruction=='distance'):
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(current_f), -CurrentValue)
lcd_gcode = "M117 retract E{:.3}".format(float(CurrentValue))
else:
# Logger.log('d', 'Mode reset: {}'.format(first_code))
if (Instruction=='speed'):
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(CurrentValue), current_e)
lcd_gcode = "M117 speed F{:d}".format(int(CurrentValue))
if (Instruction=='distance'):
if first_code == True:
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(current_f), current_e)
first_code = False
else :
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(current_f), CurrentValue)
lcd_gcode = "M117 retract E{:.3}".format(float(CurrentValue))
else:
if save_e>current_e:
# Logger.log('d', 'Mode retract')
if (Instruction=='speed'):
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(CurrentValue), current_e)
lcd_gcode = "M117 speed F{:d}".format(int(CurrentValue))
if (Instruction=='distance'):
# Most of the case addition_e =0 except case https://github.com/5axes/Calibration-Shapes/issues/147
if (addition_e>0) :
current_e = current_e - CurrentValue
addition_e = 0
else :
current_e = save_e - CurrentValue + addition_e
lines[line_index] = "G1 F{:d} E{:.5f} ; Modi".format(int(current_f), current_e)
lcd_gcode = "M117 retract E{:.3}".format(float(CurrentValue))
else:
# Logger.log('d', 'Mode reset')
if (Instruction=='speed'):
lines[line_index] = "G1 F{:d} E{:.5f}".format(int(CurrentValue), current_e)
lcd_gcode = "M117 speed F{:d}".format(int(CurrentValue))
if is_extrusion_line(line) and line[0] != ";" :
searchE = re.search(r"E([-+]?\d*\.?\d*)", line)
# Logger.log('d', 'SearchE : {}'.format(searchE.group(1)))
if searchE :
save_e=float(searchE.group(1))
# Logger.log('d', 'L : {}'.format(line))
if line.startswith(";LAYER:"):
# Initialize the change
if (layer_index==ChangeLayerOffset):
CurrentValue = StartValue
lcd_gcode = "M117 START {:s} ({:.1f}/{:.1f})".format(Instruction,StartValue,ValueChange)
first_code = True
# Logger.log('d', 'Start Layer Layer_index : {}'.format(layer_index))
# Change the current value
if ((layer_index-ChangeLayerOffset) % ChangeLayer == 0) and ((layer_index-ChangeLayerOffset)>0):
CurrentValue += ValueChange
# Add M117 to add message on LCD
if UseLcd == True :
lines.insert(line_index + 1, lcd_gcode)
result = "\n".join(lines)
data[layer_index] = result
return data