1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
10 import cStringIO as StringIO
12 from Cura.util import profile
14 def gcodePath(newType, pathType, layerThickness, startPoint):
15 return {'type': newType,
17 'layerThickness': layerThickness,
18 'points': [startPoint],
25 self.extrusionAmount = 0
27 self.progressCallback = None
31 if type(data) in types.StringTypes and os.path.isfile(data):
33 self._fileSize = os.stat(data).st_size
34 gcodeFile = open(data, 'r')
37 elif type(data) is list:
40 self._fileSize = len(data.getvalue())
41 self._load(StringIO.StringIO(data.getvalue()))
43 def calculateWeight(self):
44 #Calculates the weight of the filament in kg
45 radius = float(profile.getProfileSetting('filament_diameter')) / 2
46 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
47 return volumeM3 * profile.getPreferenceFloat('filament_physical_density')
49 def calculateCost(self):
50 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
51 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
52 if cost_kg > 0.0 and cost_meter > 0.0:
53 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
55 return "%.2f" % (self.calculateWeight() * cost_kg)
56 elif cost_meter > 0.0:
57 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
60 def _load(self, gcodeFile):
63 posOffset = [0.0, 0.0, 0.0]
66 extrudeAmountMultiply = 1.0
75 currentPath = gcodePath('move', pathType, layerThickness, pos)
76 currentPath['extruder'] = currentExtruder
78 currentLayer.append(currentPath)
79 for line in gcodeFile:
80 if type(line) is tuple:
83 #Parse Cura_SF comments
84 if line.startswith(';TYPE:'):
85 pathType = line[6:].strip()
88 #Slic3r GCode comment parser
89 comment = line[line.find(';')+1:].strip()
92 elif comment == 'perimeter':
93 pathType = 'WALL-INNER'
94 elif comment == 'skirt':
96 if comment.startswith('LAYER:'):
97 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath['points'][-1])
98 currentPath['extruder'] = currentExtruder
99 for path in currentLayer:
100 path['points'] = numpy.array(path['points'], numpy.float32)
101 path['extrusion'] = numpy.array(path['extrusion'], numpy.float32)
102 self.layerList.append(currentLayer)
103 if self.progressCallback is not None:
104 if self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize)):
105 #Abort the loading, we can safely return as the results here will be discarded
108 currentLayer = [currentPath]
109 line = line[0:line.find(';')]
110 T = getCodeInt(line, 'T')
112 if currentExtruder > 0:
113 posOffset[0] -= profile.getMachineSettingFloat('extruder_offset_x%d' % (currentExtruder))
114 posOffset[1] -= profile.getMachineSettingFloat('extruder_offset_y%d' % (currentExtruder))
116 if currentExtruder > 0:
117 posOffset[0] += profile.getMachineSettingFloat('extruder_offset_x%d' % (currentExtruder))
118 posOffset[1] += profile.getMachineSettingFloat('extruder_offset_y%d' % (currentExtruder))
120 G = getCodeInt(line, 'G')
122 if G == 0 or G == 1: #Move
123 x = getCodeFloat(line, 'X')
124 y = getCodeFloat(line, 'Y')
125 z = getCodeFloat(line, 'Z')
126 e = getCodeFloat(line, 'E')
127 #f = getCodeFloat(line, 'F')
132 pos[0] = x * scale + posOffset[0]
134 pos[1] = y * scale + posOffset[1]
136 pos[2] = z * scale + posOffset[2]
155 if moveType == 'move' and oldPos[2] != pos[2]:
156 if oldPos[2] > pos[2] and abs(oldPos[2] - pos[2]) > 5.0 and pos[2] < 1.0:
158 layerThickness = abs(oldPos[2] - pos[2])
159 if currentPath['type'] != moveType or currentPath['pathType'] != pathType:
160 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath['points'][-1])
161 currentPath['extruder'] = currentExtruder
162 currentLayer.append(currentPath)
164 currentPath['points'].append(pos)
165 currentPath['extrusion'].append(e * extrudeAmountMultiply)
167 S = getCodeFloat(line, 'S')
168 P = getCodeFloat(line, 'P')
169 elif G == 10: #Retract
170 currentPath = gcodePath('retract', pathType, layerThickness, currentPath['points'][-1])
171 currentPath['extruder'] = currentExtruder
172 currentLayer.append(currentPath)
173 currentPath['points'].append(currentPath['points'][0])
174 elif G == 11: #Push back after retract
176 elif G == 20: #Units are inches
178 elif G == 21: #Units are mm
181 x = getCodeFloat(line, 'X')
182 y = getCodeFloat(line, 'Y')
183 z = getCodeFloat(line, 'Z')
184 center = [0.0,0.0,0.0]
185 if x is None and y is None and z is None:
195 elif G == 90: #Absolute position
197 elif G == 91: #Relative position
200 x = getCodeFloat(line, 'X')
201 y = getCodeFloat(line, 'Y')
202 z = getCodeFloat(line, 'Z')
203 e = getCodeFloat(line, 'E')
207 posOffset[0] = pos[0] - x
209 posOffset[1] = pos[1] - y
211 posOffset[2] = pos[2] - z
213 print "Unknown G code:" + str(G)
215 M = getCodeInt(line, 'M')
217 if M == 0: #Message with possible wait (ignored)
219 elif M == 1: #Message with possible wait (ignored)
221 elif M == 25: #Stop SD printing
223 elif M == 80: #Enable power supply
225 elif M == 81: #Suicide/disable power supply
227 elif M == 82: #Absolute E
229 elif M == 83: #Relative E
231 elif M == 84: #Disable step drivers
233 elif M == 92: #Set steps per unit
235 elif M == 101: #Enable extruder
237 elif M == 103: #Disable extruder
239 elif M == 104: #Set temperature, no wait
241 elif M == 105: #Get temperature
243 elif M == 106: #Enable fan
245 elif M == 107: #Disable fan
247 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
249 elif M == 109: #Set temperature, wait
251 elif M == 110: #Reset N counter
253 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
255 elif M == 117: #LCD message
257 elif M == 140: #Set bed temperature
259 elif M == 190: #Set bed temperature & wait
261 elif M == 221: #Extrude amount multiplier
262 s = getCodeFloat(line, 'S')
264 extrudeAmountMultiply = s / 100.0
266 print "Unknown M code:" + str(M)
267 for path in currentLayer:
268 path['points'] = numpy.array(path['points'], numpy.float32)
269 path['extrusion'] = numpy.array(path['extrusion'], numpy.float32)
270 self.layerList.append(currentLayer)
271 if self.progressCallback is not None and self._fileSize > 0:
272 self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize))
274 def getCodeInt(line, code):
275 n = line.find(code) + 1
278 m = line.find(' ', n)
282 return int(line[n:m])
286 def getCodeFloat(line, code):
287 n = line.find(code) + 1
290 m = line.find(' ', n)
293 return float(line[n:])
294 return float(line[n:m])
298 if __name__ == '__main__':
300 for filename in sys.argv[1:]:
303 print time.time() - t