chiark / gitweb /
plugins: Support user configuration of default values
[cura.git] / plugins / pauseAtZ.py
1 #Name: Pause at height
2 #Info: Pause the printer at a certain height
3 #Depend: GCode
4 #Type: postprocess
5 #Param: pauseLevel(float:1.0) Pause height (mm)
6 #Param: parkX(float:150) Head park X (mm)
7 #Param: parkY(float:0) Head park Y (mm)
8 #Param: moveZ(float:5) Head move Z (mm)
9 #Param: parkMinZ(float:15) Minimum head park Z (mm, abs)
10 #Param: retractAmount(float:1) Retraction amount (mm)
11 #Param: homeX(bool:true) Re-home X on restart
12 #Param: homeY(bool:false) Re-home Y on restart
13
14 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
15 import re
16 from Cura.util import profile
17
18 def getPrintZValue(lineBlock):
19         '''
20         look for the last z value found just before (or at the same time) G1 code in the given block
21         '''
22         lastZ = -1
23         for line in lineBlock:
24                 lastZ = getValue(line, 'Z', lastZ)
25                 if line.startswith('G1 ') and (getValue(line, 'X', None) is not None or getValue(line, 'Y', None) is not None):
26                         break
27
28         return lastZ
29
30
31 def getValue(line, key, default = None):
32         if not key in line or (';' in line and line.find(key) > line.find(';')):
33                 return default
34         subPart = line[line.find(key) + 1:]
35         m = re.search('^[0-9]+\.?[0-9]*', subPart)
36         if m is None:
37                 return default
38         try:
39                 return float(m.group(0))
40         except:
41                 return default
42
43 with open(filename, "r") as f:
44         lines = f.readlines()
45
46 z = 0.
47 x = 0.
48 y = 0.
49 pauseState = 0
50 #state 0 system is not active until we get to a smaller layer than the last encountered layer (default at 99999) (print one at a time support.)
51 #state 1 system is active and we are looking for our target layer z
52 #state 2 system found the layer it need to write. We will wait for the first G1 or G0 code to write the content just before. state will be set to 0
53
54
55 with open(filename, "w") as f:
56         lineIndex = 0
57         lastLayerIndex = 99999
58         layerZ = 0
59         for lIndex in xrange(len(lines)):
60                 line = lines[lIndex]
61                 if line.startswith(';'):
62                         if line.startswith(';LAYER:'):
63                                 currentLayer = int(line[7:].strip())
64
65                                 if currentLayer < lastLayerIndex:
66                                         pauseState = 1
67
68                                 lastLayerIndex = currentLayer
69                                 if pauseState == 1:
70                                         layerZ = getPrintZValue(lines[lIndex:lIndex+20])
71                                         if layerZ >= pauseLevel:
72                                                 pauseState = 2
73
74                         f.write(line)
75                         continue
76
77                 x = getValue(line, 'X', x)
78                 y = getValue(line, 'Y', y)
79
80                 if pauseState == 2:
81                         g = getValue(line, 'G', None)
82                         if g == 1 or g == 0:# We will do the pause just before printing content. We need to pause from the previous XY position. Not the current.
83                                 z = layerZ
84
85                                 pauseState = 0
86                                 f.write(";TYPE:CUSTOM\n")
87                                 #Retract
88                                 f.write("M83\n")
89                                 f.write("G1 E-%f F6000\n" % (retractAmount))
90
91                                 zChanged = False
92                                 #Change z before doing the move because the nozzle can hit the glass lock on the UM2
93                                 if z + moveZ < parkMinZ:
94                                         zChanged = True
95                                         f.write("G1 Z%f F300\n" % (parkMinZ))
96
97                                 elif moveZ > 0:
98                                         newZ = z + moveZ
99                                         maxZ = profile.getMachineSettingFloat('machine_height') - 10 #For Safety Leave a 10mm space (endstop)
100                                         if maxZ < newZ:
101                                                 newZ = maxZ
102
103                                         if newZ > z:
104                                                 zChanged = True
105                                                 f.write("G1 Z%f F300\n" % (newZ))
106
107                                 #Move the head away
108                                 f.write("G1 X%f Y%f F9000\n" % (parkX, parkY))
109
110                                 #Disable the E steppers
111                                 f.write("M84 E0\n")
112                                 #Wait till the user continues printing
113                                 f.write("M0\n")
114
115                                 #Re-home the axes, in case the operator has perhaps jostled the printer
116                                 if homeX or homeY:
117                                         g1 = "G1"
118                                         g28 = "G28"
119                                         if homeX:
120                                                 if parkX > 25: g1 += " X25"
121                                                 g28 += " X"
122                                         if homeY:
123                                                 if parkY > 25: g1 += " Y25"
124                                                 g28 += ' Y'
125                                         f.write(g1 + " F9000\n")
126                                         f.write(g28 + "\n")
127
128                                 #Push the filament back, and retract again, the properly primes the nozzle when changing filament.
129                                 f.write("G1 E%f F6000\n" % (retractAmount))
130                                 f.write("G1 E-%f F6000\n" % (retractAmount))
131
132                                 #Move the head back. Move Z at the same time to prevent hitting the glass locks on the UM2
133                                 if zChanged :
134                                         f.write("G1 X%f Y%f Z%f F9000\n" % (x, y, z))
135                                 else:
136                                         f.write("G1 X%f Y%f F9000\n" % (x, y))
137
138                                 f.write("G1 E%f F6000\n" % (retractAmount))
139                                 f.write("G1 F9000\n")
140                                 f.write("M82\n")
141
142                 f.write(line)