1 #Name: Tweak At Z 3.1.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: speed(int:) New Speed (%)
9 #Param: flowrate(int:) New Flow Rate (%)
10 #Param: platformTemp(int:) New Bed Temp (deg C)
11 #Param: extruderOne(int:) New Extruder 1 Temp (deg C)
12 #Param: extruderTwo(int:) New Extruder 2 Temp (deg C)
13 #Ex3 #Param: extruderThree(int:) New Extruder 3 Temp (deg C)
14 #Param: fanSpeed(int:) New Fan Speed (0-255 PWM)
16 ## Written by Steven Morlock, smorloc@gmail.com
17 ## Modified by Ricardo Gomez, ricardoga@otulook.com, to add Bed Temperature and make it work with Cura_13.06.04+
18 ## Modified by Stefan Heule, Dim3nsioneer@gmx.ch, to add Flow Rate, restoration of initial values when returning to low Z, extended stage numbers, direct stage manipulation by GCODE-comments, UltiGCode regocnition, addition of fan speed, alternative selection by layer no., disabling extruder three
19 ## This script is licensed under the Creative Commons - Attribution - Share Alike (CC BY-SA) terms
22 # M220 S<factor in percent> - set speed factor override percentage
23 # M221 S<factor in percent> - set flow factor override percentage
24 # M104 S<temp> T<0-#toolheads> - set extruder <T> to target temperature <S>
25 # M140 S<temp> - set bed target temperature
26 # M106 S<PWM> - set fan speed to target speed <S>
29 #V3.0.1: TweakAtZ-state default 1 (i.e. the plugin works without any TweakAtZ comment)
30 #V3.1: Recognizes UltiGCode and deactivates value reset, fan speed added, alternatively layer no. to tweak at, extruder three temperature disabled by '#Ex3'
31 #V3.1.1: Bugfix reset flow rate
37 def getValue(line, key, default = None):
38 if not key in line or (';' in line and line.find(key) > line.find(';') and not ";TweakAtZ" in key and not ";LAYER:" in key):
40 subPart = line[line.find(key) + len(key):] #allows for string lengths larger than 1
41 if ";TweakAtZ" in key:
42 m = re.search('^[0-3]', subPart)
43 elif ";LAYER:" in key:
44 m = re.search('^[+-]?[0-9]*', subPart)
46 m = re.search('^[0-9]+\.?[0-9]*', subPart)
50 return float(m.group(0))
54 with open(filename, "r") as f:
62 #Ex3 old_extruderThree = -1
68 layer = -100000 #layer no. may be negative (raft) but never that low
69 state = 1 #state 0: deactivated, state 1: activated, state 2: active, but below z, state 3: active, passed z
70 no_reset = 0 #Default setting is reset (ok for Marlin/Sprinter), has to be set to 1 for UltiGCode (work-around for missing default values)
73 targetL_i = int(targetL)
78 with open(filename, "w") as f:
81 if 'FLAVOR:UltiGCode' in line: #Flavor is UltiGCode! No reset of values
83 if ';TweakAtZ-state' in line: #checks for state change comment
84 state = getValue(line, ';TweakAtZ-state', state)
85 if ';LAYER:' in line: #new layer no. found
86 layer = getValue(line, ';LAYER:', layer)
87 if targetL_i > -100000: #target selected by layer no.
88 if state == 2 and layer >= targetL_i: #determine targetZ from layer no.
90 if (getValue(line, 'T', None) is not None) and (getValue(line, 'M', None) is None): #looking for single T-command
91 pres_ext = getValue(line, 'T', pres_ext)
92 if 'M190' in line or 'M140' in line and state < 3: #looking for bed temp, stops after target z is passed
93 old_platformTemp = getValue(line, 'S', old_platformTemp)
94 if 'M109' in line or 'M104' in line and state < 3: #looking for extruder temp, stops after target z is passed
95 if getValue(line, 'T', pres_ext) == 0:
96 old_extruderOne = getValue(line, 'S', old_extruderOne)
97 elif getValue(line, 'T', pres_ext) == 1:
98 old_extruderTwo = getValue(line, 'S', old_extruderTwo)
99 #Ex3 elif getValue(line, 'T', pres_ext) == 2:
100 #Ex3 old_extruderThree = getValue(line, 'S', old_extruderThree)
101 if 'M107' in line: #fan is stopped; is always updated in order not to miss switch off for next object
103 if 'M106' in line and state < 3: #looking for fan speed
104 old_fanSpeed = getValue(line, 'S', old_fanSpeed)
105 if 'M221' in line and state < 3: #looking for flow rate
106 old_flowrate = getValue(line, 'S', old_flowrate)
107 if 'G1' in line or 'G0' in line:
108 newZ = getValue(line, 'Z', z)
109 x = getValue(line, 'X', x)
110 y = getValue(line, 'Y', y)
113 if z < targetZ and state == 1:
115 if z >= targetZ and state == 2:
117 if targetL_i > -100000:
118 f.write(";TweakAtZ V%s: executed at Layer %d\n" % (version,targetL_i))
120 f.write(";TweakAtZ V%s: executed at %1.2f mm\n" % (version,targetZ))
121 if speed is not None and speed != '':
122 f.write("M220 S%f\n" % float(speed))
123 if flowrate is not None and flowrate != '':
124 f.write("M221 S%f\n" % float(flowrate))
125 if platformTemp is not None and platformTemp != '':
126 f.write("M140 S%f\n" % float(platformTemp))
127 if extruderOne is not None and extruderOne != '':
128 f.write("M104 S%f T0\n" % float(extruderOne))
129 if extruderTwo is not None and extruderTwo != '':
130 f.write("M104 S%f T1\n" % float(extruderTwo))
131 #Ex3 if extruderThree is not None and extruderThree != '':
132 #Ex3 f.write("M104 S%f T2\n" % float(extruderThree))
133 if fanSpeed is not None and fanSpeed != '':
134 f.write("M106 S%d\n" % int(fanSpeed))
135 if z < targetZ and state == 3: #re-activates the plugin if executed by pre-print G-command, resets settings
137 if no_reset == 0: #executes only for UM Original and UM2 with RepRap flavor
138 if targetL_i > -100000:
139 f.write(";TweakAtZ V%s: reset below Layer %d\n" % (version,targetL_i))
141 f.write(";TweakAtZ V%s: reset below %1.2f mm\n" % (version,targetZ))
142 if speed is not None and speed != '':
143 f.write("M220 S%f\n" % float(old_speed))
144 if flowrate is not None and flowrate != '':
145 f.write("M221 S%f\n" % float(old_flowrate))
146 if platformTemp is not None and platformTemp != '':
147 f.write("M140 S%f\n" % float(old_platformTemp))
148 if extruderOne is not None and extruderOne != '':
149 f.write("M104 S%f T0\n" % float(old_extruderOne))
150 if extruderTwo is not None and extruderTwo != '':
151 f.write("M104 S%f T1\n" % float(old_extruderTwo))
152 #Ex3 if extruderThree is not None and extruderThree != '':
153 #Ex3 f.write("M104 S%f T2\n" % float(old_extruderThree))
154 if fanSpeed is not None and fanSpeed != '':
155 f.write("M106 S%d;\n" % int(old_fanSpeed))