mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-23 06:37:52 -05:00
feat(fill): fill the gaps left where parts hit a clamp
A clamped fill lays parts out as if the clamps were absent and drops the ones that hit a clamp, which can leave room for a part between two clamps or beside one. After dropping, fill each clear rectangle the part fits with the remaining demand, certified like the main fill, and report the gap-filled layout as a preview. Existing parts never move. On a 120 x 60 sheet with front clamps at 20/40/60/80, the dropped layout of 12 x 6 parts goes from 81 to 82 parts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5776fa80c9
commit
42880d947c
2 files changed
+93
No files matched your search
@@ -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<NestProgress> report) : System.IProgress<NestProgress>
|
||||
{
|
||||
public void Report(NestProgress value) => report(value);
|
||||
|
||||
@@ -71,19 +71,81 @@ public static partial class PlateFillService
|
||||
// is requested.
|
||||
var result = new List<Part>();
|
||||
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<Part>();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Gap-fill rounds after dropping clamp hits; a round that adds nothing ends it.</summary>
|
||||
private const int MaxGapFillRounds = 3;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
private static void FillGapsBesideClamps(
|
||||
Func<NestItem, Box, IProgress<NestProgress>, List<Part>> fill, Plate plate,
|
||||
NestItem item, List<NestItem> demand, Box area, List<Part> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills <paramref name="workArea"/> 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
|
||||
|
||||
Reference in new issue
Block a user