diff --git a/src/draftwright/_pmi_part21.py b/src/draftwright/_pmi_part21.py index 1a3c148d..3c2f18b2 100644 --- a/src/draftwright/_pmi_part21.py +++ b/src/draftwright/_pmi_part21.py @@ -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\d+(?:\.\d+)?)\s*x\s*(?P\d+(?:\.\d+)?)-" + r"(?P[A-Za-z0-9]+)\s+(?PRH|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]] = [] @@ -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] = [] @@ -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) ) @@ -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) ) diff --git a/src/draftwright/pmi.py b/src/draftwright/pmi.py index da824678..fd456405 100644 --- a/src/draftwright/pmi.py +++ b/src/draftwright/pmi.py @@ -56,6 +56,7 @@ ) from draftwright._pmi_linear_geometry import _proved_planar_linear from draftwright._pmi_part21 import ( + _AMBIGUOUS_MANUFACTURING_CALLOUTS, CommonLabelFact, DatumDefinitionFact, DatumOccurrenceFact, @@ -63,6 +64,8 @@ DimensionDisplayFact, GeometricToleranceFact, MaterialFact, + _prose_thread_designation, + _structured_thread_designation, dimension_basic_policy, match_common_label, match_datum_occurrence, @@ -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()) @@ -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: @@ -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 @@ -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( diff --git a/tests/test_issue_1298_manufacturing_requirements.py b/tests/test_issue_1298_manufacturing_requirements.py index 973a865e..5c353d1f 100644 --- a/tests/test_issue_1298_manufacturing_requirements.py +++ b/tests/test_issue_1298_manufacturing_requirements.py @@ -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 = [ diff --git a/tests/test_pmi_part21.py b/tests/test_pmi_part21.py index 77b3b3a3..c382d44e 100644 --- a/tests/test_pmi_part21.py +++ b/tests/test_pmi_part21.py @@ -499,6 +499,187 @@ def test_equal_manufacturing_values_on_distinct_support_stay_separate_issue_2137 } +def test_distinct_same_named_threads_do_not_share_presentation_support_issue_2231(tmp_path): + import draftwright.pmi as pmi + + path = tmp_path / "two-threads.step" + path.write_text( + _step( + "#1=PROPERTY_DEFINITION('manufacturing requirement','internal thread',#50);", + "#2=PROPERTY_DEFINITION('manufacturing requirement','internal thread',#50);", + "#3=PROPERTY_DEFINITION_REPRESENTATION(#1,#11);", + "#4=PROPERTY_DEFINITION_REPRESENTATION(#2,#12);", + "#11=REPRESENTATION('internal thread',(#21),#51);", + "#12=REPRESENTATION('internal thread',(#22),#51);", + "#21=DESCRIPTIVE_REPRESENTATION_ITEM('','M2 x 0.4-6H RH, full thread through; DIA 1.6 tapping drill through');", + "#22=DESCRIPTIVE_REPRESENTATION_ITEM('','M4 x 0.7-6H RH, full thread through; DIA 3.3 tapping drill through');", + "#31=SHAPE_ASPECT('internal thread','',#50,.T.);", + "#32=SHAPE_ASPECT('internal thread','',#50,.T.);", + "#33=GEOMETRIC_ITEM_SPECIFIC_USAGE('','',#31,#50,#101);", + "#34=GEOMETRIC_ITEM_SPECIFIC_USAGE('','',#32,#50,#102);", + "#35=DRAUGHTING_CALLOUT('internal thread',());", + "#36=DRAUGHTING_CALLOUT('internal thread',());", + "#37=DRAUGHTING_MODEL_ITEM_ASSOCIATION('','',#31,#50,#35);", + "#38=DRAUGHTING_MODEL_ITEM_ASSOCIATION('','',#32,#50,#36);", + "#40=GENERAL_PROPERTY('','user defined attribute',$);", + "#41=PROPERTY_DEFINITION('internal thread','pmi-assist',#31);", + "#42=PROPERTY_DEFINITION('internal thread','pmi-assist',#32);", + "#43=GENERAL_PROPERTY_ASSOCIATION('',$,#40,#41);", + "#44=GENERAL_PROPERTY_ASSOCIATION('',$,#40,#42);", + "#45=PROPERTY_DEFINITION_REPRESENTATION(#41,#61);", + "#46=PROPERTY_DEFINITION_REPRESENTATION(#42,#62);", + "#61=REPRESENTATION('internal thread',(#71,#72,#73,#74,#75),#51);", + "#62=REPRESENTATION('internal thread',(#81,#82,#83,#84,#85),#51);", + "#71=DESCRIPTIVE_REPRESENTATION_ITEM('thread side','internal');", + "#72=DESCRIPTIVE_REPRESENTATION_ITEM('designation','M2x0.4');", + "#73=DESCRIPTIVE_REPRESENTATION_ITEM('fit class','6H');", + "#74=DESCRIPTIVE_REPRESENTATION_ITEM('hand','right');", + "#75=DESCRIPTIVE_REPRESENTATION_ITEM('through','true');", + "#81=DESCRIPTIVE_REPRESENTATION_ITEM('thread side','internal');", + "#82=DESCRIPTIVE_REPRESENTATION_ITEM('designation','M4x0.7');", + "#83=DESCRIPTIVE_REPRESENTATION_ITEM('fit class','6H');", + "#84=DESCRIPTIVE_REPRESENTATION_ITEM('hand','right');", + "#85=DESCRIPTIVE_REPRESENTATION_ITEM('through','true');", + "#50=PRODUCT_DEFINITION_SHAPE('','',#52);", + "#52=PRODUCT_DEFINITION('part','',#53,#51);", + "#54=SHAPE_DEFINITION_REPRESENTATION(#50,#55);", + ), + encoding="utf-8", + ) + + prose = read_manufacturing_requirements(path) + assert len(prose) == 2 + assert all(not fact.reference_item_ids and not fact.shape_aspect_ids for fact in prose) + assert all("multiple distinct supports" in fact.reason for fact in prose) + structured = read_structured_manufacturing_requirements(path) + assert [fact.reference_item_ids for fact in structured] == [("#101",), ("#102",)] + + sources, records = pmi._manufacturing_requirement_projection(path) + assert len(records) == 2 + assert [record.reference_item_ids for record in records] == [("#101",), ("#102",)] + assert [record.source_ids for record in records] == [ + ("manufacturing_requirement:#1", "manufacturing_requirement:#41"), + ("manufacturing_requirement:#2", "manufacturing_requirement:#42"), + ] + assert all(not record.lowering_blockers for record in records) + assert {source.source_id for source in sources} == { + "manufacturing_requirement:#1", + "manufacturing_requirement:#2", + "manufacturing_requirement:#41", + "manufacturing_requirement:#42", + } + + +def test_single_thread_prose_cannot_claim_two_presented_faces_issue_2231(tmp_path, monkeypatch): + import draftwright.pmi as pmi + + path = tmp_path / "one-prose-two-faces.step" + path.write_text( + _step( + "#1=PROPERTY_DEFINITION('manufacturing requirement','internal thread',#50);", + "#2=PROPERTY_DEFINITION_REPRESENTATION(#1,#3);", + "#3=REPRESENTATION('internal thread',(#4),#51);", + "#4=DESCRIPTIVE_REPRESENTATION_ITEM('','M2 x 0.4-6H RH, full thread through; DIA 1.6 tapping drill through');", + "#5=SHAPE_ASPECT('internal thread','',#50,.T.);", + "#6=SHAPE_ASPECT('internal thread','',#50,.T.);", + "#7=GEOMETRIC_ITEM_SPECIFIC_USAGE('','',#5,#50,#101);", + "#8=GEOMETRIC_ITEM_SPECIFIC_USAGE('','',#6,#50,#102);", + "#9=DRAUGHTING_CALLOUT('internal thread',());", + "#10=DRAUGHTING_CALLOUT('internal thread',());", + "#11=DRAUGHTING_MODEL_ITEM_ASSOCIATION('','',#5,#50,#9);", + "#12=DRAUGHTING_MODEL_ITEM_ASSOCIATION('','',#6,#50,#10);", + "#20=GENERAL_PROPERTY('','user defined attribute',$);", + "#21=PROPERTY_DEFINITION('internal thread','pmi-assist',#5);", + "#22=GENERAL_PROPERTY_ASSOCIATION('',$,#20,#21);", + "#23=PROPERTY_DEFINITION_REPRESENTATION(#21,#24);", + "#24=REPRESENTATION('internal thread',(#25,#26,#27,#28),#51);", + "#25=DESCRIPTIVE_REPRESENTATION_ITEM('thread side','internal');", + "#26=DESCRIPTIVE_REPRESENTATION_ITEM('designation','M2x0.4');", + "#27=DESCRIPTIVE_REPRESENTATION_ITEM('fit class','6H');", + "#28=DESCRIPTIVE_REPRESENTATION_ITEM('hand','right');", + "#50=PRODUCT_DEFINITION_SHAPE('','',#52);", + "#52=PRODUCT_DEFINITION('part','',#53,#51);", + "#54=SHAPE_DEFINITION_REPRESENTATION(#50,#55);", + ), + encoding="utf-8", + ) + + (prose,) = read_manufacturing_requirements(path) + assert prose.callout_ids == ("#9", "#10") + assert prose.shape_aspect_ids == prose.reference_item_ids == () + assert "multiple distinct supports" in prose.reason + with monkeypatch.context() as patch: + patch.setattr(pmi, "read_structured_manufacturing_requirements", lambda _path: ()) + _sources, (record,) = pmi._manufacturing_requirement_projection(path) + assert record.reference_item_ids == () + assert record.lowering_blockers == (prose.reason,) + _sources, (paired,) = pmi._manufacturing_requirement_projection(path) + assert paired.reference_item_ids == ("#101",) + assert paired.source_ids == ( + "manufacturing_requirement:#1", + "manufacturing_requirement:#21", + ) + assert not paired.lowering_blockers + + +def test_ambiguous_thread_designation_does_not_invent_prose_ownership_issue_2231(monkeypatch): + import draftwright.pmi as pmi + + prose = tuple( + ManufacturingRequirementFact( + entity_id=f"#prose{index}", + semantic_name="internal thread", + text="M2 x 0.4-6H RH, full thread through; DIA 1.6 tapping drill through", + reason="thread presentation callouts have multiple distinct supports", + ) + for index in (1, 2) + ) + structured = tuple( + StructuredManufacturingFact( + entity_id=f"#uda{index}", + kind="internal_thread", + fields=( + ("designation", "M2x0.4"), + ("fit class", "6H"), + ("hand", "right"), + ), + reference_item_ids=(f"#face{index}",), + ) + for index in (1, 2) + ) + monkeypatch.setattr(pmi, "read_manufacturing_requirements", lambda _path: prose) + monkeypatch.setattr( + pmi, "read_structured_manufacturing_requirements", lambda _path: structured + ) + + _sources, records = pmi._manufacturing_requirement_projection("unused.step") + + assert len(records) == 4 + assert all(not record.source_ids for record in records) + assert all(not record.reference_item_ids for record in records[:2]) + assert all(record.lowering_blockers for record in records[:2]) + assert [record.reference_item_ids for record in records[2:]] == [ + ("#face1",), + ("#face2",), + ] + + +def test_thread_reconciliation_requires_complete_valid_designations_issue_2231(): + from draftwright._pmi_part21 import ( + _prose_thread_designation, + _structured_thread_designation, + ) + + assert _prose_thread_designation("M2 x 0.4") is None + assert _structured_thread_designation((("designation", "M2x0.4"),)) is None + assert ( + _structured_thread_designation( + (("designation", "M2x0.4"), ("fit class", "6H"), ("hand", "clockwise")) + ) + is None + ) + + def _read_surface_labels(tmp_path, name: str, *instances: str): step = tmp_path / f"{name}.step" step.write_text(_step(*instances), encoding="utf-8")