9 class gcodePath(object):
10 def __init__(self, newType, pathType, layerThickness, startPoint):
12 self.pathType = pathType
13 self.layerThickness = layerThickness
14 self.list = [startPoint]
20 self.extrusionAmount = 0
21 self.totalMoveTimeMinute = 0
22 self.progressCallback = None
24 def load(self, filename):
25 if os.path.isfile(filename):
26 self._fileSize = os.stat(filename).st_size
27 gcodeFile = open(filename, 'r')
31 def loadList(self, l):
34 def calculateWeight(self):
35 #Calculates the weight of the filament in kg
36 radius = float(profile.getProfileSetting('filament_diameter')) / 2
37 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
38 return volumeM3 * profile.getPreferenceFloat('filament_density')
40 def calculateCost(self):
41 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
42 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
43 if cost_kg > 0.0 and cost_meter > 0.0:
44 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
46 return "%.2f" % (self.calculateWeight() * cost_kg)
47 elif cost_meter > 0.0:
48 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
51 def _load(self, gcodeFile):
53 pos = util3d.Vector3()
54 posOffset = util3d.Vector3()
59 totalMoveTimeMinute = 0.0
67 currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
68 currentPath.list[0].e = totalExtrusion
69 currentLayer.append(currentPath)
70 for line in gcodeFile:
71 if type(line) is tuple:
73 if self.progressCallback != None:
74 if filePos != gcodeFile.tell():
75 filePos = gcodeFile.tell()
76 self.progressCallback(float(filePos) / float(self._fileSize))
78 #Parse Cura_SF comments
79 if line.startswith(';TYPE:'):
80 pathType = line[6:].strip()
81 if pathType != "CUSTOM":
85 #Slic3r GCode comment parser
86 comment = line[line.find(';')+1:].strip()
89 elif comment == 'perimeter':
90 pathType = 'WALL-INNER'
91 elif comment == 'skirt':
93 if comment.startswith('LAYER:'):
94 self.layerList.append(currentLayer)
96 if pathType != "CUSTOM":
98 line = line[0:line.find(';')]
99 T = self.getCodeInt(line, 'T')
101 if currentExtruder > 0:
102 posOffset.x -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
103 posOffset.y -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
105 if currentExtruder > 0:
106 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
107 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
109 G = self.getCodeInt(line, 'G')
111 if G == 0 or G == 1: #Move
112 x = self.getCodeFloat(line, 'X')
113 y = self.getCodeFloat(line, 'Y')
114 z = self.getCodeFloat(line, 'Z')
115 e = self.getCodeFloat(line, 'E')
116 f = self.getCodeFloat(line, 'F')
120 pos.x = x * scale + posOffset.x
125 pos.y = y * scale + posOffset.y
130 pos.z = z * scale + posOffset.z
135 if x is not None or y is not None or z is not None:
136 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
144 totalExtrusion += e - currentE
153 if totalExtrusion > maxExtrusion:
154 maxExtrusion = totalExtrusion
155 if moveType == 'move' and oldPos.z != pos.z:
156 if oldPos.z > pos.z and abs(oldPos.z - pos.z) > 5.0 and pos.z < 1.0:
158 layerThickness = abs(oldPos.z - pos.z)
159 if currentPath.type != moveType or currentPath.pathType != pathType:
160 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
161 currentLayer.append(currentPath)
163 newPos.e = totalExtrusion
164 currentPath.list.append(newPos)
166 S = self.getCodeFloat(line, 'S')
168 totalMoveTimeMinute += S / 60
169 P = self.getCodeFloat(line, 'P')
171 totalMoveTimeMinute += P / 60 / 1000
172 elif G == 20: #Units are inches
174 elif G == 21: #Units are mm
177 x = self.getCodeFloat(line, 'X')
178 y = self.getCodeFloat(line, 'Y')
179 z = self.getCodeFloat(line, 'Z')
180 if x is None and y is None and z is None:
181 pos = util3d.Vector3()
189 elif G == 90: #Absolute position
191 elif G == 91: #Relative position
194 x = self.getCodeFloat(line, 'X')
195 y = self.getCodeFloat(line, 'Y')
196 z = self.getCodeFloat(line, 'Z')
197 e = self.getCodeFloat(line, 'E')
201 posOffset.x = pos.x - x
203 posOffset.y = pos.y - y
205 posOffset.z = pos.z - z
207 print "Unknown G code:" + str(G)
209 M = self.getCodeInt(line, 'M')
211 if M == 1: #Message with possible wait (ignored)
213 elif M == 80: #Enable power supply
215 elif M == 81: #Suicide/disable power supply
217 elif M == 84: #Disable step drivers
219 elif M == 92: #Set steps per unit
221 elif M == 104: #Set temperature, no wait
223 elif M == 105: #Get temperature
225 elif M == 106: #Enable fan
227 elif M == 107: #Disable fan
229 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
231 elif M == 109: #Set temperature, wait
233 elif M == 110: #Reset N counter
235 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
237 elif M == 140: #Set bed temperature
239 elif M == 190: #Set bed temperature & wait
242 print "Unknown M code:" + str(M)
243 self.layerList.append(currentLayer)
244 self.extrusionAmount = maxExtrusion
245 self.totalMoveTimeMinute = totalMoveTimeMinute
246 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
247 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
249 def getCodeInt(self, line, code):
250 if code not in self.regMatch:
251 self.regMatch[code] = re.compile(code + '([^\s]+)')
252 m = self.regMatch[code].search(line)
256 return int(m.group(1))
260 def getCodeFloat(self, line, code):
261 if code not in self.regMatch:
262 self.regMatch[code] = re.compile(code + '([^\s]+)')
263 m = self.regMatch[code].search(line)
267 return float(m.group(1))
271 if __name__ == '__main__':
272 for filename in sys.argv[1:]:
273 gcode().load(filename)