1 from __future__ import absolute_import
2 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
10 from Cura.util import profile
12 #class gcodePath(object):
13 # def __init__(self, newType, pathType, layerThickness, startPoint):
15 # self.pathType = pathType
16 # self.layerThickness = layerThickness
17 # self.points = [startPoint]
18 # self.extrusion = [0.0]
19 def gcodePath(newType, pathType, layerThickness, startPoint):
20 return {'type': newType,
22 'layerThickness': layerThickness,
23 'points': [startPoint],
30 self.extrusionAmount = 0
31 self.totalMoveTimeMinute = 0
33 self.progressCallback = None
35 def load(self, filename):
36 if os.path.isfile(filename):
37 self.filename = filename
38 self._fileSize = os.stat(filename).st_size
39 gcodeFile = open(filename, 'r')
43 def loadList(self, l):
47 def calculateWeight(self):
48 #Calculates the weight of the filament in kg
49 radius = float(profile.getProfileSetting('filament_diameter')) / 2
50 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
51 return volumeM3 * profile.getPreferenceFloat('filament_physical_density')
53 def calculateCost(self):
54 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
55 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
56 if cost_kg > 0.0 and cost_meter > 0.0:
57 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
59 return "%.2f" % (self.calculateWeight() * cost_kg)
60 elif cost_meter > 0.0:
61 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
64 def _load(self, gcodeFile):
67 posOffset = [0.0, 0.0, 0.0]
72 extrudeAmountMultiply = 1.0
73 totalMoveTimeMinute = 0.0
82 currentPath = gcodePath('move', pathType, layerThickness, pos[:])
83 currentPath['extruder'] = currentExtruder
85 currentLayer.append(currentPath)
86 for line in gcodeFile:
87 if type(line) is tuple:
90 #Parse Cura_SF comments
91 if line.startswith(';TYPE:'):
92 pathType = line[6:].strip()
95 #Slic3r GCode comment parser
96 comment = line[line.find(';')+1:].strip()
99 elif comment == 'perimeter':
100 pathType = 'WALL-INNER'
101 elif comment == 'skirt':
103 if comment.startswith('LAYER:'):
104 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath['points'][-1])
105 currentPath['extruder'] = currentExtruder
106 for path in currentLayer:
107 path['points'] = numpy.array(path['points'], numpy.float32)
108 path['extrusion'] = numpy.array(path['extrusion'], numpy.float32)
109 self.layerList.append(currentLayer)
110 if self.progressCallback is not None:
111 if self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize)):
112 #Abort the loading, we can safely return as the results here will be discarded
115 currentLayer = [currentPath]
116 line = line[0:line.find(';')]
117 T = getCodeInt(line, 'T')
119 if currentExtruder > 0:
120 posOffset[0] -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
121 posOffset[1] -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
123 if currentExtruder > 0:
124 posOffset[0] += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
125 posOffset[1] += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
127 G = getCodeInt(line, 'G')
129 if G == 0 or G == 1: #Move
130 x = getCodeFloat(line, 'X')
131 y = getCodeFloat(line, 'Y')
132 z = getCodeFloat(line, 'Z')
133 e = getCodeFloat(line, 'E')
134 #f = getCodeFloat(line, 'F')
138 pos[0] = x * scale + posOffset[0]
140 pos[1] = y * scale + posOffset[1]
142 pos[2] = z * scale + posOffset[2]
152 #if x is not None or y is not None or z is not None:
153 # diffX = oldPos[0] - pos[0]
154 # diffY = oldPos[1] - pos[1]
155 # totalMoveTimeMinute += math.sqrt(diffX * diffX + diffY * diffY) / feedRate
166 if totalExtrusion > maxExtrusion:
167 maxExtrusion = totalExtrusion
170 if moveType == 'move' and oldPos[2] != pos[2]:
171 if oldPos[2] > pos[2] and abs(oldPos[2] - pos[2]) > 5.0 and pos[2] < 1.0:
173 layerThickness = abs(oldPos[2] - pos[2])
174 if currentPath['type'] != moveType or currentPath['pathType'] != pathType:
175 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath['points'][-1])
176 currentPath['extruder'] = currentExtruder
177 currentLayer.append(currentPath)
179 currentPath['points'].append(pos[:])
180 currentPath['extrusion'].append(e * extrudeAmountMultiply)
182 S = getCodeFloat(line, 'S')
184 totalMoveTimeMinute += S / 60.0
185 P = getCodeFloat(line, 'P')
187 totalMoveTimeMinute += P / 60.0 / 1000.0
188 elif G == 20: #Units are inches
190 elif G == 21: #Units are mm
193 x = getCodeFloat(line, 'X')
194 y = getCodeFloat(line, 'Y')
195 z = getCodeFloat(line, 'Z')
196 if profile.getPreference('machine_center_is_zero') == 'True':
197 center = [profile.getProfileSettingFloat('machine_width') / 2, profile.getProfileSettingFloat('machine_depth') / 2,0.0]
199 center = [0.0,0.0,0.0]
200 if x is None and y is None and z is None:
209 elif G == 90: #Absolute position
211 elif G == 91: #Relative position
214 x = getCodeFloat(line, 'X')
215 y = getCodeFloat(line, 'Y')
216 z = getCodeFloat(line, 'Z')
217 e = getCodeFloat(line, 'E')
221 posOffset[0] = pos[0] - x
223 posOffset[1] = pos[1] - y
225 posOffset[2] = pos[2] - z
227 print "Unknown G code:" + str(G)
229 M = getCodeInt(line, 'M')
231 if M == 0: #Message with possible wait (ignored)
233 elif M == 1: #Message with possible wait (ignored)
235 elif M == 80: #Enable power supply
237 elif M == 81: #Suicide/disable power supply
239 elif M == 82: #Absolute E
241 elif M == 83: #Relative E
243 elif M == 84: #Disable step drivers
245 elif M == 92: #Set steps per unit
247 elif M == 101: #Enable extruder
249 elif M == 103: #Disable extruder
251 elif M == 104: #Set temperature, no wait
253 elif M == 105: #Get temperature
255 elif M == 106: #Enable fan
257 elif M == 107: #Disable fan
259 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
261 elif M == 109: #Set temperature, wait
263 elif M == 110: #Reset N counter
265 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
267 elif M == 117: #LCD message
269 elif M == 140: #Set bed temperature
271 elif M == 190: #Set bed temperature & wait
273 elif M == 221: #Extrude amount multiplier
274 s = getCodeFloat(line, 'S')
276 extrudeAmountMultiply = s / 100.0
278 print "Unknown M code:" + str(M)
279 for path in currentLayer:
280 path['points'] = numpy.array(path['points'], numpy.float32)
281 path['extrusion'] = numpy.array(path['extrusion'], numpy.float32)
282 self.layerList.append(currentLayer)
283 if self.progressCallback is not None and self._fileSize > 0:
284 self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize))
285 self.extrusionAmount = maxExtrusion
286 self.totalMoveTimeMinute = totalMoveTimeMinute
287 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
288 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
290 def getCodeInt(line, code):
291 n = line.find(code) + 1
294 m = line.find(' ', n)
298 return int(line[n:m])
302 def getCodeFloat(line, code):
303 n = line.find(code) + 1
306 m = line.find(' ', n)
309 return float(line[n:])
310 return float(line[n:m])
314 if __name__ == '__main__':
316 for filename in sys.argv[1:]:
319 print g.totalMoveTimeMinute
320 print time.time() - t