diff --git a/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs b/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs index d5624bb6..25910622 100644 --- a/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs +++ b/OpenNest.Engine.Tests/Jobs/ClampZoneInteractiveFillTests.cs @@ -368,6 +368,37 @@ public class ClampZoneInteractiveFillTests AssertClearOfClamps(parts, ZoneA, ZoneB); } + [Fact] + public void DroppedClampHitsLeaveNoGapThePartStillFits() + { + // A 120 x 60 sheet with four front clamps 20 apart. Nine 6.1-high rows fit only with + // the front row beside the clamps, so the parts that straddle a clamp are dropped, which + // opens room for one between two clamps that the fill must take. + var zones = new[] { 20.0, 40.0, 60.0, 80.0 } + .Select(x => new Box(x, 0, 1.5, 0.625)).ToArray(); + var plate = new Plate(new Size(60, 120)) + { + EdgeSpacing = new Spacing(Edge, Edge), + PartSpacing = PartSpacing, + }; + for (var i = 0; i < zones.Length; i++) + plate.ClampZones.Add(new ClampZone($"front-{i}", zones[i])); + var drawing = RectDrawing(12, 6.1); + var item = new NestItem { Drawing = drawing }; + + var parts = PlateFillService.FillItem("Fill", plate, item, plate.WorkArea(), + null, CancellationToken.None); + + AssertClearOfClamps(parts, zones); + 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 >= 12 - 1e-6 && gap.Width >= 6.1 - 1e-6) + || (gap.Length >= 6.1 - 1e-6 && gap.Width >= 12 - 1e-6)); + Assert.Contains(parts, part => part.BoundingBox.Bottom < zones[0].Top + Edge); + } + private sealed class InlineProgress(System.Action report) : System.IProgress { public void Report(NestProgress value) => report(value); diff --git a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs index 2bb90edd..5762135e 100644 --- a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs +++ b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs @@ -71,19 +71,81 @@ public static partial class PlateFillService // is requested. var result = new List(); var request = item.Quantity; + var dropped = false; for (var attempt = 0; attempt <= MaxClampRefills; attempt++) { var proposed = fill(CopyDemand(item, request), area, progress); result = new List(); AddClear(result, proposed, plate, area, demand); + dropped = result.Count < proposed.Count; if (item.Quantity <= 0 || result.Count >= item.Quantity || proposed.Count < request || token.IsCancellationRequested) break; request += item.Quantity - result.Count; } + if (dropped) + { + var before = result.Count; + FillGapsBesideClamps(fill, plate, item, demand, area, result, token); + // The progress sink re-certifies these and promotes them only over its floor. + if (result.Count > before) + progress?.Report(new NestProgress + { + BestParts = result, + ActiveWorkArea = area, + IsOverallBest = true, + Description = $"Filled gaps beside clamps: {result.Count} parts", + }); + } return result; } + /// Gap-fill rounds after dropping clamp hits; a round that adds nothing ends it. + private const int MaxGapFillRounds = 3; + + /// + /// Fills the clear rectangles left once the parts that hit a clamp are dropped: the + /// clamp-blind layout put a part across each clamp, and dropping it can leave room for a + /// part between two clamps or beside one. Each free rectangle large enough for the part is + /// filled with the remaining demand and every proposal is certified like the main fill. + /// Only adds parts; the main layout is never moved. + /// + private static void FillGapsBesideClamps( + Func, List> fill, Plate plate, + NestItem item, List demand, Box area, List result, + CancellationToken token) + { + var size = item.Drawing.Program.BoundingBox(); + var keepOuts = InteractiveClamps.ClearanceRectangles(plate); + for (var round = 0; round < MaxGapFillRounds; round++) + { + var before = result.Count; + var obstacles = keepOuts.ToList(); + obstacles.AddRange(plate.Parts.Where(part => !part.BaseDrawing.IsCutOff) + .Concat(result) + .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)) + return; + if (!Fits(gap, size)) + continue; + var remaining = item.Quantity > 0 ? item.Quantity - result.Count : item.Quantity; + AddClear(result, fill(CopyDemand(item, remaining), gap, null), plate, gap, demand); + } + if (result.Count == before) + return; + } + } + + private static bool Fits(Box gap, Box size) + { + const double slack = 1e-6; + return (gap.Length + slack >= size.Length && gap.Width + slack >= size.Width) + || (gap.Length + slack >= size.Width && gap.Width + slack >= size.Length); + } + /// /// Fills ignoring the clamps and drops the parts that hit one /// (a layout packed around the clamps is usually worse). When every clamp in the area lies