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
5 changes: 3 additions & 2 deletions docs/reference/sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,9 @@ the dimension renderer actually consumes. A lane is an
integer from 1 through 8: a one-based drafting-spaced rank from that dimension's physical
witness, not a distance. The shared measured-candidate solve may resolve it into proven
whitespace inside or outside the view and records that candidate's region explicitly. The
current lane-capable slice is the linear width/length dimensions of slots and the
overall OD diameter of a rotational feature. Capability is
current lane-capable slice is the linear width/length dimensions of slots, the
overall OD diameter of a rotational feature, and step diameter leaders.
Capability is
declared in one compiler-owned registry so another dimension family is added deliberately, not
by teaching each renderer a private spelling.

Expand Down
336 changes: 282 additions & 54 deletions src/draftwright/annotations/_diameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,122 @@ def _diameter_bucket_item(entry):
)


def _diameter_bucket_lane(entry) -> int | None:
"""The planner permits one exact lane for a physical diameter bucket."""
lanes = {
dim.lane
for group in entry[6]
for dim in group.dims
if dim.kind == "diameter" and dim.lane is not None
}
if len(lanes) > 1:
raise ValueError("one physical diameter cannot have conflicting requested lanes")
return next(iter(lanes)) if len(lanes) == 1 else None


def _render_diameter_lanes(
dwg,
a,
indexed_buckets,
*,
axis,
prefix,
start,
ctx,
radial_candidates,
place_jobs,
leader_reach,
) -> int:
"""Offer only the requested witness-relative rank to the shared leader solve."""
vb = dwg.view_bounds("front")
if vb is None:
return 0
spacing = dwg.draft.font_size + 2 * dwg.draft.pad_around_text
jobs = []
names = set()
requested_lanes = {}
covers_diameters_by_name = {}
source_ids_by_name = {}
include_source_pmi = not ctx.document_member or a.pmi_mode == "annotate"
for index, entry in indexed_buckets:
_anchor, dia, value_text, refs, tolerance, rider, groups = entry
lane = _diameter_bucket_lane(entry)
if lane is None:
continue
representative = groups[0].facts
owner = next(iter(refs)) if len(refs) == 1 else None
name = f"{prefix}{start + index}"
names.add(name)
requested_lanes[name] = lane
source_ids_by_name[name] = (
tuple(
dict.fromkeys(
source_id
for group in groups
for aspect in (group.facts.get("thread"), group.facts.get("knurl"))
for source_id in getattr(aspect, "source_ids", ())
)
)
if include_source_pmi
else ()
)
if axis == "y":
covers_diameters_by_name[name] = dia
reach = leader_reach(dwg.draft) + (lane - 1) * spacing
if axis == "y":
candidates = radial_candidates(
dwg,
"front",
vb,
representative,
reach,
rim=dia / 2 * a.SCALE,
directions=_END_DIAMETER_LEAD_DIRS,
provenance=owner,
)
else:
candidates = radial_candidates(
dwg,
"front",
vb,
representative,
reach,
source_bounds=_diameter_source_bounds(dwg, "front", representative, dia),
directions=_DIAMETER_LEAD_DIRS[axis],
provenance=owner,
)
label = f"ø{value_text}{_tol_suffix(tolerance, dwg.draft)}"
if rider:
label += f" {rider}"
mids = tuple(
parameter.id
for group in groups
for parameter in group.dims
if parameter.kind == "diameter"
)
jobs.append((name, "front", vb, label, candidates, mids))
if not jobs:
return 0
return cast(
int,
place_jobs(
dwg,
a,
jobs,
noun="diameter at requested lane",
drop_code="diameter_dropped",
ctx=ctx,
geom_clear=True,
joint=True,
expand_lanes=False,
straight_only_names=frozenset(names),
requested_lanes=requested_lanes,
covers_diameters_by_name=covers_diameters_by_name,
source_ids_by_name=source_ids_by_name,
),
)


def _counted_column_items(buckets, draft):
"""Share exactly two equal step labels while retaining both physical identities."""
entries = list(buckets.values())
Expand Down Expand Up @@ -591,6 +707,143 @@ def _matching_bore_values(bore_specs, origin):
}


def _render_x_diameter_buckets(
dwg,
a,
buckets,
start,
*,
ctx,
trace,
include_source_pmi,
radial_candidates,
place_jobs,
leader_reach,
place_what_fits,
) -> int:
"""Keep the ordinary X row and explicit-lane X leaders as separate owners."""
indexed = list(enumerate(buckets.values()))

def typed(entry):
return include_source_pmi and any(
isinstance(group.facts.get("thread"), ThreadRequirement)
or isinstance(group.facts.get("knurl"), KnurlRequirement)
for group in entry[6]
)

lane_entries = [(index, entry) for index, entry in indexed if _diameter_bucket_lane(entry)]
typed_entries = [
(index, entry)
for index, entry in indexed
if not _diameter_bucket_lane(entry) and typed(entry)
]
plain_entries = [
(index, entry)
for index, entry in indexed
if not _diameter_bucket_lane(entry) and not typed(entry)
]
placed = 0
# A requested lane splits the otherwise unchanged contiguous legacy row.
for _, run in groupby(enumerate(plain_entries), key=lambda item: item[1][0] - item[0]):
entries = [entry for _ordinal, entry in run]
placed += _diameter_row_below(
dwg,
[_diameter_bucket_item(entry) for _index, entry in entries],
start=start + entries[0][0],
trace=trace,
ctx=ctx,
place_what_fits=place_what_fits,
)
placed += _render_diameter_leaders(
dwg,
a,
typed_entries,
prefix="m_dia_x",
start=start,
ctx=ctx,
include_source_pmi=include_source_pmi,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
)
unplaced = [
entry
for _index, entry in plain_entries
if any(
not dwg.registry.has_measurement(group.dim(kind="diameter").id) for group in entry[6]
)
]
placed += _render_diameter_leaders(
dwg,
a,
list(enumerate(unplaced)),
prefix="m_dia_x",
start=start + len(buckets),
ctx=ctx,
include_source_pmi=include_source_pmi,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
)
placed += _render_diameter_lanes(
dwg,
a,
lane_entries,
axis="x",
prefix="m_dia_x",
start=start,
ctx=ctx,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
)
return placed


def _render_z_diameter_buckets(
dwg,
a,
buckets,
start,
*,
ctx,
trace,
radial_candidates,
place_jobs,
leader_reach,
place_what_fits,
) -> int:
"""Keep Z column names stable when one bucket selects an explicit lane."""
indexed = list(enumerate(buckets.values()))
placed = 0
for _, run in groupby(
enumerate((index, entry) for index, entry in indexed if not _diameter_bucket_lane(entry)),
key=lambda item: item[1][0] - item[0],
):
entries = [entry for _ordinal, entry in run]
placed += _diameter_column_left(
dwg,
_counted_column_items({index: entry for index, entry in entries}, dwg.draft),
start=start + entries[0][0],
trace=trace,
ctx=ctx,
place_what_fits=place_what_fits,
)
placed += _render_diameter_lanes(
dwg,
a,
[(index, entry) for index, entry in indexed if _diameter_bucket_lane(entry)],
axis="z",
prefix="m_dia_z",
start=start,
ctx=ctx,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
)
return placed


def render_diameters(
dwg,
plan,
Expand Down Expand Up @@ -670,71 +923,29 @@ def _next_start(prefix):
start_y = _next_start("m_dia_y") if only is not None else 0
start_z = _next_start("m_dia_z") if only is not None else 0
trace = getattr(ctx, "trace", None) # the immediate placers report to the trace too
indexed_row = list(enumerate(row_buckets.values()))

def _typed(entry):
return include_source_pmi and any(
isinstance(group.facts.get("thread"), ThreadRequirement)
or isinstance(group.facts.get("knurl"), KnurlRequirement)
for group in entry[6]
)

typed_entries = [(index, entry) for index, entry in indexed_row if _typed(entry)]
plain_entries = [(index, entry) for index, entry in indexed_row if not _typed(entry)]
placed = 0
# Preserve the legacy tidy row for short plain diameters. Split only where a typed
# bucket owns the intervening stable name; each contiguous run can then use the legacy
# solver without renumbering a source-owned callout.
for _, run in groupby(
enumerate(plain_entries),
key=lambda item: item[1][0] - item[0],
):
entries = [entry for _ordinal, entry in run]
placed += _diameter_row_below(
dwg,
[_diameter_bucket_item(entry) for _index, entry in entries],
start=start_x + entries[0][0],
trace=trace,
ctx=ctx,
place_what_fits=place_what_fits,
)
placed += _render_diameter_leaders(
placed = _render_x_diameter_buckets(
dwg,
a,
typed_entries,
prefix="m_dia_x",
start=start_x,
row_buckets,
start_x,
ctx=ctx,
trace=trace,
include_source_pmi=include_source_pmi,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
place_what_fits=place_what_fits,
)
unplaced = [
entry
for _index, entry in plain_entries
if any(
not dwg.registry.has_measurement(group.dim(kind="diameter").id) for group in entry[6]
)
]
placed += _render_diameter_leaders(
placed += _render_z_diameter_buckets(
dwg,
a,
list(enumerate(unplaced)),
prefix="m_dia_x",
start=start_x + len(row_buckets),
col_buckets,
start_z,
ctx=ctx,
include_source_pmi=include_source_pmi,
trace=trace,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
)
placed += _diameter_column_left(
dwg,
_counted_column_items(col_buckets, dwg.draft),
start=start_z,
trace=trace,
ctx=ctx,
place_what_fits=place_what_fits,
)

Expand All @@ -760,9 +971,10 @@ def _typed(entry):
hole_circles.append((px, py, diameter / 2 * a.SCALE))
jobs = []
covered_by_name = {}
for i, (_anchor, dia, value_text, refs, dtol, thr, feature_groups) in enumerate(
end_buckets.values()
):
for i, entry in enumerate(end_buckets.values()):
if _diameter_bucket_lane(entry):
continue
_anchor, dia, value_text, refs, dtol, thr, feature_groups = entry
representative = feature_groups[0].facts
owner = next(iter(refs)) if len(refs) == 1 else None
# Materialise now: a generator expression would close over ``owner``
Expand Down Expand Up @@ -812,6 +1024,22 @@ def _typed(entry):
ann = ctx.registry.named(name)
if ann is not None:
ann.covers_diameters = (dia,)
placed += _render_diameter_lanes(
dwg,
a,
[
(index, entry)
for index, entry in enumerate(end_buckets.values())
if _diameter_bucket_lane(entry)
],
axis="y",
prefix="m_dia_y",
start=start_y,
ctx=ctx,
radial_candidates=radial_candidates,
place_jobs=place_jobs,
leader_reach=leader_reach,
)
# A ⌀ leader routed diagonally into the body — cutting
# the silhouette, or an end feature whose diagonal merely grazes it — is re-routed
# to the clear side (the margin the feature sits at). Auto-pass only: the finalize
Expand Down
Loading
Loading