chiark / gitweb /
Fix pyserial 3.0 compatibility issue
[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: retractAmount(float:1) Retraction amount (mm)
10
11 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
12 import re
13 from Cura.util import profile
14
15 def getPrintZValue(lineBlock):
16         '''
17         look for the last z value found just before (or at the same time) G1 code in the given block
18         '''
19         lastZ = -1
20         for line in lineBlock:
21                 lastZ = getValue(line, 'Z', lastZ)
22                 if line.startswith('G1 ') and (getValue(line, 'X', None) is not None or getValue(line, 'Y', None) is not None):
23                         break
24
25         return lastZ
26
27
28 def getValue(line, key, default = None):
29         if not key in line or (';' in line and line.find(key) > line.find(';')):
30                 return default
31         subPart = line[line.find(key) + 1:]
32         m = re.search('^[0-9]+\.?[0-9]*', subPart)
33         if m is None:
34                 return default
35         try:
36                 return float(m.group(0))
37         except:
38                 return default
39
40 with open(filename, "r") as f:
41         lines = f.readlines()
42
43 z = 0.
44 x = 0.
45 y = 0.
46 pauseState = 0
47 #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.)
48 #state 1 system is active and we are looking for our target layer z
49 #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
50
51
52 with open(filename, "w") as f:
53         lineIndex = 0
54         lastLayerIndex = 99999
55         layerZ = 0
56         for lIndex in xrange(len(lines)):
57                 line = lines[lIndex]
58                 if line.startswith(';'):
59                         if line.startswith(';LAYER:'):
60                                 currentLayer = int(line[7:].strip())
61
62                                 if currentLayer < lastLayerIndex:
63                                         pauseState = 1
64
65                                 lastLayerIndex = currentLayer
66                                 if pauseState == 1:
67                                         layerZ = getPrintZValue(lines[lIndex:lIndex+20])
68                                         if layerZ >= pauseLevel:
69                                                 pauseState = 2
70
71                         f.write(line)
72                         continue
73
74                 x = getValue(line, 'X', x)
75                 y = getValue(line, 'Y', y)
76
77                 if pauseState == 2:
78                         g = getValue(line, 'G', None)
79                         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.
80                                 z = layerZ
81
82                                 pauseState = 0
83                                 f.write(";TYPE:CUSTOM\n")
84                                 #Retract
85                                 f.write("M83\n")
86                                 f.write("G1 E-%f F6000\n" % (retractAmount))
87
88                                 zChanged = False
89                                 #Change z before doing the move because the nozzle can hit the glass lock on the UM2
90                                 if z + moveZ < 15:
91                                         zChanged = True
92                                         f.write("G1 Z15 F300\n")
93
94                                 elif moveZ > 0:
95                                         newZ = z + moveZ
96                                         maxZ = profile.getMachineSettingFloat('machine_height') - 10 #For Safety Leave a 10mm space (endstop)
97                                         if maxZ < newZ:
98                                                 newZ = maxZ
99
100                                         if newZ > z:
101                                                 zChanged = True
102                                                 f.write("G1 Z%f F300\n" % (newZ))
103
104                                 #Move the head away
105                                 f.write("G1 X%f Y%f F9000\n" % (parkX, parkY))
106
107                                 #Disable the E steppers
108                                 f.write("M84 E0\n")
109                                 #Wait till the user continues printing
110                                 f.write("M0\n")
111                                 #Push the filament back, and retract again, the properly primes the nozzle when changing filament.
112                                 f.write("G1 E%f F6000\n" % (retractAmount))
113                                 f.write("G1 E-%f F6000\n" % (retractAmount))
114
115                                 #Move the head back. Move Z at the same time to prevent hitting the glass locks on the UM2
116                                 if zChanged :
117                                         f.write("G1 X%f Y%f Z%f F9000\n" % (x, y, z))
118                                 else:
119                                         f.write("G1 X%f Y%f F9000\n" % (x, y))
120
121                                 f.write("G1 E%f F6000\n" % (retractAmount))
122                                 f.write("G1 F9000\n")
123                                 f.write("M82\n")
124
125                 f.write(line)