1 #Name: Tweak At Z 4.0.1
2 #Info: Change printing parameters at a given height
6 #Param: targetZ(float:5.0) Z height to tweak at (mm)
7 #Param: targetL(int:) (ALT) Layer no. to tweak at
8 #Param: twLayers(int:1) No. of layers used for change
9 #Param: behavior(list:Tweak value and keep it for the rest,Tweak value for single layer only) Tweak behavior
10 #Param: speed(int:) New TOTAL Speed (%)
11 #Param: printspeed(int:) New PRINT Speed (%)
12 #Param: flowrate(int:) New General Flow Rate (%)
13 #Param: flowrateOne(int:) New Flow Rate Extruder 1 (%)
14 #Param: flowrateTwo(int:) New Flow Rate Extruder 2 (%)
15 #Param: platformTemp(int:) New Bed Temp (deg C)
16 #Param: extruderOne(int:) New Extruder 1 Temp (deg C)
17 #Param: extruderTwo(int:) New Extruder 2 Temp (deg C)
18 #Param: fanSpeed(int:) New Fan Speed (0-255 PWM)
20 ## Written by Steven Morlock, smorloc@gmail.com
21 ## Modified by Ricardo Gomez, ricardoga@otulook.com, to add Bed Temperature and make it work with Cura_13.06.04+
22 ## Modified by Stefan Heule, Dim3nsioneer@gmx.ch since V3.0 (see changelog below)
23 ## This script is licensed under the Creative Commons - Attribution - Share Alike (CC BY-SA) terms
26 ## M220 S<factor in percent> - set speed factor override percentage
27 ## M221 S<factor in percent> - set flow factor override percentage
28 ## M221 S<factor in percent> T<0-#toolheads> - set flow factor override percentage for single extruder
29 ## M104 S<temp> T<0-#toolheads> - set extruder <T> to target temperature <S>
30 ## M140 S<temp> - set bed target temperature
31 ## M106 S<PWM> - set fan speed to target speed <S>
32 ## M605/606 to save and recall material settings on the UM2
34 ##history / changelog:
35 ##V3.0.1: TweakAtZ-state default 1 (i.e. the plugin works without any TweakAtZ comment)
36 ##V3.1: Recognizes UltiGCode and deactivates value reset, fan speed added, alternatively layer no. to tweak at,
37 ## extruder three temperature disabled by '#Ex3'
38 ##V3.1.1: Bugfix reset flow rate
39 ##V3.1.2: Bugfix disable TweakAtZ on Cool Head Lift
40 ##V3.2: Flow rate for specific extruder added (only for 2 extruders), bugfix parser,
41 ## added speed reset at the end of the print
42 ##V4.0: Progress bar, tweaking over multiple layers, M605&M606 implemented, reset after one layer option,
43 ## extruder three code removed, tweaking print speed, save call of Publisher class,
44 ## uses previous value from other plugins also on UltiGCode
45 ##V4.0.1: Bugfix for doubled G1 commands
53 #MacOS release currently lacks some wx components, like the Publisher.
54 from wx.lib.pubsub import Publisher
58 def getValue(line, key, default = None):
59 if not key in line or (';' in line and line.find(key) > line.find(';') and
60 not ";TweakAtZ" in key and not ";LAYER:" in key):
62 subPart = line[line.find(key) + len(key):] #allows for string lengths larger than 1
63 if ";TweakAtZ" in key:
64 m = re.search('^[0-4]', subPart)
65 elif ";LAYER:" in key:
66 m = re.search('^[+-]?[0-9]*', subPart)
68 #the minus at the beginning allows for negative values, e.g. for delta printers
69 m = re.search('^[-]?[0-9]+\.?[0-9]*', subPart)
73 return float(m.group(0))
77 with open(filename, "r") as f:
79 #Check which tweaks should apply
80 TweakProp = {'speed': speed is not None and speed != '',
81 'flowrate': flowrate is not None and flowrate != '',
82 'flowrateOne': flowrateOne is not None and flowrateOne != '',
83 'flowrateTwo': flowrateTwo is not None and flowrateTwo != '',
84 'platformTemp': platformTemp is not None and platformTemp != '',
85 'extruderOne': extruderOne is not None and extruderOne != '',
86 'extruderTwo': extruderTwo is not None and extruderTwo != '',
87 'fanSpeed': fanSpeed is not None and fanSpeed != ''}
88 TweakPrintSpeed = printspeed is not None and printspeed != ''
89 TweakStrings = {'speed': "M220 S%f\n",
90 'flowrate': "M221 S%f\n",
91 'flowrateOne': "M221 T0 S%f\n",
92 'flowrateTwo': "M221 T1 S%f\n",
93 'platformTemp': "M140 S%f\n",
94 'extruderOne': "M104 S%f T0\n",
95 'extruderTwo': "M104 S%f T1\n",
96 'fanSpeed': "M106 S%d\n"}
99 target_values[key]=eval(key)
100 old = {'speed': -1, 'flowrate': -1, 'flowrateOne': -1, 'flowrateTwo': -1, 'platformTemp': -1, 'extruderOne': -1,
101 'extruderTwo': -1, 'fanSpeed': -1, 'state': -1}
103 twLayers = max(int(twLayers),1) #for the case someone entered something as 'funny' as -1
111 layer = -100000 #layer no. may be negative (raft) but never that low
112 # state 0: deactivated, state 1: activated, state 2: active, but below z,
113 # state 3: active and partially executed (multi layer), state 4: active and passed z
115 # IsUM2: Used for reset of values (ok for Marlin/Sprinter),
116 # has to be set to 1 for UltiGCode (work-around for missing default values)
121 oldValueUnknown = False
126 targetL_i = int(targetL)
131 if Publisher is not None:
132 if targetL_i > -100000:
133 wx.CallAfter(Publisher().sendMessage, "pluginupdate",
134 "OpenPluginProgressWindow;TweakAtZ;Tweak At Z plugin is executed at layer " + str(targetL_i))
136 wx.CallAfter(Publisher().sendMessage, "pluginupdate",
137 "OpenPluginProgressWindow;TweakAtZ;Tweak At Z plugin is executed at height " + str(targetZ) + "mm")
138 with open(filename, "w") as file:
140 if int(i*100/l) > lastpercentage and Publisher is not None: #progressbar
141 lastpercentage = int(i*100/l)
142 wx.CallAfter(Publisher().sendMessage, "pluginupdate", "Progress;" + str(lastpercentage))
143 if ';Layer count:' in line:
145 file.write(';TweakAtZ instances: %d\n' % TWinstances)
146 if not ('M84' in line or 'M25' in line or ('G1' in line and TweakPrintSpeed and state==3) or
147 ';TweakAtZ instances:' in line):
149 IsUM2 = ('FLAVOR:UltiGCode' in line) or IsUM2 #Flavor is UltiGCode!
150 if ';TweakAtZ-state' in line: #checks for state change comment
151 state = getValue(line, ';TweakAtZ-state', state)
152 if ';TweakAtZ instances:' in line:
154 tempTWi = int(line[20:])
156 tempTWi = TWinstances
157 TWinstances = tempTWi
158 if ';Small layer' in line: #checks for begin of Cool Head Lift
161 #if ('G4' in line) and old['state'] > -1:
163 if ';LAYER:' in line: #new layer no. found
164 layer = getValue(line, ';LAYER:', layer)
165 if targetL_i > -100000: #target selected by layer no.
166 if state == 2 and layer >= targetL_i: #determine targetZ from layer no.
168 if (getValue(line, 'T', None) is not None) and (getValue(line, 'M', None) is None): #looking for single T-cmd
169 pres_ext = getValue(line, 'T', pres_ext)
170 if 'M190' in line or 'M140' in line and state < 3: #looking for bed temp, stops after target z is passed
171 old['platformTemp'] = getValue(line, 'S', old['platformTemp'])
172 if 'M109' in line or 'M104' in line and state < 3: #looking for extruder temp, stops after target z is passed
173 if getValue(line, 'T', pres_ext) == 0:
174 old['extruderOne'] = getValue(line, 'S', old['extruderOne'])
175 elif getValue(line, 'T', pres_ext) == 1:
176 old['extruderTwo'] = getValue(line, 'S', old['extruderTwo'])
177 if 'M107' in line: #fan is stopped; is always updated in order not to miss switch off for next object
179 if 'M106' in line and state < 3: #looking for fan speed
180 old['fanSpeed'] = getValue(line, 'S', old['fanSpeed'])
181 if 'M221' in line and state < 3: #looking for flow rate
182 tmp_extruder = getValue(line,'T',None)
183 if tmp_extruder == None: #check if extruder is specified
184 old['flowrate'] = getValue(line, 'S', old['flowrate'])
185 elif tmp_extruder == 0: #first extruder
186 old['flowrateOne'] = getValue(line, 'S', old['flowrateOne'])
187 elif tmp_extruder == 1: #second extruder
188 old['flowrateOne'] = getValue(line, 'S', old['flowrateOne'])
189 if ('M84' in line or 'M25' in line):
190 if state>0 and speed is not None and speed != '': #'finish' commands for UM Original and UM2
191 file.write("M220 S100 ; speed reset to 100% at the end of print\n")
192 file.write("M117 \n")
194 if 'G1' in line or 'G0' in line:
195 newZ = getValue(line, 'Z', z)
196 x = getValue(line, 'X', None)
197 y = getValue(line, 'Y', None)
198 e = getValue(line, 'E', None)
199 f = getValue(line, 'F', None)
200 if TweakPrintSpeed and state==3:
201 # check for pure print movement in target range:
202 if 'G1' in line and x != None and y != None and f != None and e != None and newZ==z:
203 file.write("G1 F%d X%1.3f Y%1.3f E%1.5f\n" % (int(f/100.0*float(printspeed)),getValue(line,'X'),
204 getValue(line,'Y'),getValue(line,'E')))
205 else: #G1 command but not a print movement
207 # no tweaking on retraction hops which have no x and y coordinate:
208 if (newZ != z) and (x is not None) and (y is not None):
210 if z < targetZ and state == 1:
212 if z >= targetZ and state == 2:
215 for key in TweakProp:
216 if TweakProp[key] and old[key]==-1: #old value is not known
217 oldValueUnknown = True
218 if oldValueUnknown: #the tweaking has to happen within one layer
220 if IsUM2: #Parameters have to be stored in the printer (UltiGCode=UM2)
221 file.write("M605 S%d;stores parameters before tweaking\n" % (TWinstances-1))
222 if behavior == 1: #single layer tweak only and then reset
224 if TweakPrintSpeed and behavior == 0:
225 twLayers = done_layers + 1
227 if twLayers-done_layers>0: #still layers to go?
228 if targetL_i > -100000:
229 file.write(";TweakAtZ V%s: executed at Layer %d\n" % (version,layer))
230 file.write("M117 Printing... tw@L%4d\n" % layer)
232 file.write(";TweakAtZ V%s: executed at %1.2f mm\n" % (version,z))
233 file.write("M117 Printing... tw@%5.1f\n" % z)
234 for key in TweakProp:
236 file.write(TweakStrings[key] %
237 float(old[key]+(float(target_values[key])-
238 float(old[key]))/float(twLayers)*float(done_layers+1)))
242 if behavior == 1: #reset values after one layer
243 if targetL_i > -100000:
244 file.write(";TweakAtZ V%s: reset on Layer %d\n" % (version,layer))
246 file.write(";TweakAtZ V%s: reset at %1.2f mm\n" % (version,z))
247 if not oldValueUnknown:
248 if not IsUM2:#executes only for UM Original and UM2 with RepRap flavor
249 for key in TweakProp:
251 file.write(TweakStrings[key] % float(old[key]))
252 else: #executes on UM2 with Ultigcode
253 file.write("M606 S%d;recalls saved settings\n" % (TWinstances-1))
254 # re-activates the plugin if executed by pre-print G-command, resets settings:
255 if z < targetZ and state >= 3:
258 if targetL_i > -100000:
259 file.write(";TweakAtZ V%s: reset below Layer %d\n" % (version,targetL_i))
261 file.write(";TweakAtZ V%s: reset below %1.2f mm\n" % (version,targetZ))
262 if not IsUM2: #executes only for UM Original and UM2 with RepRap flavor
263 for key in TweakProp:
265 file.write(TweakStrings[key] % float(old[key]))
267 file.write("M606 ;recalls saved settings\n")
269 if Publisher is not None:
270 wx.CallAfter(Publisher().sendMessage, "pluginupdate", "Progress;100")
272 wx.CallAfter(Publisher().sendMessage, "pluginupdate", "ClosePluginProgressWindow")