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
216 changes: 141 additions & 75 deletions src/draftwright/model/compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
_authored_linear_stations,
_authored_location_matches,
_authored_same_span,
_authored_step_baseline,
_decorated,
_extent_can_convey,
_is_zero_step_position,
Expand Down Expand Up @@ -1179,6 +1180,35 @@ def _step_chain_covers_extent(
)


def _no_envelope_height_omission(model: PartModel, bb: Any) -> Omission | None:
"""Decide whether the bbox fallback is withheld without an envelope feature.

The author cannot name the fallback in a complete authored set (#876). Keep
``feature=None`` in that ledger row for the mixed-identity audit (#1230).
For automatic PMI, a proved end-to-end source on the sole turned profile
owns the same height; drawing the fallback would repeat its tolerance.
"""
value = float(bb.size.Z)
if model.authored_dimensions is not None:
return Omission(None, "height.length", value, _AUTHORED_OMISSION)
if model.pmi_annotations_enabled:
baseline = _authored_step_baseline(model)
if (
baseline is not None
and baseline[0] == 2
and abs(baseline[1] - float(bb.min.Z)) <= 0.01
):
source_id = baseline[2].get(round(float(bb.max.Z), 6))
if source_id is not None:
return Omission(
None,
"height.length",
value,
f"authored PMI dimension {source_id} covers this extent",
)
return None


def _compile_overall_height(
model: PartModel, marked, *, planned, include_overall: bool, step_chain_approved: bool
) -> tuple[ApprovedLadder | None, ApprovedContingency | None, list[Omission]]:
Expand Down Expand Up @@ -1220,38 +1250,8 @@ def _compile_overall_height(
mark[1] == _AUTHORED_OMISSION or mark[1].startswith("authored PMI dimension ")
):
return None, None, [Omission(env, "height.length", mark[0], mark[1])]
elif model.authored_dimensions is not None:
# No `EnvelopeFeature`, so the height falls back to the bounding box and no
# parameter anywhere names it. An AUTHORED set is the drawing's complete
# dimensioning, so it must not acquire a measurement its author had no way to ask
# for: declaring `.envelope()` is how the overall height becomes nameable (#876).
# This is the one place the fallback lives, so refusing it is one branch rather
# than a rule every renderer has to remember.
# `feature=None` here, while the APPROVED rung below now carries a minted envelope
# identity (#1230). The two halves of a documented-joinable pair therefore disagree for
# this one measurement: `Drawing.measurement_keys` says it is "deliberately the SAME row
# shape … so a drawn measurement and a suppressed one are directly comparable", and
# `audit._correspondence` keys on the feature string — so it attributes a lost `dim_od`
# and `ldr_z0` but not a lost `dim_height` (#1233).
#
# Not closed here, and the reason is measured rather than chosen. Passing the same
# identity to both omissions is three lines and does work — but `height.length` is the
# ONLY producer of `feature=None` rows in the public ledger (zero featureless rows across
# the whole detected corpus; on `test_real_mixed_featureless_and_feature_suppressions_
# have_a_total_order`'s own fixture it is the single one). Giving it a feature deletes
# the last real source of the mixed `None`/`str` case that #1077's guard exists to sort,
# leaving that test asserting something no build can produce. Whether `feature=None`
# should stay representable in the public ledger is a question about the ledger's shape,
# not about this rung. Tracked on #1230.
return (
None,
None,
[
Omission(
None, "height.length", float(bb.size.Z), "not in the authored dimension set"
)
],
)
elif (omission := _no_envelope_height_omission(model, bb)) is not None:
return None, None, [omission]
value = float(env.height) if env is not None else float(bb.size.Z)
# A model with no `EnvelopeFeature` still gets an identity for its overall height, from a
# bounding-box envelope minted here. Without it `_dim_id` returns None, the rung carries no
Expand Down Expand Up @@ -1655,6 +1655,113 @@ def _authored_bore_axis_location_matches(
)


def _authored_pattern_centre_bore_axis_matches(
source: AuthoredDimension, location: ApprovedDimension, model: PartModel
) -> bool:
"""A bore-axis witness may also state a coaxial pattern centre's location."""
identity = location.id
if identity is None:
return False
feature = identity.feature
if (
not isinstance(feature, PatternFeature)
or feature.frame.axis != "z"
or location.discriminator not in ("x", "y")
or not identity.parameter.endswith(f".centre.{location.discriminator}")
or location.span is None
):
return False
centre = location.span[1]
bores = [
candidate
for candidate in model.features
if isinstance(candidate, HoleFeature)
and candidate.frame.axis == "z"
and all(abs(candidate.frame.origin[i] - centre[i]) <= 0.01 for i in range(3))
]
return len(bores) == 1 and _authored_bore_axis_location_matches(
source, replace(location, id=DimensionId(bores[0], identity.parameter))
)


def _source_owned_location_coverage(
model: PartModel, locations: list[ApprovedDimension]
) -> tuple[list[ApprovedDimension], list[Omission]]:
"""Remove only proven location duplicates of renderable source PMI."""
if model.authored_dimensions is not None or not model.pmi_annotations_enabled:
return locations, []
sources = tuple(
feature
for feature in model.features
if isinstance(feature, AuthoredDimension)
and id(feature) not in model.hidden_authored_dimension_ids
)
unique_cover: dict[int, list[AuthoredDimension]] = {}
for source in sources:
stations = _authored_linear_stations(source)
if stations is None:
continue
matching = [
location
for location in locations
if location.discriminator == "xyz"[stations[0]]
and location.span is not None
and (
_authored_same_span(source, location.span, location.value)
or _authored_location_matches(
source, location.span, location.discriminator, location.value
)
or _authored_bore_axis_location_matches(source, location)
or _authored_pattern_centre_bore_axis_matches(source, location, model)
)
]
if len(matching) == 1:
unique_cover.setdefault(id(matching[0]), []).append(source)
elif len(matching) == 2:
# A centre bore and coaxial pattern centre are two owners of one
# physical station, not an ambiguous choice between targets.
pattern = next(
(
item
for item in matching
if item.id is not None and isinstance(item.id.feature, PatternFeature)
),
None,
)
bore = next(
(
item
for item in matching
if item.id is not None and isinstance(item.id.feature, HoleFeature)
),
None,
)
if (
pattern is not None
and bore is not None
and _authored_pattern_centre_bore_axis_matches(source, pattern, model)
and _authored_bore_axis_location_matches(source, bore)
):
for location in matching:
unique_cover.setdefault(id(location), []).append(source)
uncovered = []
omissions = []
for location in locations:
covering = unique_cover.get(id(location), [])
if len(covering) != 1 or location.id is None:
uncovered.append(location)
else:
omissions.append(
Omission(
location.id.feature,
location.id.parameter,
location.value,
f"authored PMI dimension {covering[0].source_id} covers this location",
)
)
return uncovered, omissions


def _compile_off_axis_hole_locations(
model: PartModel,
) -> tuple[list[ApprovedDimension], list[Omission]]:
Expand Down Expand Up @@ -2112,49 +2219,8 @@ def compile_dimensions(
location_omissions.extend(off_axis_omissions)
# A source location may cover one directional component without covering its
# orthogonal sibling. Only exact witness correspondence can remove that component.
sources = tuple(
f
for f in model.features
if isinstance(f, AuthoredDimension)
and model.pmi_annotations_enabled
and id(f) not in model.hidden_authored_dimension_ids
)
if sources and model.authored_dimensions is None:
unique_cover: dict[int, list[AuthoredDimension]] = {}
for source in sources:
stations = _authored_linear_stations(source)
if stations is None:
continue
matching = [
location
for location in locations
if location.discriminator == "xyz"[stations[0]]
and location.span is not None
and (
_authored_same_span(source, location.span, location.value)
or _authored_location_matches(
source, location.span, location.discriminator, location.value
)
or _authored_bore_axis_location_matches(source, location)
)
]
if len(matching) == 1:
unique_cover.setdefault(id(matching[0]), []).append(source)
uncovered = []
for location in locations:
covering = unique_cover.get(id(location), [])
if len(covering) != 1 or location.id is None:
uncovered.append(location)
else:
location_omissions.append(
Omission(
location.id.feature,
location.id.parameter,
location.value,
f"authored PMI dimension {covering[0].source_id} covers this location",
)
)
locations = uncovered
locations, source_location_omissions = _source_owned_location_coverage(model, locations)
location_omissions.extend(source_location_omissions)
result = RenderableDimensionPlan(
groups=tuple(groups_out),
ladders=tuple(ladders),
Expand Down
75 changes: 75 additions & 0 deletions tests/test_authored_pmi_precedence.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,34 @@ def test_authored_z_height_does_not_reappear_as_an_overall_ladder():
)


def test_authored_turned_overall_covers_bbox_height_without_an_envelope():
bbox = (Pos(0, 0, 26) * Box(24, 24, 52)).bounding_box()
steps = [
StepFeature(Frame((0, 0, (lo + hi) / 2), "z"), hi - lo, diameter, ((0, 0, lo), (0, 0, hi)))
for lo, hi, diameter in ((0, 12, 24), (12, 34, 15), (34, 52, 10))
]
source = AuthoredDimension(
Frame((0, 0, 26), "z"),
"linear",
52,
"52 +0.1/0",
"Z",
ref_pts=((0, 0, 0), (0, 0, 52)),
source_id="dimension:overall",
)
model = PartModel(bbox, "z", [*steps, source])
plan = compile_dimensions(model)
assert plan.ladder("overall_height") is None
assert any(
omission.parameter_id == "height.length" and "dimension:overall" in omission.reason
for omission in plan.diagnostics
)
unrelated = compile_dimensions(
replace(model, features=[*steps, replace(source, ref_pts=((20, 0, 0), (20, 0, 52)))])
)
assert not any("dimension:overall" in omission.reason for omission in unrelated.diagnostics)


def test_authored_hole_x_location_covers_only_the_x_component():
bbox = (Pos(20, 15, 2.5) * Box(40, 30, 5)).bounding_box()
hole = HoleFeature(Frame((10, 5, 2), "z"), 4, depth=None, through=True)
Expand Down Expand Up @@ -781,6 +809,53 @@ def test_authored_bore_axis_station_covers_only_its_physical_hole_location():
)


def test_authored_central_bore_axis_covers_coaxial_pattern_centre_location():
bbox = (Pos(0, 0, 6) * Box(120, 80, 12)).bounding_box()
member = HoleFeature(Frame((0, 0, 12), "z"), 6.6, depth=12, through=True)
pattern = PatternFeature(
Frame((0, 0, 12), "z"),
"grid",
4,
member,
members=((-48, -30, 12), (48, -30, 12), (48, 30, 12), (-48, 30, 12)),
grid=(60, 96),
rows=2,
cols=2,
)
bore = HoleFeature(Frame((0, 0, 12), "z"), 22, depth=12, through=True)
source = AuthoredDimension(
Frame((-30, 0, 4), "x"),
"linear",
60,
"60",
"X",
ref_pts=((-60, 0, 4), (0, 0, 4)),
source_id="dimension:bore-centre",
basic=True,
)
model = PartModel(
bbox,
"z",
[pattern, bore, source],
datums=[Datum("datum_xy", "point", (-60, -40, 0))],
)
plan = compile_dimensions(model)
assert not [
location
for location in plan.locations
if location.id.feature is pattern and location.discriminator == "x"
]
assert any(
omission.feature is pattern and "dimension:bore-centre" in omission.reason
for omission in plan.diagnostics
)
without_bore = compile_dimensions(replace(model, features=[pattern, source]))
assert any(
location.id.feature is pattern and location.discriminator == "x"
for location in without_bore.locations
)


def test_pattern_member_provenance_is_per_measurement():
hole = HoleFeature(Frame((0, 0, 0), "z"), 4, depth=None, through=True)
pattern = PatternFeature(
Expand Down
Loading