Files
OpenNest/OpenNest.Tests/CNC/PlateRapidEnumeratorTests.cs
T
ajandClaude Fable 5.1 8f27eac2ca feat(cutting): plan each scrap cut-off segment where the route passes it
Plates with staged automatic scrap cut-offs could not be planned: P260810-01
plate 1 ran out of budget even at 10x effort, while the same plate plans
Ready in 3.6k expansions without the lines. A cut-off was one fixed program
cut whole before every part its line passes through, which dragged the tour
to the middle of the sheet and left the head at the far edge of the line;
and cutting every line first (the rejected alternative) weakens the sheet
and breaks the containment operators rely on when they fast-forward a
changed program.

A cut-off stays one definition, but Plate.RegenerateCutOffs now makes one
part per trimmed segment, each with its own place in the cut sequence.
CutOffSegments gives each segment the span of the nominal line it stands
for (end of the segment before it to start of the one after), and the
dependency rules of the planner and Part Sequencing use that span, so a
segment precedes only the parts beside it. The free-order search plans the
parts as if the segments were not there, so adding lines no longer changes
a plate's route, then CutOffInsertion slides each segment into the latest
gap before its first part where the rapid into its pierce and the rapid
from its end cross nothing cut before; only when one fits nowhere is the
plate searched with the segments among the parts. The search looks through
segments wherever they sit (entries face the next part, hole routes rank
from the pre-segment position, rapids are still checked from the segment's
end), so an applied plan replanned with the order kept replays Ready.
Segments saved out of consecutive order are recorded per segment in the
.nest file (`segments`); older readers load them consecutively.

P260810-01 plate 1 now plans Ready in 3639 expansions with every check
passing; rapid travel 1836 against 1547 without the lines. Plates without
cut-offs keep their PlanningCharacterizationTests digests, and the
problem-nest corpus has no plate below its baseline.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-22 11:38:38 -04:00

143 lines
5.9 KiB
C#

using OpenNest.CNC;
using OpenNest.Geometry;
using OpenNest.IO;
namespace OpenNest.Tests.CNC;
public class PlateRapidEnumeratorTests
{
[Theory]
[InlineData(1, CutDirection.AwayFromOrigin)]
[InlineData(2, CutDirection.AwayFromOrigin)]
[InlineData(3, CutDirection.AwayFromOrigin)]
[InlineData(4, CutDirection.AwayFromOrigin)]
[InlineData(1, CutDirection.TowardOrigin)]
[InlineData(2, CutDirection.TowardOrigin)]
[InlineData(3, CutDirection.TowardOrigin)]
[InlineData(4, CutDirection.TowardOrigin)]
public void Enumerate_AppliedAutomaticCutoffs_ConnectsFromFinalCut(int quadrant, CutDirection direction)
{
var program = new Program();
program.Codes.Add(new RapidMove(0, 0));
program.Codes.Add(new LinearMove(70, 0));
program.Codes.Add(new LinearMove(70, 20));
program.Codes.Add(new LinearMove(0, 20));
program.Codes.Add(new LinearMove(0, 0));
var drawing = new Drawing("rectangle", program);
var plate = new Plate(81, 120) { Quadrant = quadrant, PartSpacing = 0.5 };
plate.Parts.Add(new Part(drawing,
new Vector(quadrant is 2 or 3 ? -80 : 10, quadrant is 3 or 4 ? -40 : 20)));
var settings = new CutOffSettings { CutDirection = direction, Overtravel = 2 };
var plan = AutomaticCutOffPlanner.Create(plate, new AutomaticCutOffOptions { Spacing = 35 }, settings);
Assert.False(plan.HasBlockingDiagnostics);
Assert.Equal(3, plan.Definitions.Count);
foreach (var definition in plan.Definitions)
plate.CutOffs.Add(definition);
plate.RegenerateCutOffs(settings);
// Two interrupted skeleton cuts followed by an uninterrupted tail separator.
var cutoffs = plate.Parts.Where(p => p.BaseDrawing.IsCutOff).ToArray();
Assert.Equal(new[] { 2, 2, 1 }, plate.CutOffs.Select(c => TestHelpers.CutOffParts(plate, c).Length));
Assert.All(cutoffs, p => Assert.Single(p.Program.Codes.OfType<LinearMove>()));
var before = plate.Parts.Select(p => NestWriter.GetProgramText(p.Program)).ToArray();
var expected = new List<RapidEnumerator.Segment>();
var position = Vector.Zero;
foreach (var part in plate.Parts)
{
Assert.Equal(Mode.Absolute, part.Program.Mode);
foreach (var motion in part.Program.Codes.Cast<Motion>())
{
var destination = part.Location + motion.EndPoint;
if (motion is RapidMove)
expected.Add(new RapidEnumerator.Segment(position, destination));
position = destination;
}
}
var actual = RapidEnumerator.Enumerate(plate.Parts);
Assert.Equal(expected, actual);
Assert.Equal(before, plate.Parts.Select(p => NestWriter.GetProgramText(p.Program)));
plate.RegenerateCutOffs(settings);
Assert.Equal(expected, RapidEnumerator.Enumerate(plate.Parts));
}
[Theory]
[InlineData(Mode.Absolute, false)]
[InlineData(Mode.Incremental, false)]
[InlineData(Mode.Absolute, true)]
[InlineData(Mode.Incremental, true)]
public void Enumerate_OpenProgram_AdvancesPastLastPierce(Mode mode, bool endWithArc)
{
var program = new Program();
program.Codes.Add(new RapidMove(2, 3));
program.Codes.Add(new LinearMove(7, 3));
if (endWithArc)
program.Codes.Add(new ArcMove(8, 4, 7, 4));
program.Mode = mode;
var first = new Part(new Drawing("open path", program), new Vector(100, 200));
var next = NextPart();
var before = NestWriter.GetProgramText(first.Program);
var segments = RapidEnumerator.Enumerate(new[] { first, next });
Assert.Equal(2, segments.Count);
Assert.Equal(new Vector(102, 203), segments[0].To);
Assert.Equal(endWithArc ? new Vector(108, 204) : new Vector(107, 203), segments[1].From);
Assert.Equal(new Vector(12, 23), segments[1].To);
Assert.Equal(before, NestWriter.GetProgramText(first.Program));
}
[Fact]
public void Enumerate_FinalSubprogram_UsesItsCutEndpointForNextPart()
{
var hole = new Program(Mode.Incremental);
hole.Codes.Add(new RapidMove(0.5, 0));
hole.Codes.Add(new LinearMove(0, 0.1));
var program = new Program();
program.Codes.Add(new RapidMove(1, 0));
program.Codes.Add(new LinearMove(2, 0));
program.Codes.Add(new SubProgramCall { Id = 1, Program = hole, Offset = new Vector(2, 2) });
var first = new Part(new Drawing("hole last", program), new Vector(100, 200));
var segments = RapidEnumerator.Enumerate(new[] { first, NextPart() });
Assert.Equal(3, segments.Count);
Assert.Equal(new Vector(102, 200), segments[1].From);
Assert.Equal(new Vector(102.5, 202), segments[1].To);
Assert.Equal(new Vector(102.5, 202.1), segments[2].From);
Assert.Equal(new Vector(12, 23), segments[2].To);
}
[Fact]
public void Enumerate_TrailingRapid_RemainsTheNextPartsStartPosition()
{
var program = new Program();
program.Codes.Add(new RapidMove(1, 0));
program.Codes.Add(new LinearMove(2, 0));
program.Codes.Add(new RapidMove(3, 4));
var first = new Part(new Drawing("park after cut", program), new Vector(100, 200));
var segments = RapidEnumerator.Enumerate(new[] { first, NextPart() });
Assert.Equal(3, segments.Count);
Assert.Equal(new Vector(102, 200), segments[1].From);
Assert.Equal(new Vector(103, 204), segments[1].To);
Assert.Equal(new Vector(103, 204), segments[2].From);
}
[Fact]
public void Enumerate_EmptyPlate_HasNoRapids()
{
Assert.Empty(RapidEnumerator.Enumerate(Array.Empty<Part>()));
}
private static Part NextPart()
{
var program = new Program();
program.Codes.Add(new RapidMove(2, 3));
program.Codes.Add(new LinearMove(4, 3));
return new Part(new Drawing("next", program), new Vector(10, 20));
}
}