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
53 changes: 53 additions & 0 deletions src/draftwright/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,59 @@ def get_annotation(self, name):
"""Return the named annotation object, or ``None`` if no such name (#27)."""
return self._registry.named(name)

def declaration_id_of(self, name: str) -> str | None:
"""Return a placed mark's unique Sheet declaration ID, if one owns it.

A mark can be generated from several declarations (or from automatic
planning with none). In those cases ``None`` is safer than guessing an
editable line. An unknown annotation name raises ``KeyError``.
"""
if name not in self._registry:
raise KeyError(name)
model = self.model()
if model is None or not model.declaration_identities:
return None

def ids_for(owner) -> set[str]:
pairs = tuple(zip(model.features, model.declaration_identities, strict=True))
exact = {
identity.declaration_id
for feature, identity in pairs
if identity is not None and feature is owner
}
if exact:
return exact
return {
identity.declaration_id
for feature, identity in pairs
if identity is not None and feature == owner
}

declaration = self._registry.declaration_of(name)
if declaration is not None:
ids = ids_for(declaration)
else:
owners = (
*self._registry.features_of(name),
*(measurement.feature for measurement in self._registry.measurement_of(name)),
)
ids = set().union(*(ids_for(owner) for owner in owners if owner is not None))
return next(iter(ids)) if len(ids) == 1 else None

def label_box(self, name: str) -> tuple[float, float, float, float] | None:
"""Return the placed text/value box for picking, not the whole ink span.

Coordinates are page millimetres, like ``view_bounds``. Marks without a
distinct label return ``None``; unknown names raise ``KeyError``.
"""
annotation = self.get_annotation(name)
if annotation is None:
raise KeyError(name)
box = getattr(annotation, "label_bbox", None)
if box is None:
return None
return float(box[0]), float(box[1]), float(box[2]), float(box[3])

def preview_annotation(self, name: str, path: str | os.PathLike) -> str:
return preview_export_annotation(
name,
Expand Down
66 changes: 66 additions & 0 deletions tests/test_declaration_identity_issue_1710.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Build-scoped declaration identity follows intent, never a list position (#1710)."""

from types import SimpleNamespace

import pytest
from build123d import Box, Cylinder

from draftwright import Sheet
from draftwright.drawing import Drawing
from draftwright.model import DeclarationIdentity
from draftwright.registry import AnnotationRegistry


def _sheet() -> Sheet:
Expand Down Expand Up @@ -136,6 +140,68 @@ def test_identity_reaches_the_built_drawing_model() -> None:
drawing = sheet.build()

assert drawing.model().declaration_identities == (DeclarationIdentity("declaration:1"),)
marks = drawing.annotations_of(drawing.model().features[0])
assert marks
assert all(drawing.declaration_id_of(name) == "declaration:1" for name in marks)
assert any(drawing.label_box(name) is not None for name in marks)
assert drawing.declaration_id_of("title_block") is None
with pytest.raises(KeyError):
drawing.declaration_id_of("no-such-annotation")


def test_public_pick_identity_handles_explicit_measurement_and_ambiguous_owners() -> None:
first, second, explicit = object(), object(), object()
note = SimpleNamespace(origin=first)
model = SimpleNamespace(
features=(first, second, explicit, note),
declaration_identities=tuple(
DeclarationIdentity(f"declaration:{index}") for index in (1, 2, 3, 4)
),
)
registry = AnnotationRegistry()
label = SimpleNamespace(label_bbox=(1, 2, 3, 4))
registry.add(label, "feature", "front", feature=first)
registry.add(
label,
"measurement",
"front",
measurement=SimpleNamespace(feature=second, parameter="step.diameter"),
)
registry.add(label, "explicit", "front", declaration=explicit)
registry.add(label, "note", "front", declaration=note)
registry.add(
label,
"shared",
"front",
feature=first,
measurement=SimpleNamespace(feature=second, parameter="step.diameter"),
)
drawing = SimpleNamespace(
_registry=registry, model=lambda: model, get_annotation=registry.named
)

assert Drawing.declaration_id_of(drawing, "feature") == "declaration:1"
assert Drawing.declaration_id_of(drawing, "measurement") == "declaration:2"
assert Drawing.declaration_id_of(drawing, "explicit") == "declaration:3"
assert Drawing.declaration_id_of(drawing, "note") == "declaration:4"
assert Drawing.declaration_id_of(drawing, "shared") is None
assert Drawing.label_box(drawing, "measurement") == (1.0, 2.0, 3.0, 4.0)
registry.add(SimpleNamespace(label_bbox=None), "furniture", "front")
assert Drawing.declaration_id_of(drawing, "furniture") is None
assert Drawing.label_box(drawing, "furniture") is None
with pytest.raises(KeyError):
Drawing.label_box(drawing, "missing")

equal_a, equal_b, copy = (SimpleNamespace(value=1) for _ in range(3))
equal_model = SimpleNamespace(
features=(equal_a, equal_b),
declaration_identities=(DeclarationIdentity("equal:a"), DeclarationIdentity("equal:b")),
)
registry.add(label, "equal-ambiguous", "front", feature=copy)
drawing.model = lambda: equal_model
assert Drawing.declaration_id_of(drawing, "equal-ambiguous") is None
drawing.model = lambda: None
assert Drawing.declaration_id_of(drawing, "feature") is None


def test_synthetic_rotational_feature_preserves_declared_identity_alignment() -> None:
Expand Down
Loading