2012-12-06 13:51:55 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-02-28 14:02:24 +00:00
|
|
|
import sys
|
|
|
|
import math
|
|
|
|
import re
|
2012-03-18 12:00:31 +00:00
|
|
|
import os
|
2012-02-28 14:02:24 +00:00
|
|
|
|
2013-01-18 22:23:50 +00:00
|
|
|
from octoprint.util import util3d
|
2013-01-01 20:04:00 +00:00
|
|
|
|
|
|
|
preferences = {
|
|
|
|
"extruder_offset_x1": -22.0,
|
|
|
|
"extruder_offset_y1": 0.0,
|
|
|
|
"extruder_offset_x2": 0.0,
|
|
|
|
"extruder_offset_y2": 0.0,
|
|
|
|
"extruder_offset_x3": 0.0,
|
|
|
|
"extruder_offset_y3": 0.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
def getPreference(key, default=None):
|
|
|
|
if preferences.has_key(key):
|
|
|
|
return preferences[key]
|
|
|
|
else:
|
|
|
|
return default
|
2012-02-28 14:02:24 +00:00
|
|
|
|
2013-01-30 19:56:17 +00:00
|
|
|
class AnalysisAborted(Exception):
|
|
|
|
pass
|
|
|
|
|
2012-05-04 15:57:24 +00:00
|
|
|
class gcodePath(object):
|
2012-05-07 11:24:26 +00:00
|
|
|
def __init__(self, newType, pathType, layerThickness, startPoint):
|
2012-03-26 15:14:31 +00:00
|
|
|
self.type = newType
|
|
|
|
self.pathType = pathType
|
2012-05-07 11:24:26 +00:00
|
|
|
self.layerThickness = layerThickness
|
2012-03-26 15:14:31 +00:00
|
|
|
self.list = [startPoint]
|
|
|
|
|
2012-05-04 15:57:24 +00:00
|
|
|
class gcode(object):
|
2012-03-22 15:04:53 +00:00
|
|
|
def __init__(self):
|
2012-03-22 14:30:28 +00:00
|
|
|
self.regMatch = {}
|
2012-03-29 09:01:33 +00:00
|
|
|
self.layerList = []
|
2012-03-22 15:04:53 +00:00
|
|
|
self.extrusionAmount = 0
|
|
|
|
self.totalMoveTimeMinute = 0
|
|
|
|
self.progressCallback = None
|
2013-01-30 19:56:17 +00:00
|
|
|
self._abort = False
|
2012-03-22 14:30:28 +00:00
|
|
|
|
2012-03-22 15:04:53 +00:00
|
|
|
def load(self, filename):
|
2012-05-31 16:01:57 +00:00
|
|
|
if os.path.isfile(filename):
|
|
|
|
self._fileSize = os.stat(filename).st_size
|
|
|
|
gcodeFile = open(filename, 'r')
|
|
|
|
self._load(gcodeFile)
|
|
|
|
gcodeFile.close()
|
2012-04-02 16:56:44 +00:00
|
|
|
|
|
|
|
def loadList(self, l):
|
|
|
|
self._load(l)
|
2013-01-30 19:56:17 +00:00
|
|
|
|
|
|
|
def abort(self):
|
|
|
|
self._abort = True
|
2012-04-02 16:56:44 +00:00
|
|
|
|
|
|
|
def _load(self, gcodeFile):
|
|
|
|
filePos = 0
|
2012-03-15 16:14:20 +00:00
|
|
|
pos = util3d.Vector3()
|
|
|
|
posOffset = util3d.Vector3()
|
2012-03-16 12:27:04 +00:00
|
|
|
currentE = 0.0
|
|
|
|
totalExtrusion = 0.0
|
2012-03-16 12:43:49 +00:00
|
|
|
maxExtrusion = 0.0
|
2012-04-29 12:22:20 +00:00
|
|
|
currentExtruder = 0
|
2012-11-06 07:52:16 +00:00
|
|
|
extrudeAmountMultiply = 1.0
|
2012-03-16 14:41:39 +00:00
|
|
|
totalMoveTimeMinute = 0.0
|
2012-02-28 14:02:24 +00:00
|
|
|
scale = 1.0
|
|
|
|
posAbs = True
|
2013-02-04 05:53:01 +00:00
|
|
|
posAbsExtruder = True;
|
2012-02-28 16:39:46 +00:00
|
|
|
feedRate = 3600
|
2012-05-07 11:24:26 +00:00
|
|
|
layerThickness = 0.1
|
2012-02-28 14:02:24 +00:00
|
|
|
pathType = 'CUSTOM';
|
2012-02-28 16:39:46 +00:00
|
|
|
startCodeDone = False
|
2012-03-29 09:01:33 +00:00
|
|
|
currentLayer = []
|
2012-05-07 11:24:26 +00:00
|
|
|
currentPath = gcodePath('move', pathType, layerThickness, pos.copy())
|
2012-03-26 15:14:31 +00:00
|
|
|
currentPath.list[0].e = totalExtrusion
|
2012-11-06 07:52:16 +00:00
|
|
|
currentPath.list[0].extrudeAmountMultiply = extrudeAmountMultiply
|
2012-03-29 09:01:33 +00:00
|
|
|
currentLayer.append(currentPath)
|
2012-03-22 14:12:37 +00:00
|
|
|
for line in gcodeFile:
|
2013-01-30 19:56:17 +00:00
|
|
|
if self._abort:
|
2013-02-01 10:01:20 +00:00
|
|
|
raise AnalysisAborted()
|
2012-09-07 10:04:55 +00:00
|
|
|
if type(line) is tuple:
|
|
|
|
line = line[0]
|
2012-04-02 16:56:44 +00:00
|
|
|
if self.progressCallback != None:
|
2012-12-28 19:04:43 +00:00
|
|
|
if isinstance(gcodeFile, (file)):
|
2012-04-02 16:56:44 +00:00
|
|
|
self.progressCallback(float(filePos) / float(self._fileSize))
|
2012-12-28 19:04:43 +00:00
|
|
|
elif isinstance(gcodeFile, (list)):
|
|
|
|
self.progressCallback(float(filePos) / float(len(gcodeFile)))
|
|
|
|
filePos += 1
|
|
|
|
|
2012-03-29 09:01:33 +00:00
|
|
|
#Parse Cura_SF comments
|
2012-02-28 14:02:24 +00:00
|
|
|
if line.startswith(';TYPE:'):
|
|
|
|
pathType = line[6:].strip()
|
2012-02-28 16:39:46 +00:00
|
|
|
if pathType != "CUSTOM":
|
|
|
|
startCodeDone = True
|
2012-03-27 11:48:40 +00:00
|
|
|
|
2012-03-26 13:03:26 +00:00
|
|
|
if ';' in line:
|
2012-03-27 11:48:40 +00:00
|
|
|
#Slic3r GCode comment parser
|
2012-03-26 13:03:26 +00:00
|
|
|
comment = line[line.find(';')+1:].strip()
|
|
|
|
if comment == 'fill':
|
|
|
|
pathType = 'FILL'
|
|
|
|
elif comment == 'perimeter':
|
|
|
|
pathType = 'WALL-INNER'
|
|
|
|
elif comment == 'skirt':
|
|
|
|
pathType = 'SKIRT'
|
2012-05-07 11:24:26 +00:00
|
|
|
if comment.startswith('LAYER:'):
|
|
|
|
self.layerList.append(currentLayer)
|
|
|
|
currentLayer = []
|
2012-03-26 13:03:26 +00:00
|
|
|
if pathType != "CUSTOM":
|
|
|
|
startCodeDone = True
|
|
|
|
line = line[0:line.find(';')]
|
2012-04-29 12:22:20 +00:00
|
|
|
T = self.getCodeInt(line, 'T')
|
|
|
|
if T is not None:
|
|
|
|
if currentExtruder > 0:
|
2013-01-01 20:04:00 +00:00
|
|
|
posOffset.x -= getPreference('extruder_offset_x%d' % (currentExtruder), 0.0)
|
|
|
|
posOffset.y -= getPreference('extruder_offset_y%d' % (currentExtruder), 0.0)
|
2012-04-29 12:50:52 +00:00
|
|
|
currentExtruder = T
|
|
|
|
if currentExtruder > 0:
|
2013-01-01 20:04:00 +00:00
|
|
|
posOffset.x += getPreference('extruder_offset_x%d' % (currentExtruder), 0.0)
|
|
|
|
posOffset.y += getPreference('extruder_offset_y%d' % (currentExtruder), 0.0)
|
2012-03-27 11:48:40 +00:00
|
|
|
|
2012-02-28 14:02:24 +00:00
|
|
|
G = self.getCodeInt(line, 'G')
|
|
|
|
if G is not None:
|
|
|
|
if G == 0 or G == 1: #Move
|
|
|
|
x = self.getCodeFloat(line, 'X')
|
|
|
|
y = self.getCodeFloat(line, 'Y')
|
|
|
|
z = self.getCodeFloat(line, 'Z')
|
|
|
|
e = self.getCodeFloat(line, 'E')
|
2012-02-28 16:39:46 +00:00
|
|
|
f = self.getCodeFloat(line, 'F')
|
2012-03-16 14:41:39 +00:00
|
|
|
oldPos = pos.copy()
|
2012-02-28 14:02:24 +00:00
|
|
|
if x is not None:
|
|
|
|
if posAbs:
|
2012-04-29 12:22:20 +00:00
|
|
|
pos.x = x * scale + posOffset.x
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
|
|
|
pos.x += x * scale
|
|
|
|
if y is not None:
|
|
|
|
if posAbs:
|
2012-04-29 12:22:20 +00:00
|
|
|
pos.y = y * scale + posOffset.y
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
|
|
|
pos.y += y * scale
|
|
|
|
if z is not None:
|
|
|
|
if posAbs:
|
2012-04-29 12:22:20 +00:00
|
|
|
pos.z = z * scale + posOffset.z
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
|
|
|
pos.z += z * scale
|
2012-02-28 16:39:46 +00:00
|
|
|
if f is not None:
|
|
|
|
feedRate = f
|
2012-03-16 14:41:39 +00:00
|
|
|
if x is not None or y is not None or z is not None:
|
|
|
|
totalMoveTimeMinute += (oldPos - pos).vsize() / feedRate
|
2012-02-28 14:02:24 +00:00
|
|
|
moveType = 'move'
|
|
|
|
if e is not None:
|
2013-02-04 05:53:01 +00:00
|
|
|
if posAbsExtruder:
|
2012-03-16 12:27:04 +00:00
|
|
|
if e > currentE:
|
|
|
|
moveType = 'extrude'
|
|
|
|
if e < currentE:
|
|
|
|
moveType = 'retract'
|
2013-03-02 12:39:44 +00:00
|
|
|
totalExtrusion += e - currentE
|
|
|
|
currentE = e
|
2012-03-16 12:27:04 +00:00
|
|
|
else:
|
|
|
|
if e > 0:
|
|
|
|
moveType = 'extrude'
|
|
|
|
if e < 0:
|
|
|
|
moveType = 'retract'
|
2013-03-01 16:55:25 +00:00
|
|
|
totalExtrusion += e
|
2013-03-02 12:39:44 +00:00
|
|
|
currentE += e
|
2012-03-16 12:43:49 +00:00
|
|
|
if totalExtrusion > maxExtrusion:
|
|
|
|
maxExtrusion = totalExtrusion
|
2012-05-16 13:14:17 +00:00
|
|
|
if moveType == 'move' and oldPos.z != pos.z:
|
2012-07-07 06:30:53 +00:00
|
|
|
if oldPos.z > pos.z and abs(oldPos.z - pos.z) > 5.0 and pos.z < 1.0:
|
|
|
|
oldPos.z = 0.0
|
2012-05-16 13:14:17 +00:00
|
|
|
layerThickness = abs(oldPos.z - pos.z)
|
2012-03-26 15:14:31 +00:00
|
|
|
if currentPath.type != moveType or currentPath.pathType != pathType:
|
2012-05-07 11:24:26 +00:00
|
|
|
currentPath = gcodePath(moveType, pathType, layerThickness, currentPath.list[-1])
|
2012-03-29 09:01:33 +00:00
|
|
|
currentLayer.append(currentPath)
|
2012-03-26 15:14:31 +00:00
|
|
|
newPos = pos.copy()
|
|
|
|
newPos.e = totalExtrusion
|
2012-11-06 07:52:16 +00:00
|
|
|
newPos.extrudeAmountMultiply = extrudeAmountMultiply
|
2012-03-26 15:14:31 +00:00
|
|
|
currentPath.list.append(newPos)
|
2012-06-19 19:57:16 +00:00
|
|
|
elif G == 4: #Delay
|
|
|
|
S = self.getCodeFloat(line, 'S')
|
|
|
|
if S is not None:
|
|
|
|
totalMoveTimeMinute += S / 60
|
|
|
|
P = self.getCodeFloat(line, 'P')
|
|
|
|
if P is not None:
|
|
|
|
totalMoveTimeMinute += P / 60 / 1000
|
2012-02-28 14:02:24 +00:00
|
|
|
elif G == 20: #Units are inches
|
|
|
|
scale = 25.4
|
|
|
|
elif G == 21: #Units are mm
|
|
|
|
scale = 1.0
|
|
|
|
elif G == 28: #Home
|
|
|
|
x = self.getCodeFloat(line, 'X')
|
|
|
|
y = self.getCodeFloat(line, 'Y')
|
|
|
|
z = self.getCodeFloat(line, 'Z')
|
|
|
|
if x is None and y is None and z is None:
|
2012-03-15 16:14:20 +00:00
|
|
|
pos = util3d.Vector3()
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
|
|
|
if x is not None:
|
|
|
|
pos.x = 0.0
|
|
|
|
if y is not None:
|
|
|
|
pos.y = 0.0
|
|
|
|
if z is not None:
|
|
|
|
pos.z = 0.0
|
|
|
|
elif G == 90: #Absolute position
|
|
|
|
posAbs = True
|
2013-02-04 05:53:01 +00:00
|
|
|
posAbsExtruder = True
|
2012-02-28 14:02:24 +00:00
|
|
|
elif G == 91: #Relative position
|
|
|
|
posAbs = False
|
2013-02-04 05:53:01 +00:00
|
|
|
posAbsExtruder = False
|
2012-02-28 14:02:24 +00:00
|
|
|
elif G == 92:
|
|
|
|
x = self.getCodeFloat(line, 'X')
|
|
|
|
y = self.getCodeFloat(line, 'Y')
|
|
|
|
z = self.getCodeFloat(line, 'Z')
|
|
|
|
e = self.getCodeFloat(line, 'E')
|
|
|
|
if e is not None:
|
|
|
|
currentE = e
|
|
|
|
if x is not None:
|
2012-06-23 10:15:29 +00:00
|
|
|
posOffset.x = pos.x - x
|
2012-02-28 14:02:24 +00:00
|
|
|
if y is not None:
|
2012-06-23 10:15:29 +00:00
|
|
|
posOffset.y = pos.y - y
|
2012-02-28 14:02:24 +00:00
|
|
|
if z is not None:
|
2012-06-23 10:15:29 +00:00
|
|
|
posOffset.z = pos.z - z
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
|
|
|
print "Unknown G code:" + str(G)
|
|
|
|
else:
|
|
|
|
M = self.getCodeInt(line, 'M')
|
|
|
|
if M is not None:
|
|
|
|
if M == 1: #Message with possible wait (ignored)
|
|
|
|
pass
|
2012-07-05 20:58:39 +00:00
|
|
|
elif M == 80: #Enable power supply
|
|
|
|
pass
|
|
|
|
elif M == 81: #Suicide/disable power supply
|
|
|
|
pass
|
2013-02-04 05:53:01 +00:00
|
|
|
elif M == 82: # Use absolute extruder positions
|
|
|
|
posAbsExtruder = True
|
|
|
|
elif M == 83: # Use relative extruder positions
|
|
|
|
posAbsExtruder = False
|
2012-02-28 14:02:24 +00:00
|
|
|
elif M == 84: #Disable step drivers
|
|
|
|
pass
|
|
|
|
elif M == 92: #Set steps per unit
|
|
|
|
pass
|
2012-09-28 13:09:16 +00:00
|
|
|
elif M == 101: #Enable extruder
|
|
|
|
pass
|
|
|
|
elif M == 103: #Disable extruder
|
|
|
|
pass
|
2012-02-28 14:02:24 +00:00
|
|
|
elif M == 104: #Set temperature, no wait
|
|
|
|
pass
|
|
|
|
elif M == 105: #Get temperature
|
|
|
|
pass
|
|
|
|
elif M == 106: #Enable fan
|
|
|
|
pass
|
|
|
|
elif M == 107: #Disable fan
|
|
|
|
pass
|
|
|
|
elif M == 108: #Extruder RPM (these should not be in the final GCode, but they are)
|
|
|
|
pass
|
2012-03-21 09:42:17 +00:00
|
|
|
elif M == 109: #Set temperature, wait
|
|
|
|
pass
|
2012-06-01 10:14:20 +00:00
|
|
|
elif M == 110: #Reset N counter
|
|
|
|
pass
|
2012-02-28 14:02:24 +00:00
|
|
|
elif M == 113: #Extruder PWM (these should not be in the final GCode, but they are)
|
|
|
|
pass
|
2012-07-05 20:58:39 +00:00
|
|
|
elif M == 140: #Set bed temperature
|
|
|
|
pass
|
|
|
|
elif M == 190: #Set bed temperature & wait
|
|
|
|
pass
|
2012-11-06 07:52:16 +00:00
|
|
|
elif M == 221: #Extrude amount multiplier
|
|
|
|
s = self.getCodeFloat(line, 'S')
|
|
|
|
if s != None:
|
|
|
|
extrudeAmountMultiply = s / 100.0
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
|
|
|
print "Unknown M code:" + str(M)
|
2012-03-29 09:01:33 +00:00
|
|
|
self.layerList.append(currentLayer)
|
2012-03-16 12:45:03 +00:00
|
|
|
self.extrusionAmount = maxExtrusion
|
2012-03-16 14:41:39 +00:00
|
|
|
self.totalMoveTimeMinute = totalMoveTimeMinute
|
2012-02-28 14:02:24 +00:00
|
|
|
|
2012-03-22 14:30:28 +00:00
|
|
|
def getCodeInt(self, line, code):
|
|
|
|
if code not in self.regMatch:
|
|
|
|
self.regMatch[code] = re.compile(code + '([^\s]+)')
|
|
|
|
m = self.regMatch[code].search(line)
|
2012-02-28 14:02:24 +00:00
|
|
|
if m == None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return int(m.group(1))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2012-03-22 14:30:28 +00:00
|
|
|
def getCodeFloat(self, line, code):
|
|
|
|
if code not in self.regMatch:
|
|
|
|
self.regMatch[code] = re.compile(code + '([^\s]+)')
|
|
|
|
m = self.regMatch[code].search(line)
|
2012-02-28 14:02:24 +00:00
|
|
|
if m == None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return float(m.group(1))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2012-03-22 14:30:28 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
for filename in sys.argv[1:]:
|
2012-03-22 15:04:53 +00:00
|
|
|
gcode().load(filename)
|
2012-03-22 14:30:28 +00:00
|
|
|
|