From 096cf9d663ff25ff27d90f02ccfbb920cff9b992 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 1 Dec 2026 15:57:57 -0500 Subject: [PATCH] fix(fill): refill clamp gaps in multi-part group fills A group fill dropped every copy that hit a clamp and left the room it freed empty. The gaps are now filled with whole copies, through the gap search the single-part fill already used. Co-Authored-By: Claude Opus 5.5 --- .../Jobs/ClampZoneInteractiveFillTests.cs | 30 ++++++++++++++++ .../Jobs/Placement/PlateFillService.cs | 35 ++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs b/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs index d055c429..8acc8d58 100644 --- a/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs +++ b/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs @@ -449,6 +449,36 @@ public class ClampZoneInteractiveFillTests Assert.Equal(58, parts.Count); } + [Fact] + public void GroupFillTakesTheGapsItsDroppedCopiesLeave() + { + // A copy of the two-part group that straddles a clamp is dropped whole; the room that + // leaves beside the clamps must take whole copies again. + var plate = RailPlate(out var zones); + var group = new List + { + new(RectDrawing(7, 7)), + new(RectDrawing(7, 7), new Vector(7 + PartSpacing, 0)), + }; + var footprint = group.Select(part => part.BoundingBox).ToList().GetBoundingBox(); + + var parts = PlateFillService.FillGroup("Fill", plate, group, plate.WorkArea(), + null, CancellationToken.None); + + // The service holds parts off a clamp by the validator's keep-out, which the fillers' + // edge tolerance may reach into by less than the spacing slack. + var keepOuts = zones.Select(zone => Inflated(zone, Edge - NestTolerances.SpacingSlack)); + Assert.DoesNotContain(parts, part => + keepOuts.Any(keepOut => Overlaps(part.BoundingBox, keepOut))); + Assert.Equal(0, parts.Count % group.Count); + var obstacles = zones.Select(zone => Inflated(zone, Edge)) + .Concat(parts.Select(part => part.BoundingBox.Offset(PartSpacing))).ToList(); + var gaps = new RemnantFinder(plate.WorkArea(), obstacles).FindRemnants(); + Assert.DoesNotContain(gaps, gap => + (gap.Length >= footprint.Length - 1e-6 && gap.Width >= footprint.Width - 1e-6) + || (gap.Length >= footprint.Width - 1e-6 && gap.Width >= footprint.Length - 1e-6)); + } + [Fact] public void TheClampedRowLinesUpBetweenTheClamps() { diff --git a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs index 5dbc6b50..e8d41347 100644 --- a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs +++ b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs @@ -228,9 +228,23 @@ public static partial class PlateFillService private static void FillGapsBesideClamps( Func, List> fill, Plate plate, NestItem item, List demand, Box area, List result, - CancellationToken token) + CancellationToken token) => + FillGaps(plate, area, item.Drawing.Program.BoundingBox(), result, 1, demand, + () => item.Quantity > 0 && result.Count >= item.Quantity, + gap => fill(item.WithQuantity( + item.Quantity > 0 ? item.Quantity - result.Count : item.Quantity), gap, null), + token); + + /// + /// for any unit: proposes + /// copies of a unit of parts whose footprint is + /// in each free rectangle that can hold one, and each copy is + /// certified whole. Stops once holds. + /// + private static void FillGaps(Plate plate, Box area, Box size, List result, + int groupSize, List demand, Func satisfied, + Func> fillGap, CancellationToken token) { - var size = item.Drawing.Program.BoundingBox(); var keepOuts = InteractiveClamps.ClearanceRectangles(plate); for (var round = 0; round < MaxGapFillRounds; round++) { @@ -241,13 +255,11 @@ public static partial class PlateFillService .Select(part => part.BoundingBox.Offset(plate.PartSpacing))); foreach (var gap in new RemnantFinder(area, obstacles).FindRemnants()) { - if (token.IsCancellationRequested - || (item.Quantity > 0 && result.Count >= item.Quantity)) + if (token.IsCancellationRequested || satisfied()) return; if (!Fits(gap, size)) continue; - var remaining = item.Quantity > 0 ? item.Quantity - result.Count : item.Quantity; - AddClear(result, fill(item.WithQuantity(remaining), gap, null), plate, gap, demand); + AddClear(result, fillGap(gap), plate, gap, demand, groupSize: groupSize); } if (result.Count == before) return; @@ -342,13 +354,18 @@ public static partial class PlateFillService return FillItem(strategy, plate, new NestItem { Drawing = groupParts[0].BaseDrawing }, workArea, progress, token); - // As FillItem; a copy is dropped whole when any member hits a clamp. + // As FillItem; a copy is dropped whole when any member hits a clamp, and the gaps + // the dropped copies leave are filled with whole copies. var groupSize = groupParts.Count; + var size = groupParts.Select(part => part.BoundingBox).ToList().GetBoundingBox(); return FillBesideClamps(filler, plate, workArea, progress, token, (area, sink) => { + var proposed = filler.Fill(groupParts, area, sink, token); var result = new List(); - AddClear(result, filler.Fill(groupParts, area, sink, token), plate, area, - groupSize: groupSize); + AddClear(result, proposed, plate, area, groupSize: groupSize); + if (result.Count < proposed.Count - proposed.Count % groupSize) + FillGaps(plate, area, size, result, groupSize, null, () => false, + gap => filler.Fill(groupParts, gap, null, token), token); return result; }, null, groupSize); }