diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bf32c4..58c8039 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,15 @@ All notable changes to tolstack. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow [Semantic Versioning](https://semver.org/). Config files carry their own `version` (currently 1). +## [0.8.1] - 2026-11-23 + +### Fixed +- **A contact could get the outline of a face of an earlier model.** The true outline of a face was kept in a table + keyed by the memory id of its shape; Python gives the id of a freed object to a new one, so in a session that + loads more than one model (the web app, the tests) a face could be measured against a stale outline and report a + wrong overlap area. Seen as a test that failed now and then on any system (896 mm2 instead of 1200). The outline is + now kept on the face itself. + ## [0.8.0] - 2026-11-13 The stack written by hand gets the web app, and the locating fits it needs can be found in the CAD. diff --git a/pyproject.toml b/pyproject.toml index 65bba49..760d64e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "tolstack" -version = "0.8.0" +version = "0.8.1" description = "1-D tolerance stack-up (gap / step) from a STEP assembly and 2D drawings, or from rows written by hand, exported to Excel" readme = "README.md" license = {text = "MIT"} diff --git a/tests/test_tolstack.py b/tests/test_tolstack.py index 532a92d..631f51d 100644 --- a/tests/test_tolstack.py +++ b/tests/test_tolstack.py @@ -300,3 +300,35 @@ def test_stepped_seat_gives_one_option_per_contact_level(tmp_path): for option in options: chain = build_chain(start, end, model.parts, model.contacts, steps=option.steps) assert sum(c.direction * c.nominal for c in chain) == pytest.approx(10.0) + + +def test_outline_is_kept_on_the_face_not_by_object_id(tmp_path): + """The outline of a face must come from that face. It was looked up by id(shape), and Python hands the id + of a freed object to a new one: a later model could get an earlier model's outline (seen as 896 mm2 + instead of 1200 in the test above, now and then, on any system).""" + import sys + + from OCP.BRepAlgoAPI import BRepAlgoAPI_Cut + + sys.path.insert(0, str(Path(__file__).parents[1] / "examples")) + import make_demo + + from tolstack import cad + from tolstack.pipeline import load_model + + assert not hasattr(cad, "_OUTLINES") # no table shared between models + + def ring_on_lid(holed: bool, name: str) -> float: + plate = make_demo.box(0, 0, 0, 40, 40, 5) + if holed: + plate = BRepAlgoAPI_Cut(plate, make_demo.box(10, 10, -1, 30, 30, 6)).Shape() + step = make_demo.write_step(tmp_path / name, [("RING", plate), ("LID", make_demo.box(0, 0, 5, 40, 40, 7))]) + model = load_model(str(step), "z") + contact = [c for c in model.contacts if model.parts[c.lower].name == "RING"][0] + assert contact.lower_face.outline_built or contact.upper_face.outline_built + return contact.overlap + + # the same part and face numbers again and again, with and without the hole + for turn in range(8): + assert ring_on_lid(False, f"plate{turn}.step") == pytest.approx(40 * 40, rel=1e-3) + assert ring_on_lid(True, f"ring{turn}.step") == pytest.approx(40 * 40 - 20 * 20, rel=1e-3) diff --git a/tolstack/__init__.py b/tolstack/__init__.py index edf0206..613bedb 100644 --- a/tolstack/__init__.py +++ b/tolstack/__init__.py @@ -2,4 +2,4 @@ from .stack import Contributor, Result, analyze __all__ = ["Contributor", "Result", "analyze"] -__version__ = "0.8.0" +__version__ = "0.8.1" diff --git a/tolstack/cad.py b/tolstack/cad.py index 7a7e17d..2b6c065 100644 --- a/tolstack/cad.py +++ b/tolstack/cad.py @@ -58,6 +58,10 @@ class PlanarFace: area: float shape: TopoDS_Face | None = field(default=None, repr=False, compare=False) axis: str = "z" + # The true outline, kept on the face once face_outline has built it (None can be the answer, so a flag says + # whether it was built). + outline: object = field(default=None, repr=False, compare=False) + outline_built: bool = field(default=False, repr=False, compare=False) @property def kind(self) -> str: @@ -213,15 +217,15 @@ def planar_faces(part: Part, axis: str, min_area: float = 0.01) -> list[PlanarFa return result -_OUTLINES: dict = {} - - def face_outline(face: PlanarFace, deflection: float = 0.02): """True 2-D outline of a planar face (holes included) as a shapely geometry, projected on the - plane normal to the stack-up axis. Built from the face triangulation; None if meshing fails.""" - key = (id(face.shape), face.part, face.face_id) - if key in _OUTLINES: - return _OUTLINES[key] + plane normal to the stack-up axis. Built from the face triangulation; None if meshing fails. + + The outline is kept on the face itself. It used to sit in a table keyed by id(face.shape): Python gives + the id of a freed object to a new one, so a face of a later model could get the outline of a face of an + earlier model with the same part and face numbers, and with it a wrong contact area.""" + if face.outline_built: + return face.outline outline = None if face.shape is not None: from shapely.geometry import Polygon @@ -248,7 +252,7 @@ def face_outline(face: PlanarFace, deflection: float = 0.02): triangles.append(tri) if triangles: outline = unary_union(triangles).buffer(0) - _OUTLINES[key] = outline + face.outline, face.outline_built = outline, True return outline