mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-07 06:57:28 -06:00
Add typing
As per code style. Contributes to issue CURA-5330.
This commit is contained in:
parent
45fc8480a1
commit
af06096e08
1 changed files with 26 additions and 22 deletions
|
@ -1,4 +1,4 @@
|
||||||
# Copyright (c) 2017 Ultimaker B.V.
|
# Copyright (c) 2018 Ultimaker B.V.
|
||||||
# Cura is released under the terms of the LGPLv3 or higher.
|
# Cura is released under the terms of the LGPLv3 or higher.
|
||||||
|
|
||||||
from UM.Application import Application
|
from UM.Application import Application
|
||||||
|
@ -23,12 +23,16 @@ from cura.Settings.ExtruderManager import ExtruderManager
|
||||||
import numpy
|
import numpy
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
from typing import Dict, List, NamedTuple, Optional, Union
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
# This parser is intented for interpret the common firmware codes among all the different flavors
|
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
|
||||||
class FlavorParser:
|
class FlavorParser:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
Application.getInstance().hideMessageSignal.connect(self._onHideMessage)
|
Application.getInstance().hideMessageSignal.connect(self._onHideMessage)
|
||||||
self._cancelled = False
|
self._cancelled = False
|
||||||
self._message = None
|
self._message = None
|
||||||
|
@ -45,7 +49,7 @@ class FlavorParser:
|
||||||
|
|
||||||
Preferences.getInstance().addPreference("gcodereader/show_caution", True)
|
Preferences.getInstance().addPreference("gcodereader/show_caution", True)
|
||||||
|
|
||||||
def _clearValues(self):
|
def _clearValues(self) -> None:
|
||||||
self._extruder_number = 0
|
self._extruder_number = 0
|
||||||
self._extrusion_length_offset = [0]
|
self._extrusion_length_offset = [0]
|
||||||
self._layer_type = LayerPolygon.Inset0Type
|
self._layer_type = LayerPolygon.Inset0Type
|
||||||
|
@ -57,7 +61,7 @@ class FlavorParser:
|
||||||
self._is_absolute_extrusion = True # It can become absolute (M82, default) or relative (M83)
|
self._is_absolute_extrusion = True # It can become absolute (M82, default) or relative (M83)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _getValue(line, code):
|
def _getValue(line: str, code: str) -> Optional[Union[str, int, float]]:
|
||||||
n = line.find(code)
|
n = line.find(code)
|
||||||
if n < 0:
|
if n < 0:
|
||||||
return None
|
return None
|
||||||
|
@ -72,29 +76,29 @@ class FlavorParser:
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _getInt(self, line, code):
|
def _getInt(self, line: str, code: str) -> Optional[int]:
|
||||||
value = self._getValue(line, code)
|
value = self._getValue(line, code)
|
||||||
try:
|
try:
|
||||||
return int(value)
|
return int(value)
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _getFloat(self, line, code):
|
def _getFloat(self, line: str, code: str) -> Optional[float]:
|
||||||
value = self._getValue(line, code)
|
value = self._getValue(line, code)
|
||||||
try:
|
try:
|
||||||
return float(value)
|
return float(value)
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _onHideMessage(self, message):
|
def _onHideMessage(self, message: str) -> None:
|
||||||
if message == self._message:
|
if message == self._message:
|
||||||
self._cancelled = True
|
self._cancelled = True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _getNullBoundingBox():
|
def _getNullBoundingBox() -> AxisAlignedBox:
|
||||||
return AxisAlignedBox(minimum=Vector(0, 0, 0), maximum=Vector(10, 10, 10))
|
return AxisAlignedBox(minimum=Vector(0, 0, 0), maximum=Vector(10, 10, 10))
|
||||||
|
|
||||||
def _createPolygon(self, layer_thickness, path, extruder_offsets):
|
def _createPolygon(self, layer_thickness: float, path: List[Position], extruder_offsets: List[float]) -> bool:
|
||||||
countvalid = 0
|
countvalid = 0
|
||||||
for point in path:
|
for point in path:
|
||||||
if point[5] > 0:
|
if point[5] > 0:
|
||||||
|
@ -140,12 +144,12 @@ class FlavorParser:
|
||||||
this_layer.polygons.append(this_poly)
|
this_layer.polygons.append(this_poly)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _createEmptyLayer(self, layer_number):
|
def _createEmptyLayer(self, layer_number: int) -> None:
|
||||||
self._layer_data_builder.addLayer(layer_number)
|
self._layer_data_builder.addLayer(layer_number)
|
||||||
self._layer_data_builder.setLayerHeight(layer_number, 0)
|
self._layer_data_builder.setLayerHeight(layer_number, 0)
|
||||||
self._layer_data_builder.setLayerThickness(layer_number, 0)
|
self._layer_data_builder.setLayerThickness(layer_number, 0)
|
||||||
|
|
||||||
def _calculateLineWidth(self, current_point, previous_point, current_extrusion, previous_extrusion, layer_thickness):
|
def _calculateLineWidth(self, current_point: Position, previous_point: Position, current_extrusion: float, previous_extrusion: float, layer_thickness: float) -> float:
|
||||||
# Area of the filament
|
# Area of the filament
|
||||||
Af = (self._filament_diameter / 2) ** 2 * numpy.pi
|
Af = (self._filament_diameter / 2) ** 2 * numpy.pi
|
||||||
# Length of the extruded filament
|
# Length of the extruded filament
|
||||||
|
@ -167,7 +171,7 @@ class FlavorParser:
|
||||||
return 0.35
|
return 0.35
|
||||||
return line_width
|
return line_width
|
||||||
|
|
||||||
def _gCode0(self, position, params, path):
|
def _gCode0(self, position: Position, params: Position, path: List[Position]) -> Position:
|
||||||
x, y, z, f, e = position
|
x, y, z, f, e = position
|
||||||
|
|
||||||
if self._is_absolute_positioning:
|
if self._is_absolute_positioning:
|
||||||
|
@ -203,7 +207,7 @@ class FlavorParser:
|
||||||
_gCode1 = _gCode0
|
_gCode1 = _gCode0
|
||||||
|
|
||||||
## Home the head.
|
## Home the head.
|
||||||
def _gCode28(self, position, params, path):
|
def _gCode28(self, position: Position, params: Position, path: List[Position]) -> Position:
|
||||||
return self._position(
|
return self._position(
|
||||||
params.x if params.x is not None else position.x,
|
params.x if params.x is not None else position.x,
|
||||||
params.y if params.y is not None else position.y,
|
params.y if params.y is not None else position.y,
|
||||||
|
@ -212,20 +216,20 @@ class FlavorParser:
|
||||||
position.e)
|
position.e)
|
||||||
|
|
||||||
## Set the absolute positioning
|
## Set the absolute positioning
|
||||||
def _gCode90(self, position, params, path):
|
def _gCode90(self, position: Position, params: Position, path: List[Position]) -> Position:
|
||||||
self._is_absolute_positioning = True
|
self._is_absolute_positioning = True
|
||||||
self._is_absolute_extrusion = True
|
self._is_absolute_extrusion = True
|
||||||
return position
|
return position
|
||||||
|
|
||||||
## Set the relative positioning
|
## Set the relative positioning
|
||||||
def _gCode91(self, position, params, path):
|
def _gCode91(self, position: Position, params: Position, path: List[Position]) -> Position:
|
||||||
self._is_absolute_positioning = False
|
self._is_absolute_positioning = False
|
||||||
self._is_absolute_extrusion = False
|
self._is_absolute_extrusion = False
|
||||||
return position
|
return position
|
||||||
|
|
||||||
## Reset the current position to the values specified.
|
## Reset the current position to the values specified.
|
||||||
# For example: G92 X10 will set the X to 10 without any physical motion.
|
# For example: G92 X10 will set the X to 10 without any physical motion.
|
||||||
def _gCode92(self, position, params, path):
|
def _gCode92(self, position: Position, params: Position, path: List[Position]) -> Position:
|
||||||
if params.e is not None:
|
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
|
# 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
|
self._extrusion_length_offset[self._extruder_number] += position.e[self._extruder_number] - params.e
|
||||||
|
@ -237,7 +241,7 @@ class FlavorParser:
|
||||||
params.f if params.f is not None else position.f,
|
params.f if params.f is not None else position.f,
|
||||||
position.e)
|
position.e)
|
||||||
|
|
||||||
def processGCode(self, G, line, position, path):
|
def processGCode(self, G: int, line: str, position: Position, path: List[Position]) -> Position:
|
||||||
func = getattr(self, "_gCode%s" % G, None)
|
func = getattr(self, "_gCode%s" % G, None)
|
||||||
line = line.split(";", 1)[0] # Remove comments (if any)
|
line = line.split(";", 1)[0] # Remove comments (if any)
|
||||||
if func is not None:
|
if func is not None:
|
||||||
|
@ -264,21 +268,21 @@ class FlavorParser:
|
||||||
return func(position, params, path)
|
return func(position, params, path)
|
||||||
return position
|
return position
|
||||||
|
|
||||||
def processTCode(self, T, line, position, path):
|
def processTCode(self, T: int, line: str, position: Position, path: List[Position]) -> Position:
|
||||||
self._extruder_number = T
|
self._extruder_number = T
|
||||||
if self._extruder_number + 1 > len(position.e):
|
if self._extruder_number + 1 > len(position.e):
|
||||||
self._extrusion_length_offset.extend([0] * (self._extruder_number - len(position.e) + 1))
|
self._extrusion_length_offset.extend([0] * (self._extruder_number - len(position.e) + 1))
|
||||||
position.e.extend([0] * (self._extruder_number - len(position.e) + 1))
|
position.e.extend([0] * (self._extruder_number - len(position.e) + 1))
|
||||||
return position
|
return position
|
||||||
|
|
||||||
def processMCode(self, M, line, position, path):
|
def processMCode(self, M: int, line: str, position: Position, path: List[Position]) -> Position:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
_type_keyword = ";TYPE:"
|
_type_keyword = ";TYPE:"
|
||||||
_layer_keyword = ";LAYER:"
|
_layer_keyword = ";LAYER:"
|
||||||
|
|
||||||
## For showing correct x, y offsets for each extruder
|
## For showing correct x, y offsets for each extruder
|
||||||
def _extruderOffsets(self):
|
def _extruderOffsets(self) -> Dict[int, float]:
|
||||||
result = {}
|
result = {}
|
||||||
for extruder in ExtruderManager.getInstance().getExtruderStacks():
|
for extruder in ExtruderManager.getInstance().getExtruderStacks():
|
||||||
result[int(extruder.getMetaData().get("position", "0"))] = [
|
result[int(extruder.getMetaData().get("position", "0"))] = [
|
||||||
|
@ -286,7 +290,7 @@ class FlavorParser:
|
||||||
extruder.getProperty("machine_nozzle_offset_y", "value")]
|
extruder.getProperty("machine_nozzle_offset_y", "value")]
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def processGCodeStream(self, stream):
|
def processGCodeStream(self, stream: str) -> Optional[CuraSceneNode]:
|
||||||
Logger.log("d", "Preparing to load GCode")
|
Logger.log("d", "Preparing to load GCode")
|
||||||
self._cancelled = False
|
self._cancelled = False
|
||||||
# We obtain the filament diameter from the selected extruder to calculate line widths
|
# We obtain the filament diameter from the selected extruder to calculate line widths
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue