diff --git a/OpenNest.Core/AutomaticCutOffPlanner.cs b/OpenNest.Core/AutomaticCutOffPlanner.cs index bc7f5e4a..a8c8a78e 100644 --- a/OpenNest.Core/AutomaticCutOffPlanner.cs +++ b/OpenNest.Core/AutomaticCutOffPlanner.cs @@ -49,6 +49,7 @@ public enum AutomaticCutOffDiagnosticCode ShiftedCut, HorizontalRemnant, NoSafeHorizontalSeparator, + ClampMovedTailSeparator, } public sealed record AutomaticCutOffDiagnostic( @@ -167,7 +168,21 @@ public static class AutomaticCutOffPlanner occupied = System.Math.Min(occupied, length); occupiedY = System.Math.Min(occupiedY, width); var margin = System.Math.Max(plate.PartSpacing, settings.PartClearance) + GeometryTolerance; - var separator = occupied + margin; + var nominalSeparator = occupied + margin; + // A separator on a clamp moves just past it, away from the origin: the tail is a + // little shorter, but a clamp in the way no longer costs the whole remnant. Repeat + // for a clamp whose clearance the moved line now meets. + var separator = nominalSeparator; + ClampZone movedPast = null; + for (var pass = 0; pass <= clamps.Count && double.IsFinite(separator); pass++) + { + var blocking = clamps.FirstOrDefault(zone => CrossesClamp(zone, sign * separator)); + if (blocking == null) + break; + movedPast = blocking; + var farEdge = sign > 0 ? blocking.Bounds.Right : -blocking.Bounds.Left; + separator = farEdge + plate.ClampClearance + GeometryTolerance; + } var hasTailCandidate = double.IsFinite(separator) && separator < length - GeometryTolerance; var diagnostics = new List(); @@ -306,7 +321,14 @@ public static class AutomaticCutOffPlanner } } if (keepsTail) + { + if (movedPast != null) + diagnostics.Add(new AutomaticCutOffDiagnostic( + AutomaticCutOffDiagnosticCode.ClampMovedTailSeparator, + $"The tail separator moved from {sign * nominalSeparator:G8} to {sign * separator:G8} " + + $"to clear clamp '{movedPast.Id}' (including its clearance).", X: sign * separator)); AddCandidate(sign * separator, true); + } else if (hasTailCandidate) diagnostics.Add(new AutomaticCutOffDiagnostic( AutomaticCutOffDiagnosticCode.TailBelowMinimum, diff --git a/OpenNest.Tests/CutOffs/AutomaticCutOffPlannerTests.cs b/OpenNest.Tests/CutOffs/AutomaticCutOffPlannerTests.cs index 939534bc..17ee064a 100644 --- a/OpenNest.Tests/CutOffs/AutomaticCutOffPlannerTests.cs +++ b/OpenNest.Tests/CutOffs/AutomaticCutOffPlannerTests.cs @@ -37,6 +37,14 @@ public class AutomaticCutOffPlannerTests private static double[] Positions(AutomaticCutOffPlan plan) => plan.Definitions.Select(c => c.Position.X).ToArray(); + private static void AssertPositions(double[] expected, AutomaticCutOffPlan plan) + { + var actual = Positions(plan); + Assert.Equal(expected.Length, actual.Length); + for (var index = 0; index < expected.Length; index++) + Assert.Equal(expected[index], actual[index], 9); + } + private static (Vector From, Vector To)[] Segments(Program program) => Enumerable.Range(0, program.Codes.Count / 2).Select(i => ( Assert.IsType(program.Codes[2 * i]).EndPoint, @@ -226,19 +234,22 @@ public class AutomaticCutOffPlannerTests [InlineData(2)] [InlineData(3)] [InlineData(4)] - public void ClampWall_SkipsNominalLineAndRefusesTailSeparator(int quadrant) + public void ClampWall_SkipsNominalLineAndMovesTailSeparatorPastClamp(int quadrant) { var plate = MakePlate(quadrant); var sign = quadrant is 2 or 3 ? -1 : 1; plate.EdgeSpacing = new Spacing(0.25, 0.25); // Real rail zones are stored in the sheet's lower-left frame in every quadrant. + // The tail clamp covers 80.6..80.8; with 0.25 clearance the separator moves to 81.05. plate.SetClampRail(new ClampRail(0.2, 0.625, new[] { 35.2, 80.7 })); var before = plate.Parts.ToArray(); var plan = Plan(plate); - Assert.Equal(new[] { sign * 70.0 }, Positions(plan)); - Assert.False(plan.HasSeparatedTail); - Assert.Equal(0, plan.TailLength); - Assert.Contains(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.NoSafeTailSeparator); + var separator = 81.05 + Tolerance.Epsilon; + AssertPositions(new[] { sign * 70.0, sign * separator }, plan); + Assert.True(plan.HasSeparatedTail); + Assert.Equal(120 - separator, plan.TailLength, 9); + Assert.Contains(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.ClampMovedTailSeparator); + Assert.DoesNotContain(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.NoSafeTailSeparator); Assert.Equal(before, plate.Parts.ToArray()); Assert.Empty(plate.CutOffs); } @@ -257,7 +268,7 @@ public class AutomaticCutOffPlannerTests } [Fact] - public void ExistingLineAcrossClampCannotCertifyTail_AndPartialFootprintBlocks() + public void ExistingLineAcrossClampCannotCertifyTail_SeparatorMovesPastClamp() { var plate = MakePlate(); plate.EdgeSpacing = new Spacing(0.25, 0.25); @@ -267,9 +278,10 @@ public class AutomaticCutOffPlannerTests existing.Regenerate(plate, new CutOffSettings()); var original = existing.Drawing.Program; var plan = Plan(plate); - Assert.False(plan.HasSeparatedTail); - Assert.Null(plan.TailSeparatorX); - Assert.Contains(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.NoSafeTailSeparator); + // The existing line is not certified; a new separator past the clamp keeps the tail. + Assert.Equal(81.05 + Tolerance.Epsilon, plan.TailSeparatorX!.Value, 9); + Assert.Equal(plan.TailSeparatorX, plan.Definitions[^1].Position.X); + Assert.DoesNotContain(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.ExistingCutOff); Assert.Same(original, existing.Drawing.Program); Assert.Same(existing, Assert.Single(plate.CutOffs)); } @@ -310,10 +322,11 @@ public class AutomaticCutOffPlannerTests [InlineData(2)] [InlineData(3)] [InlineData(4)] - public void RailClampsBlockGridAndTailInEveryQuadrant(int quadrant) + public void RailClampsBlockGridAndMoveTailInEveryQuadrant(int quadrant) { // Rail clamps centered 35 and 80.5 from the origin, 1.5 wide, plus 0.25 clearance: - // the grid line at 35 and the separator at 80.50001 both cross a clamp. + // the grid line at 35 is skipped and the separator at 80.50001 moves past the + // clamp's clearance (79.75..81.25 + 0.25) to 81.50001. var plate = MakePlate(quadrant); var sign = quadrant is 2 or 3 ? -1 : 1; plate.EdgeSpacing = new Spacing(0.25, 0.25); @@ -321,10 +334,46 @@ public class AutomaticCutOffPlannerTests var plan = Plan(plate); - Assert.Equal(new[] { sign * 70.0 }, Positions(plan)); + AssertPositions(new[] { sign * 70.0, sign * (81.5 + Tolerance.Epsilon) }, plan); + Assert.True(plan.HasSeparatedTail); + Assert.Single(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.ClampBlockedCut); + var moved = Assert.Single(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.ClampMovedTailSeparator); + Assert.Equal(plan.TailSeparatorX, moved.X); + } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(4)] + public void TailSeparatorStepsPastAdjacentClamps(int quadrant) + { + // Moving past the first clamp (79.75..81.25) lands in the second one's clearance + // (81.6..83.1 less 0.25), so the separator steps past both to 83.35001. + var plate = MakePlate(quadrant); + var sign = quadrant is 2 or 3 ? -1 : 1; + plate.EdgeSpacing = new Spacing(0.25, 0.25); + plate.SetClampRail(new ClampRail(1.5, 0.625, new[] { 80.5, 82.35 })); + + var plan = Plan(plate); + + Assert.Equal(sign * (83.35 + Tolerance.Epsilon), plan.TailSeparatorX!.Value, 9); + } + + [Fact] + public void ClampMovedTailBelowMinimumKeepsNoTail() + { + // Past the clamp the tail is 120 - 81.50001 = 38.49999, under a 38.6 minimum. + var plate = MakePlate(); + plate.EdgeSpacing = new Spacing(0.25, 0.25); + plate.SetClampRail(new ClampRail(1.5, 0.625, new[] { 80.5 })); + + var plan = AutomaticCutOffPlanner.Create(plate, + new AutomaticCutOffOptions { Spacing = 35, MinimumTailLength = 38.6 }, new CutOffSettings()); + Assert.False(plan.HasSeparatedTail); - Assert.Equal(2, plan.Diagnostics.Count(d => d.Code == AutomaticCutOffDiagnosticCode.ClampBlockedCut)); - Assert.Contains(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.NoSafeTailSeparator); + Assert.Contains(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.TailBelowMinimum); + Assert.DoesNotContain(plan.Diagnostics, d => d.Code == AutomaticCutOffDiagnosticCode.ClampMovedTailSeparator); } [Theory] @@ -363,11 +412,10 @@ public class AutomaticCutOffPlannerTests Assert.Empty(plan.Definitions); } - [Theory] - [InlineData(35)] - [InlineData(80.50001)] - public void SameLineLimitedManualCutOnClamp_StillBlocksWholePlan(double x) + [Fact] + public void SameLineLimitedManualCutOnClamp_StillBlocksWholePlan() { + const double x = 35; var plate = MakePlate(); plate.ClampZones.Add(new ClampZone("under-manual", new Box(x - 0.75, 0, 1.5, 0.625))); var manual = new CutOff(new Vector(x, 0), CutOffAxis.Vertical) { StartLimit = 10, EndLimit = 60 }; diff --git a/docs/automatic-scrap-cutoffs.md b/docs/automatic-scrap-cutoffs.md index cf43d117..04d6da6c 100644 --- a/docs/automatic-scrap-cutoffs.md +++ b/docs/automatic-scrap-cutoffs.md @@ -22,7 +22,7 @@ Only real parts contribute to the occupied envelope; existing cutoff parts do no 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. -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. A tolerance-close existing line is tested at its own X as well as the proposed X, because that existing line is what would be certified. A same-line manual cut with different limits still blocks the whole plan, even on a clamp. Blocked lines are skipped with a clamp diagnostic; a blocked tail also reports `NoSafeTailSeparator` rather than claiming separation. 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. +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. A tolerance-close existing line is tested at its own X as well as the proposed X, because that existing line is what would be certified. A same-line manual cut with different limits 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. For a 120-by-81-inch sheet whose parts extend through 80 inches, the fixed grid would put skeleton lines at 35 and 70 inches, followed by a separator beyond 80 inches at the required margin. With a 17.5-inch minimum strip, the planner can move those two lines toward the origin to shorten their trimmed cut segments. The separator does not move, and there is no line at 105 inches through the retained tail.