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
76 changes: 75 additions & 1 deletion src/draftwright/_pmi_part21.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class DimensionDisplayFact:
value_decimals: int | None
tolerance_decimals: int | None
unit_name: str
basic: bool = False


@dataclass(frozen=True)
Expand All @@ -140,6 +141,7 @@ class DimensionAssociationFact:
reference_item_groups: tuple[tuple[str, ...], ...]
callout_id: str = ""
reason: str = ""
basic: bool = False


@dataclass(frozen=True)
Expand Down Expand Up @@ -1365,6 +1367,14 @@ def read_dimension_display_facts(step_file: str | Path) -> tuple[DimensionDispla
if not kind:
continue
nominal_refs = representations.get(representation_ref, ())
basic = any(
_text(item.params[0]).casefold() == "dimensional note"
and _text(item.params[1]).casefold() == "theoretical"
for ref in nominal_refs
if (item := _entity_named(step.get(ref), "DESCRIPTIVE_REPRESENTATION_ITEM"))
is not None
and len(item.params) >= 2
)
nominal_ref = ""
for ref in nominal_refs:
representation_item = _entity_named(step.get(ref), "REPRESENTATION_ITEM")
Expand Down Expand Up @@ -1414,11 +1424,33 @@ def read_dimension_display_facts(step_file: str | Path) -> tuple[DimensionDispla
_value_format_decimals(step, nominal_ref, qualifications),
next(iter(tolerance_decimals)) if len(tolerance_decimals) == 1 else None,
unit_name,
basic,
)
)
return tuple(facts)


def _collect_dimension_representations(
instance,
entity_id: str,
representation_items: dict[str, tuple[str, ...]],
dimension_representations: dict[str, list[str]],
) -> None:
representation = _entity_named(instance, "SHAPE_DIMENSION_REPRESENTATION")
if representation is not None and len(representation.params) >= 2:
representation_items[entity_id] = _references(representation.params[1])
dimension_link = _entity_named(instance, "DIMENSIONAL_CHARACTERISTIC_REPRESENTATION")
if dimension_link is None or len(dimension_link.params) < 2:
return
characteristic_ref, representation_ref = dimension_link.params[:2]
if isinstance(characteristic_ref, p21.Reference) and isinstance(
representation_ref, p21.Reference
):
dimension_representations.setdefault(str(characteristic_ref), []).append(
str(representation_ref)
)


def read_dimension_associations(step_file: str | Path) -> tuple[DimensionAssociationFact, ...]:
"""Read exact Part21 topology groups for size and location characteristics.

Expand All @@ -1432,6 +1464,8 @@ def read_dimension_associations(step_file: str | Path) -> tuple[DimensionAssocia
aspect_items: dict[str, list[str]] = {}
association_callouts: dict[str, list[str]] = {}
callout_names: dict[str, str] = {}
dimension_representations: dict[str, list[str]] = {}
representation_items: dict[str, tuple[str, ...]] = {}

for section in step.data:
for entity_id, instance in section.instances.items():
Expand Down Expand Up @@ -1504,6 +1538,9 @@ def read_dimension_associations(step_file: str | Path) -> tuple[DimensionAssocia
for ref in _references(association.params[4])
if _instance_is(step, ref, "DRAUGHTING_CALLOUT")
)
_collect_dimension_representations(
instance, entity_id, representation_items, dimension_representations
)

def related_items(root: str) -> tuple[str, ...]:
pending = [root]
Expand All @@ -1521,6 +1558,16 @@ def related_items(root: str) -> tuple[str, ...]:
facts: list[DimensionAssociationFact] = []
for entity_id, kind, semantic_name, shape_aspect_ids, expand_direct_members in characteristics:
reasons: list[str] = []
dimensional_notes = [
_text(item.params[1]).casefold()
for representation_ref in dimension_representations.get(entity_id, ())
for item_ref in representation_items.get(representation_ref, ())
if (item := _entity_named(step.get(item_ref), "DESCRIPTIVE_REPRESENTATION_ITEM"))
is not None
and len(item.params) >= 2
and _text(item.params[0]).casefold() == "dimensional note"
]
basic = "theoretical" in dimensional_notes
callout_ids = tuple(dict.fromkeys(association_callouts.get(entity_id, ())))
callout_id = callout_ids[0] if len(callout_ids) == 1 else ""
if len(callout_ids) != 1:
Expand Down Expand Up @@ -1549,6 +1596,7 @@ def related_items(root: str) -> tuple[str, ...]:
reference_item_groups=item_groups,
callout_id=callout_id,
reason="; ".join(dict.fromkeys(reasons)),
basic=basic,
)
)
return tuple(facts)
Expand Down Expand Up @@ -1583,10 +1631,36 @@ def match_dimension_display(
and fact.kind == kind
and math.isclose(fact.authored_value, authored_value, rel_tol=1e-9, abs_tol=1e-12)
]
policies = {(fact.value_decimals, fact.tolerance_decimals, fact.unit_name) for fact in matches}
policies = {
(fact.value_decimals, fact.tolerance_decimals, fact.unit_name, fact.basic)
for fact in matches
}
return matches[0] if matches and len(policies) == 1 else None


def dimension_basic_policy(
facts: tuple[DimensionDisplayFact, ...],
semantic_name: str,
kind: str,
authored_value: float,
association: DimensionAssociationFact | None,
) -> tuple[bool, tuple[str, ...]]:
"""Return basic status and any ambiguity, preferring exact Part21 identity."""
if association is not None:
return association.basic, ()
policies = {
fact.basic
for fact in facts
if fact.semantic_name == semantic_name
and fact.kind == kind
and math.isclose(fact.authored_value, authored_value, rel_tol=1e-9, abs_tol=1e-12)
}
blockers = (
("Part21 basic-dimension status is ambiguous for this value",) if len(policies) > 1 else ()
)
return policies == {True}, blockers


def read_geometric_tolerances(step_file: str | Path) -> tuple[GeometricToleranceFact, ...]:
"""Read supported geometric-tolerance facts without inferring entity order.

Expand Down
45 changes: 45 additions & 0 deletions src/draftwright/_pmi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,51 @@
"radius": "R",
}


def _make_label(
kind: str,
value: float,
upper_tol: float | None,
lower_tol: float | None,
*,
lower_bound: float | None = None,
upper_bound: float | None = None,
value_decimals: int | None = None,
tolerance_decimals: int | None = None,
unit_name: str = "",
) -> str:
"""Format an XCAF dimension label with optional deviation or limit tolerance."""
from draftwright._core import _fmt
from draftwright._geometry import _fmt_pmi_magnitude

prefix = _DIM_PREFIX.get(kind, "")
base = f"{prefix}{_fmt(value, value_decimals)}"
if lower_bound is not None and upper_bound is not None:
base = (
f"{prefix}{_fmt_pmi_magnitude(lower_bound, value_decimals)} - "
f"{prefix}{_fmt_pmi_magnitude(upper_bound, value_decimals)}"
)
return f"{base} {unit_name}" if unit_name else base
# OCCT returns unsigned tolerance magnitudes; spell both directions out.
if upper_tol is not None and lower_tol is not None:
if abs(upper_tol) == abs(lower_tol) and abs(upper_tol) > 1e-9:
base += f" ±{_fmt_pmi_magnitude(abs(upper_tol), tolerance_decimals)}"
elif abs(lower_tol) <= 1e-9:
base += f" +{_fmt_pmi_magnitude(abs(upper_tol), tolerance_decimals)}/0"
elif abs(upper_tol) <= 1e-9:
base += f" 0/-{_fmt_pmi_magnitude(abs(lower_tol), tolerance_decimals)}"
else:
base += (
f" +{_fmt_pmi_magnitude(abs(upper_tol), tolerance_decimals)}"
f"/-{_fmt_pmi_magnitude(abs(lower_tol), tolerance_decimals)}"
)
elif upper_tol is not None:
base += f" +{_fmt_pmi_magnitude(abs(upper_tol), tolerance_decimals)}"
elif lower_tol is not None:
base += f" -{_fmt_pmi_magnitude(abs(lower_tol), tolerance_decimals)}"
return f"{base} {unit_name}" if unit_name else base


# int → short tag for XCAFDimTolObjects_GeomToleranceType
_GTOL_TYPE: dict[int, str] = {
1: "angularity",
Expand Down
Loading
Loading