chiark / gitweb /
change TweakAtZ version to 3.1.2
[cura.git] / plugins / TweakAtZ.py
1 #Name: Tweak At Z 3.1.2
2 #Info: Change printing parameters at a given height
3 #Help: TweakAtZ
4 #Depend: GCode
5 #Type: postprocess
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)
15
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
20
21 # Uses -
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>
27
28 #history / changelog:
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
32 #V3.1.2: Bugfix disable TweakAtZ on Cool Head Lift / Retraction Hop
33
34 version = '3.1.2'
35
36 import re
37
38 def getValue(line, key, default = None):
39         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                 return default
41         subPart = line[line.find(key) + len(key):] #allows for string lengths larger than 1
42         if ";TweakAtZ" in key:
43                 m = re.search('^[0-3]', subPart)
44         elif ";LAYER:" in key:
45                 m = re.search('^[+-]?[0-9]*', subPart)
46         else:
47                 m = re.search('^[0-9]+\.?[0-9]*', subPart)
48         if m == None:
49                 return default
50         try:
51                 return float(m.group(0))
52         except:
53                 return default
54
55 with open(filename, "r") as f:
56         lines = f.readlines()
57
58 old_speed = 100
59 old_flowrate = 100
60 old_platformTemp = -1
61 old_extruderOne = -1
62 old_extruderTwo = -1
63 #Ex3 old_extruderThree = -1
64 old_fanSpeed = 0
65 pres_ext = 0
66 z = 0
67 x = None
68 y = None
69 layer = -100000 #layer no. may be negative (raft) but never that low
70 state = 1 #state 0: deactivated, state 1: activated, state 2: active, but below z, state 3: active, passed z
71 old_state = -1
72 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
74 try:
75         targetL_i = int(targetL)
76         targetZ = 100000
77 except:
78         targetL_i = -100000
79
80 with open(filename, "w") as f:
81         for line in lines:
82                 f.write(line)
83                 if 'FLAVOR:UltiGCode' in line: #Flavor is UltiGCode! No reset of values
84                         no_reset = 1
85                 if ';TweakAtZ-state' in line: #checks for state change comment
86                         state = getValue(line, ';TweakAtZ-state', state)
87                 if ';Small layer' in line: #checks for begin of Cool Head Lift
88                         old_state = state
89                         state = 0
90                 if ('G4' in line) and old_state > -1:
91                         state = old_state
92                         old_state = -1
93                 if ';LAYER:' in line: #new layer no. found
94                         layer = getValue(line, ';LAYER:', layer)
95                         if targetL_i > -100000: #target selected by layer no.
96                                 if state == 2 and layer >= targetL_i: #determine targetZ from layer no.
97                                         targetZ = z + 0.001
98                 if (getValue(line, 'T', None) is not None) and (getValue(line, 'M', None) is None): #looking for single T-command
99                         pres_ext = getValue(line, 'T', pres_ext)
100                 if 'M190' in line or 'M140' in line and state < 3: #looking for bed temp, stops after target z is passed
101                         old_platformTemp = getValue(line, 'S', old_platformTemp)
102                 if 'M109' in line or 'M104' in line and state < 3: #looking for extruder temp, stops after target z is passed
103                         if getValue(line, 'T', pres_ext) == 0:
104                                 old_extruderOne = getValue(line, 'S', old_extruderOne)
105                         elif getValue(line, 'T', pres_ext) == 1:
106                                 old_extruderTwo = getValue(line, 'S', old_extruderTwo)
107 #Ex3                        elif getValue(line, 'T', pres_ext) == 2:
108 #Ex3                                old_extruderThree = getValue(line, 'S', old_extruderThree)
109                 if 'M107' in line: #fan is stopped; is always updated in order not to miss switch off for next object
110                         old_fanSpeed = 0
111                 if 'M106' in line and state < 3: #looking for fan speed
112                         old_fanSpeed = getValue(line, 'S', old_fanSpeed)
113                 if 'M221' in line and state < 3: #looking for flow rate
114                         old_flowrate = getValue(line, 'S', old_flowrate)
115                 if 'G1' in line or 'G0' in line:
116                         newZ = getValue(line, 'Z', z)
117                         x = getValue(line, 'X', None)
118                         y = getValue(line, 'Y', None)
119                         if (newZ != z) and (x is not None) and (y is not None): #no tweaking on retraction hops which have no x and y coordinate
120                                 z = newZ
121                                 if z < targetZ and state == 1:
122                                         state = 2
123                                 if z >= targetZ and state == 2:
124                                         state = 3
125                                         if targetL_i > -100000:
126                                                 f.write(";TweakAtZ V%s: executed at Layer %d\n" % (version,targetL_i))
127                                         else:
128                                                 f.write(";TweakAtZ V%s: executed at %1.2f mm\n" % (version,targetZ))
129                                         if speed is not None and speed != '':
130                                                 f.write("M220 S%f\n" % float(speed))
131                                         if flowrate is not None and flowrate != '':
132                                                 f.write("M221 S%f\n" % float(flowrate))
133                                         if platformTemp is not None and platformTemp != '':
134                                                 f.write("M140 S%f\n" % float(platformTemp))
135                                         if extruderOne is not None and extruderOne != '':
136                                                 f.write("M104 S%f T0\n" % float(extruderOne))
137                                         if extruderTwo is not None and extruderTwo != '':
138                                                 f.write("M104 S%f T1\n" % float(extruderTwo))
139 #Ex3                                    if extruderThree is not None and extruderThree != '':
140 #Ex3                                            f.write("M104 S%f T2\n" % float(extruderThree))                                 
141                                         if fanSpeed is not None and fanSpeed != '':
142                                                 f.write("M106 S%d\n" % int(fanSpeed))                                   
143                                 if z < targetZ and state == 3: #re-activates the plugin if executed by pre-print G-command, resets settings
144                                         state = 2
145                                         if no_reset == 0: #executes only for UM Original and UM2 with RepRap flavor
146                                                 if targetL_i > -100000:
147                                                         f.write(";TweakAtZ V%s: reset below Layer %d\n" % (version,targetL_i))
148                                                 else:
149                                                         f.write(";TweakAtZ V%s: reset below %1.2f mm\n" % (version,targetZ))
150                                                 if speed is not None and speed != '':
151                                                         f.write("M220 S%f\n" % float(old_speed))
152                                                 if flowrate is not None and flowrate != '':
153                                                         f.write("M221 S%f\n" % float(old_flowrate))
154                                                 if platformTemp is not None and platformTemp != '':
155                                                         f.write("M140 S%f\n" % float(old_platformTemp))
156                                                 if extruderOne is not None and extruderOne != '':
157                                                         f.write("M104 S%f T0\n" % float(old_extruderOne))
158                                                 if extruderTwo is not None and extruderTwo != '':
159                                                         f.write("M104 S%f T1\n" % float(old_extruderTwo))
160 #Ex3                                                if extruderThree is not None and extruderThree != '':
161 #Ex3                                                        f.write("M104 S%f T2\n" % float(old_extruderThree))                                 
162                                                 if fanSpeed is not None and fanSpeed != '':
163                                                         f.write("M106 S%d;\n" % int(old_fanSpeed))                                      
164