refactor: share NestItem copies, support analysis cache and lead selection

- NestItem.WithQuantity replaces eight hand-written copies that changed only
  the quantity, so a new field is copied everywhere.
- The WinForms support overlay uses Desktop's SupportAnalysisCache and
  SupportClearance instead of its own copy.
- CuttingParameters.LeadInFor/LeadOutFor replace the contour-type switches in
  ContourCuttingStrategy and ActionLeadIn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 11:28:56 -05:00
1 parent b692ae463a
commit be4b04c7b5
13 files changed
+47 -139

No files matched your search

+2 -22
View File
@@ -54,17 +54,7 @@ namespace OpenNest.Engine.Fill
var estimatedMax =
bbox.Area() > 0 ? (int)(workArea.Area() / bbox.Area()) * 2 : 1000;
workItems.Add(
new NestItem
{
Drawing = item.Drawing,
Quantity = System.Math.Max(1, estimatedMax),
Priority = item.Priority,
StepAngle = item.StepAngle,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
}
);
workItems.Add(item.WithQuantity(System.Math.Max(1, estimatedMax)));
}
else
{
@@ -173,17 +163,7 @@ namespace OpenNest.Engine.Fill
var remaining = item.Quantity - placedCount;
if (remaining > 0)
{
leftovers.Add(
new NestItem
{
Drawing = item.Drawing,
Quantity = remaining,
Priority = item.Priority,
StepAngle = item.StepAngle,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
}
);
leftovers.Add(item.WithQuantity(remaining));
}
}
+2 -12
View File
@@ -272,19 +272,9 @@ public static class CurrentPlatesNest
private static List<NestItem> Copy(IEnumerable<NestItem> items) => items
.Where(i => i?.Drawing != null && i.Quantity > 0)
.Select(i => With(i, i.Quantity))
.Select(i => i.WithQuantity(i.Quantity))
.ToList();
private static NestItem With(NestItem item, int quantity) => new()
{
Drawing = item.Drawing,
Quantity = quantity,
Priority = item.Priority,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
StepAngle = item.StepAngle,
};
private static List<NestItem> Subtract(List<NestItem> items, IReadOnlyDictionary<Drawing, int> placed)
{
var left = new List<NestItem>();
@@ -294,7 +284,7 @@ public static class CurrentPlatesNest
var take = System.Math.Min(item.Quantity, used.GetValueOrDefault(item.Drawing));
used[item.Drawing] = used.GetValueOrDefault(item.Drawing) - take;
if (item.Quantity - take > 0)
left.Add(With(item, item.Quantity - take));
left.Add(item.WithQuantity(item.Quantity - take));
}
return left;
}
@@ -39,15 +39,7 @@ internal static class LeftoverPacker
var packed = new List<Part>();
var remaining = items
.Where(item => item.Quantity > 0)
.Select(item => new NestItem
{
Drawing = item.Drawing,
Quantity = item.Quantity,
Priority = item.Priority,
StepAngle = item.StepAngle,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
})
.Select(item => item.WithQuantity(item.Quantity))
.ToList();
var sizes = remaining.ToDictionary(
item => item,
@@ -131,15 +131,7 @@ internal static class PlateFillOrchestrator
private static bool IsPairable(NestItem item) => item.Quantity >= 2 && item.Quantity % 2 == 0;
private static List<NestItem> Copies(IEnumerable<NestItem> items) =>
items.Select(item => new NestItem
{
Drawing = item.Drawing,
Priority = item.Priority,
Quantity = item.Quantity,
StepAngle = item.StepAngle,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
}).ToList();
items.Select(item => item.WithQuantity(item.Quantity)).ToList();
/// <summary>Packs everything but quantity-two items, then places those as pairs.</summary>
private static List<Part> PackThenPairs(
@@ -75,7 +75,7 @@ public static partial class PlateFillService
var dropped = false;
for (var attempt = 0; attempt <= MaxClampRefills; attempt++)
{
var proposed = fill(CopyDemand(item, request), area, progress);
var proposed = fill(item.WithQuantity(request), area, progress);
result = new List<Part>();
AddClear(result, proposed, plate, area, demand);
dropped = result.Count < proposed.Count;
@@ -129,7 +129,7 @@ public static partial class PlateFillService
if (!Fits(slot, item.Drawing.Program.BoundingBox()))
continue;
var remaining = item.Quantity > 0 ? item.Quantity - repacked.Count : item.Quantity;
AddClear(repacked, fill(CopyDemand(item, remaining), slot, null), plate, slot, demand);
AddClear(repacked, fill(item.WithQuantity(remaining), slot, null), plate, slot, demand);
}
FillGapsBesideClamps(fill, plate, item, demand, area, repacked, token);
return repacked.Count > gapFilled.Count ? repacked : gapFilled;
@@ -247,7 +247,7 @@ public static partial class PlateFillService
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);
AddClear(result, fill(item.WithQuantity(remaining), gap, null), plate, gap, demand);
}
if (result.Count == before)
return;
@@ -359,7 +359,7 @@ public static partial class PlateFillService
foreach (var area in ClearRegionBoxes(plate, box))
{
token.ThrowIfCancellationRequested();
var remaining = items.Select(item => CopyDemand(item,
var remaining = items.Select(item => item.WithQuantity(
item.Quantity > 0 ? System.Math.Max(0, item.Quantity - result.Count(part =>
ReferenceEquals(part.BaseDrawing, item.Drawing))) : item.Quantity)).ToList();
var proposed = filler.PackArea(area, remaining, null, token);
@@ -471,16 +471,6 @@ public static partial class PlateFillService
Description = $"Clamp-clear proposal: {parts.Count} parts",
});
private static NestItem CopyDemand(NestItem item, int quantity) => new()
{
Drawing = item.Drawing,
Quantity = quantity,
Priority = item.Priority,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
StepAngle = item.StepAngle,
};
private static List<Part> FillClearAreas(Plate plate, Box box, NestItem item,
CancellationToken token, Func<Box, NestItem, List<Part>> fill,
List<Part> previouslyPlaced = null) =>
@@ -496,7 +486,7 @@ public static partial class PlateFillService
token.ThrowIfCancellationRequested();
if (quantity > 0 && result.Count >= quantity)
break;
var demand = item == null ? null : CopyDemand(item,
var demand = item == null ? null : item.WithQuantity(
quantity > 0 ? quantity - result.Count : quantity);
AddClear(result, fill(area, demand), plate, area,
item == null ? null : new List<NestItem> { item }, previouslyPlaced, groupSize);
@@ -165,15 +165,7 @@ public static class PlateLeftoverFill
break;
if (!Fits(region, size))
continue;
var demand = new NestItem
{
Drawing = item.Drawing,
Quantity = cap - placed,
Priority = item.Priority,
RotationStart = item.RotationStart,
RotationEnd = item.RotationEnd,
StepAngle = item.StepAngle,
};
var demand = item.WithQuantity(cap - placed);
var proposed = PlateFillService.FillItem(strategy, plate, demand, region, null, token)
?.Where(p => rotation.Allows(p.Rotation))
.ToList();
+14
View File
@@ -31,5 +31,19 @@ namespace OpenNest.Engine
/// The rotation end angle in radians.
/// </summary>
public double RotationEnd { get; set; }
/// <summary>
/// A copy of this item with the given quantity and every other setting unchanged.
/// </summary>
public NestItem WithQuantity(int quantity) =>
new()
{
Drawing = Drawing,
Priority = Priority,
Quantity = quantity,
StepAngle = StepAngle,
RotationStart = RotationStart,
RotationEnd = RotationEnd,
};
}
}