Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions src/draftwright/annotations/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -1889,26 +1893,11 @@ 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 ()
segs = _reserved_strip_segments(lo, hi, occupied, idx, pad)
Expand All @@ -1926,18 +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 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
Expand Down
25 changes: 25 additions & 0 deletions src/draftwright/annotations/_placement_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down
141 changes: 141 additions & 0 deletions tests/test_strip_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,147 @@ 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))
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),
(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_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

Expand Down
Loading