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 self._fileSize = os.stat(filename).st_size
26 gcodeFile = open(filename, 'r')
30 def loadList(self, l):
33 def calculateWeight(self):
34 #Calculates the weight of the filament in kg
35 radius = float(profile.getProfileSetting('filament_diameter')) / 2
36 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
37 return volumeM3 * profile.getPreferenceFloat('filament_density')
39 def calculateCost(self):
40 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
41 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
42 if cost_kg > 0.0 and cost_meter > 0.0:
43 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
45 return "%.2f" % (self.calculateWeight() * cost_kg)
46 elif cost_meter > 0.0:
47 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
50 def _load(self, gcodeFile):
52 pos = util3d.Vector3()
53 posOffset = util3d.Vector3()
58 totalMoveTimeMinute = 0.0
66 currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
67 currentPath.list[0].e = totalExtrusion
68 currentLayer.append(currentPath)
69 for line in gcodeFile:
70 if self.progressCallback != None:
71 if filePos != gcodeFile.tell():
72 filePos = gcodeFile.tell()
73 self.progressCallback(float(filePos) / float(self._fileSize))
75 #Parse Cura_SF comments
76 if line.startswith(';TYPE:'):
77 pathType = line[6:].strip()
78 if pathType != "CUSTOM":
82 #Slic3r GCode comment parser
83 comment = line[line.find(';')+1:].strip()
86 elif comment == 'perimeter':
87 pathType = 'WALL-INNER'
88 elif comment == 'skirt':
90 if comment.startswith('LAYER:'):
91 self.layerList.append(currentLayer)
93 if pathType != "CUSTOM":
95 line = line[0:line.find(';')]
96 T = self.getCodeInt(line, 'T')
98 if currentExtruder > 0:
99 posOffset.x -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
100 posOffset.y -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
102 if currentExtruder > 0:
103 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
104 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
106 G = self.getCodeInt(line, 'G')
108 if G == 0 or G == 1: #Move
109 x = self.getCodeFloat(line, 'X')
110 y = self.getCodeFloat(line, 'Y')
111 z = self.getCodeFloat(line, 'Z')
112 e = self.getCodeFloat(line, 'E')
113 f = self.getCodeFloat(line, 'F')
117 pos.x = x * scale + posOffset.x
122 pos.y = y * scale + posOffset.y
127 pos.z = z * scale + posOffset.z
132 if x is not None or y is not None or z is not None:
133 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
141 totalExtrusion += e - currentE
150 if totalExtrusion > maxExtrusion:
151 maxExtrusion = totalExtrusion
152 if moveType == 'move' and oldPos.z != pos.z:
153 layerThickness = abs(oldPos.z - pos.z)
154 if currentPath.type != moveType or currentPath.pathType != pathType:
155 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
156 currentLayer.append(currentPath)
158 newPos.e = totalExtrusion
159 currentPath.list.append(newPos)
160 elif G == 20: #Units are inches
162 elif G == 21: #Units are mm
165 x = self.getCodeFloat(line, 'X')
166 y = self.getCodeFloat(line, 'Y')
167 z = self.getCodeFloat(line, 'Z')
168 if x is None and y is None and z is None:
169 pos = util3d.Vector3()
177 elif G == 90: #Absolute position
179 elif G == 91: #Relative position
182 x = self.getCodeFloat(line, 'X')
183 y = self.getCodeFloat(line, 'Y')
184 z = self.getCodeFloat(line, 'Z')
185 e = self.getCodeFloat(line, 'E')
189 posOffset.x = pos.x + x
191 posOffset.y = pos.y + y
193 posOffset.z = pos.z + z
195 print "Unknown G code:" + str(G)
197 M = self.getCodeInt(line, 'M')
199 if M == 1: #Message with possible wait (ignored)
201 elif M == 84: #Disable step drivers
203 elif M == 92: #Set steps per unit
205 elif M == 104: #Set temperature, no wait
207 elif M == 105: #Get temperature
209 elif M == 106: #Enable fan
211 elif M == 107: #Disable fan
213 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
215 elif M == 109: #Set temperature, wait
217 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
220 print "Unknown M code:" + str(M)
221 self.layerList.append(currentLayer)
222 self.extrusionAmount = maxExtrusion
223 self.totalMoveTimeMinute = totalMoveTimeMinute
224 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
225 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
227 def getCodeInt(self, line, code):
228 if code not in self.regMatch:
229 self.regMatch[code] = re.compile(code + '([^\s]+)')
230 m = self.regMatch[code].search(line)
234 return int(m.group(1))
238 def getCodeFloat(self, line, code):
239 if code not in self.regMatch:
240 self.regMatch[code] = re.compile(code + '([^\s]+)')
241 m = self.regMatch[code].search(line)
245 return float(m.group(1))
249 if __name__ == '__main__':
250 for filename in sys.argv[1:]:
251 gcode().load(filename)