From 4cacddc059f77a2839aa4f011aa5692feba52071 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 2 Dec 2026 13:33:22 -0500 Subject: [PATCH] fix(cutoffs): hull open outlines from their arcs, not whole circles A cut-off beside a part whose outline is open (a tab gap) is trimmed against the part's convex hull. CollectPoints gave each arc its whole circle's quadrant points, so a shallow arc stretched the hull far past the part: a radius-15 lug bottom trimmed a full-height cut-off to the top of the part (N26-2EV) and a concave arc split another in two (N26-2F2). Loading a nest regenerates cut-offs from the tabbed programs, so the cut-off looked broken after save and load. The hull now takes points on each arc and circumscribed ones just outside it (within 0.001), so it still holds every arc. CollectPoints is unchanged because FindBestRotation relies on it. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/Plate.CutOffs.cs | 39 ++++++++++++- OpenNest.Tests/CutOffs/CutOffGeometryTests.cs | 58 +++++++++++++++++++ docs/automatic-scrap-cutoffs.md | 2 +- 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/OpenNest.Core/Plate.CutOffs.cs b/OpenNest.Core/Plate.CutOffs.cs index d1b16288..0f1d073f 100644 --- a/OpenNest.Core/Plate.CutOffs.cs +++ b/OpenNest.Core/Plate.CutOffs.cs @@ -266,7 +266,7 @@ namespace OpenNest return perimeter; } - var points = outline.CollectPoints(); + var points = HullPoints(outline); if (points.Count < 3) return null; @@ -279,5 +279,42 @@ namespace OpenNest return null; } } + + // How far an open outline's hull may stand off its arcs. + private const double HullArcTolerance = 0.001; + + /// + /// Points whose convex hull holds every entity and stays within + /// of its arcs. CollectPoints adds each arc's + /// whole-circle quadrant points, which a shallow arc never reaches: a radius-15 arc + /// on a 4-wide part would stretch the hull 15 past the part and trim every cut-off + /// beside it. Each arc gives its points on the arc and the circumscribed ones just + /// outside, so every piece between them lies inside the hull. + /// + private static List HullPoints(IEnumerable entities) + { + var points = new List(); + foreach (var entity in entities) + { + switch (entity) + { + case Arc arc: + var segments = arc.SegmentsForTolerance(HullArcTolerance); + points.AddRange(arc.ToPoints(segments)); + points.AddRange(arc.ToPoints(segments, circumscribe: true)); + break; + case Circle circle: + points.AddRange(circle.ToPoints(circle.SegmentsForTolerance(HullArcTolerance), circumscribe: true)); + break; + case Shape shape: + points.AddRange(HullPoints(shape.Entities)); + break; + default: + points.AddRange(new[] { entity }.CollectPoints()); + break; + } + } + return points; + } } } diff --git a/OpenNest.Tests/CutOffs/CutOffGeometryTests.cs b/OpenNest.Tests/CutOffs/CutOffGeometryTests.cs index 13f63b0f..1f9f32fb 100644 --- a/OpenNest.Tests/CutOffs/CutOffGeometryTests.cs +++ b/OpenNest.Tests/CutOffs/CutOffGeometryTests.cs @@ -290,6 +290,64 @@ public class CutOffGeometryTests Assert.Equal(6, cutoff.Drawing.Program.Codes.Count); } + // A 4 x 5 outline left open by a 0.06 tab, its bottom a shallow arc (radius about 15) + // bowing up into the part, like a lifting lug with a tab in its perimeter. + private static Program MakeTabbedShallowArcPart() + { + var pgm = new Program(); + pgm.Codes.Add(new RapidMove(new Vector(4, 0.06))); + pgm.Codes.Add(new LinearMove(new Vector(4, 5))); + pgm.Codes.Add(new LinearMove(new Vector(0, 5))); + pgm.Codes.Add(new LinearMove(new Vector(0, 0))); + pgm.Codes.Add(new ArcMove(new Vector(4, 0), new Vector(2, -15), RotationType.CW)); + return pgm; + } + + [Fact] + public void OpenOutline_HullStaysWithinTheShallowArc() + { + var part = new Part(new Drawing("lug", MakeTabbedShallowArcPart())); + + var hull = Plate.BuildPartPerimeter(part); + + // An arc's hull reaches only as far as the arc does, not its whole circle's quadrants. + Assert.IsType(hull); + Assert.Equal(0, hull.BoundingBox.Left, 3); + Assert.Equal(4, hull.BoundingBox.Right, 3); + Assert.Equal(0, hull.BoundingBox.Bottom, 3); + Assert.Equal(5, hull.BoundingBox.Top, 3); + } + + [Fact] + public void OpenOutline_CircumscribesItsArcs() + { + // An open half circle bulging past its endpoints: the hull holds the whole arc. + var pgm = new Program(); + pgm.Codes.Add(new RapidMove(new Vector(0, 0))); + pgm.Codes.Add(new LinearMove(new Vector(0, -1))); + pgm.Codes.Add(new ArcMove(new Vector(0, 1), new Vector(0, 0))); + var part = new Part(new Drawing("d", pgm)); + + var hull = Plate.BuildPartPerimeter(part); + + Assert.True(hull.BoundingBox.Right >= 1 - OpenNest.Math.Tolerance.Epsilon); + Assert.True(hull.BoundingBox.Right < 1.01); + } + + [Fact] + public void Regenerate_BesideOpenShallowArcPart_CutsFullLength() + { + var plate = new Plate(100, 100); + plate.Parts.Add(new Part(new Drawing("lug", MakeTabbedShallowArcPart()))); + var cutoff = new CutOff(new Vector(5, 0), CutOffAxis.Vertical); + plate.CutOffs.Add(cutoff); + + plate.RegenerateCutOffs(new CutOffSettings { PartClearance = 0.02 }); + + var segment = Assert.Single(plate.Parts, p => p.BaseDrawing.IsCutOff); + Assert.Equal((0.0, 100.0), CutOffSegments.Extent(segment, vertical: true)); + } + [Fact] public void CollectPoints_LinesAndArcs_ReturnsAllPoints() { diff --git a/docs/automatic-scrap-cutoffs.md b/docs/automatic-scrap-cutoffs.md index 141e261f..3ca52152 100644 --- a/docs/automatic-scrap-cutoffs.md +++ b/docs/automatic-scrap-cutoffs.md @@ -31,7 +31,7 @@ The same margin is the *cut clearance*: no new line is cut within it of a parall At the default 0.02-inch cut-off clearance and zero part spacing the cut clearance is 0.02 inch; with no clearance and no part spacing it falls to the numerical tolerance, where only same-line cuts match. -Cutoff trimming measures clearance from the part contour, excluding tagged lead-in and lead-out moves when building the closed outline. Leads must not turn a recessed outline into a convex hull and leave oversized gaps elsewhere. Genuinely open contours (a tab gap, for example) still use the conservative hull/bounds fallback, but the hull is built from the material contour only: a cut-off may cross a lead, and hulling a lead's pierce point stopped cut-offs far short of the part. With a nonzero clearance, a line passing exactly through a corner of the clearance outline is trimmed to the outline like any other line. Automatic strip lines sit on a regular lattice and often land on such corners; they once fell back to the part's bounding box there, and the strip search preferred those lines because the box fallback measured as less cutting. The conservative curve/offset approximation can leave a small extra gap (about 0.0003 in on a straight edge at the default 0.02-in clearance), not an extra quarter inch. +Cutoff trimming measures clearance from the part contour, excluding tagged lead-in and lead-out moves when building the closed outline. Leads must not turn a recessed outline into a convex hull and leave oversized gaps elsewhere. Genuinely open contours (a tab gap, for example) still use the conservative hull/bounds fallback, but the hull is built from the material contour only: a cut-off may cross a lead, and hulling a lead's pierce point stopped cut-offs far short of the part. Its arcs are hulled from points on and just outside each arc (within 0.001), never from the arc's whole-circle quadrant points: a shallow arc's circle can reach far past the part, and those points once trimmed a cut-off beside a tabbed part to the top of its bounding box. With a nonzero clearance, a line passing exactly through a corner of the clearance outline is trimmed to the outline like any other line. Automatic strip lines sit on a regular lattice and often land on such corners; they once fell back to the part's bounding box there, and the strip search preferred those lines because the box fallback measured as less cutting. The conservative curve/offset approximation can leave a small extra gap (about 0.0003 in on a straight edge at the default 0.02-in clearance), not an extra quarter inch. If no safe separator fits inside the sheet, there is no retained-tail claim and the nominal cuts may cover the full sheet length. A proposed tail is not reported as separated unless its generated program spans the full sheet width. An active clamp blocks any proposed full-width line whose X coordinate falls in the clamp's whole footprint plus its clamp clearance, including a nominal grid line or an equivalent existing line. An existing line within the cut clearance is tested at its own X as well as the proposed X, because that existing line is what would be certified. A manual cut with different limits on or beside the line still blocks the whole plan, even on a clamp. Blocked strip lines are skipped with a clamp diagnostic. A tail separator that would cross a clamp moves just past it instead, away from the origin, to the far edge of the clamp's footprint plus its clearance (stepping past any clamp whose clearance it then meets), and reports `ClampMovedTailSeparator`. The tail it keeps is shorter by that distance and must still meet the minimum tail length. A separator pushed off the sheet, or blocked another way, reports `NoSafeTailSeparator` rather than claiming separation. On a 120-inch sheet with parts through 100 inches and rail clamps every 20 inches (1.5 wide, 0.25 clearance), the separator at 100.25 sits on the clamp spanning 99.25 to 100.75, so it moves to 101 and keeps a 19-inch tail. Removed clamps on a sheet are absent from its active zones and do not block cuts. Stored zones are relative to the sheet's lower-left corner; the planner maps them into the plate's quadrant frame (the same frame as part and cut-off coordinates) before comparing, so the rule holds in all four quadrants. Obstructed or minimum-length-filtered lines produce diagnostics; they are not proof of a completed partition.