Detect circular references in sketches, and add corresponding tests (#11716)

* Possible fix for 10482 circular reference regression with tests

* Remove redundant test

* Cleanup pre PR

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
bgbsww
2024-01-06 19:13:44 -05:00
committed by GitHub
parent d9017bcca3
commit 494cb9388c
3 changed files with 40 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
from FreeCAD import Vector
from FreeCAD import Vector, newDocument, closeDocument
import Part
import unittest
@@ -23,3 +23,27 @@ class RegressionTests(unittest.TestCase):
def test_OptimalBox(self):
box = Part.makeBox(1, 1, 1)
self.assertTrue(box.optimalBoundingBox(True, False).isValid())
def test_CircularReference(self):
# put me in def setup(self): ?
self.Doc = newDocument("TestSketchExpr")
cube = self.Doc.addObject("Part::Box","Cube")
cube.setExpression('Length', 'Width + 10mm')
with self.assertRaises(RuntimeError) as context:
cube.setExpression('Width', 'Length + 10mm')
assert "Width reference creates a cyclic dependency." in str(context.exception)
cube.setExpression('.Placement.Base.x', '.Placement.Base.y + 10mm')
with self.assertRaises(RuntimeError) as context:
cube.setExpression('.Placement.Base.y', '.Placement.Base.x + 10mm')
assert ".Placement.Base.y reference creates a cyclic dependency." in str(context.exception)
cube.recompute()
v1 = cube.Placement.Base
cube.recompute()
assert cube.Placement.Base.isEqual(v1,1e-6)
# Put me in def tearDown(self): ?
closeDocument(self.Doc.Name)

View File

@@ -57,12 +57,26 @@ class TestSketchExpression(unittest.TestCase):
sketch.addConstraint(conList)
del geoList, conList
# Check that circular references get detected
length = sketch.addConstraint(Sketcher.Constraint("Distance", 0, 6.0))
height = sketch.addConstraint(Sketcher.Constraint("Distance", 3, 5.0))
sketch.renameConstraint(length, "Length")
sketch.renameConstraint(height, "Height")
sketch.setExpression("Constraints[{}]".format(height), ".Constraints.Length")
with self.assertRaises(RuntimeError) as context:
sketch.setExpression("Constraints.Length", ".Constraints.Length * 2")
assert ".Constraints.Length reference creates a cyclic dependency." in str(
context.exception
)
with self.assertRaises(RuntimeError) as context:
sketch.setExpression("Constraints.Length", ".Constraints.Height * 2")
assert ".Constraints.Length reference creates a cyclic dependency." in str(
context.exception
)
def tearDown(self):
# comment out to omit closing document for debugging
FreeCAD.closeDocument(self.Doc.Name)