fix(task-5): cut faces on the real chord, cross sections that see features, a section with no dashed cut edge

Three defects, all of them the same mistake in three places: the cut face was
built from something other than the geometry it claims to show.

1. A prismatic cut face removed a feature by its BOUNDING BOX. `Hole.removal_box`
   is a square, so the gap punched in the cut face was the full diameter
   wherever the plane passed. On the plan's 80x50x8 plate with a Ho10 hole at
   (40, 25): a plane at y=29.9 grazes the hole (true chord 1.990) and the
   section still showed an 8 mm gap through solid metal - 560.0 mm2 hatched
   against a true 624.08, a 64 mm2 error in the hatched area AND a wrong drawn
   outline. `Slot` had the same obround-as-box. Features now answer
   `cut_spans(part, p1, unit)` with their own exact chord: `_circle_chord` for a
   hole, the union of two circles and a rotated rectangle for a slot, the
   polygon's own crossings for a pocket, and an exact slab clip of
   `removal_box` as the default. `_box_span` (the corner projection, which is
   the box's shadow and not a chord at all) is gone.

2. A transverse revolved section ignored every feature and the honesty rule
   never fired. A cross section through a DIN 6885-1 keyway came out as a plain
   hatched disc with `omitted == ()` - the commonest mechanical cross section
   there is, hatched straight over the 12x5 keyway. The face is now built by
   `_transverse_face`: its radii come from `radial_profile` (so a groove, an
   undercut, a bore or an ISO 6410 internal thread is in the face already,
   which the raw segment diameters could not report), a non-axisymmetric
   feature is asked for its own `cross_section` notch record, and one the plane
   really passes through with no such record is listed in `omitted` with its
   reason rather than hatched over. `Keyway._notch` is the one calculation the
   end view and the cut face both come out of.

3. A section drew its own cut boundary as a HIDDEN (dashed) line, because the
   longitudinal branch reused `silhouette_prims` and the silhouette draws the
   bore dashed - correct for an un-cut view, wrong for a section, where that
   line IS the hatch loop's boundary. ISO 128-50 bounds a surface lying in the
   cutting plane with a continuous wide line and ISO 128-3 leaves hidden detail
   out of a sectional view; via ROLE_LAYER['hidden'] this would also have put
   the cut boundary on the HIDDEN layer in Task 7. New `section_profile_prims`
   emits the cut outline all-continuous, and `half=True` keeps the dashed bore
   on the -Y half only, which is the half a half section does not cut.

And the claim the last commit made without earning it: the hatched area and the
drawn outline are now literally one calculation. The longitudinal loop is the
*emitted* outline flattened (`_flatten_path`), so a chamfer or fillet that
substitutes a silhouette corner is cut out of the hatch exactly where it is
drawn - a 2 mm 45 deg chamfer takes its 2 mm2 off the 1000 mm2 half face
instead of being hatched over.

Eleven new tests, every one of them red on the parent commit: the hole chord at
0/4/4.9 mm off centre, a tangent plane leaving the face whole, the 45 deg slot,
the keyway cross section against its own analytic area, the feature a cross
section cannot project, the feature the plane misses, the groove read off the
profile, the continuous cut boundary, the half section's dashed half, and the
chamfered cut face.

No new ActiveX member is called: all three fixes are pure geometry in the
backend-free `engineering/mech` package.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
This commit is contained in:
Umut CAN
2026-09-22 22:16:47 +03:00
co-authored by Claude Opus 5
parent 72bd17d024
commit 978c1075ad
4 changed files with 626 additions and 73 deletions
+159 -2
View File
@@ -12,9 +12,19 @@ import math
import pytest
from engineering.mech.features import AxialBore, Hole, Pocket, Thread
from engineering.mech.features import (
AxialBore,
Chamfer,
CrossHole,
Hole,
Keyway,
Pocket,
RetainingGroove,
Slot,
Thread,
)
from engineering.mech.part import PrismaticPart, RevolvedPart, Segment
from engineering.mech.primitives import Circle, HatchArea, Text
from engineering.mech.primitives import Arc, Circle, HatchArea, Line, Text
from engineering.mech.views import (
CIRCLE_SEGMENTS,
SECTION_PLANE,
@@ -124,6 +134,55 @@ def test_iso6410_an_internal_thread_hatch_runs_to_the_major_diameter():
assert min(y for x, y in upper if x > 25.0 + EPS) == pytest.approx(8.0)
def test_a_section_draws_its_own_cut_boundary_continuous_and_never_dashed():
"""ISO 128-50: a surface lying in the cutting plane is bounded by a
continuous wide line, and ISO 128-3 leaves hidden detail out of a sectional
view. The bore wall is the case that catches a section built out of the
un-cut silhouette: there the material hides the bore, so the silhouette
draws it dashed - but y = +-10 is the hatch loop's own boundary, and a
dashed line drawn on the hatch boundary means the outline and the hatched
area disagree at identical coordinates.
"""
part = RevolvedPart(name="sleeve", segments=(Segment(30.0, 40.0, 20.0),))
view = build_view(part, "section", plane=axis_plane(), style="full")
assert "hidden" not in {getattr(prim, "role", None) for prim in view.prims}
bore = [
line
for line in view.prims
if isinstance(line, Line)
and abs(abs(line.p1[1]) - 10.0) < EPS
and abs(abs(line.p2[1]) - 10.0) < EPS
]
assert len(bore) == 2
assert {line.role for line in bore} == {"visible"}
(upper, _), (lower, _) = cut_loops(part, axis_plane(), style="full")
assert min(y for _, y in upper) == pytest.approx(10.0)
assert max(y for _, y in lower) == pytest.approx(-10.0)
def test_a_half_section_keeps_its_dashed_bore_only_on_the_half_it_does_not_cut():
part = RevolvedPart(name="sleeve", segments=(Segment(30.0, 40.0, 20.0),))
view = build_view(part, "section", plane=axis_plane(style="half"), style="half")
hidden = [prim for prim in view.prims if getattr(prim, "role", None) == "hidden"]
assert hidden, "the un-sectioned half still shows its bore, dashed"
assert all(line.p1[1] < 0.0 and line.p2[1] < 0.0 for line in hidden)
assert all(abs(abs(line.p1[1]) - 10.0) < EPS for line in hidden)
# the cut half carries the hatch, and nothing dashed on it
((upper, _),) = cut_loops(part, axis_plane(style="half"), style="half")
assert min(y for _, y in upper) == pytest.approx(10.0)
def test_a_chamfer_is_cut_out_of_the_hatched_area_exactly_where_it_is_drawn():
"""The loop is the emitted outline flattened, so a corner substitution
reaches the cut face too: a 2 mm 45 deg chamfer takes a 2 mm2 triangle off
the 1000 mm2 half face, and a loop built from the raw profile would hatch
over the chamfer it just drew."""
(upper, _), _lower = cut_loops(
shaft(Chamfer(id="c1", at="start", size=2.0)), axis_plane(), style="full"
)
assert polygon_area(upper) == pytest.approx(20 * 12.5 + 40 * 15.0 + 15 * 10.0 - 2.0)
def test_the_section_view_carries_a_hatch_area_per_loop_with_the_parts_material():
view = build_view(shaft(), "section", plane=axis_plane(), style="full")
hatches = [p for p in view.prims if isinstance(p, HatchArea)]
@@ -177,6 +236,66 @@ def test_a_revolved_style_cross_section_draws_its_boundary_as_phantom():
assert {c.role for c in circles} == {"phantom"}
def keyed_shaft(*features) -> RevolvedPart:
"""A plain 40 mm shaft; DIN 6885-1 gives its keyway 12 mm x 5 mm deep."""
return RevolvedPart(name="keyed", segments=(Segment(60.0, 40.0),), features=tuple(features))
def test_a_cross_section_through_a_keyway_cuts_the_notch_out_of_the_disc():
"""The commonest mechanical cross section there is. The plane at x=20 is
squarely inside the slot, so the face is a notched disc - not the plain
hatched circle a face built from the segment diameter alone produces."""
keyway = Keyway(id="kw", segment=0, length=30.0, offset=5.0)
plane = normalise_plane({"p1": (20.0, -40.0), "p2": (20.0, 40.0)})
view = build_view(keyed_shaft(keyway), "section", plane=plane, style="full")
assert view.omitted == ()
arcs = [prim for prim in view.prims if isinstance(prim, Arc)]
assert len(arcs) == 1 and arcs[0].radius == pytest.approx(20.0)
assert not [prim for prim in view.prims if isinstance(prim, Circle)]
radius, half, floor = 20.0, 6.0, 15.0
top = math.sqrt(radius * radius - half * half)
# the disc, less the notch rectangle, less the cap the slot mouth removes
cap = radius * radius * math.acos(top / radius) - top * half
expected = math.pi * radius * radius - 2.0 * half * (top - floor) - cap
(hatch,) = [prim for prim in view.prims if isinstance(prim, HatchArea)]
assert polygon_area(hatch.loops[0]) == pytest.approx(expected, rel=2.1e-4)
# and it really is a notch: the whole disc is 56 mm2 larger
assert polygon_area(hatch.loops[0]) < math.pi * radius * radius - 50.0
# the arc the face is bounded by is the arc the end view draws
assert arcs[0].start_deg == pytest.approx(math.degrees(math.atan2(top, -half)) % 360.0)
assert arcs[0].end_deg == pytest.approx(math.degrees(math.atan2(top, half)) % 360.0)
def test_a_cross_section_reports_the_feature_it_cannot_project():
"""The honesty rule, on the one view where silence is indistinguishable
from a correct drawing: the face is hatched either way."""
cross = CrossHole(id="xh", x=20.0, diameter=6.0)
plane = normalise_plane({"p1": (20.0, -40.0), "p2": (20.0, 40.0)})
view = build_view(keyed_shaft(cross), "section", plane=plane, style="full")
assert [entry["feature"] for entry in view.omitted] == ["xh"]
assert "transverse" in view.omitted[0]["reason"]
def test_a_feature_the_transverse_plane_misses_is_reported_as_off_the_plane():
keyway = Keyway(id="kw", segment=0, length=30.0, offset=5.0)
plane = normalise_plane({"p1": (50.0, -40.0), "p2": (50.0, 40.0)})
view = build_view(keyed_shaft(keyway), "section", plane=plane, style="full")
assert [entry["feature"] for entry in view.omitted] == ["kw"]
assert "not on the cutting plane" in view.omitted[0]["reason"]
def test_a_transverse_face_is_read_off_the_radial_profile_not_the_segment():
"""A groove is in the profile, so it is in the cross section - at its own
reduced diameter, and without being reported missing."""
groove = RetainingGroove(id="rg", x=30.0, d=40.0, m=1.85, d2=37.5)
plane = normalise_plane({"p1": (30.0, -40.0), "p2": (30.0, 40.0)})
view = build_view(keyed_shaft(groove), "section", plane=plane, style="full")
assert view.omitted == ()
circles = [prim for prim in view.prims if isinstance(prim, Circle)]
assert {round(circle.radius, 6) for circle in circles} == {37.5 / 2.0}
def test_a_half_transverse_section_is_refused_with_its_reason():
part = RevolvedPart(name="sleeve", segments=(Segment(30.0, 40.0, 20.0),))
plane = normalise_plane({"p1": (15.0, -30.0), "p2": (15.0, 30.0)})
@@ -205,6 +324,44 @@ def test_a_through_hole_splits_the_cut_face_in_two():
assert total == pytest.approx((80.0 - 10.0) * 8.0)
@pytest.mark.parametrize("offset", [0.0, 4.0, 4.9])
def test_a_hole_is_cut_on_its_chord_and_not_on_the_box_around_it(offset):
"""The gap a hole punches in a cut face is the chord the plane actually
cuts, and it shrinks to nothing as the plane leaves the centre. Removing
the hole by its bounding box reports the full 10 mm diameter at every
offset: 32 mm2 of missing cut face at 4 mm off centre and 64 mm2 at 4.9,
with an 8 mm gap drawn through solid metal where the plane merely grazes
the hole."""
hole = Hole(id="h1", x=40.0, y=25.0, diameter=10.0)
plane = normalise_plane({"p1": (-10.0, 25.0 + offset), "p2": (100.0, 25.0 + offset)})
view = build_view(plate(hole), "section", plane=plane, style="full")
chord = 2.0 * math.sqrt(5.0**2 - offset**2)
total = sum(polygon_area(prim.loops[0]) for prim in view.prims if isinstance(prim, HatchArea))
assert total == pytest.approx((80.0 - chord) * 8.0)
if offset > EPS:
assert total != pytest.approx((80.0 - 10.0) * 8.0)
def test_a_plane_tangent_to_a_hole_leaves_the_cut_face_whole():
hole = Hole(id="h1", x=40.0, y=25.0, diameter=10.0)
plane = normalise_plane({"p1": (-10.0, 30.0), "p2": (100.0, 30.0)})
view = build_view(plate(hole), "section", plane=plane, style="full")
hatches = [prim for prim in view.prims if isinstance(prim, HatchArea)]
assert len(hatches) == 1
assert polygon_area(hatches[0].loops[0]) == pytest.approx(80.0 * 8.0)
def test_an_angled_slot_is_cut_on_its_obround_and_not_on_the_box_around_it():
"""A 45 degree slot's bounding box is 24.14 mm across the cutting line; the
slot itself is 14.14. The box would erase 10 mm of material the slot never
touches."""
slot = Slot(id="s1", x=40.0, y=25.0, length=30.0, width=10.0, angle=45.0)
plane = normalise_plane({"p1": (-10.0, 25.0), "p2": (100.0, 25.0)})
view = build_view(plate(slot), "section", plane=plane, style="full")
total = sum(polygon_area(prim.loops[0]) for prim in view.prims if isinstance(prim, HatchArea))
assert total == pytest.approx((80.0 - 10.0 * math.sqrt(2.0)) * 8.0)
def test_a_pocket_reduces_the_cut_face_to_the_remaining_thickness():
pocket = Pocket(
id="p1",