fix linting issues

This commit is contained in:
phaseloop
2025-11-05 11:21:34 +00:00
parent 0738446626
commit 802b698752
2 changed files with 86 additions and 42 deletions

View File

@@ -1,5 +1,9 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
"""
Testing functions for VCarve operation module
"""
# ***************************************************************************
# * Copyright (c) 2020 sliptonic <shopinthewoods@gmail.com> *
# * *
@@ -29,8 +33,10 @@ import Path.Op.Vcarve as PathVcarve
import math
from CAMTests.PathTestUtils import PathTestWithAssets
# pylint: disable=too-few-public-methods, protected-access
class VbitTool(object):
class VbitTool:
"""Faked out vcarve tool"""
def __init__(self, dia, angle, tipDia):
@@ -52,6 +58,7 @@ class TestPathVcarve(PathTestWithAssets):
FreeCAD.closeDocument(self.doc.Name)
def testFinishingPass(self):
"""Check if enabling finishing pass adds another path with required z-depth"""
self.doc = FreeCAD.newDocument()
part1 = FreeCAD.ActiveDocument.addObject("Part::Feature", "TestShape")
part2 = FreeCAD.ActiveDocument.addObject("Part::Feature", "TestShape")
@@ -198,7 +205,6 @@ class TestPathVcarve(PathTestWithAssets):
newPosition = FreeCAD.Base.Vector(0, 1.7, 3)
assert not PathVcarve.canSkipRepositioning(positionHistory, newPosition, 0.01)
def test18(self):
"""Verify if canSkipRepositioning allows to skip if new edge ends in current position"""
@@ -212,20 +218,22 @@ class TestPathVcarve(PathTestWithAssets):
# new position is same as previous position so we can G1 from (0,1,5) to (0,0,0) because
# we already travelled this path before and it's carved - no need to raise toolbit
newPosition = FreeCAD.Base.Vector(0, 0, 0)
assert PathVcarve.canSkipRepositioning(positionHistory, newPosition, defaultTolerance)
assert PathVcarve.canSkipRepositioning(
positionHistory, newPosition, defaultTolerance
)
# same but should fail because we are out of tolerance
newPosition = FreeCAD.Base.Vector(0, 0.1, 0)
assert not PathVcarve.canSkipRepositioning(positionHistory, newPosition, defaultTolerance)
assert not PathVcarve.canSkipRepositioning(
positionHistory, newPosition, defaultTolerance
)
# same but is OK because we are within tolerance
newPosition = FreeCAD.Base.Vector(0, 0.1, 0)
assert PathVcarve.canSkipRepositioning(positionHistory, newPosition, 0.1)
def test19(self):
"""Verify virtualBackTrackEdges() various scenarios
"""
"""Verify virtualBackTrackEdges() various scenarios"""
defaultTolerance = Path.Preferences.defaultGeometryTolerance()
@@ -237,17 +245,24 @@ class TestPathVcarve(PathTestWithAssets):
]
# new edge ends at current position
newEdge = Part.Edge(Part.LineSegment(FreeCAD.Base.Vector(1,2,3), FreeCAD.Base.Vector(0,1,5)))
newEdge = Part.Edge(
Part.LineSegment(FreeCAD.Base.Vector(1, 2, 3), FreeCAD.Base.Vector(0, 1, 5))
)
virtualEdges = PathVcarve.generateVirtualBackTrackEdges(positionHistory, newEdge, defaultTolerance)
virtualEdges = PathVcarve.generateVirtualBackTrackEdges(
positionHistory, newEdge, defaultTolerance
)
assert len(virtualEdges) == 1
virtualEdge = virtualEdges[0]
# virtualEdge is essentially a reversed newEdge
assert virtualEdge.valueAt(virtualEdge.FirstParameter) == newEdge.valueAt(newEdge.LastParameter)
assert virtualEdge.valueAt(virtualEdge.LastParameter) == newEdge.valueAt(newEdge.FirstParameter)
assert virtualEdge.valueAt(virtualEdge.FirstParameter) == newEdge.valueAt(
newEdge.LastParameter
)
assert virtualEdge.valueAt(virtualEdge.LastParameter) == newEdge.valueAt(
newEdge.FirstParameter
)
# test scenario 2 - refer to function comments for explanation
@@ -257,22 +272,26 @@ class TestPathVcarve(PathTestWithAssets):
]
# new edge ends at previous position
newEdge = Part.Edge(Part.LineSegment(FreeCAD.Base.Vector(1,2,3), FreeCAD.Base.Vector(0,0,0)))
newEdge = Part.Edge(
Part.LineSegment(FreeCAD.Base.Vector(1, 2, 3), FreeCAD.Base.Vector(0, 0, 0))
)
virtualEdges = PathVcarve.generateVirtualBackTrackEdges(positionHistory, newEdge, defaultTolerance)
virtualEdges = PathVcarve.generateVirtualBackTrackEdges(
positionHistory, newEdge, defaultTolerance
)
assert len(virtualEdges) == 2
virtualEdge1 = virtualEdges[0]
virtualEdge2 = virtualEdges[1]
# 2 virtual edges (current position, previous position) and (previous position, new edge start)
# 2 virtual edges (current position, previous position) and
# (previous position, new edge start)
assert virtualEdge1.valueAt(virtualEdge1.FirstParameter) == positionHistory[-1]
assert virtualEdge1.valueAt(virtualEdge1.LastParameter) == positionHistory[-2]
assert virtualEdge2.valueAt(virtualEdge2.FirstParameter) == positionHistory[-2]
assert virtualEdge2.valueAt(virtualEdge2.LastParameter) == newEdge.valueAt(newEdge.FirstParameter)
assert virtualEdge2.valueAt(virtualEdge2.LastParameter) == newEdge.valueAt(
newEdge.FirstParameter
)

View File

@@ -145,6 +145,7 @@ def _sortVoronoiWires(wires, start=FreeCAD.Vector(0, 0, 0)):
return result
def getReversedEdge(edge):
# returns a reversed edge (copy of original edge)
curve = edge.Curve
@@ -167,7 +168,7 @@ def generateVirtualBackTrackEdges(positionHistory, nextEdge, tolerance) -> list:
if not positionHistory:
return []
backTrackEdges = []
currentPosition = positionHistory[-1]
@@ -177,7 +178,7 @@ def generateVirtualBackTrackEdges(positionHistory, nextEdge, tolerance) -> list:
nextEdgeEnd = nextEdge.valueAt(nextEdge.LastParameter)
# Scenario 1
#
#
# in some cases travelling between wires looks like that:
# A ========= B ------- D
# |
@@ -186,7 +187,7 @@ def generateVirtualBackTrackEdges(positionHistory, nextEdge, tolerance) -> list:
# we follow first wire from A to B - new wire starts at C and goes through B -> D
# Repositioning to position C using G0 command does not make sense and it's slow
# We can insert "virtual" edge B->C at the beginning of a second wire to make
# continous CNC head movement
# continuous CNC head movement
#
if nextEdgeEnd.isEqual(currentPosition, tolerance):
@@ -212,11 +213,9 @@ def generateVirtualBackTrackEdges(positionHistory, nextEdge, tolerance) -> list:
# instead of G0 - just carve the edge in reverse direction
backTrackEdges.append(getReversedEdge(nextEdge))
return backTrackEdges
def canSkipRepositioning(positionHistory, newPosition, tolerance):
"""
Calculate if it makes sense to raise head to safe height and reposition before
@@ -398,7 +397,9 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
"App::PropertyLinkList",
"BaseShapes",
"Path",
QT_TRANSLATE_NOOP("App::Property", "Additional base objects to be engraved"),
QT_TRANSLATE_NOOP(
"App::Property", "Additional base objects to be engraved"
),
)
obj.setEditorMode("BaseShapes", 2) # hide
@@ -437,7 +438,9 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
"App::PropertyFloat",
"Discretize",
"Path",
QT_TRANSLATE_NOOP("App::Property", "The deflection value for discretizing arcs"),
QT_TRANSLATE_NOOP(
"App::Property", "The deflection value for discretizing arcs"
),
)
obj.addProperty(
"App::PropertyFloat",
@@ -501,7 +504,9 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
dist = ptv[-1].distanceToPoint(ptv[0])
if dist < FreeCAD.Base.Precision.confusion():
Path.Log.debug(
"Removing bad carve point: {} from polygon origin".format(dist)
"Removing bad carve point: {} from polygon origin".format(
dist
)
)
del ptv[-1]
ptv.append(ptv[0])
@@ -592,8 +597,6 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
# but using some routing logic we may avoid raising CNC toolbit and using G0
# and instead traverse back already carved edges at full speed
edge_list = backtrack_edges + wire
e = edge_list[0]
@@ -607,14 +610,24 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
path.append(Path.Command("G0", {"Z": obj.SafeHeight.Value}))
path.append(
Path.Command(
"G0", {"X": newPosition.x, "Y": newPosition.y, "Z": obj.SafeHeight.Value}
"G0",
{
"X": newPosition.x,
"Y": newPosition.y,
"Z": obj.SafeHeight.Value,
},
)
)
path.append(
Path.Command(
"G1",
{"X": newPosition.x, "Y": newPosition.y, "Z": newPosition.z, "F": vSpeed},
{
"X": newPosition.x,
"Y": newPosition.y,
"Z": newPosition.z,
"F": vSpeed,
},
)
)
else: # skip repositioning
@@ -652,7 +665,9 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
_maximumUsableDepth = _get_maximumUsableDepth(wires, geom)
if _maximumUsableDepth is not None:
maximumUsableDepth = _maximumUsableDepth
Path.Log.debug(f"Maximum usable depth for current face: {maximumUsableDepth}")
Path.Log.debug(
f"Maximum usable depth for current face: {maximumUsableDepth}"
)
# first pass
cutWires(wires, pathlist, obj.OptimizeMovements)
@@ -691,7 +706,9 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
if obj.ToolController.Tool.CuttingEdgeAngle >= 180.0:
Path.Log.info(
translate("CAM_Vcarve", "Engraver cutting edge angle must be < 180 degrees.")
translate(
"CAM_Vcarve", "Engraver cutting edge angle must be < 180 degrees."
)
)
return
@@ -709,9 +726,9 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
if not faces:
for model in self.model:
if model.isDerivedFrom("Sketcher::SketchObject") or model.isDerivedFrom(
"Part::Part2DObject"
):
if model.isDerivedFrom(
"Sketcher::SketchObject"
) or model.isDerivedFrom("Part::Part2DObject"):
faces.extend(model.Shape.Faces)
if faces:
@@ -759,10 +776,14 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
"""Debug function to display calculated voronoi medial wires"""
if not getattr(self, "voronoiDebugMedialCache", None):
Path.Log.error("debugVoronoi: empty debug cache. Recompute VCarve operation first")
Path.Log.error(
"debugVoronoi: empty debug cache. Recompute VCarve operation first"
)
return
vPart = FreeCAD.activeDocument().addObject("App::Part", f"{obj.Name}-VoronoiDebugMedial")
vPart = FreeCAD.activeDocument().addObject(
"App::Part", f"{obj.Name}-VoronoiDebugMedial"
)
wiresToShow = []
@@ -786,10 +807,14 @@ class ObjectVcarve(PathEngraveBase.ObjectOp):
"""Debug function to display calculated voronoi edges"""
if not getattr(self, "voronoiDebugEdgeCache", None):
Path.Log.error("debugVoronoi: empty debug cache. Recompute VCarve operation first")
Path.Log.error(
"debugVoronoi: empty debug cache. Recompute VCarve operation first"
)
return
vPart = FreeCAD.activeDocument().addObject("App::Part", f"{obj.Name}-VoronoiDebugEdge")
vPart = FreeCAD.activeDocument().addObject(
"App::Part", f"{obj.Name}-VoronoiDebugEdge"
)
edgesToShow = []