2012-12-06 13:51:55 +00:00
|
|
|
from __future__ import absolute_import
|
2013-08-07 15:01:48 +00:00
|
|
|
__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
|
2012-12-06 13:51:55 +00:00
|
|
|
|
2012-02-28 14:02:24 +00:00
|
|
|
import sys
|
|
|
|
import math
|
2012-03-18 12:00:31 +00:00
|
|
|
import os
|
2013-06-18 19:28:05 +00:00
|
|
|
import base64
|
|
|
|
import zlib
|
|
|
|
import logging
|
2012-02-28 14:02:24 +00:00
|
|
|
|
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
|
|
|
|
|
2013-08-07 15:01:48 +00:00
|
|
|
def gcodePath(newType, pathType, layerThickness, startPoint):
|
|
|
|
return {'type': newType,
|
|
|
|
'pathType': pathType,
|
|
|
|
'layerThickness': layerThickness,
|
|
|
|
'points': [startPoint],
|
|
|
|
'extrusion': [0.0]}
|
2012-03-26 15:14:31 +00:00
|
|
|
|
2012-05-04 15:57:24 +00:00
|
|
|
class gcode(object):
|
2012-03-22 15:04:53 +00:00
|
|
|
def __init__(self):
|
2013-06-18 19:28:05 +00:00
|
|
|
self._logger = logging.getLogger(__name__)
|
|
|
|
|
2013-08-07 15:01:48 +00:00
|
|
|
self.layerList = None
|
2012-03-22 15:04:53 +00:00
|
|
|
self.extrusionAmount = 0
|
|
|
|
self.totalMoveTimeMinute = 0
|
2013-08-07 15:01:48 +00:00
|
|
|
self.filename = None
|
2012-03-22 15:04:53 +00:00
|
|
|
self.progressCallback = None
|
2013-01-30 19:56:17 +00:00
|
|
|
self._abort = False
|
2013-08-07 15:01:48 +00:00
|
|
|
self._filamentDiameter = 0
|
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):
|
2013-08-07 15:01:48 +00:00
|
|
|
self.filename = filename
|
2012-05-31 16:01:57 +00:00
|
|
|
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):
|
2013-08-07 15:01:48 +00:00
|
|
|
self.filename = None
|
2012-04-02 16:56:44 +00:00
|
|
|
self._load(l)
|
2013-01-30 19:56:17 +00:00
|
|
|
|
|
|
|
def abort(self):
|
|
|
|
self._abort = True
|
2013-08-07 15:01:48 +00:00
|
|
|
|
|
|
|
def calculateVolumeCm3(self):
|
|
|
|
radius = self._filamentDiameter / 2
|
|
|
|
return (self.extrusionAmount * (math.pi * radius * radius)) / 1000
|
|
|
|
|
|
|
|
def calculateWeight(self):
|
|
|
|
#Calculates the weight of the filament in kg
|
2013-08-11 15:39:45 +00:00
|
|
|
volumeM3 = self.calculateVolumeCm3() /(1000*1000)
|
2013-08-07 15:01:48 +00:00
|
|
|
return volumeM3 * getPreference('filament_physical_density')
|
|
|
|
|
|
|
|
def calculateCost(self):
|
|
|
|
cost_kg = getPreference('filament_cost_kg')
|
|
|
|
cost_meter = getPreference('filament_cost_meter')
|
|
|
|
if cost_kg > 0.0 and cost_meter > 0.0:
|
|
|
|
return "%.2f / %.2f" % (self.calculateWeight() * cost_kg, self.extrusionAmount / 1000 * cost_meter)
|
|
|
|
elif cost_kg > 0.0:
|
|
|
|
return "%.2f" % (self.calculateWeight() * cost_kg)
|
|
|
|
elif cost_meter > 0.0:
|
|
|
|
return "%.2f" % (self.extrusionAmount / 1000 * cost_meter)
|
|
|
|
return None
|
2012-04-02 16:56:44 +00:00
|
|
|
|
|
|
|
def _load(self, gcodeFile):
|
|
|
|
filePos = 0
|
2013-08-07 15:01:48 +00:00
|
|
|
self.layerList = []
|
|
|
|
pos = [0.0, 0.0, 0.0]
|
|
|
|
posOffset = [0.0, 0.0, 0.0]
|
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-03-16 14:41:39 +00:00
|
|
|
totalMoveTimeMinute = 0.0
|
2013-08-07 15:01:48 +00:00
|
|
|
absoluteE = True
|
2012-02-28 14:02:24 +00:00
|
|
|
scale = 1.0
|
|
|
|
posAbs = True
|
2013-08-07 15:01:48 +00:00
|
|
|
feedRate = 3600.0
|
|
|
|
unknownGcodes = {}
|
|
|
|
unknownMcodes = {}
|
|
|
|
|
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]
|
2013-08-07 15:01:48 +00:00
|
|
|
filePos += 1
|
2013-08-07 15:23:40 +00:00
|
|
|
if self.progressCallback is not None and (filePos % 1000 == 0):
|
2012-12-28 19:04:43 +00:00
|
|
|
if isinstance(gcodeFile, (file)):
|
2013-08-07 15:01:48 +00:00
|
|
|
self.progressCallback(float(gcodeFile.tell()) / float(self._fileSize))
|
2012-12-28 19:04:43 +00:00
|
|
|
elif isinstance(gcodeFile, (list)):
|
|
|
|
self.progressCallback(float(filePos) / float(len(gcodeFile)))
|
|
|
|
|
2012-03-26 13:03:26 +00:00
|
|
|
if ';' in line:
|
|
|
|
comment = line[line.find(';')+1:].strip()
|
2013-08-11 15:39:45 +00:00
|
|
|
if comment.startswith("filament_diameter"):
|
2013-08-07 15:01:48 +00:00
|
|
|
self._filamentDiameter = float(comment.split("=", 1)[1].strip())
|
2013-06-18 19:28:05 +00:00
|
|
|
elif comment.startswith("CURA_PROFILE_STRING"):
|
|
|
|
curaOptions = self._parseCuraProfileString(comment)
|
2013-08-07 15:01:48 +00:00
|
|
|
if "filament_diameter" in curaOptions:
|
2013-06-18 19:28:05 +00:00
|
|
|
try:
|
2013-08-07 15:01:48 +00:00
|
|
|
self._filamentDiameter = float(curaOptions["filament_diameter"])
|
2013-06-18 19:28:05 +00:00
|
|
|
except:
|
2013-08-07 15:01:48 +00:00
|
|
|
self._filamentDiameter = 0.0
|
2012-03-26 13:03:26 +00:00
|
|
|
line = line[0:line.find(';')]
|
2013-08-07 15:01:48 +00:00
|
|
|
|
|
|
|
T = getCodeInt(line, 'T')
|
2012-04-29 12:22:20 +00:00
|
|
|
if T is not None:
|
|
|
|
if currentExtruder > 0:
|
2013-08-07 15:01:48 +00:00
|
|
|
posOffset[0] -= getPreference('extruder_offset_x%d' % (currentExtruder), 0.0)
|
|
|
|
posOffset[1] -= getPreference('extruder_offset_y%d' % (currentExtruder), 0.0)
|
2012-04-29 12:50:52 +00:00
|
|
|
currentExtruder = T
|
|
|
|
if currentExtruder > 0:
|
2013-08-07 15:01:48 +00:00
|
|
|
posOffset[0] += getPreference('extruder_offset_x%d' % (currentExtruder), 0.0)
|
|
|
|
posOffset[1] += getPreference('extruder_offset_y%d' % (currentExtruder), 0.0)
|
2012-03-27 11:48:40 +00:00
|
|
|
|
2013-08-07 15:01:48 +00:00
|
|
|
G = getCodeInt(line, 'G')
|
2012-02-28 14:02:24 +00:00
|
|
|
if G is not None:
|
|
|
|
if G == 0 or G == 1: #Move
|
2013-08-07 15:01:48 +00:00
|
|
|
x = getCodeFloat(line, 'X')
|
|
|
|
y = getCodeFloat(line, 'Y')
|
|
|
|
z = getCodeFloat(line, 'Z')
|
|
|
|
e = getCodeFloat(line, 'E')
|
|
|
|
f = getCodeFloat(line, 'F')
|
|
|
|
oldPos = pos
|
|
|
|
pos = pos[:]
|
|
|
|
if posAbs:
|
|
|
|
if x is not None:
|
|
|
|
pos[0] = x * scale + posOffset[0]
|
|
|
|
if y is not None:
|
|
|
|
pos[1] = y * scale + posOffset[1]
|
|
|
|
if z is not None:
|
|
|
|
pos[2] = z * scale + posOffset[2]
|
|
|
|
else:
|
|
|
|
if x is not None:
|
|
|
|
pos[0] += x * scale
|
|
|
|
if y is not None:
|
|
|
|
pos[1] += y * scale
|
|
|
|
if z is not None:
|
|
|
|
pos[2] += 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:
|
2013-08-07 15:01:48 +00:00
|
|
|
diffX = oldPos[0] - pos[0]
|
|
|
|
diffY = oldPos[1] - pos[1]
|
|
|
|
totalMoveTimeMinute += math.sqrt(diffX * diffX + diffY * diffY) / feedRate
|
2012-02-28 14:02:24 +00:00
|
|
|
moveType = 'move'
|
|
|
|
if e is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
if absoluteE:
|
|
|
|
e -= currentE
|
|
|
|
if e > 0.0:
|
|
|
|
moveType = 'extrude'
|
|
|
|
if e < 0.0:
|
|
|
|
moveType = 'retract'
|
|
|
|
totalExtrusion += e
|
|
|
|
currentE += e
|
2012-03-16 12:43:49 +00:00
|
|
|
if totalExtrusion > maxExtrusion:
|
|
|
|
maxExtrusion = totalExtrusion
|
2013-08-07 15:01:48 +00:00
|
|
|
else:
|
|
|
|
e = 0.0
|
|
|
|
if moveType == 'move' and oldPos[2] != pos[2]:
|
|
|
|
if oldPos[2] > pos[2] and abs(oldPos[2] - pos[2]) > 5.0 and pos[2] < 1.0:
|
|
|
|
oldPos[2] = 0.0
|
2012-06-19 19:57:16 +00:00
|
|
|
elif G == 4: #Delay
|
2013-08-07 15:01:48 +00:00
|
|
|
S = getCodeFloat(line, 'S')
|
2012-06-19 19:57:16 +00:00
|
|
|
if S is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
totalMoveTimeMinute += S / 60.0
|
|
|
|
P = getCodeFloat(line, 'P')
|
2012-06-19 19:57:16 +00:00
|
|
|
if P is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
totalMoveTimeMinute += P / 60.0 / 1000.0
|
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
|
2013-08-07 15:01:48 +00:00
|
|
|
x = getCodeFloat(line, 'X')
|
|
|
|
y = getCodeFloat(line, 'Y')
|
|
|
|
z = getCodeFloat(line, 'Z')
|
|
|
|
if getPreference('machine_center_is_zero') == 'True':
|
|
|
|
center = [getPreference('machine_width') / 2, getPreference('machine_depth') / 2,0.0]
|
|
|
|
else:
|
|
|
|
center = [0.0,0.0,0.0]
|
2012-02-28 14:02:24 +00:00
|
|
|
if x is None and y is None and z is None:
|
2013-08-07 15:01:48 +00:00
|
|
|
pos = center
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
2013-08-07 15:01:48 +00:00
|
|
|
pos = pos[:]
|
2012-02-28 14:02:24 +00:00
|
|
|
if x is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
pos[0] = center[0]
|
2012-02-28 14:02:24 +00:00
|
|
|
if y is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
pos[1] = center[1]
|
2012-02-28 14:02:24 +00:00
|
|
|
if z is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
pos[2] = center[2]
|
2012-02-28 14:02:24 +00:00
|
|
|
elif G == 90: #Absolute position
|
|
|
|
posAbs = True
|
|
|
|
elif G == 91: #Relative position
|
|
|
|
posAbs = False
|
|
|
|
elif G == 92:
|
2013-08-07 15:01:48 +00:00
|
|
|
x = getCodeFloat(line, 'X')
|
|
|
|
y = getCodeFloat(line, 'Y')
|
|
|
|
z = getCodeFloat(line, 'Z')
|
|
|
|
e = getCodeFloat(line, 'E')
|
2012-02-28 14:02:24 +00:00
|
|
|
if e is not None:
|
|
|
|
currentE = e
|
|
|
|
if x is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
posOffset[0] = pos[0] - x
|
2012-02-28 14:02:24 +00:00
|
|
|
if y is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
posOffset[1] = pos[1] - y
|
2012-02-28 14:02:24 +00:00
|
|
|
if z is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
posOffset[2] = pos[2] - z
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
2013-05-09 15:07:33 +00:00
|
|
|
if G not in unknownGcodes:
|
2013-06-18 19:28:05 +00:00
|
|
|
self._logger.info("Unknown G code: %r" % G)
|
2013-08-07 15:01:48 +00:00
|
|
|
unknownGcodes[G] = True
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
2013-08-07 15:01:48 +00:00
|
|
|
M = getCodeInt(line, 'M')
|
2012-02-28 14:02:24 +00:00
|
|
|
if M is not None:
|
2013-08-07 15:01:48 +00:00
|
|
|
if M == 0: #Message with possible wait (ignored)
|
|
|
|
pass
|
|
|
|
elif M == 1: #Message with possible wait (ignored)
|
2012-02-28 14:02:24 +00:00
|
|
|
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-08-07 15:01:48 +00:00
|
|
|
elif M == 82: #Absolute E
|
|
|
|
absoluteE = True
|
|
|
|
elif M == 83: #Relative E
|
|
|
|
absoluteE = 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
|
2013-08-07 15:01:48 +00:00
|
|
|
elif M == 117: #LCD message
|
|
|
|
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
|
2013-08-07 15:01:48 +00:00
|
|
|
s = getCodeFloat(line, 'S')
|
2012-02-28 14:02:24 +00:00
|
|
|
else:
|
2013-05-09 15:07:33 +00:00
|
|
|
if M not in unknownMcodes:
|
2013-06-18 19:28:05 +00:00
|
|
|
self._logger.info("Unknown M code: %r" % M)
|
2013-08-07 15:01:48 +00:00
|
|
|
unknownMcodes[M] = True
|
|
|
|
if self.progressCallback is not None:
|
|
|
|
self.progressCallback(100.0)
|
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
|
|
|
|
2013-06-18 19:28:05 +00:00
|
|
|
def _parseCuraProfileString(self, comment):
|
|
|
|
return {key: value for (key, value) in map(lambda x: x.split("=", 1), zlib.decompress(base64.b64decode(comment[len("CURA_PROFILE_STRING:"):])).split("\b"))}
|
|
|
|
|
2013-08-07 15:01:48 +00:00
|
|
|
def getCodeInt(line, code):
|
|
|
|
n = line.find(code) + 1
|
|
|
|
if n < 1:
|
|
|
|
return None
|
|
|
|
m = line.find(' ', n)
|
|
|
|
try:
|
|
|
|
if m < 0:
|
|
|
|
return int(line[n:])
|
|
|
|
return int(line[n:m])
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getCodeFloat(line, code):
|
|
|
|
n = line.find(code) + 1
|
|
|
|
if n < 1:
|
|
|
|
return None
|
|
|
|
m = line.find(' ', n)
|
|
|
|
try:
|
|
|
|
if m < 0:
|
|
|
|
return float(line[n:])
|
|
|
|
return float(line[n:m])
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2012-03-22 14:30:28 +00:00
|
|
|
if __name__ == '__main__':
|
2013-08-07 15:01:48 +00:00
|
|
|
from time import time
|
|
|
|
t = time()
|
2012-03-22 14:30:28 +00:00
|
|
|
for filename in sys.argv[1:]:
|
2013-08-07 15:01:48 +00:00
|
|
|
g = gcode()
|
|
|
|
g.load(filename)
|
|
|
|
print g.totalMoveTimeMinute
|
|
|
|
print g.extrusionAmount
|
|
|
|
print g.calculateVolumeCm3()
|
|
|
|
print time() - t
|
2012-03-22 14:30:28 +00:00
|
|
|
|