1 from __future__ import absolute_import
9 from util import util3d
10 from util import profile
13 def __init__(self, newType, pathType, startPoint):
15 self.pathType = pathType
16 self.list = [startPoint]
22 self.extrusionAmount = 0
23 self.totalMoveTimeMinute = 0
24 self.progressCallback = None
26 def load(self, filename):
27 self._fileSize = os.stat(filename).st_size
28 gcodeFile = open(filename, 'r')
32 def loadList(self, l):
35 def calculateWeight(self):
36 #Calculates the weight of the filament in kg
37 radius = float(profile.getProfileSetting('filament_diameter')) / 2
38 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
39 return volumeM3 * profile.getPreferenceFloat('filament_density')
41 def calculateCost(self):
42 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
43 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
44 if cost_kg > 0.0 and cost_meter > 0.0:
45 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
47 return "%.2f" % (self.calculateWeight() * cost_kg)
48 elif cost_meter > 0.0:
49 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
52 def _load(self, gcodeFile):
54 pos = util3d.Vector3()
55 posOffset = util3d.Vector3()
60 totalMoveTimeMinute = 0.0
67 currentPath = gcodePath('move', pathType, pos.copy())
68 currentPath.list[0].e = totalExtrusion
69 currentLayer.append(currentPath)
70 for line in gcodeFile:
71 if self.progressCallback != None:
72 if filePos != gcodeFile.tell():
73 filePos = gcodeFile.tell()
74 self.progressCallback(float(filePos) / float(self._fileSize))
76 #Parse Cura_SF comments
77 if line.startswith(';TYPE:'):
78 pathType = line[6:].strip()
79 if pathType != "CUSTOM":
83 #Slic3r GCode comment parser
84 comment = line[line.find(';')+1:].strip()
87 elif comment == 'perimeter':
88 pathType = 'WALL-INNER'
89 elif comment == 'skirt':
91 if pathType != "CUSTOM":
93 line = line[0:line.find(';')]
94 T = self.getCodeInt(line, 'T')
96 if currentExtruder > 0:
97 posOffset.x -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
98 posOffset.y -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
100 if currentExtruder > 0:
101 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
102 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
104 G = self.getCodeInt(line, 'G')
106 if G == 0 or G == 1: #Move
107 x = self.getCodeFloat(line, 'X')
108 y = self.getCodeFloat(line, 'Y')
109 z = self.getCodeFloat(line, 'Z')
110 e = self.getCodeFloat(line, 'E')
111 f = self.getCodeFloat(line, 'F')
115 pos.x = x * scale + posOffset.x
120 pos.y = y * scale + posOffset.y
125 pos.z = z * scale + posOffset.z
128 #Check if we have a new layer.
129 if oldPos.z < pos.z and startCodeDone and len(currentLayer) > 0:
130 self.layerList.append(currentLayer)
134 if x is not None or y is not None or z is not None:
135 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
143 totalExtrusion += e - currentE
152 if totalExtrusion > maxExtrusion:
153 maxExtrusion = totalExtrusion
154 if currentPath.type != moveType or currentPath.pathType != pathType:
155 currentPath = gcodePath(moveType, pathType, currentPath.list[-1])
156 currentLayer.append(currentPath)
158 newPos.e = totalExtrusion
159 currentPath.list.append(newPos)
160 elif G == 20: #Units are inches
162 elif G == 21: #Units are mm
165 x = self.getCodeFloat(line, 'X')
166 y = self.getCodeFloat(line, 'Y')
167 z = self.getCodeFloat(line, 'Z')
168 if x is None and y is None and z is None:
169 pos = util3d.Vector3()
177 elif G == 90: #Absolute position
179 elif G == 91: #Relative position
182 x = self.getCodeFloat(line, 'X')
183 y = self.getCodeFloat(line, 'Y')
184 z = self.getCodeFloat(line, 'Z')
185 e = self.getCodeFloat(line, 'E')
189 posOffset.x = pos.x + x
191 posOffset.y = pos.y + y
193 posOffset.z = pos.z + z
195 print "Unknown G code:" + str(G)
197 M = self.getCodeInt(line, 'M')
199 if M == 1: #Message with possible wait (ignored)
201 elif M == 84: #Disable step drivers
203 elif M == 92: #Set steps per unit
205 elif M == 104: #Set temperature, no wait
207 elif M == 105: #Get temperature
209 elif M == 106: #Enable fan
211 elif M == 107: #Disable fan
213 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
215 elif M == 109: #Set temperature, wait
217 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
220 print "Unknown M code:" + str(M)
221 self.layerList.append(currentLayer)
222 self.extrusionAmount = maxExtrusion
223 self.totalMoveTimeMinute = totalMoveTimeMinute
224 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
225 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
227 def getCodeInt(self, line, code):
228 if code not in self.regMatch:
229 self.regMatch[code] = re.compile(code + '([^\s]+)')
230 m = self.regMatch[code].search(line)
234 return int(m.group(1))
238 def getCodeFloat(self, line, code):
239 if code not in self.regMatch:
240 self.regMatch[code] = re.compile(code + '([^\s]+)')
241 m = self.regMatch[code].search(line)
245 return float(m.group(1))
249 if __name__ == '__main__':
250 for filename in sys.argv[1:]:
251 gcode().load(filename)