From dfa6dfb95b511aa489faaeab38aede6ca153f453 Mon Sep 17 00:00:00 2001 From: Paul Fremantle Date: Mon, 5 Oct 2026 16:21:52 +0000 Subject: [PATCH 1/3] Reject strip annotations whose full ink leaves the sheet --- src/draftwright/annotations/_common.py | 10 +++++++ tests/test_strip_layout.py | 40 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/draftwright/annotations/_common.py b/src/draftwright/annotations/_common.py index 82701fd2..043452ae 100644 --- a/src/draftwright/annotations/_common.py +++ b/src/draftwright/annotations/_common.py @@ -1911,6 +1911,7 @@ def _predicted_box(name, pos): # view as a last resort must still not print over the block. _tb = pending_title_block_box(dwg) keep_out = (_tb,) if _tb is not None else () + page = _drawing_bounds(dwg) if hasattr(dwg, "page_w") and hasattr(dwg, "page_h") else None segs = _reserved_strip_segments(lo, hi, occupied, idx, pad) # Fill innermost-first (nearest the view), matching the old cursor's stack order. segs.sort(key=lambda s: abs((s[0] if inner == lo else s[1]) - inner)) @@ -1929,6 +1930,15 @@ def _real_box_conflict(name, real, annotation=None): """Return the hard-obstacle reason for a built survivor, if any.""" if real is None: return None + # A bounded strip coordinate does not bound an oblique dimension's remote + # witness extensions. Validate the complete ink even on the force path. + if page is not None and ( + real[0] < page[0] - 1e-6 + or real[1] < page[1] - 1e-6 + or real[2] > page[2] + 1e-6 + or real[3] > page[3] + 1e-6 + ): + return "real_box_page_bounds" if not force and _box_hits(real, blockers): return "real_box_corridor_blocked" fb = (forbid or {}).get(name) diff --git a/tests/test_strip_layout.py b/tests/test_strip_layout.py index 38b3c54a..c6367491 100644 --- a/tests/test_strip_layout.py +++ b/tests/test_strip_layout.py @@ -752,6 +752,46 @@ def place(s, obj, name, view=None, feature=None, measurement=None): assert len(left) == 1, "the unplaceable candidate must be returned, not dropped silently" +def test_oblique_pmi_cannot_escape_sheet_through_a_nearly_parallel_strip_issue_2189(): + from build123d_drafting.helpers import Draft + + from draftwright._core import Strip + from draftwright.annotations._common import _geom_box, place_strip_candidates + from draftwright.annotations._pmi_dimensions import _oblique_pmi_dim_spec + + drawing = _StripProbeDrawing(draft=Draft(font_size=3.0)) + strip = Strip(anchor=20.0, outer_limit=0.0, direction=-1.0, gap=2.0) + spec = _oblique_pmi_dim_spec( + (30.0, 50.0, 0), + (60.0, 49.0, 0), + strip, + "60", + "pmi_oblique", + "front", + "left", + drawing.draft, + ) + assert spec is not None + # The selected line is inside the strip, but its near-horizontal witness projects + # hundreds of millimetres down the page when extended toward the left strip. + assert _geom_box(spec["build"](16.0))[1] < 10.0 + + left = place_strip_candidates( + drawing, + strip, + "front", + "x", + [(spec["name"], spec["build"])], + tier=5.0, + ctx=drawing, + force=True, + footprints={spec["name"]: lambda pos: (pos - 2.0, 45.0, pos + 2.0, 55.0)}, + ) + + assert [name for name, _build in left] == [spec["name"]] + assert drawing.added == [] + + def test_dimension_witness_continues_leader_shaft_but_not_crossing_shelf(): from build123d_drafting.helpers import Dimension, Draft, Leader From 5a10ee911183d7592d695f5893543188bc3a6787 Mon Sep 17 00:00:00 2001 From: Paul Fremantle Date: Mon, 5 Oct 2026 16:36:37 +0000 Subject: [PATCH 2/3] Constrain strip ink only for drawings with explicit bounds --- src/draftwright/annotations/_common.py | 49 ++++++------------- .../annotations/_placement_geometry.py | 25 ++++++++++ tests/test_strip_layout.py | 1 + 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/draftwright/annotations/_common.py b/src/draftwright/annotations/_common.py index 043452ae..74ea7a5c 100644 --- a/src/draftwright/annotations/_common.py +++ b/src/draftwright/annotations/_common.py @@ -51,6 +51,10 @@ from draftwright.annotations._placement_geometry import ( _box_hits as _box_hits, ) +from draftwright.annotations._placement_geometry import ( + _box_outside_explicit_drawable_bounds, + _first_box_conflict, +) from draftwright.annotations._placement_geometry import ( _geom_box as _geom_box, ) @@ -1889,29 +1893,13 @@ def _predicted_box(name, pos): if r is not None and r[perp] < band_hi and r[perp + 2] > band_lo ] blockers = () if force else corridor_blockers(dwg, view, exact_leaders=bool(exact_ink)) - # The title block is drawn near the end of `_PASS_SEQUENCE`, so it is - # never in `occupied` above. `pending_title_block_box` knows its fixed box - # from the sheet geometry, so a strip placer can honour it regardless. - # - # A hard 2-D box checked against each candidate's REAL footprint in - # `_real_box_conflict` below, the way `forbid` guards this same block for GD&T frames - # — not an entry in the carve. The carve inflates by `pad`, the separation two - # dimension LINES need from each other, and projects onto the stacking axis, claiming - # every position at that coordinate; against a block this large both over-claim - # badly, refusing dims that clear it by a millimetre. A 2-D test against the - # candidate's own footprint refuses exactly the dims that land on it. - # - # `forbid` also pre-checks the PREDICTED box inside the segment solve, so a rejection - # frees its slot for a refill in the same pass. Not mirrored here: on this corpus - # either check alone catches every case (measured by deleting each in turn), and an - # untested second branch is worth less than the packing it might win. The real-box - # check is the one kept because it cannot be defeated by a prediction miss. - # - # Honoured under `force` too, for the reason `forbid` is: a dim kept on its natural - # view as a last resort must still not print over the block. + # The title block is drawn after this pass, so it is absent from occupancy. + # Check its fixed 2-D box against REAL candidate ink, not the inflated strip + # carve: projection would reject dimensions that clear it. A predicted-box + # precheck would permit refill, but the real-box check alone is sufficient and + # cannot miss underpredicted ink. Even forced candidates must respect the block. _tb = pending_title_block_box(dwg) keep_out = (_tb,) if _tb is not None else () - page = _drawing_bounds(dwg) if hasattr(dwg, "page_w") and hasattr(dwg, "page_h") else None segs = _reserved_strip_segments(lo, hi, occupied, idx, pad) # Fill innermost-first (nearest the view), matching the old cursor's stack order. segs.sort(key=lambda s: abs((s[0] if inner == lo else s[1]) - inner)) @@ -1927,27 +1915,20 @@ def _predicted_box(name, pos): todo = list(cands) def _real_box_conflict(name, real, annotation=None): - """Return the hard-obstacle reason for a built survivor, if any.""" if real is None: return None - # A bounded strip coordinate does not bound an oblique dimension's remote - # witness extensions. Validate the complete ink even on the force path. - if page is not None and ( - real[0] < page[0] - 1e-6 - or real[1] < page[1] - 1e-6 - or real[2] > page[2] + 1e-6 - or real[3] > page[3] + 1e-6 - ): + # A bounded strip does not bound oblique witness ink; check even under force. + if _box_outside_explicit_drawable_bounds(dwg, real): return "real_box_page_bounds" if not force and _box_hits(real, blockers): return "real_box_corridor_blocked" fb = (forbid or {}).get(name) if fb is not None and _box_hits(real, (fb,)): return "real_box_forbid" - if _box_hits(real, out_of_band): - return "real_box_out_of_band" - if _box_hits(real, keep_out): - return "real_box_title_block" + if reason := _first_box_conflict( + real, (("real_box_out_of_band", out_of_band), ("real_box_title_block", keep_out)) + ): + return reason if ( annotation is not None and not force diff --git a/src/draftwright/annotations/_placement_geometry.py b/src/draftwright/annotations/_placement_geometry.py index 11d3f7e3..8911b6ce 100644 --- a/src/draftwright/annotations/_placement_geometry.py +++ b/src/draftwright/annotations/_placement_geometry.py @@ -39,6 +39,31 @@ def _geom_box(o, cache=None): return None +def _box_outside_explicit_drawable_bounds(dwg, box, *, tolerance=1e-6): + """Reject complete ink outside a real drawing's declared content rectangle. + + Lightweight placement probes without explicit page geometry intentionally have + no page constraint; guessing margins from their dimensions would invent one. + """ + page = getattr(dwg, "drawable_bounds", None) + return page is not None and any( + ( + box[0] < page[0] - tolerance, + box[1] < page[1] - tolerance, + box[2] > page[2] + tolerance, + box[3] > page[3] + tolerance, + ) + ) + + +def _first_box_conflict(box, named_obstacles): + """Return the first hard-obstacle reason, preserving caller-specified order.""" + for reason, obstacles in named_obstacles: + if _box_hits(box, obstacles): + return reason + return None + + def leader_callout_geometry(tip, elbow, draft, *, text_side="auto", callout_box=None): """Exact cheap label box and line segments for a callout-bearing ``Leader``. diff --git a/tests/test_strip_layout.py b/tests/test_strip_layout.py index c6367491..8bd043b0 100644 --- a/tests/test_strip_layout.py +++ b/tests/test_strip_layout.py @@ -760,6 +760,7 @@ def test_oblique_pmi_cannot_escape_sheet_through_a_nearly_parallel_strip_issue_2 from draftwright.annotations._pmi_dimensions import _oblique_pmi_dim_spec drawing = _StripProbeDrawing(draft=Draft(font_size=3.0)) + drawing.drawable_bounds = (10.0, 10.0, 90.0, 90.0) strip = Strip(anchor=20.0, outer_limit=0.0, direction=-1.0, gap=2.0) spec = _oblique_pmi_dim_spec( (30.0, 50.0, 0), From 7609f67cf02d955de99549c19df81d53c2f54b64 Mon Sep 17 00:00:00 2001 From: Paul Fremantle Date: Mon, 5 Oct 2026 17:56:20 +0000 Subject: [PATCH 3/3] Test PMI drop and sibling recovery after page rejection --- tests/test_strip_layout.py | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tests/test_strip_layout.py b/tests/test_strip_layout.py index 8bd043b0..c40c6f0d 100644 --- a/tests/test_strip_layout.py +++ b/tests/test_strip_layout.py @@ -793,6 +793,106 @@ def test_oblique_pmi_cannot_escape_sheet_through_a_nearly_parallel_strip_issue_2 assert drawing.added == [] +def test_rejected_oblique_pmi_reports_drop_without_occupying_sibling_slots(tmp_path): + from types import SimpleNamespace + + from build123d_drafting.helpers import Draft + + from draftwright._core import Strip, _dim + from draftwright.annotations._common import ( + CorridorCandidate, + PlacementContext, + drain_corridors, + register_corridor, + ) + from draftwright.annotations._pmi_dimensions import _oblique_pmi_dim_spec, _record_pmi_drop + from draftwright.annotations.solve_trace import SolveTrace + from draftwright.registry import AnnotationRegistry + + draft = Draft(font_size=3.0) + registry = AnnotationRegistry() + ctx = PlacementContext(registry=registry, items=[], trace=SolveTrace(tmp_path / "trace.json")) + strip = Strip(anchor=40.0, outer_limit=0.0, direction=-1.0, gap=2.0) + + class Drawing: + page_w = page_h = 100.0 + drawable_bounds = (0.0, 0.0, 100.0, 100.0) + + def __init__(self): + self.draft = draft + + def annotations(self): + return registry.names() + + def iter_annotations(self): + return registry.iter_named() + + def view_of(self, name): + return registry.view_of(name) + + drawing = Drawing() + escaped = _oblique_pmi_dim_spec( + (60.0, 50.0, 0), (90.0, 49.0, 0), strip, "60", "pmi_oblique", "front", "left", draft + ) + assert escaped is not None + assert escaped["build"](16.0).bounding_box().min.Y < 0.0 + rec = SimpleNamespace(pmi_kind="linear", dominant_axis="X", source_id="pmi-60") + dropped = [] + + def drop_oblique(name): + dropped.append(name) + _record_pmi_drop(ctx, "X", "60", rec) + + def queue(name, build, order, *, on_drop, footprint): + register_corridor( + ctx, + ("front", "left"), + strip, + "front", + "x", + 5.0, + CorridorCandidate( + name=name, + build=build, + order=(order, name), + on_place=lambda _name: None, + on_drop=on_drop, + force=True, + obligation_class="required", + footprint=footprint, + ), + ) + + queue( + escaped["name"], + escaped["build"], + 0, + on_drop=drop_oblique, + footprint=lambda pos: (pos - 2.0, 45.0, pos + 2.0, 55.0), + ) + for order, (name, y0, y1) in enumerate((("pmi_35", 20.0, 30.0), ("pmi_85", 35.0, 45.0)), 1): + queue( + name, + lambda pos, y0=y0, y1=y1, label=name: _dim( + (70.0, y0, 0), (70.0, y1, 0), "left", 70.0 - pos, draft, label=label + ), + order, + on_drop=lambda name: dropped.append(name), + footprint=lambda pos, y0=y0, y1=y1: (pos - 2.0, y0, pos + 2.0, y1), + ) + + drain_corridors(ctx, drawing) + assert escaped["name"] in dropped + assert registry.names() == {"pmi_35", "pmi_85"}, dropped + assert any(issue.code == "pmi_dropped" for issue in registry.issues) + assert any( + rejection == {"name": escaped["name"], "reason": "real_box_page_bounds"} + for solve in ctx.trace.solves + for placement_pass in solve["passes"] + for rejection in placement_pass["rejected"] + ) + + def test_dimension_witness_continues_leader_shaft_but_not_crossing_shelf(): from build123d_drafting.helpers import Dimension, Draft, Leader