1 from __future__ import absolute_import
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.points = [startPoint]
17 # self.extrusion = [0.0]
18 def gcodePath(newType, pathType, layerThickness, startPoint):
19 return {'type': newType,
21 'layerThickness': layerThickness,
22 'points': [startPoint],
29 self.extrusionAmount = 0
30 self.totalMoveTimeMinute = 0
32 self.progressCallback = None
34 def load(self, filename):
35 if os.path.isfile(filename):
36 self.filename = filename
37 self._fileSize = os.stat(filename).st_size
38 gcodeFile = open(filename, 'r')
42 def loadList(self, l):
46 def calculateWeight(self):
47 #Calculates the weight of the filament in kg
48 radius = float(profile.getProfileSetting('filament_diameter')) / 2
49 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
50 return volumeM3 * profile.getPreferenceFloat('filament_physical_density')
52 def calculateCost(self):
53 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
54 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
55 if cost_kg > 0.0 and cost_meter > 0.0:
56 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
58 return "%.2f" % (self.calculateWeight() * cost_kg)
59 elif cost_meter > 0.0:
60 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
63 def _load(self, gcodeFile):
65 posOffset = [0.0, 0.0, 0.0]
70 extrudeAmountMultiply = 1.0
71 totalMoveTimeMinute = 0.0
79 currentPath = gcodePath('move', pathType, layerThickness, pos[:])
80 currentPath['extruder'] = currentExtruder
82 currentLayer.append(currentPath)
83 for line in gcodeFile:
84 if type(line) is tuple:
87 #Parse Cura_SF comments
88 if line.startswith(';TYPE:'):
89 pathType = line[6:].strip()
92 #Slic3r GCode comment parser
93 comment = line[line.find(';')+1:].strip()
96 elif comment == 'perimeter':
97 pathType = 'WALL-INNER'
98 elif comment == 'skirt':
100 if comment.startswith('LAYER:'):
101 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath['points'][-1])
102 currentPath['extruder'] = currentExtruder
103 for path in currentLayer:
104 path['points'] = numpy.array(path['points'], numpy.float32)
105 path['extrusion'] = numpy.array(path['extrusion'], numpy.float32)
106 self.layerList.append(currentLayer)
107 if self.progressCallback is not None:
108 if self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize)):
109 #Abort the loading, we can safely return as the results here will be discarded
112 currentLayer = [currentPath]
113 line = line[0:line.find(';')]
114 T = getCodeInt(line, 'T')
116 if currentExtruder > 0:
117 posOffset[0] -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
118 posOffset[1] -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
120 if currentExtruder > 0:
121 posOffset[0] += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
122 posOffset[1] += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
124 G = getCodeInt(line, 'G')
126 if G == 0 or G == 1: #Move
127 x = getCodeFloat(line, 'X')
128 y = getCodeFloat(line, 'Y')
129 z = getCodeFloat(line, 'Z')
130 e = getCodeFloat(line, 'E')
131 #f = getCodeFloat(line, 'F')
135 pos[0] = x * scale + posOffset[0]
137 pos[1] = y * scale + posOffset[1]
139 pos[2] = z * scale + posOffset[2]
149 #if x is not None or y is not None or z is not None:
150 # diffX = oldPos[0] - pos[0]
151 # diffY = oldPos[1] - pos[1]
152 # totalMoveTimeMinute += math.sqrt(diffX * diffX + diffY * diffY) / feedRate
163 if totalExtrusion > maxExtrusion:
164 maxExtrusion = totalExtrusion
167 if moveType == 'move' and oldPos[2] != pos[2]:
168 if oldPos[2] > pos[2] and abs(oldPos[2] - pos[2]) > 5.0 and pos[2] < 1.0:
170 layerThickness = abs(oldPos[2] - pos[2])
171 if currentPath['type'] != moveType or currentPath['pathType'] != pathType:
172 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath['points'][-1])
173 currentPath['extruder'] = currentExtruder
174 currentLayer.append(currentPath)
176 currentPath['points'].append(pos[:])
177 currentPath['extrusion'].append(e * extrudeAmountMultiply)
179 S = getCodeFloat(line, 'S')
181 totalMoveTimeMinute += S / 60.0
182 P = getCodeFloat(line, 'P')
184 totalMoveTimeMinute += P / 60.0 / 1000.0
185 elif G == 20: #Units are inches
187 elif G == 21: #Units are mm
190 x = getCodeFloat(line, 'X')
191 y = getCodeFloat(line, 'Y')
192 z = getCodeFloat(line, 'Z')
193 if x is None and y is None and z is None:
202 elif G == 90: #Absolute position
204 elif G == 91: #Relative position
207 x = getCodeFloat(line, 'X')
208 y = getCodeFloat(line, 'Y')
209 z = getCodeFloat(line, 'Z')
210 e = getCodeFloat(line, 'E')
214 posOffset[0] = pos[0] - x
216 posOffset[1] = pos[1] - y
218 posOffset[2] = pos[2] - z
220 print "Unknown G code:" + str(G)
222 M = getCodeInt(line, 'M')
224 if M == 0: #Message with possible wait (ignored)
226 elif M == 1: #Message with possible wait (ignored)
228 elif M == 80: #Enable power supply
230 elif M == 81: #Suicide/disable power supply
232 elif M == 82: #Absolute E
234 elif M == 83: #Relative E
236 elif M == 84: #Disable step drivers
238 elif M == 92: #Set steps per unit
240 elif M == 101: #Enable extruder
242 elif M == 103: #Disable extruder
244 elif M == 104: #Set temperature, no wait
246 elif M == 105: #Get temperature
248 elif M == 106: #Enable fan
250 elif M == 107: #Disable fan
252 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
254 elif M == 109: #Set temperature, wait
256 elif M == 110: #Reset N counter
258 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
260 elif M == 117: #LCD message
262 elif M == 140: #Set bed temperature
264 elif M == 190: #Set bed temperature & wait
266 elif M == 221: #Extrude amount multiplier
267 s = getCodeFloat(line, 'S')
269 extrudeAmountMultiply = s / 100.0
271 print "Unknown M code:" + str(M)
272 for path in currentLayer:
273 path['points'] = numpy.array(path['points'], numpy.float32)
274 path['extrusion'] = numpy.array(path['extrusion'], numpy.float32)
275 self.layerList.append(currentLayer)
276 if self.progressCallback is not None and self._fileSize > 0:
277 self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize))
278 self.extrusionAmount = maxExtrusion
279 self.totalMoveTimeMinute = totalMoveTimeMinute
280 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
281 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
283 def getCodeInt(line, code):
284 n = line.find(code) + 1
287 m = line.find(' ', n)
291 return int(line[n:m])
295 def getCodeFloat(line, code):
296 n = line.find(code) + 1
299 m = line.find(' ', n)
302 return float(line[n:])
303 return float(line[n:m])
307 if __name__ == '__main__':
309 for filename in sys.argv[1:]:
312 print g.totalMoveTimeMinute
313 print time.time() - t