diff --git a/src/draftwright/builder.py b/src/draftwright/builder.py index b13799f96..2acc5bbbe 100644 --- a/src/draftwright/builder.py +++ b/src/draftwright/builder.py @@ -90,6 +90,7 @@ from draftwright.layout_selection import ( annotation_demand_carrier_evidence, choose_pre_render_profile, + pre_render_view_page_overflow, select_best_annotation_layout, ) from draftwright.linting import LintIssue @@ -1739,6 +1740,46 @@ def analyse( page_override=(a.PAGE_W, a.PAGE_H), arrangements_override=(selected_profile.arrangement or a.arrangement,), ) + # The protected-gutter profile can worsen a caller-fixed, already + # overfull sheet by pushing principal views beyond its physical page. + # Compare only cheap pre-render geometry. If the established + # conservative profile reduces at least one per-view overflow and + # increases none, select it *before* building the one drawing. + # This is neither a finished-baseline comparison nor a safety + # admission. FTC09's plan moved 6 mm farther out, withholding a + # width and PMI carrier that the conservative profile preserves. + overflow = pre_render_view_page_overflow(a) + if selected_profile.view_gutters and overflow: + conservative_profile = candidate_profile("iso-growth", a.SCALE) + with use_layout_profile(conservative_profile): + conservative_analysis = analyse( + reuse=a, + views=tuple(a.planned_views or third_angle_view_names()), + scale_override=a.SCALE, + page_override=(a.PAGE_W, a.PAGE_H), + arrangements_override=(conservative_profile.arrangement or a.arrangement,), + ) + conservative_overflow = pre_render_view_page_overflow(conservative_analysis) + views_to_compare = set(overflow) | set(conservative_overflow) + no_worse = all( + conservative_overflow.get(view, 0.0) <= overflow.get(view, 0.0) + 1e-6 + for view in views_to_compare + ) + strictly_better = any( + conservative_overflow.get(view, 0.0) < overflow.get(view, 0.0) - 1e-6 + for view in views_to_compare + ) + if no_worse and strictly_better: + selected_profile = conservative_profile + a = conservative_analysis + pre_render_choice = { + **pre_render_choice, + "proposed_profile": profile_name, + "profile": "iso-growth", + "reason": "protected_gutters_worsen_off_page_views", + "proposed_view_overflow_mm": overflow, + "selected_view_overflow_mm": conservative_overflow, + } # Pass 1: place + annotate from the estimated layout, then measure the real # per-view footprints and re-pack the blocks disjoint if a view actually diff --git a/src/draftwright/layout_selection.py b/src/draftwright/layout_selection.py index d38a6866f..6293d0c02 100644 --- a/src/draftwright/layout_selection.py +++ b/src/draftwright/layout_selection.py @@ -13,6 +13,7 @@ ) if TYPE_CHECKING: + from draftwright._core import Analysis from draftwright.compose import AnnotationSchemeShadowReport, StripDepths from draftwright.drawing import Drawing from draftwright.layout_scheme import AnnotationScheme @@ -78,7 +79,7 @@ def choose_pre_render_profile( else: profile, reason = "planned", "all_typed_corridors_fit" return { - "version": 4, + "version": 5, "profile": profile, "reason": reason, "page": [float(page[0]), float(page[1])], @@ -91,6 +92,51 @@ def choose_pre_render_profile( } +def pre_render_view_page_overflow(analysis: Analysis) -> dict[str, float]: + """Measure principal-view geometry outside the caller's fixed page before render. + + This is a profile-choice signal, not a completeness verdict: annotation ink and + requirements still need the independent finished-drawing checks. The settled + analysis already owns these projected centres and extents, so no drawing or + baseline solve is needed to find a view the candidate has pushed off-paper. + """ + + boxes = { + "front": ( + analysis.FV_X - analysis.fv_hw, + analysis.FV_Y - analysis.fv_hh, + analysis.FV_X + analysis.fv_hw, + analysis.FV_Y + analysis.fv_hh, + ), + "plan": ( + analysis.PV_X - analysis.fv_hw, + analysis.PV_Y - analysis.pv_hh, + analysis.PV_X + analysis.fv_hw, + analysis.PV_Y + analysis.pv_hh, + ), + "side": ( + analysis.SV_X - analysis.sv_hw, + analysis.SV_Y - analysis.fv_hh, + analysis.SV_X + analysis.sv_hw, + analysis.SV_Y + analysis.fv_hh, + ), + } + overflow = {} + for view in analysis.planned_views or ("front", "plan", "side"): + if view not in boxes: + continue + x0, y0, x1, y1 = boxes[view] + outside = ( + max(0.0, -x0) + + max(0.0, -y0) + + max(0.0, x1 - analysis.PAGE_W) + + max(0.0, y1 - analysis.PAGE_H) + ) + if outside > 1e-6: + overflow[view] = outside + return overflow + + def annotation_demand_carrier_evidence(scheme: AnnotationScheme, registry) -> dict[str, object]: """Join pre-render measurement requests to *live* exact carrier provenance. diff --git a/tests/test_annotation_layout_product.py b/tests/test_annotation_layout_product.py index 7ddf49b8b..69a1114f1 100644 --- a/tests/test_annotation_layout_product.py +++ b/tests/test_annotation_layout_product.py @@ -9,9 +9,16 @@ from typer.testing import CliRunner from draftwright import ScaleCompletenessWarning, Sheet, build_drawing -from draftwright.annotation_layout_profile import annotation_layout_policy, candidate_profile +from draftwright.annotation_layout_profile import ( + annotation_layout_policy, + candidate_profile, + current_layout_profile, +) from draftwright.cli import app -from draftwright.layout_selection import select_best_annotation_layout +from draftwright.layout_selection import ( + pre_render_view_page_overflow, + select_best_annotation_layout, +) from draftwright.linting import LintIssue from draftwright.sheet_emit import generate_sheet_script @@ -49,7 +56,7 @@ def test_best_layout_selects_verified_larger_iso_on_same_sheet_and_scale(): decision = selected.annotation_scheme_decision assert decision["policy"] == "compare" - assert decision["pre_render_choice"]["version"] == 4 + assert decision["pre_render_choice"]["version"] == 5 assert decision["pre_render_choice"]["page"] == [selected.page_w, selected.page_h] assert decision["pre_render_choice"]["scale"] == selected.scale assert decision["safety_evidence"]["version"] == 12 @@ -103,6 +110,71 @@ def observe(*args, **kwargs): assert (drawing.page_w, drawing.page_h, drawing.scale) == (297.0, 210.0, 2.0) +def test_pre_render_view_overflow_measures_only_planned_principal_geometry(): + analysis = SimpleNamespace( + FV_X=50.0, + FV_Y=50.0, + PV_X=50.0, + PV_Y=108.0, + SV_X=98.0, + SV_Y=50.0, + fv_hw=20.0, + fv_hh=30.0, + pv_hh=5.0, + sv_hw=5.0, + PAGE_W=100.0, + PAGE_H=110.0, + planned_views=("front", "plan"), + ) + + assert pre_render_view_page_overflow(analysis) == {"plan": 3.0} + analysis.planned_views = ("front", "plan", "side") + assert pre_render_view_page_overflow(analysis) == {"plan": 3.0, "side": 3.0} + + +def test_candidate_preview_chooses_one_build_without_gutters_when_views_are_off_page( + monkeypatch, +): + import draftwright.builder as builder + + original_choice = builder.choose_pre_render_profile + original_assemble = builder._assemble + assembly_profiles = [] + + def dense_choice(*args, **kwargs): + return {**original_choice(*args, **kwargs), "profile": "columns"} + + def observe(*args, **kwargs): + assembly_profiles.append(current_layout_profile()) + return original_assemble(*args, **kwargs) + + monkeypatch.setattr(builder, "choose_pre_render_profile", dense_choice) + monkeypatch.setattr(builder, "_assemble", observe) + drawing = build_drawing( + Box(190, 5, 279), + page="A3", + scale=1, + scale_policy="permissive", + annotation_layout="candidate-preview", + ) + + choice = drawing.annotation_scheme_decision["pre_render_choice"] + # Measured repacking may reassemble this same drawing, but every pass must + # use the profile selected before rendering; no baseline profile is built. + assert assembly_profiles + assert all(profile == candidate_profile("iso-growth", 1) for profile in assembly_profiles) + assert choice["proposed_profile"] == "columns" + assert choice["profile"] == "iso-growth" + assert choice["reason"] == "protected_gutters_worsen_off_page_views" + assert ( + choice["selected_view_overflow_mm"]["plan"] < choice["proposed_view_overflow_mm"]["plan"] + ) + assert ( + "view_page_containment" + in drawing.annotation_scheme_decision["safety_evidence"]["failed_checks"] + ) + + def test_candidate_preview_without_automatic_annotations_does_not_apply_profile(): drawing = build_drawing( Box(20, 10, 5), diff --git a/tests/test_pre_render_profile_choice.py b/tests/test_pre_render_profile_choice.py index f18cb6dd7..69047f69e 100644 --- a/tests/test_pre_render_profile_choice.py +++ b/tests/test_pre_render_profile_choice.py @@ -36,7 +36,7 @@ def test_fully_planned_demand_chooses_capped_profile(): auto_dims=True, ) - assert choice["version"] == 4 + assert choice["version"] == 5 assert choice["profile"] == "planned" assert choice["page"] == [297.0, 210.0] assert choice["scale"] == 1.0