Added filament diameter retrieval from gcode generated by Cura, fixed Slic3r version

master
Gina Häußge 2013-06-18 21:28:05 +02:00
parent e32afe147a
commit 7c85013389
2 changed files with 28 additions and 12 deletions

View File

@ -64,7 +64,7 @@ class GcodeManager:
if gcode.extrusionAmount: if gcode.extrusionAmount:
analysisResult["filament"] = "%.2fm" % (gcode.extrusionAmount / 1000) analysisResult["filament"] = "%.2fm" % (gcode.extrusionAmount / 1000)
if gcode.extrusionVolume: if gcode.extrusionVolume:
analysisResult["filament"] += " / %.2fcc" % gcode.extrusionVolume analysisResult["filament"] += " / %.2fc" % gcode.extrusionVolume
dirty = True dirty = True
if dirty: if dirty:

View File

@ -4,6 +4,9 @@ import sys
import math import math
import re import re
import os import os
import base64
import zlib
import logging
from octoprint.util import util3d from octoprint.util import util3d
@ -34,10 +37,12 @@ class gcodePath(object):
class gcode(object): class gcode(object):
def __init__(self): def __init__(self):
self._logger = logging.getLogger(__name__)
self.regMatch = {} self.regMatch = {}
self.layerList = [] self.layerList = []
self.extrusionAmount = 0 self.extrusionAmount = 0
self.extrusionVolume = 0 self.extrusionVolume = None
self.totalMoveTimeMinute = 0 self.totalMoveTimeMinute = 0
self.progressCallback = None self.progressCallback = None
self._abort = False self._abort = False
@ -62,17 +67,16 @@ class gcode(object):
currentE = 0.0 currentE = 0.0
totalExtrusion = 0.0 totalExtrusion = 0.0
maxExtrusion = 0.0 maxExtrusion = 0.0
extrusionDiameter = 0
currentExtruder = 0 currentExtruder = 0
extrudeAmountMultiply = 1.0 extrudeAmountMultiply = 1.0
totalMoveTimeMinute = 0.0 totalMoveTimeMinute = 0.0
filamentDiameter = 0.0
scale = 1.0 scale = 1.0
posAbs = True posAbs = True
posAbsExtruder = True; posAbsExtruder = True;
feedRate = 3600 feedRate = 3600
layerThickness = 0.1 layerThickness = 0.1
pathType = 'CUSTOM'; pathType = 'CUSTOM';
startCodeDone = False
currentLayer = [] currentLayer = []
unknownGcodes={} unknownGcodes={}
unknownMcodes={} unknownMcodes={}
@ -99,7 +103,7 @@ class gcode(object):
startCodeDone = True startCodeDone = True
if ';' in line: if ';' in line:
#Slic3r GCode comment parser # Slic3r GCode comment parser
comment = line[line.find(';')+1:].strip() comment = line[line.find(';')+1:].strip()
if comment == 'fill': if comment == 'fill':
pathType = 'FILL' pathType = 'FILL'
@ -107,13 +111,21 @@ class gcode(object):
pathType = 'WALL-INNER' pathType = 'WALL-INNER'
elif comment == 'skirt': elif comment == 'skirt':
pathType = 'SKIRT' pathType = 'SKIRT'
elif comment.startswith('filament_diameter = '): elif comment.startswith("filament_diameter"):
filamentDiameter = int(line[line.find('=')+1:]) filamentDiameter = float(line.split("=", 1)[1].strip())
# Cura Gcode comment parser
if comment.startswith('LAYER:'): if comment.startswith('LAYER:'):
self.layerList.append(currentLayer) self.layerList.append(currentLayer)
currentLayer = [] currentLayer = []
if pathType != "CUSTOM": elif comment.startswith("CURA_PROFILE_STRING"):
startCodeDone = True curaOptions = self._parseCuraProfileString(comment)
if "filament_diameter" in curaOptions.keys():
try:
filamentDiameter = float(curaOptions["filament_diameter"])
except:
filamentDiameter = 0.0
line = line[0:line.find(';')] line = line[0:line.find(';')]
T = self.getCodeInt(line, 'T') T = self.getCodeInt(line, 'T')
if T is not None: if T is not None:
@ -227,7 +239,7 @@ class gcode(object):
posOffset.z = pos.z - z posOffset.z = pos.z - z
else: else:
if G not in unknownGcodes: if G not in unknownGcodes:
print "Unknown G code:" + str(G) self._logger.info("Unknown G code: %r" % G)
unknownGcodes[G] = True unknownGcodes[G] = True
else: else:
M = self.getCodeInt(line, 'M') M = self.getCodeInt(line, 'M')
@ -276,11 +288,12 @@ class gcode(object):
extrudeAmountMultiply = s / 100.0 extrudeAmountMultiply = s / 100.0
else: else:
if M not in unknownMcodes: if M not in unknownMcodes:
print "Unknown M code:" + str(M) self._logger.info("Unknown M code: %r" % M)
unknownMcodes[M] = True unknownMcodes[M] = True
self.layerList.append(currentLayer) self.layerList.append(currentLayer)
self.extrusionAmount = maxExtrusion self.extrusionAmount = maxExtrusion
self.extrusionVolume = math.pi * math.pow(filamentDiameter / 2.0, 2) * maxExtrusion / 1000.0 if filamentDiameter is not None and filamentDiameter > 0:
self.extrusionVolume = math.pi * math.pow(filamentDiameter / 2.0, 2) * maxExtrusion / 1000.0
self.totalMoveTimeMinute = totalMoveTimeMinute self.totalMoveTimeMinute = totalMoveTimeMinute
def getCodeInt(self, line, code): def getCodeInt(self, line, code):
@ -305,6 +318,9 @@ class gcode(object):
except: except:
return None return None
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"))}
if __name__ == '__main__': if __name__ == '__main__':
for filename in sys.argv[1:]: for filename in sys.argv[1:]:
gcode().load(filename) gcode().load(filename)