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
72 changes: 64 additions & 8 deletions src/draftwright/_pmi_part21.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,46 @@ def _name_tokens(value: str) -> frozenset[str]:
return frozenset(words)


_AMBIGUOUS_MANUFACTURING_CALLOUTS = "thread presentation callouts have multiple distinct supports"


_THREAD_DESIGNATION = re.compile(
r"^M(?P<nominal>\d+(?:\.\d+)?)\s*x\s*(?P<pitch>\d+(?:\.\d+)?)-"
r"(?P<fit>[A-Za-z0-9]+)\s+(?P<hand>RH|LH)\b",
re.IGNORECASE,
)


def _prose_thread_designation(text: str) -> tuple[float, float, str, str] | None:
match = _THREAD_DESIGNATION.match(text.strip())
if match is None:
return None
return (
float(match["nominal"]),
float(match["pitch"]),
match["fit"].upper(),
match["hand"].upper(),
)


def _structured_thread_designation(fields) -> tuple[float, float, str, str] | None:
values = dict(fields)
designation = values.get("designation")
fit = values.get("fit class")
hand = values.get("hand")
if not isinstance(designation, str) or not isinstance(fit, str) or not isinstance(hand, str):
return None
match = re.fullmatch(r"M(\d+(?:\.\d+)?)\s*x\s*(\d+(?:\.\d+)?)", designation, re.I)
if match is None or hand.casefold() not in ("rh", "lh", "right", "left"):
return None
return (
float(match[1]),
float(match[2]),
fit.upper(),
"RH" if hand.casefold() in ("rh", "right") else "LH",
)


def _manufacturing_graph(step):
"""Collect the Part21 indexes shared by manufacturing-requirement facts."""
definitions: list[tuple[str, object]] = []
Expand Down Expand Up @@ -569,7 +609,6 @@ def read_manufacturing_requirements(
aspect_items,
) = _manufacturing_graph(step)
facts: list[ManufacturingRequirementFact] = []

for entity_id, definition in definitions:
semantic_name = _text(definition.params[1])
reasons: list[str] = []
Expand Down Expand Up @@ -625,6 +664,29 @@ def read_manufacturing_requirements(
for callout_id, name in callout_names.items()
if semantic_tokens and semantic_tokens <= _name_tokens(name)
)
matched_aspects = tuple(
dict.fromkeys(
aspect_id
for callout_id in callout_ids
for aspect_id in callout_aspects.get(callout_id, ())
)
)
matched_items = tuple(
dict.fromkeys(
item_id
for aspect_id in matched_aspects
for item_id in aspect_items.get(aspect_id, ())
)
)
# A part-owned thread prose property has no relation to any particular
# one of several presented faces. Even one property can match several
# identically named callouts, so do not let it claim their union.
ambiguous_callouts = (
semantic_name.casefold() in {"internal thread", "external thread"}
and len(matched_items) > 1
)
if ambiguous_callouts:
reasons.append(_AMBIGUOUS_MANUFACTURING_CALLOUTS)
missing_callout_associations = tuple(
callout_id for callout_id in callout_ids if not callout_aspects.get(callout_id)
)
Expand All @@ -633,13 +695,7 @@ def read_manufacturing_requirements(
"matched semantic callout(s) have no shape-aspect association: "
+ ", ".join(missing_callout_associations)
)
shape_aspect_ids = tuple(
dict.fromkeys(
aspect_id
for callout_id in callout_ids
for aspect_id in callout_aspects.get(callout_id, ())
)
)
shape_aspect_ids = () if ambiguous_callouts else matched_aspects
missing_aspect_items = tuple(
aspect_id for aspect_id in shape_aspect_ids if not aspect_items.get(aspect_id)
)
Expand Down
36 changes: 34 additions & 2 deletions src/draftwright/pmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@
)
from draftwright._pmi_linear_geometry import _proved_planar_linear
from draftwright._pmi_part21 import (
_AMBIGUOUS_MANUFACTURING_CALLOUTS,
CommonLabelFact,
DatumDefinitionFact,
DatumOccurrenceFact,
DimensionAssociationFact,
DimensionDisplayFact,
GeometricToleranceFact,
MaterialFact,
_prose_thread_designation,
_structured_thread_designation,
dimension_basic_policy,
match_common_label,
match_datum_occurrence,
Expand Down Expand Up @@ -1505,6 +1508,9 @@ def _manufacturing_requirement_projection(
structured_facts = ()

paired: set[str] = set()
ambiguous_prose = [
fact for fact in requirement_facts if _AMBIGUOUS_MANUFACTURING_CALLOUTS in fact.reason
]

def paired_structured(requirement):
kind = "_".join(requirement.semantic_name.casefold().split())
Expand All @@ -1524,6 +1530,26 @@ def paired_structured(requirement):
and support == set(fact.reference_item_ids)
and fact.entity_id not in paired
]
if not candidates and not support and requirement in ambiguous_prose:
# A unique designation can pair prose with structured PMI; its
# exact face is still checked during geometry lowering.
designation = _prose_thread_designation(requirement.text)
if (
designation is not None
and sum(
_prose_thread_designation(fact.text) == designation
for fact in ambiguous_prose
)
== 1
):
candidates = [
fact
for fact in structured_facts
if fact.kind == kind
and len(fact.reference_item_ids) == 1
and _structured_thread_designation(fact.fields) == designation
and fact.entity_id not in paired
]
return candidates[0] if len(candidates) == 1 else None

for requirement in requirement_facts:
Expand All @@ -1538,7 +1564,9 @@ def paired_structured(requirement):
blockers = tuple(
reason
for reason in (
requirement.reason,
requirement.reason.replace(_AMBIGUOUS_MANUFACTURING_CALLOUTS, "").strip("; ")
if structured is not None
else requirement.reason,
structured.reason if structured is not None else "",
)
if reason
Expand All @@ -1553,7 +1581,11 @@ def paired_structured(requirement):
part21_id=requirement.entity_id,
source_category="manufacturing_requirement",
lowering_blockers=blockers,
reference_item_ids=requirement.reference_item_ids,
reference_item_ids=(
structured.reference_item_ids
if structured is not None and not requirement.reference_item_ids
else requirement.reference_item_ids
),
semantic_name=requirement.semantic_name,
shape_aspect_ids=tuple(
dict.fromkeys(
Expand Down
86 changes: 86 additions & 0 deletions tests/test_issue_1298_manufacturing_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,92 @@ def test_typed_internal_thread_and_knurl_render_manufacturing_complete_labels_on
assert not [issue for issue in knurled.lint() if issue.code == "pmi_not_rendered"]


def test_two_distinct_internal_threads_keep_own_topology_and_render_issue_2231():
part = Box(20, 20, 20)
holes = []
raw = []
for y, diameter, nominal, pitch, face_id, prose_id, structured_id in (
(-5.0, 1.6, 2, 0.4, "#101", "#1", "#41"),
(5.0, 3.3, 4, 0.7, "#102", "#2", "#42"),
):
cutter = Cylinder(diameter / 2, 20, align=(Align.CENTER, Align.CENTER, Align.MIN)).rotate(
Axis.Y, 90
)
part -= Pos(-10, y, 0) * cutter
holes.append(HoleFeature(Frame((-10.0, y, 0.0), "x"), diameter, 20.0, True))
raw.append(
replace(
_raw(
"internal_thread",
f"M{nominal} x {pitch}-6H RH, full thread through; "
f"DIA {diameter} tapping drill through",
_reference(
diameter=diameter,
interval=(-10.0, 10.0),
sense="internal",
axis_origin=(0.0, y, 0.0),
),
prose_id,
),
source_ids=(
f"manufacturing_requirement:{prose_id}",
f"manufacturing_requirement:{structured_id}",
),
reference_item_ids=(face_id,),
structured_fields=(
("thread side", "internal"),
("designation", f"M{nominal}x{pitch}"),
("fit class", "6H"),
("hand", "right"),
("through", "true"),
("tapping drill diameter", float(diameter)),
),
)
)

model = lower_ap242_manufacturing_requirements(
PartModel(part.bounding_box(), "x", [*holes, *raw])
)
assert not any(isinstance(feature, PmiFeature) for feature in model.features)
typed = [feature.thread for feature in model.features if isinstance(feature, HoleFeature)]
assert [thread.nominal_diameter for thread in typed] == [2.0, 4.0]
assert [thread.reference_item_ids for thread in typed] == [("#101",), ("#102",)]
assert all(thread.through and thread.drill_depth is None for thread in typed)
assert all(len(thread.source_ids) == 2 for thread in typed)

drawing = build_drawing(part, model=model, pmi="annotate", page="A2")
labels = [
drawing.registry.named(name).label
for name in drawing.registry.names()
if getattr(drawing.registry.named(name), "label", "")
]
assert sum("M2 x 0.4-6H RH" in label for label in labels) == 1, labels
assert sum("M4 x 0.7-6H RH" in label for label in labels) == 1
assert not [
issue
for issue in drawing.lint()
if issue.code in {"pmi_not_rendered", "pmi_not_lowered", "pmi_dropped"}
]
for hole in [
feature for feature in drawing.model().features if isinstance(feature, HoleFeature)
]:
thread_labels = [
(name, annotation.label)
for name, annotation in drawing.annotations_of(hole).items()
if "-6H RH" in getattr(annotation, "label", "")
]
assert len(thread_labels) == 1
name, label = thread_labels[0]
assert f"M{hole.thread.nominal_diameter:g}" in label
assert drawing.registry.feature_of(name) is hole
measurements = drawing.registry.measurement_of(name)
assert measurements, name
assert any(
measurement.parameter == "bore.diameter" and measurement.feature is hole
for measurement in measurements
), measurements


def test_typed_manufacturing_row_keeps_plain_sibling_diameters_in_the_shared_solve():
align = (Align.CENTER, Align.CENTER, Align.MIN)
segments = [
Expand Down
Loading
Loading