diff --git a/plugins/GCodeReader/FlavorParser.py b/plugins/GCodeReader/FlavorParser.py index 696b3b180b..5da1a79fb7 100644 --- a/plugins/GCodeReader/FlavorParser.py +++ b/plugins/GCodeReader/FlavorParser.py @@ -23,9 +23,9 @@ import numpy import math import re from typing import Dict, List, NamedTuple, Optional, Union -from collections import namedtuple -Position = NamedTuple("Position", [("x", float), ("y", float), ("z", float), ("f", float), ("e", float)]) +PositionOptional = NamedTuple("Position", [("x", Optional[float]), ("y", Optional[float]), ("z", Optional[float]), ("f", Optional[float]), ("e", Optional[float])]) +Position = NamedTuple("Position", [("x", float), ("y", float), ("z", float), ("f", float), ("e", List[float])]) ## This parser is intended to interpret the common firmware codes among all the # different flavors @@ -40,9 +40,9 @@ class FlavorParser: self._clearValues() self._scene_node = None # X, Y, Z position, F feedrate and E extruder values are stored - self._position = namedtuple('Position', ['x', 'y', 'z', 'f', 'e']) + self._position = Position self._is_layers_in_file = False # Does the Gcode have the layers comment? - self._extruder_offsets = {} # Offsets for multi extruders. key is index, value is [x-offset, y-offset] + self._extruder_offsets = {} # type: Dict[int, List[float]] # Offsets for multi extruders. key is index, value is [x-offset, y-offset] self._current_layer_thickness = 0.2 # default self._filament_diameter = 2.85 # default @@ -50,10 +50,10 @@ class FlavorParser: def _clearValues(self) -> None: self._extruder_number = 0 - self._extrusion_length_offset = [0] + self._extrusion_length_offset = [0] # type: List[float] self._layer_type = LayerPolygon.Inset0Type self._layer_number = 0 - self._previous_z = 0 + self._previous_z = 0 # type: float self._layer_data_builder = LayerDataBuilder.LayerDataBuilder() self._is_absolute_positioning = True # It can be absolute (G90) or relative (G91) self._is_absolute_extrusion = True # It can become absolute (M82, default) or relative (M83) @@ -77,14 +77,14 @@ class FlavorParser: def _getInt(self, line: str, code: str) -> Optional[int]: value = self._getValue(line, code) try: - return int(value) + return int(value) # type: ignore except: return None def _getFloat(self, line: str, code: str) -> Optional[float]: value = self._getValue(line, code) try: - return float(value) + return float(value) # type: ignore except: return None @@ -169,7 +169,7 @@ class FlavorParser: return 0.35 return line_width - def _gCode0(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: + def _gCode0(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position: x, y, z, f, e = position if self._is_absolute_positioning: @@ -205,7 +205,7 @@ class FlavorParser: _gCode1 = _gCode0 ## Home the head. - def _gCode28(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: + def _gCode28(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position: return self._position( params.x if params.x is not None else position.x, params.y if params.y is not None else position.y, @@ -214,20 +214,20 @@ class FlavorParser: position.e) ## Set the absolute positioning - def _gCode90(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: + def _gCode90(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position: self._is_absolute_positioning = True self._is_absolute_extrusion = True return position ## Set the relative positioning - def _gCode91(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: + def _gCode91(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position: self._is_absolute_positioning = False self._is_absolute_extrusion = False return position ## Reset the current position to the values specified. # For example: G92 X10 will set the X to 10 without any physical motion. - def _gCode92(self, position: Position, params: Position, path: List[List[Union[float, int]]]) -> Position: + def _gCode92(self, position: Position, params: PositionOptional, path: List[List[Union[float, int]]]) -> Position: if params.e is not None: # Sometimes a G92 E0 is introduced in the middle of the GCode so we need to keep those offsets for calculate the line_width self._extrusion_length_offset[self._extruder_number] += position.e[self._extruder_number] - params.e @@ -260,7 +260,7 @@ class FlavorParser: f = float(item[1:]) / 60 if item[0] == "E": e = float(item[1:]) - params = self._position(x, y, z, f, e) + params = PositionOptional(x, y, z, f, e) return func(position, params, path) return position @@ -322,12 +322,13 @@ class FlavorParser: lifetime=0, title = catalog.i18nc("@info:title", "G-code Details")) + assert(self._message is not None) # use for typing purposes self._message.setProgress(0) self._message.show() Logger.log("d", "Parsing Gcode...") - current_position = self._position(0, 0, 0, 0, [0]) + current_position = Position(0, 0, 0, 0, [0]) current_path = [] min_layer_number = 0 negative_layers = 0