Skip to content
Merged
20 changes: 10 additions & 10 deletions src/draftwright/annotations/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,18 +1313,18 @@ class PlacementContext:
corridor_batch: dict = field(default_factory=dict)
escalations: list = field(default_factory=list)
detail_requests: list = field(default_factory=list)
# Fallthrough callbacks a pass's on_drop queues to run AFTER every corridor has
# drained: a mid-drain carve could occupy space a later sibling
# corridor's force candidate needs; deferral makes "post-drain" literally true.
# Fallthrough callbacks run after corridor drain so no mid-drain carve
# occupies a later sibling's forced candidate.
post_drain: list = field(default_factory=list)
# Whole dimensions whose ordinary exterior corridor was genuinely full.
# The automatic/finalize entry points opt in with ``[]`` and drain them as
# one bounded interior assignment after every exterior fallthrough settles.
defer_pattern_pitch: bool = False
deferred_pattern_pitches: list = field(default_factory=list)
deferred_pattern_pitch_names: set[str] = field(default_factory=set)
# Full exterior-corridor fallthroughs enter one bounded interior solve
# after every exterior pass settles; automatic/finalize opt in with ``[]``.
interior_dimensions: list | None = None
# Compatible automatic/deferred feature-callout jobs collected across the
# hole + post-drain machined passes. ``None`` is intentional: direct
# renderer calls and finished-sheet live verbs keep their immediate behavior;
# the orchestrator/finalize paths opt in with ``[]`` and drain once.
# Automatic/deferred callout jobs from hole and machined passes.
# ``None`` preserves immediate direct-renderer/live-verb behavior;
# orchestrator/finalize opt in with ``[]`` and drain once.
feature_leaders: list | None = None
# Optional pre-render annotation lanes. Automatic builds attach the scheme's
# deterministic plan; live edits leave this unset and retain their existing order.
Expand Down
11 changes: 10 additions & 1 deletion src/draftwright/annotations/_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
from draftwright.model.ir import PatternFeature


def _place_pitch_after_pmi(place_pitch_dim, *args, ctx, **kwargs):
"""Offer generated pitch only after source PMI has used the shared strips."""
if ctx.defer_pattern_pitch:
ctx.deferred_pattern_pitch_names.add(args[8])
ctx.deferred_pattern_pitches.append(partial(place_pitch_dim, *args, ctx=ctx, **kwargs))
return
return place_pitch_dim(*args, ctx=ctx, **kwargs)


def _furnish_uncalled_patterns(
dwg, a: Analysis, view_of_axis, plan, *, ctx, furnished, add_furniture
):
Expand Down Expand Up @@ -53,7 +62,7 @@ def _furnish_uncalled_patterns(
j = 0
while any(
nm == f"dim_pitch_{view}{j}" or nm.startswith(f"dim_pitch_{view}{j}_")
for nm in ctx.registry.names()
for nm in (*ctx.registry.names(), *ctx.deferred_pattern_pitch_names)
):
j += 1
add_furniture(
Expand Down
13 changes: 8 additions & 5 deletions src/draftwright/annotations/holes.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def add_feature_furniture(dwg, feature, model, a, *, view: str | None = None, ct
while any(
nm in (f"bc_{view}{j}", f"dim_pitch_{view}{j}")
or nm.startswith(f"dim_pitch_{view}{j}_")
for nm in ctx.registry.names()
for nm in (*ctx.registry.names(), *ctx.deferred_pattern_pitch_names)
):
j += 1
_add_furniture(
Expand Down Expand Up @@ -754,7 +754,8 @@ def _add_furniture(
pitch = group.dim(kind="length", role="pitch") if group is not None else None
if pitch is None:
return # not approved — the compiler withheld the value, so there is none to print
_place_pitch_dim(
_patterns._place_pitch_after_pmi(
_place_pitch_dim,
dwg,
a,
view,
Expand Down Expand Up @@ -827,7 +828,7 @@ def _add_grid_pitch_dims(
ctx=ctx,
name_prefix=name_prefix,
drop_code=drop_code,
place_pitch_dim=_place_pitch_dim,
place_pitch_dim=partial(_patterns._place_pitch_after_pmi, _place_pitch_dim),
)


Expand Down Expand Up @@ -1095,15 +1096,17 @@ def _place(off, side_vec=side, dim=None):

def render_pocket_patterns(dwg, plan, a, *, ctx, only=None) -> int:
"""Render grouped pocket arrays with the live hole pitch-placement binding."""
pitch_placer = partial(_patterns._place_pitch_after_pmi, _place_pitch_dim)
return _patterns.render_pocket_patterns(
dwg, plan, a, ctx=ctx, only=only, place_pitch_dim=_place_pitch_dim
dwg, plan, a, ctx=ctx, only=only, place_pitch_dim=pitch_placer
)


def render_slot_patterns(dwg, plan, a, *, ctx, only=None) -> int:
"""Render grouped slot arrays with the live hole pitch-placement binding."""
pitch_placer = partial(_patterns._place_pitch_after_pmi, _place_pitch_dim)
return _patterns.render_slot_patterns(
dwg, plan, a, ctx=ctx, only=only, place_pitch_dim=_place_pitch_dim
dwg, plan, a, ctx=ctx, only=only, place_pitch_dim=pitch_placer
)


Expand Down
44 changes: 38 additions & 6 deletions src/draftwright/annotations/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,42 @@ def _retire_optional_section_reservations_for_pmi(ctx, dwg, sections) -> None:
_clear_section_reservation(dwg, section)


def _place_deferred_pattern_pitches(ctx) -> None:
"""Offer compiled pattern pitch to the room left by imported source PMI."""
pending, ctx.deferred_pattern_pitches = ctx.deferred_pattern_pitches, []
for place in pending:
place()
ctx.deferred_pattern_pitch_names.clear()


def _has_imported_linear_dimensions(model) -> bool:
"""Source-owned lengths that should solve before generated pattern pitch."""
return any(
feature.kind == "authored_dimension" and getattr(feature, "source", None) == "ap242_pmi"
for feature in model.features
)


def _defer_generated_pitch_for_source_pmi(a, ctx, model) -> bool:
"""Use source-first order only where the approved pitch is generated ink."""
return (
model.authored_dimensions is None
and _has_imported_linear_dimensions(model)
and (a.pmi_mode == "annotate" or (ctx.model_declared and not ctx.document_member))
)


def _configure_model_context(ctx, dwg, a, model) -> None:
"""Share the model and source-annotation policy with each annotation pass."""
ctx.part_model = model
ctx.model_declared = dwg.model_declared
ctx.document_member = getattr(dwg, "document_member", False)
ctx.document_source_annotation_ids = getattr(
dwg, "document_source_annotation_ids", frozenset()
)
ctx.defer_pattern_pitch = _defer_generated_pitch_for_source_pmi(a, ctx, model)


def _concentric_bore_diams(a: Analysis) -> list:
"""Distinct bore diameters on the rotation axis, in z_diams order (#10).

Expand Down Expand Up @@ -933,6 +969,7 @@ def _s_drain():
# and callout layout remains stable.
_retire_optional_section_reservations_for_pmi(ctx, dwg, _sections)
drain_and_reconcile(ctx, dwg)
_place_deferred_pattern_pitches(ctx)

def _s_grooves():
# Turned/circlip-groove callouts: {width} WIDE × ø{dia} via a leader off
Expand Down Expand Up @@ -1129,12 +1166,7 @@ def _auto_annotate(dwg: DrawingPort, a: Analysis, *, detail_view: bool = False):
# model. The ensured model is threaded onto the run's ctx so every pass reads
# it there, without accessing the drawing's private state.
_model = cast(PartModel, dwg.model() if dwg.model() is not None else build_model(a))
ctx.part_model = _model
ctx.model_declared = dwg.model_declared
ctx.document_member = getattr(dwg, "document_member", False)
ctx.document_source_annotation_ids = getattr(
dwg, "document_source_annotation_ids", frozenset()
)
_configure_model_context(ctx, dwg, a, _model)
# Plan dimensions once and thread the groups to every renderer that reads them.
_groups = plan_dimensions(_model, planned_views=a.planned_views)
# Share one compiled plan so the ladder, shoulders, and detail escalation
Expand Down
Loading
Loading