From 643dcba468ebbb7edf0976a2721ccb305b80e565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 13 Apr 2021 14:47:41 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20analysis:=20New=20--layers=20parame?= =?UTF-8?q?ter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Analysis subcommand now has a new --layers parameter that allows us to export information on all detected layers. Also recognizes zhop. Helpful for comparing analysis with gcv results, see #4086 --- src/octoprint/cli/analysis.py | 4 +- src/octoprint/util/gcodeInterpreter.py | 53 ++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/octoprint/cli/analysis.py b/src/octoprint/cli/analysis.py index 7129b8806..b2cf736de 100644 --- a/src/octoprint/cli/analysis.py +++ b/src/octoprint/cli/analysis.py @@ -77,6 +77,7 @@ def util(): @click.option("--g90-extruder", "g90_extruder", is_flag=True) @click.option("--bed-z", "bedz", type=float, default=0) @click.option("--progress", "progress", is_flag=True) +@click.option("--layers", "layers", is_flag=True) @click.argument("path", type=click.Path()) def gcode_command( path, @@ -90,6 +91,7 @@ def gcode_command( g90_extruder, bedz, progress, + layers, ): """Runs a GCODE file analysis.""" @@ -125,7 +127,7 @@ def gcode_command( def progress_callback(percentage): click.echo("PROGRESS:{}".format(percentage)) - interpreter = gcode(progress_callback=progress_callback) + interpreter = gcode(progress_callback=progress_callback, incl_layers=layers) interpreter.load( path, diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index 34f1520f8..0d26282ec 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -211,9 +211,8 @@ regex_command = re.compile( class gcode(object): - def __init__(self, progress_callback=None): + def __init__(self, incl_layers=False, progress_callback=None): self._logger = logging.getLogger(__name__) - self.layerList = None self.extrusionAmount = [0] self.extrusionVolume = [0] self.totalMoveTimeMinute = 0 @@ -224,6 +223,25 @@ class gcode(object): self._minMax = MinMax3D() self._progress_callback = progress_callback + self._incl_layers = incl_layers + self._layers = [] + self._current_layer = None + + def _track_layer(self, pos): + if not self._incl_layers: + return + + if self._current_layer is None or self._current_layer["z"] != pos.z: + self._current_layer = {"z": pos.z, "minmax": MinMax3D(), "commands": 1} + self._layers.append(self._current_layer) + + elif self._current_layer: + self._current_layer["minmax"].record(pos) + + def _track_command(self): + if self._current_layer: + self._current_layer["commands"] += 1 + @property def dimensions(self): size = self._minMax.size @@ -240,6 +258,23 @@ class gcode(object): "maxZ": None if math.isinf(self._minMax.max.z) else self._minMax.max.z, } + @property + def layers(self): + return [ + { + "num": num + 1, + "z": layer["z"], + "commands": layer["commands"], + "bounds": { + "minX": layer["minmax"].min.x, + "maxX": layer["minmax"].max.x, + "minY": layer["minmax"].min.y, + "maxY": layer["minmax"].max.y, + }, + } + for num, layer in enumerate(self._layers) + ] + def load( self, filename, @@ -453,6 +488,10 @@ class gcode(object): # time to add is maximum of both totalMoveTimeMinute += max(moveTimeXYZ, extrudeTime) + # process layers if there's extrusion + if e: + self._track_layer(pos) + elif gcode == "G4": # Delay S = getCodeFloat(line, "S") if S is not None: @@ -513,7 +552,6 @@ class gcode(object): pos.y = y if z is not None: pos.z = z - # M codes elif gcode == "M82": # Absolute E relativeE = False @@ -581,6 +619,9 @@ class gcode(object): for _ in range(len(totalExtrusion), currentExtruder + 1): totalExtrusion.append(0.0) + if gcode or tool: + self._track_command() + if throttle is not None: throttle(lineNo, readBytes) if self._progress_callback is not None: @@ -605,13 +646,17 @@ class gcode(object): } def get_result(self): - return { + result = { "total_time": self.totalMoveTimeMinute, "extrusion_length": self.extrusionAmount, "extrusion_volume": self.extrusionVolume, "dimensions": self.dimensions, "printing_area": self.printing_area, } + if self._incl_layers: + result["layers"] = self.layers + + return result def getCodeInt(line, code):