1 from __future__ import absolute_import
9 from util import util3d
12 def __init__(self, newType, pathType, startPoint):
14 self.pathType = pathType
15 self.list = [startPoint]
21 self.extrusionAmount = 0
22 self.totalMoveTimeMinute = 0
23 self.progressCallback = None
25 def load(self, filename):
26 self._fileSize = os.stat(filename).st_size
27 gcodeFile = open(filename, 'r')
31 def loadList(self, l):
34 def _load(self, gcodeFile):
36 pos = util3d.Vector3()
37 posOffset = util3d.Vector3()
41 totalMoveTimeMinute = 0.0
48 currentPath = gcodePath('move', pathType, pos.copy())
49 currentPath.list[0].e = totalExtrusion
50 currentLayer.append(currentPath)
51 for line in gcodeFile:
52 if self.progressCallback != None:
53 if filePos != gcodeFile.tell():
54 filePos = gcodeFile.tell()
55 self.progressCallback(float(filePos) / float(self._fileSize))
57 #Parse Cura_SF comments
58 if line.startswith(';TYPE:'):
59 pathType = line[6:].strip()
60 if pathType != "CUSTOM":
64 #Slic3r GCode comment parser
65 comment = line[line.find(';')+1:].strip()
68 elif comment == 'perimeter':
69 pathType = 'WALL-INNER'
70 elif comment == 'skirt':
72 if pathType != "CUSTOM":
74 line = line[0:line.find(';')]
76 G = self.getCodeInt(line, 'G')
78 if G == 0 or G == 1: #Move
79 x = self.getCodeFloat(line, 'X')
80 y = self.getCodeFloat(line, 'Y')
81 z = self.getCodeFloat(line, 'Z')
82 e = self.getCodeFloat(line, 'E')
83 f = self.getCodeFloat(line, 'F')
100 #Check if we have a new layer.
101 if oldPos.z != pos.z and startCodeDone:
102 self.layerList.append(currentLayer)
106 if x is not None or y is not None or z is not None:
107 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
115 totalExtrusion += e - currentE
124 if totalExtrusion > maxExtrusion:
125 maxExtrusion = totalExtrusion
126 if currentPath.type != moveType or currentPath.pathType != pathType:
127 currentPath = gcodePath(moveType, pathType, currentPath.list[-1])
128 currentLayer.append(currentPath)
130 newPos.e = totalExtrusion
131 currentPath.list.append(newPos)
132 elif G == 20: #Units are inches
134 elif G == 21: #Units are mm
137 x = self.getCodeFloat(line, 'X')
138 y = self.getCodeFloat(line, 'Y')
139 z = self.getCodeFloat(line, 'Z')
140 if x is None and y is None and z is None:
141 pos = util3d.Vector3()
149 elif G == 90: #Absolute position
151 elif G == 91: #Relative position
154 x = self.getCodeFloat(line, 'X')
155 y = self.getCodeFloat(line, 'Y')
156 z = self.getCodeFloat(line, 'Z')
157 e = self.getCodeFloat(line, 'E')
161 posOffset.x = pos.x + x
163 posOffset.y = pos.y + y
165 posOffset.z = pos.z + z
167 print "Unknown G code:" + str(G)
169 M = self.getCodeInt(line, 'M')
171 if M == 1: #Message with possible wait (ignored)
173 elif M == 84: #Disable step drivers
175 elif M == 92: #Set steps per unit
177 elif M == 104: #Set temperature, no wait
179 elif M == 105: #Get temperature
181 elif M == 106: #Enable fan
183 elif M == 107: #Disable fan
185 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
187 elif M == 109: #Set temperature, wait
189 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
192 print "Unknown M code:" + str(M)
193 self.layerList.append(currentLayer)
194 self.extrusionAmount = maxExtrusion
195 self.totalMoveTimeMinute = totalMoveTimeMinute
196 print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
197 print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
199 def getCodeInt(self, line, code):
200 if code not in self.regMatch:
201 self.regMatch[code] = re.compile(code + '([^\s]+)')
202 m = self.regMatch[code].search(line)
206 return int(m.group(1))
210 def getCodeFloat(self, line, code):
211 if code not in self.regMatch:
212 self.regMatch[code] = re.compile(code + '([^\s]+)')
213 m = self.regMatch[code].search(line)
217 return float(m.group(1))
221 if __name__ == '__main__':
222 for filename in sys.argv[1:]:
223 gcode().load(filename)