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 extrudeAmountMultiply = 1.0
60 totalMoveTimeMinute = 0.0
68 currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
69 currentPath.list[0].e = totalExtrusion
70 currentPath.list[0].extrudeAmountMultiply = extrudeAmountMultiply
71 currentLayer.append(currentPath)
72 for line in gcodeFile:
73 if type(line) is tuple:
75 if self.progressCallback != None:
76 if filePos != gcodeFile.tell():
77 filePos = gcodeFile.tell()
78 self.progressCallback(float(filePos) / float(self._fileSize))
80 #Parse Cura_SF comments
81 if line.startswith(';TYPE:'):
82 pathType = line[6:].strip()
83 if pathType != "CUSTOM":
87 #Slic3r GCode comment parser
88 comment = line[line.find(';')+1:].strip()
91 elif comment == 'perimeter':
92 pathType = 'WALL-INNER'
93 elif comment == 'skirt':
95 if comment.startswith('LAYER:'):
96 self.layerList.append(currentLayer)
98 if pathType != "CUSTOM":
100 line = line[0:line.find(';')]
101 T = self.getCodeInt(line, 'T')
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 if currentExtruder > 0:
108 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
109 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
111 G = self.getCodeInt(line, 'G')
113 if G == 0 or G == 1: #Move
114 x = self.getCodeFloat(line, 'X')
115 y = self.getCodeFloat(line, 'Y')
116 z = self.getCodeFloat(line, 'Z')
117 e = self.getCodeFloat(line, 'E')
118 f = self.getCodeFloat(line, 'F')
122 pos.x = x * scale + posOffset.x
127 pos.y = y * scale + posOffset.y
132 pos.z = z * scale + posOffset.z
137 if x is not None or y is not None or z is not None:
138 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
151 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 newPos.extrudeAmountMultiply = extrudeAmountMultiply
165 currentPath.list.append(newPos)
167 S = self.getCodeFloat(line, 'S')
169 totalMoveTimeMinute += S / 60
170 P = self.getCodeFloat(line, 'P')
172 totalMoveTimeMinute += P / 60 / 1000
173 elif G == 20: #Units are inches
175 elif G == 21: #Units are mm
178 x = self.getCodeFloat(line, 'X')
179 y = self.getCodeFloat(line, 'Y')
180 z = self.getCodeFloat(line, 'Z')
181 if x is None and y is None and z is None:
182 pos = util3d.Vector3()
190 elif G == 90: #Absolute position
192 elif G == 91: #Relative position
195 x = self.getCodeFloat(line, 'X')
196 y = self.getCodeFloat(line, 'Y')
197 z = self.getCodeFloat(line, 'Z')
198 e = self.getCodeFloat(line, 'E')
202 posOffset.x = pos.x - x
204 posOffset.y = pos.y - y
206 posOffset.z = pos.z - z
208 print "Unknown G code:" + str(G)
210 M = self.getCodeInt(line, 'M')
212 if M == 1: #Message with possible wait (ignored)
214 elif M == 80: #Enable power supply
216 elif M == 81: #Suicide/disable power supply
218 elif M == 84: #Disable step drivers
220 elif M == 92: #Set steps per unit
222 elif M == 101: #Enable extruder
224 elif M == 103: #Disable extruder
226 elif M == 104: #Set temperature, no wait
228 elif M == 105: #Get temperature
230 elif M == 106: #Enable fan
232 elif M == 107: #Disable fan
234 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
236 elif M == 109: #Set temperature, wait
238 elif M == 110: #Reset N counter
240 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
242 elif M == 140: #Set bed temperature
244 elif M == 190: #Set bed temperature & wait
246 elif M == 221: #Extrude amount multiplier
247 s = self.getCodeFloat(line, 'S')
249 extrudeAmountMultiply = s / 100.0
251 print "Unknown M code:" + str(M)
252 self.layerList.append(currentLayer)
253 self.extrusionAmount = maxExtrusion
254 self.totalMoveTimeMinute = totalMoveTimeMinute
255 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
256 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
258 def getCodeInt(self, line, code):
259 if code not in self.regMatch:
260 self.regMatch[code] = re.compile(code + '([^\s]+)')
261 m = self.regMatch[code].search(line)
265 return int(m.group(1))
269 def getCodeFloat(self, line, code):
270 if code not in self.regMatch:
271 self.regMatch[code] = re.compile(code + '([^\s]+)')
272 m = self.regMatch[code].search(line)
276 return float(m.group(1))
280 if __name__ == '__main__':
281 for filename in sys.argv[1:]:
282 gcode().load(filename)