1 from __future__ import absolute_import
9 from util import util3d
10 from 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.list = [startPoint]
23 self.extrusionAmount = 0
24 self.totalMoveTimeMinute = 0
25 self.progressCallback = None
27 def load(self, filename):
28 self._fileSize = os.stat(filename).st_size
29 gcodeFile = open(filename, 'r')
33 def loadList(self, l):
36 def calculateWeight(self):
37 #Calculates the weight of the filament in kg
38 radius = float(profile.getProfileSetting('filament_diameter')) / 2
39 volumeM3 = (self.extrusionAmount * (math.pi * radius * radius)) / (1000*1000*1000)
40 return volumeM3 * profile.getPreferenceFloat('filament_density')
42 def calculateCost(self):
43 cost_kg = profile.getPreferenceFloat('filament_cost_kg')
44 cost_meter = profile.getPreferenceFloat('filament_cost_meter')
45 if cost_kg > 0.0 and cost_meter > 0.0:
46 return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
48 return "%.2f" % (self.calculateWeight() * cost_kg)
49 elif cost_meter > 0.0:
50 return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
53 def _load(self, gcodeFile):
55 pos = util3d.Vector3()
56 posOffset = util3d.Vector3()
61 totalMoveTimeMinute = 0.0
69 currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
70 currentPath.list[0].e = totalExtrusion
71 currentLayer.append(currentPath)
72 for line in gcodeFile:
73 if self.progressCallback != None:
74 if filePos != gcodeFile.tell():
75 filePos = gcodeFile.tell()
76 self.progressCallback(float(filePos) / float(self._fileSize))
78 #Parse Cura_SF comments
79 if line.startswith(';TYPE:'):
80 pathType = line[6:].strip()
81 if pathType != "CUSTOM":
85 #Slic3r GCode comment parser
86 comment = line[line.find(';')+1:].strip()
89 elif comment == 'perimeter':
90 pathType = 'WALL-INNER'
91 elif comment == 'skirt':
93 if comment.startswith('LAYER:'):
94 self.layerList.append(currentLayer)
96 if pathType != "CUSTOM":
98 line = line[0:line.find(';')]
99 T = self.getCodeInt(line, 'T')
101 if currentExtruder > 0:
102 posOffset.x -= profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
103 posOffset.y -= profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
105 if currentExtruder > 0:
106 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
107 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
109 G = self.getCodeInt(line, 'G')
111 if G == 0 or G == 1: #Move
112 x = self.getCodeFloat(line, 'X')
113 y = self.getCodeFloat(line, 'Y')
114 z = self.getCodeFloat(line, 'Z')
115 e = self.getCodeFloat(line, 'E')
116 f = self.getCodeFloat(line, 'F')
120 pos.x = x * scale + posOffset.x
125 pos.y = y * scale + posOffset.y
130 pos.z = z * scale + posOffset.z
133 #Check if we have a new layer.
134 if oldPos.z != pos.z:
135 layerThickness = abs(oldPos.z - pos.z)
138 if x is not None or y is not None or z is not None:
139 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
147 totalExtrusion += e - currentE
156 if totalExtrusion > maxExtrusion:
157 maxExtrusion = totalExtrusion
158 if currentPath.type != moveType or currentPath.pathType != pathType:
159 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
160 currentLayer.append(currentPath)
162 newPos.e = totalExtrusion
163 currentPath.list.append(newPos)
164 elif G == 20: #Units are inches
166 elif G == 21: #Units are mm
169 x = self.getCodeFloat(line, 'X')
170 y = self.getCodeFloat(line, 'Y')
171 z = self.getCodeFloat(line, 'Z')
172 if x is None and y is None and z is None:
173 pos = util3d.Vector3()
181 elif G == 90: #Absolute position
183 elif G == 91: #Relative position
186 x = self.getCodeFloat(line, 'X')
187 y = self.getCodeFloat(line, 'Y')
188 z = self.getCodeFloat(line, 'Z')
189 e = self.getCodeFloat(line, 'E')
193 posOffset.x = pos.x + x
195 posOffset.y = pos.y + y
197 posOffset.z = pos.z + z
199 print "Unknown G code:" + str(G)
201 M = self.getCodeInt(line, 'M')
203 if M == 1: #Message with possible wait (ignored)
205 elif M == 84: #Disable step drivers
207 elif M == 92: #Set steps per unit
209 elif M == 104: #Set temperature, no wait
211 elif M == 105: #Get temperature
213 elif M == 106: #Enable fan
215 elif M == 107: #Disable fan
217 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
219 elif M == 109: #Set temperature, wait
221 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
224 print "Unknown M code:" + str(M)
225 self.layerList.append(currentLayer)
226 self.extrusionAmount = maxExtrusion
227 self.totalMoveTimeMinute = totalMoveTimeMinute
228 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
229 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
231 def getCodeInt(self, line, code):
232 if code not in self.regMatch:
233 self.regMatch[code] = re.compile(code + '([^\s]+)')
234 m = self.regMatch[code].search(line)
238 return int(m.group(1))
242 def getCodeFloat(self, line, code):
243 if code not in self.regMatch:
244 self.regMatch[code] = re.compile(code + '([^\s]+)')
245 m = self.regMatch[code].search(line)
249 return float(m.group(1))
253 if __name__ == '__main__':
254 for filename in sys.argv[1:]:
255 gcode().load(filename)