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]
23 self.extrusionAmount = 0
24 self.totalMoveTimeMinute = 0
26 self.progressCallback = None
28 def load(self, filename):
29 if os.path.isfile(filename):
30 self.filename = filename
31 self._fileSize = os.stat(filename).st_size
32 gcodeFile = open(filename, 'r')
36 def loadList(self, l):
40 def calculateWeight(self):
41 #Calculates the weight of the filament in kg
42 radius = float(profile.getProfileSetting('filament_diameter')) / 2
43 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
44 return volumeM3 * profile.getPreferenceFloat('filament_physical_density')
46 def calculateCost(self):
47 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
48 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
49 if cost_kg > 0.0 and cost_meter > 0.0:
50 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
52 return "%.2f" % (self.calculateWeight() * cost_kg)
53 elif cost_meter > 0.0:
54 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
57 def _load(self, gcodeFile):
59 posOffset = [0.0, 0.0, 0.0]
64 extrudeAmountMultiply = 1.0
65 totalMoveTimeMinute = 0.0
73 currentPath = gcodePath('move', pathType, layerThickness, pos[:])
74 currentPath.extruder = currentExtruder
76 currentLayer.append(currentPath)
77 for line in gcodeFile:
78 if type(line) is tuple:
81 #Parse Cura_SF comments
82 if line.startswith(';TYPE:'):
83 pathType = line[6:].strip()
86 #Slic3r GCode comment parser
87 comment = line[line.find(';')+1:].strip()
90 elif comment == 'perimeter':
91 pathType = 'WALL-INNER'
92 elif comment == 'skirt':
94 if comment.startswith('LAYER:'):
95 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.points[-1])
96 currentPath.extruder = currentExtruder
97 for path in currentLayer:
98 path.points = numpy.array(path.points, numpy.float32)
99 path.extrusion = numpy.array(path.extrusion, numpy.float32)
100 self.layerList.append(currentLayer)
101 if self.progressCallback is not None:
102 if self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize)):
103 #Abort the loading, we can safely return as the results here will be discarded
106 currentLayer = [currentPath]
107 line = line[0:line.find(';')]
108 T = getCodeInt(line, 'T')
110 if currentExtruder > 0:
111 posOffset[0] -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
112 posOffset[1] -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
114 if currentExtruder > 0:
115 posOffset[0] += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
116 posOffset[1] += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
118 G = getCodeInt(line, 'G')
120 if G == 0 or G == 1: #Move
121 x = getCodeFloat(line, 'X')
122 y = getCodeFloat(line, 'Y')
123 z = getCodeFloat(line, 'Z')
124 e = getCodeFloat(line, 'E')
125 f = getCodeFloat(line, 'F')
129 pos[0] = x * scale + posOffset[0]
131 pos[1] = y * scale + posOffset[1]
133 pos[2] = z * scale + posOffset[2]
143 #if x is not None or y is not None or z is not None:
144 # diffX = oldPos[0] - pos[0]
145 # diffY = oldPos[1] - pos[1]
146 # totalMoveTimeMinute += math.sqrt(diffX * diffX + diffY * diffY) / feedRate
157 if totalExtrusion > maxExtrusion:
158 maxExtrusion = totalExtrusion
161 if moveType == 'move' and oldPos[2] != pos[2]:
162 if oldPos[2] > pos[2] and abs(oldPos[2] - pos[2]) > 5.0 and pos[2] < 1.0:
164 layerThickness = abs(oldPos[2] - pos[2])
165 if currentPath.type != moveType or currentPath.pathType != pathType:
166 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.points[-1])
167 currentPath.extruder = currentExtruder
168 currentLayer.append(currentPath)
170 currentPath.points.append(pos[:])
171 currentPath.extrusion.append(e * extrudeAmountMultiply)
173 S = getCodeFloat(line, 'S')
175 totalMoveTimeMinute += S / 60.0
176 P = getCodeFloat(line, 'P')
178 totalMoveTimeMinute += P / 60.0 / 1000.0
179 elif G == 20: #Units are inches
181 elif G == 21: #Units are mm
184 x = getCodeFloat(line, 'X')
185 y = getCodeFloat(line, 'Y')
186 z = getCodeFloat(line, 'Z')
187 if x is None and y is None and z is None:
196 elif G == 90: #Absolute position
198 elif G == 91: #Relative position
201 x = getCodeFloat(line, 'X')
202 y = getCodeFloat(line, 'Y')
203 z = getCodeFloat(line, 'Z')
204 e = getCodeFloat(line, 'E')
208 posOffset[0] = pos[0] - x
210 posOffset[1] = pos[1] - y
212 posOffset[2] = pos[2] - z
214 print "Unknown G code:" + str(G)
216 M = getCodeInt(line, 'M')
218 if M == 0: #Message with possible wait (ignored)
220 elif M == 1: #Message with possible wait (ignored)
222 elif M == 80: #Enable power supply
224 elif M == 81: #Suicide/disable power supply
226 elif M == 82: #Absolute E
228 elif M == 83: #Relative E
230 elif M == 84: #Disable step drivers
232 elif M == 92: #Set steps per unit
234 elif M == 101: #Enable extruder
236 elif M == 103: #Disable extruder
238 elif M == 104: #Set temperature, no wait
240 elif M == 105: #Get temperature
242 elif M == 106: #Enable fan
244 elif M == 107: #Disable fan
246 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
248 elif M == 109: #Set temperature, wait
250 elif M == 110: #Reset N counter
252 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
254 elif M == 117: #LCD message
256 elif M == 140: #Set bed temperature
258 elif M == 190: #Set bed temperature & wait
260 elif M == 221: #Extrude amount multiplier
261 s = getCodeFloat(line, 'S')
263 extrudeAmountMultiply = s / 100.0
265 print "Unknown M code:" + str(M)
266 for path in currentLayer:
267 path.points = numpy.array(path.points, numpy.float32)
268 path.extrusion = numpy.array(path.extrusion, numpy.float32)
269 self.layerList.append(currentLayer)
270 if self.progressCallback is not None and self._fileSize > 0:
271 self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize))
272 self.extrusionAmount = maxExtrusion
273 self.totalMoveTimeMinute = totalMoveTimeMinute
274 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
275 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
277 def getCodeInt(line, code):
278 n = line.find(code) + 1
281 m = line.find(' ', n)
285 return int(line[n:m])
289 def getCodeFloat(line, code):
290 n = line.find(code) + 1
293 m = line.find(' ', n)
296 return float(line[n:])
297 return float(line[n:m])
301 if __name__ == '__main__':
303 for filename in sys.argv[1:]:
306 print g.totalMoveTimeMinute
307 print time.time() - t