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 * float(profile.getPreference('filament_density'))
41 def _load(self, gcodeFile):
43 pos = util3d.Vector3()
44 posOffset = util3d.Vector3()
48 totalMoveTimeMinute = 0.0
55 currentPath = gcodePath('move', pathType, pos.copy())
56 currentPath.list[0].e = totalExtrusion
57 currentLayer.append(currentPath)
58 for line in gcodeFile:
59 if self.progressCallback != None:
60 if filePos != gcodeFile.tell():
61 filePos = gcodeFile.tell()
62 self.progressCallback(float(filePos) / float(self._fileSize))
64 #Parse Cura_SF comments
65 if line.startswith(';TYPE:'):
66 pathType = line[6:].strip()
67 if pathType != "CUSTOM":
71 #Slic3r GCode comment parser
72 comment = line[line.find(';')+1:].strip()
75 elif comment == 'perimeter':
76 pathType = 'WALL-INNER'
77 elif comment == 'skirt':
79 if pathType != "CUSTOM":
81 line = line[0:line.find(';')]
83 G = self.getCodeInt(line, 'G')
85 if G == 0 or G == 1: #Move
86 x = self.getCodeFloat(line, 'X')
87 y = self.getCodeFloat(line, 'Y')
88 z = self.getCodeFloat(line, 'Z')
89 e = self.getCodeFloat(line, 'E')
90 f = self.getCodeFloat(line, 'F')
107 #Check if we have a new layer.
108 if oldPos.z < pos.z and startCodeDone and len(currentLayer) > 0:
109 self.layerList.append(currentLayer)
113 if x is not None or y is not None or z is not None:
114 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
122 totalExtrusion += e - currentE
131 if totalExtrusion > maxExtrusion:
132 maxExtrusion = totalExtrusion
133 if currentPath.type != moveType or currentPath.pathType != pathType:
134 currentPath = gcodePath(moveType, pathType, currentPath.list[-1])
135 currentLayer.append(currentPath)
137 newPos.e = totalExtrusion
138 currentPath.list.append(newPos)
139 elif G == 20: #Units are inches
141 elif G == 21: #Units are mm
144 x = self.getCodeFloat(line, 'X')
145 y = self.getCodeFloat(line, 'Y')
146 z = self.getCodeFloat(line, 'Z')
147 if x is None and y is None and z is None:
148 pos = util3d.Vector3()
156 elif G == 90: #Absolute position
158 elif G == 91: #Relative position
161 x = self.getCodeFloat(line, 'X')
162 y = self.getCodeFloat(line, 'Y')
163 z = self.getCodeFloat(line, 'Z')
164 e = self.getCodeFloat(line, 'E')
168 posOffset.x = pos.x + x
170 posOffset.y = pos.y + y
172 posOffset.z = pos.z + z
174 print "Unknown G code:" + str(G)
176 M = self.getCodeInt(line, 'M')
178 if M == 1: #Message with possible wait (ignored)
180 elif M == 84: #Disable step drivers
182 elif M == 92: #Set steps per unit
184 elif M == 104: #Set temperature, no wait
186 elif M == 105: #Get temperature
188 elif M == 106: #Enable fan
190 elif M == 107: #Disable fan
192 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
194 elif M == 109: #Set temperature, wait
196 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
199 print "Unknown M code:" + str(M)
200 self.layerList.append(currentLayer)
201 self.extrusionAmount = maxExtrusion
202 self.totalMoveTimeMinute = totalMoveTimeMinute
203 print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
204 print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
206 def getCodeInt(self, line, code):
207 if code not in self.regMatch:
208 self.regMatch[code] = re.compile(code + '([^\s]+)')
209 m = self.regMatch[code].search(line)
213 return int(m.group(1))
217 def getCodeFloat(self, line, code):
218 if code not in self.regMatch:
219 self.regMatch[code] = re.compile(code + '([^\s]+)')
220 m = self.regMatch[code].search(line)
224 return float(m.group(1))
228 if __name__ == '__main__':
229 for filename in sys.argv[1:]:
230 gcode().load(filename)