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
22 changes: 21 additions & 1 deletion src/draftwright/annotations/_gdt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import math
from collections.abc import Callable
from dataclasses import dataclass
from dataclasses import dataclass, replace
from typing import Any

from build123d import Align, Location, Mode, ShapeList, Sketch, Text, Vector
Expand Down Expand Up @@ -59,6 +59,25 @@
_MIN_LEADER = 0.05


def _datum_alternate_view(item, views, selected_views):
"""Retarget a Y-normal datum to its other edge-on view if its strip is absent."""
if not (
isinstance(item, DatumRef)
and getattr(item.origin, "source_category", None) == "datum"
and item.reference_surface_kind == "plane"
and item.frame.axis == "y"
and item.view == "side"
and item.side in ("left", "right")
and "plan" in selected_views
and getattr(views["side"][0], item.side) is None
):
return item
side = "below" if item.side == "left" else "above"
if getattr(views["plan"][0], side) is None:
return item
return replace(item, view="plan", side=side)


@dataclass(frozen=True, slots=True)
class _GdtDropState:
"""One declared frame's identity and geometry retained until post-drain retry."""
Expand Down Expand Up @@ -802,6 +821,7 @@ def render_gdt(
tb_box = _title_block_box(dwg, a)
n = 0
for i, item in enumerate(items):
item = _datum_alternate_view(item, views, dwg.views)
name = f"m_gdt{i}"
source_ids = source_ids_for(item)
satisfaction = tuple(
Expand Down
77 changes: 71 additions & 6 deletions tests/test_gdt_placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import math
from collections import defaultdict
from pathlib import Path
from types import SimpleNamespace
from xml.etree import ElementTree

import ezdxf
import pytest
from build123d import Box, Cylinder, Draft, Pos
from build123d_drafting import DatumFeature, FeatureControlFrame, Leader

from draftwright.annotations._gdt import _datum_alternate_view
from draftwright.builder import build_drawing, detect_part_model
from draftwright.linting.structural import lint_drawing
from draftwright.model.ir import ControlFrame, DatumRef, Finish, Frame, Note, PmiFeature
Expand Down Expand Up @@ -412,13 +414,24 @@ def forbidden_sheet_route(*_args, **_kwargs):
assert [attempt["side"] for attempt in events[0]["items"][0]["attempts"]] == ["left"]


def test_imported_datum_on_absent_side_strip_drops_without_aborting_issue_2182(tmp_path):
def test_imported_datum_on_absent_side_strip_tries_normal_plan_edge_issue_2182(tmp_path):
origin = PmiFeature(
frame=Frame((0.0, -25.0, 0.0), "y"),
pmi_kind="datum",
value=0.0,
label="A",
dominant_axis="Y",
ref_bbox=(-40.0, -25.0, -10.0, 40.0, -25.0, 10.0),
source_category="datum",
reference_axis="Y",
)
datum = DatumRef(
frame=Frame((0.0, -25.0, 0.0), "y"),
letter="A",
view="side",
side="left",
source_id="datum:missing-side-strip",
origin=origin,
reference_surface_kind="plane",
)
surviving_frame = ControlFrame(
Expand All @@ -436,18 +449,70 @@ def test_imported_datum_on_absent_side_strip_drops_without_aborting_issue_2182(t
assert "m_gdt0" not in dwg.annotations()
assert "m_gdt1" in dwg.annotations()
assert any(
issue.code == "pmi_dropped" and "m_gdt0" in issue.message for issue in dwg.registry.issues
issue.code == "pmi_dropped"
and "m_gdt0" in issue.message
and "surface-normal below strip" in issue.message
for issue in dwg.registry.issues
)
solves = json.loads(trace_path.read_text())["solves"]
assert any(
solve["corridor"] == ["side", "left"]
and solve["strip"] is None
and {item["name"] for item in solve["candidates"]} == {"m_gdt0"}
and solve["outcomes"] == [{"name": "m_gdt0", "outcome": "dropped", "reason": "no_strip"}]
solve["corridor"] == ["plan", "below"]
and solve["strip"] is not None
and "m_gdt0" in {candidate["name"] for candidate in solve["candidates"]}
and any(outcome["name"] == "m_gdt0" for outcome in solve["outcomes"])
for solve in solves
)


def test_datum_alternate_view_requires_a_selected_plan_view():
origin = PmiFeature(
frame=Frame((0.0, -25.0, 0.0), "y"),
pmi_kind="datum",
value=0.0,
label="B",
dominant_axis="Y",
ref_bbox=(-40.0, -25.0, -10.0, 40.0, -25.0, 10.0),
source_category="datum",
reference_axis="Y",
)
datum = DatumRef(
frame=Frame((0.0, -25.0, 0.0), "y"),
letter="B",
view="side",
side="left",
origin=origin,
reference_surface_kind="plane",
)
zones = {
"side": (SimpleNamespace(left=None),),
"plan": (SimpleNamespace(below=object()),),
}
assert _datum_alternate_view(datum, zones, {"side": object()}) is datum
no_plan_strip = {
"side": zones["side"],
"plan": (SimpleNamespace(below=None),),
}
assert (
_datum_alternate_view(datum, no_plan_strip, {"side": object(), "plan": object()}) is datum
)
alternate = _datum_alternate_view(datum, zones, {"side": object(), "plan": object()})
assert (alternate.view, alternate.side, alternate.letter) == ("plan", "below", "B")
assert (
_datum_alternate_view(
DatumRef(
frame=datum.frame,
letter="B",
view="side",
side="left",
reference_surface_kind="plane",
),
zones,
{"side": object(), "plan": object()},
).view
== "side"
)


def test_projected_datum_stem_keeps_both_near_and_far_datums_issue_2128():
part = Box(80, 50, 20)
near = DatumRef(frame=Frame((0, 0, -10), "z"), letter="A", view="front", side="below")
Expand Down
Loading