From ee09c8368e98587a038b7bd29cb73ff96c1163a9 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 2 Dec 2026 10:11:05 -0500 Subject: [PATCH] fix(planning): tour a head-locked separator from the end it is cut from The free part order costed every cut-off from its origin-side start, though a head-locked far-edge segment may be cut from either end. Beside a sheet separator the tour therefore ended on the parts near the origin side, and on N26-2EU (four flange rings beside a head-locked scrap separator) the search learned rules from those orders until no order ended on the ring beside the far-edge start. It ran out of budget and the batch fell back to the current order, which was Ready only because the right ring happened to be last. Each reversible cut-off is now also costed from its far-edge end, and the tour keeps that end when it is shorter. N26-2EU plans Ready with a new order in under 1,000 expansions. Plates without head-locked cut-offs get the same tour as before. Co-Authored-By: Claude Opus 5.5 --- .../CuttingPlanning/JointCuttingPlanSearch.cs | 46 +++++++++++++++++- .../HeadLockedCutOffPlanningTests.cs | 47 +++++++++++++++---- docs/automatic-scrap-cutoffs.md | 2 +- docs/cutting-planner.md | 10 ++-- 4 files changed, 89 insertions(+), 16 deletions(-) diff --git a/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs b/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs index a8cddac5..de09c66e 100644 --- a/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs +++ b/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs @@ -138,7 +138,7 @@ internal static class JointCuttingPlanSearch var sequence = (prerequisites.All(p => p.Count == 0) && extents.All(e => e != null) && !snapshot.Placements.Any(p => p.IsCutOff) ? CuttingGridOrder.Plan(extents, snapshot.StartPoint, token) : null) - ?? CuttingNestedOrder.Plan(planned, snapshot.StartPoint, nesting); + ?? Tour(snapshot, planned, snapshot.StartPoint, nesting); Node resume = null; // Swaps learned so far, newest last, and swaps taken back: a swap is a guess, and a // stall after it can mean the swapped pair is what blocks now. @@ -241,7 +241,7 @@ internal static class JointCuttingPlanSearch // order; when another part follows it now, it is planned again, in the same // place, facing that one. var back = attempt.Crossed.Min(part => Array.IndexOf(sequence, part)); - var rest = CuttingNestedOrder.Plan(sequence[back..], attempt.BoundaryAt(back).Position, nesting); + var rest = Tour(snapshot, sequence[back..], attempt.BoundaryAt(back).Position, nesting); var replan = back > 0 && rest.Length != 0 && rest[0] != sequence[back] ? back - 1 : back; resume = attempt.BoundaryAt(replan); sequence = [.. sequence.Take(back), .. rest]; @@ -309,6 +309,48 @@ internal static class JointCuttingPlanSearch } } + // The proposed order of these parts. A head-locked far-edge cut-off may be cut from either end, + // and the walk tries the nearer end first, so the tour may reach it at either: each such + // cut-off in turn is costed from its other end too, and keeps that end when the tour is + // shorter. The parts before a sheet separator then finish beside the end it can be cut from, + // rather than beside its origin-side start when that start lies beyond parts already cut. + private static int[] Tour(CuttingPlanSnapshot snapshot, IReadOnlyList parts, Vector start, + CuttingNestedOrder.Context nesting) + { + var order = CuttingNestedOrder.Plan(parts, start, nesting); + if (nesting.Exits == null) + return order; + var length = Length(order, start, nesting); + foreach (var cut in parts.Where(part => snapshot.Placements[part].Reversed != null).Order()) + { + var reversed = snapshot.Placements[cut].Reversed; + var arrivals = nesting.Centers.ToArray(); + var exits = nesting.Exits.ToArray(); + arrivals[cut] = Arrival(reversed); + exits[cut] = Exit(reversed); + var flipped = nesting with { Centers = arrivals, Exits = exits }; + var candidate = CuttingNestedOrder.Plan(parts, start, flipped); + var candidateLength = Length(candidate, start, flipped); + if (candidateLength >= length - 1e-9) + continue; + (order, length, nesting) = (candidate, candidateLength, flipped); + } + return order; + } + + // The tour's modeled travel: from the start to each part's arrival, then from its exit onward. + private static double Length(IReadOnlyList order, Vector start, CuttingNestedOrder.Context nesting) + { + var length = 0.0; + var position = start; + foreach (var part in order) + { + length += position.DistanceTo(nesting.Centers[part]); + position = (nesting.Exits ?? nesting.Centers)[part]; + } + return length; + } + // Captured containment (never learned rules) and each host's placed cut contours. private static CuttingNestedOrder.Context NestedOrder(CuttingPlanSnapshot snapshot, Vector[] centers, HashSet[] prerequisites, CancellationToken token) diff --git a/OpenNest.Tests/CuttingPlanning/HeadLockedCutOffPlanningTests.cs b/OpenNest.Tests/CuttingPlanning/HeadLockedCutOffPlanningTests.cs index 7d219865..bdb9a816 100644 --- a/OpenNest.Tests/CuttingPlanning/HeadLockedCutOffPlanningTests.cs +++ b/OpenNest.Tests/CuttingPlanning/HeadLockedCutOffPlanningTests.cs @@ -117,6 +117,44 @@ public class HeadLockedCutOffPlanningTests // start left it at its bottom, and the rapid back up to the far edge ran over the ring just cut. [Fact] public void Plan_PartBeforeAHeadLockedSeparator_FacesTheFarEdgeEndItIsCutFrom() + { + var plate = FlangeRingsBesideASeparator(); + + var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, confirmedParameters: FlangeParameters(), + preservePartOrder: true)); + + Assert.True(result.Status == CuttingPlanStatus.Ready && result.IndependentlyReplayed, Describe(result)); + var last = result.ProposedOrder[^1]; + Assert.True(last.IsCutOff && last.ReversesCutOff); + Assert.Same(plate.Parts[3], result.ProposedOrder[^2].SourcePart); + } + + // The same plate with a free order. Costed from the separator's origin-side start, the tour + // ended on the bottom rings, whose rapid to either start crosses rings already cut; the search + // ran out and kept the current order. Costed from its far-edge end too, which the head lock + // lets it be cut from, the tour finishes on the top right ring and the new order plans Ready. + [Fact] + public void Plan_FreeOrder_EndsBesideTheEndAHeadLockedSeparatorIsCutFrom() + { + var plate = FlangeRingsBesideASeparator(); + + var plan = Assert.Single(CuttingPlanBatch.Capture([plate], FlangeParameters(), preservePartOrder: false) + .Plan().Plates); + + Assert.True(plan.IsRouteReady, Describe(plan.Result)); + Assert.False(plan.KeptCurrentOrder); + var last = plan.Result.ProposedOrder[^1]; + Assert.True(last.IsCutOff && last.ReversesCutOff); + Assert.Same(plate.Parts[3], plan.Result.ProposedOrder[^2].SourcePart); + + // The full search alone, without the batch's short first pass, finds it too. + var full = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, confirmedParameters: FlangeParameters(), + expansionBudget: CuttingPlanBatch.PlateBudget(plate.Parts.Count))); + Assert.True(full.Status == CuttingPlanStatus.Ready && full.IndependentlyReplayed, Describe(full)); + Assert.True(full.ProposedOrder[^1].ReversesCutOff); + } + + private static Plate FlangeRingsBesideASeparator() { var plate = new Plate(new Size(48, 96)) { @@ -140,14 +178,7 @@ public class HeadLockedCutOffPlanningTests }, settings); var cutOff = Assert.Single(Assert.Single(staged.Plans).Definitions); Assert.Equal(CutOffAxis.Vertical, cutOff.Axis); - - var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, confirmedParameters: FlangeParameters(), - preservePartOrder: true)); - - Assert.True(result.Status == CuttingPlanStatus.Ready && result.IndependentlyReplayed, Describe(result)); - var last = result.ProposedOrder[^1]; - Assert.True(last.IsCutOff && last.ReversesCutOff); - Assert.Same(plate.Parts[3], result.ProposedOrder[^2].SourcePart); + return plate; } // Six bolt holes on a 6.125 circle, the bore, then the 13.5 OD: the drawing as saved. diff --git a/docs/automatic-scrap-cutoffs.md b/docs/automatic-scrap-cutoffs.md index 5c4bea1b..141e261f 100644 --- a/docs/automatic-scrap-cutoffs.md +++ b/docs/automatic-scrap-cutoffs.md @@ -82,7 +82,7 @@ Only the segment that reaches the far edge is affected, and only when no limit s - Cut toward the far edge (the default): rapid to the segment's start, cut to the nominal far edge, lock the head height, cut on past the edge by the overcut, unlock. - Started at the far edge (`CutOff.FarEdgeFirst`): rapid to the nominal far edge (for a 48-in sheet that is really 48.5 in, the 48-in mark), lock, cut outward past the edge by the overcut, rapid back to the nominal edge with the head still locked, unlock, cut toward the origin. -Plan Cutting may cut a head-locked far-edge segment either way: the joint search, the safest-route fallback and segment placement each try the nearer end first and keep whichever passes the checks (the safest route takes the one breaking fewer). The plan summary reports how many far-edge segments change direction. Apply installs the segment's program and records the direction on its cut-off, so regenerating, saving and loading keep it. A plan made without confirmed cutting parameters only reorders and never reverses. A direction changed after planning makes the plan stale. +Plan Cutting may cut a head-locked far-edge segment either way: the joint search, the safest-route fallback and segment placement each try the nearer end first and keep whichever passes the checks (the safest route takes the one breaking fewer). The free part order is costed from whichever end makes it shorter, so the parts before a separator finish beside the end it is cut from. The plan summary reports how many far-edge segments change direction. Apply installs the segment's program and records the direction on its cut-off, so regenerating, saving and loading keep it. A plan made without confirmed cutting parameters only reorders and never reverses. A direction changed after planning makes the plan stale. The program marks the locked stretch with `HEAD HEIGHT LOCK` and `HEAD HEIGHT UNLOCK` comments (`HeadHeightLock`). Posts write their configured lock and unlock codes there and refuse a nest with a head-locked cut-off when the codes are not set; see [Cincinnati CL](cincinnati-post-output.md#head-locked-cut-off-overcuts) and [CI Fiber](cincinnati-ci-fiber-post-output.md). The overcut leg lies outside the nominal sheet; the clamp-contact check includes it. Machine behavior with the head locked (piercing at the nominal edge, beam off and rapid back) is unconfirmed: verify on the machine before production. diff --git a/docs/cutting-planner.md b/docs/cutting-planner.md index bd7906bb..a650a39a 100644 --- a/docs/cutting-planner.md +++ b/docs/cutting-planner.md @@ -233,17 +233,17 @@ traveling-salesman path over part centers from the start point: nearest neighbor then 2-opt reversals and Or-opt moves of one to three parts, never placing a part before a cutoff or nested-part prerequisite. A cutoff, or any program kept as given (a locked part), is not a center: the path reaches it where its cut starts and leaves from -where it ends (read from its program as given; the search may still reverse a -head-locked far-edge segment), and a reversed run is costed along its own legs from each -part's exit. A slender regenerated part with holes (its cut extent at least three times +where it ends (read from its program as given), and a reversed run is costed along its own +legs from each part's exit. A head-locked far-edge segment may be cut from either end, so each +one in turn is costed from its far-edge end too, and keeps that end when the path is shorter. A slender regenerated part with holes (its cut extent at least three times longer than wide) is not a center either: its holes are cut first and its outside entry leaves beside them, so after the center tour the path reaches and leaves it at whichever hole center suits the parts on either side, moves it to the gap where that detour is shortest, and improves the tour again with those points. A long strip standing beside a row of parts is then cut beside the rows at its holed end, not beside its middle, where its center is. A hole-less part keeps its center: its entry faces whatever comes next. The parts -before a line that splits the sheet therefore finish beside its start, and the part cut -just before it faces that start. A part with other parts nested in its +before a line that splits the sheet therefore finish beside the end it is cut from, and the part +cut just before it faces that end. A part with other parts nested in its cutouts (a host) is cut after all of them, so the tool reaches it from whichever was cut last, and from the middle of a crowded cutout every straight approach crosses a finished neighbor. The nested parts of each host are therefore toured as one run,