1 from __future__ import absolute_import
8 from Cura.util import util3d
9 from Cura.util import profile
11 class gcodePath(object):
12 def __init__(self, newType, pathType, layerThickness, startPoint):
14 self.pathType = pathType
15 self.layerThickness = layerThickness
16 self.list = [startPoint]
22 self.extrusionAmount = 0
23 self.totalMoveTimeMinute = 0
24 self.progressCallback = None
26 def load(self, filename):
27 if os.path.isfile(filename):
28 self._fileSize = os.stat(filename).st_size
29 gcodeFile = open(filename, 'r')
33 def loadList(self, l):
36 def calculateWeight(self):
37 #Calculates the weight of the filament in kg
38 radius = float(profile.getProfileSetting('filament_diameter')) / 2
39 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
40 return volumeM3 * profile.getPreferenceFloat('filament_density')
42 def calculateCost(self):
43 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
44 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
45 if cost_kg > 0.0 and cost_meter > 0.0:
46 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
48 return "%.2f" % (self.calculateWeight() * cost_kg)
49 elif cost_meter > 0.0:
50 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
53 def _load(self, gcodeFile):
55 pos = util3d.Vector3()
56 posOffset = util3d.Vector3()
61 extrudeAmountMultiply = 1.0
62 totalMoveTimeMinute = 0.0
71 currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
72 currentPath.list[0].e = totalExtrusion
73 currentPath.list[0].extrudeAmountMultiply = extrudeAmountMultiply
74 currentLayer.append(currentPath)
75 for line in gcodeFile:
76 if type(line) is tuple:
78 if self.progressCallback != None:
79 if filePos != gcodeFile.tell():
80 filePos = gcodeFile.tell()
81 self.progressCallback(float(filePos) / float(self._fileSize))
83 #Parse Cura_SF comments
84 if line.startswith(';TYPE:'):
85 pathType = line[6:].strip()
86 if pathType != "CUSTOM":
90 #Slic3r GCode comment parser
91 comment = line[line.find(';')+1:].strip()
94 elif comment == 'perimeter':
95 pathType = 'WALL-INNER'
96 elif comment == 'skirt':
98 if comment.startswith('LAYER:'):
99 self.layerList.append(currentLayer)
101 if pathType != "CUSTOM":
103 line = line[0:line.find(';')]
104 T = self.getCodeInt(line, 'T')
106 if currentExtruder > 0:
107 posOffset.x -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
108 posOffset.y -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
110 if currentExtruder > 0:
111 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
112 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
114 G = self.getCodeInt(line, 'G')
116 if G == 0 or G == 1: #Move
117 x = self.getCodeFloat(line, 'X')
118 y = self.getCodeFloat(line, 'Y')
119 z = self.getCodeFloat(line, 'Z')
120 e = self.getCodeFloat(line, 'E')
121 f = self.getCodeFloat(line, 'F')
125 pos.x = x * scale + posOffset.x
130 pos.y = y * scale + posOffset.y
135 pos.z = z * scale + posOffset.z
140 if x is not None or y is not None or z is not None:
141 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
156 totalExtrusion += e - currentE
158 if totalExtrusion > maxExtrusion:
159 maxExtrusion = totalExtrusion
160 if moveType == 'move' and oldPos.z != pos.z:
161 if oldPos.z > pos.z and abs(oldPos.z - pos.z) > 5.0 and pos.z < 1.0:
163 layerThickness = abs(oldPos.z - pos.z)
164 if currentPath.type != moveType or currentPath.pathType != pathType:
165 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
166 currentLayer.append(currentPath)
168 newPos.e = totalExtrusion
169 newPos.extrudeAmountMultiply = extrudeAmountMultiply
170 currentPath.list.append(newPos)
172 S = self.getCodeFloat(line, 'S')
174 totalMoveTimeMinute += S / 60
175 P = self.getCodeFloat(line, 'P')
177 totalMoveTimeMinute += P / 60 / 1000
178 elif G == 20: #Units are inches
180 elif G == 21: #Units are mm
183 x = self.getCodeFloat(line, 'X')
184 y = self.getCodeFloat(line, 'Y')
185 z = self.getCodeFloat(line, 'Z')
186 if x is None and y is None and z is None:
187 pos = util3d.Vector3()
195 elif G == 90: #Absolute position
197 elif G == 91: #Relative position
200 x = self.getCodeFloat(line, 'X')
201 y = self.getCodeFloat(line, 'Y')
202 z = self.getCodeFloat(line, 'Z')
203 e = self.getCodeFloat(line, 'E')
207 posOffset.x = pos.x - x
209 posOffset.y = pos.y - y
211 posOffset.z = pos.z - z
213 print "Unknown G code:" + str(G)
215 M = self.getCodeInt(line, 'M')
217 if M == 1: #Message with possible wait (ignored)
219 elif M == 80: #Enable power supply
221 elif M == 81: #Suicide/disable power supply
223 elif M == 82: #Absolute E
225 elif M == 83: #Relative E
227 elif M == 84: #Disable step drivers
229 elif M == 92: #Set steps per unit
231 elif M == 101: #Enable extruder
233 elif M == 103: #Disable extruder
235 elif M == 104: #Set temperature, no wait
237 elif M == 105: #Get temperature
239 elif M == 106: #Enable fan
241 elif M == 107: #Disable fan
243 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
245 elif M == 109: #Set temperature, wait
247 elif M == 110: #Reset N counter
249 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
251 elif M == 140: #Set bed temperature
253 elif M == 190: #Set bed temperature & wait
255 elif M == 221: #Extrude amount multiplier
256 s = self.getCodeFloat(line, 'S')
258 extrudeAmountMultiply = s / 100.0
260 print "Unknown M code:" + str(M)
261 self.layerList.append(currentLayer)
262 self.extrusionAmount = maxExtrusion
263 self.totalMoveTimeMinute = totalMoveTimeMinute
264 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
265 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
267 def getCodeInt(self, line, code):
268 if code not in self.regMatch:
269 self.regMatch[code] = re.compile(code + '([^\s]+)')
270 m = self.regMatch[code].search(line)
274 return int(m.group(1))
278 def getCodeFloat(self, line, code):
279 if code not in self.regMatch:
280 self.regMatch[code] = re.compile(code + '([^\s]+)')
281 m = self.regMatch[code].search(line)
285 return float(m.group(1))
289 if __name__ == '__main__':
290 for filename in sys.argv[1:]:
291 gcode().load(filename)