1 from __future__ import absolute_import
8 from Cura.util import util3d
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.list = [startPoint]
22 self.extrusionAmount = 0
23 self.totalMoveTimeMinute = 0
24 self.progressCallback = None
26 def load(self, filename):
27 if os.path.isfile(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 extrudeAmountMultiply = 1.0
62 totalMoveTimeMinute = 0.0
70 currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
71 currentPath.list[0].e = totalExtrusion
72 currentPath.list[0].extrudeAmountMultiply = extrudeAmountMultiply
73 currentLayer.append(currentPath)
74 for line in gcodeFile:
75 if type(line) is tuple:
77 if self.progressCallback != None:
78 if filePos != gcodeFile.tell():
79 filePos = gcodeFile.tell()
80 self.progressCallback(float(filePos) / float(self._fileSize))
82 #Parse Cura_SF comments
83 if line.startswith(';TYPE:'):
84 pathType = line[6:].strip()
85 if pathType != "CUSTOM":
89 #Slic3r GCode comment parser
90 comment = line[line.find(';')+1:].strip()
93 elif comment == 'perimeter':
94 pathType = 'WALL-INNER'
95 elif comment == 'skirt':
97 if comment.startswith('LAYER:'):
98 self.layerList.append(currentLayer)
100 if pathType != "CUSTOM":
102 line = line[0:line.find(';')]
103 T = self.getCodeInt(line, 'T')
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 if currentExtruder > 0:
110 posOffset.x += profile.getPreferenceFloat('extruder_offset_x%d' % (currentExtruder))
111 posOffset.y += profile.getPreferenceFloat('extruder_offset_y%d' % (currentExtruder))
113 G = self.getCodeInt(line, 'G')
115 if G == 0 or G == 1: #Move
116 x = self.getCodeFloat(line, 'X')
117 y = self.getCodeFloat(line, 'Y')
118 z = self.getCodeFloat(line, 'Z')
119 e = self.getCodeFloat(line, 'E')
120 f = self.getCodeFloat(line, 'F')
124 pos.x = x * scale + posOffset.x
129 pos.y = y * scale + posOffset.y
134 pos.z = z * scale + posOffset.z
139 if x is not None or y is not None or z is not None:
140 totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
153 totalExtrusion += e - currentE
155 if totalExtrusion > maxExtrusion:
156 maxExtrusion = totalExtrusion
157 if moveType == 'move' and oldPos.z != pos.z:
158 if oldPos.z > pos.z and abs(oldPos.z - pos.z) > 5.0 and pos.z < 1.0:
160 layerThickness = abs(oldPos.z - pos.z)
161 if currentPath.type != moveType or currentPath.pathType != pathType:
162 currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
163 currentLayer.append(currentPath)
165 newPos.e = totalExtrusion
166 newPos.extrudeAmountMultiply = extrudeAmountMultiply
167 currentPath.list.append(newPos)
169 S = self.getCodeFloat(line, 'S')
171 totalMoveTimeMinute += S / 60
172 P = self.getCodeFloat(line, 'P')
174 totalMoveTimeMinute += P / 60 / 1000
175 elif G == 20: #Units are inches
177 elif G == 21: #Units are mm
180 x = self.getCodeFloat(line, 'X')
181 y = self.getCodeFloat(line, 'Y')
182 z = self.getCodeFloat(line, 'Z')
183 if x is None and y is None and z is None:
184 pos = util3d.Vector3()
192 elif G == 90: #Absolute position
194 elif G == 91: #Relative position
197 x = self.getCodeFloat(line, 'X')
198 y = self.getCodeFloat(line, 'Y')
199 z = self.getCodeFloat(line, 'Z')
200 e = self.getCodeFloat(line, 'E')
204 posOffset.x = pos.x - x
206 posOffset.y = pos.y - y
208 posOffset.z = pos.z - z
210 print "Unknown G code:" + str(G)
212 M = self.getCodeInt(line, 'M')
214 if M == 1: #Message with possible wait (ignored)
216 elif M == 80: #Enable power supply
218 elif M == 81: #Suicide/disable power supply
220 elif M == 84: #Disable step drivers
222 elif M == 92: #Set steps per unit
224 elif M == 101: #Enable extruder
226 elif M == 103: #Disable extruder
228 elif M == 104: #Set temperature, no wait
230 elif M == 105: #Get temperature
232 elif M == 106: #Enable fan
234 elif M == 107: #Disable fan
236 elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
238 elif M == 109: #Set temperature, wait
240 elif M == 110: #Reset N counter
242 elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
244 elif M == 140: #Set bed temperature
246 elif M == 190: #Set bed temperature & wait
248 elif M == 221: #Extrude amount multiplier
249 s = self.getCodeFloat(line, 'S')
251 extrudeAmountMultiply = s / 100.0
253 print "Unknown M code:" + str(M)
254 self.layerList.append(currentLayer)
255 self.extrusionAmount = maxExtrusion
256 self.totalMoveTimeMinute = totalMoveTimeMinute
257 #print "Extruded a total of: %d mm of filament" % (self.extrusionAmount)
258 #print "Estimated print duration: %.2f minutes" % (self.totalMoveTimeMinute)
260 def getCodeInt(self, line, code):
261 if code not in self.regMatch:
262 self.regMatch[code] = re.compile(code + '([^\s]+)')
263 m = self.regMatch[code].search(line)
267 return int(m.group(1))
271 def getCodeFloat(self, line, code):
272 if code not in self.regMatch:
273 self.regMatch[code] = re.compile(code + '([^\s]+)')
274 m = self.regMatch[code].search(line)
278 return float(m.group(1))
282 if __name__ == '__main__':
283 for filename in sys.argv[1:]:
284 gcode().load(filename)