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 self.progressCallback != None:
72 if filePos != gcodeFile.tell():
73 filePos = gcodeFile.tell()
74 self.progressCallback(float(filePos) / float(self._fileSize))
76 #Parse Cura_SF comments
77 if line.startswith(';TYPE:'):
78 pathType = line[6:].strip()
79 if pathType != "CUSTOM":
83 #Slic3r GCode comment parser
84 comment = line[line.find(';')+1:].strip()
87 elif comment == 'perimeter':
88 pathType = 'WALL-INNER'
89 elif comment == 'skirt':
91 if comment.startswith('LAYER:'):
92 self.layerList.append(currentLayer)
94 if pathType != "CUSTOM":
96 line = line[0:line.find(';')]
97 T = self.getCodeInt(line, 'T')
99 if currentExtruder > 0:
100 posOffset.x -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
101 posOffset.y -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
103 if currentExtruder > 0:
104 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
105 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
107 G = self.getCodeInt(line, 'G')
109 if G == 0 or G == 1: #Move
110 x = self.getCodeFloat(line, 'X')
111 y = self.getCodeFloat(line, 'Y')
112 z = self.getCodeFloat(line, 'Z')
113 e = self.getCodeFloat(line, 'E')
114 f = self.getCodeFloat(line, 'F')
118 pos.x = x * scale + posOffset.x
123 pos.y = y * scale + posOffset.y
128 pos.z = z * scale + posOffset.z
133 if x is not None or y is not None or z is not None:
134 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
142 totalExtrusion += e - currentE
151 if totalExtrusion > maxExtrusion:
152 maxExtrusion = totalExtrusion
153 if moveType == 'move' and oldPos.z != pos.z:
154 layerThickness = abs(oldPos.z - pos.z)
155 if currentPath.type != moveType or currentPath.pathType != pathType:
156 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
157 currentLayer.append(currentPath)
159 newPos.e = totalExtrusion
160 currentPath.list.append(newPos)
161 elif G == 20: #Units are inches
163 elif G == 21: #Units are mm
166 x = self.getCodeFloat(line, 'X')
167 y = self.getCodeFloat(line, 'Y')
168 z = self.getCodeFloat(line, 'Z')
169 if x is None and y is None and z is None:
170 pos = util3d.Vector3()
178 elif G == 90: #Absolute position
180 elif G == 91: #Relative position
183 x = self.getCodeFloat(line, 'X')
184 y = self.getCodeFloat(line, 'Y')
185 z = self.getCodeFloat(line, 'Z')
186 e = self.getCodeFloat(line, 'E')
190 posOffset.x = pos.x + x
192 posOffset.y = pos.y + y
194 posOffset.z = pos.z + z
196 print "Unknown G code:" + str(G)
198 M = self.getCodeInt(line, 'M')
200 if M == 1: #Message with possible wait (ignored)
202 elif M == 84: #Disable step drivers
204 elif M == 92: #Set steps per unit
206 elif M == 104: #Set temperature, no wait
208 elif M == 105: #Get temperature
210 elif M == 106: #Enable fan
212 elif M == 107: #Disable fan
214 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
216 elif M == 109: #Set temperature, wait
218 elif M == 110: #Reset N counter
220 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
223 print "Unknown M code:" + str(M)
224 self.layerList.append(currentLayer)
225 self.extrusionAmount = maxExtrusion
226 self.totalMoveTimeMinute = totalMoveTimeMinute
227 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
228 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
230 def getCodeInt(self, line, code):
231 if code not in self.regMatch:
232 self.regMatch[code] = re.compile(code + '([^\s]+)')
233 m = self.regMatch[code].search(line)
237 return int(m.group(1))
241 def getCodeFloat(self, line, code):
242 if code not in self.regMatch:
243 self.regMatch[code] = re.compile(code + '([^\s]+)')
244 m = self.regMatch[code].search(line)
248 return float(m.group(1))
252 if __name__ == '__main__':
253 for filename in sys.argv[1:]:
254 gcode().load(filename)