feat: add virtual Nest method to NestEngineBase for polymorphic auto-nest
The auto-nest code paths (MainForm, MCP, Console) now call engine.Nest(items, progress, token) instead of manually orchestrating sequential fill+pack. The default implementation in NestEngineBase does sequential FillExact+PackArea. StripNestEngine overrides with its strip strategy. This makes the engine dropdown actually work. Also consolidates ComputeRemainderWithin into NestEngineBase, removing duplicates from MainForm and StripNestEngine. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -329,51 +329,10 @@ static class NestConsole
|
||||
|
||||
Console.WriteLine($"AutoNest: {nestItems.Count} drawing(s), {nestItems.Sum(i => i.Quantity)} total parts");
|
||||
|
||||
var fillItems = nestItems
|
||||
.Where(i => i.Quantity > 1)
|
||||
.OrderBy(i => i.Priority)
|
||||
.ThenByDescending(i => i.Drawing.Area)
|
||||
.ToList();
|
||||
|
||||
var packItems = nestItems
|
||||
.Where(i => i.Quantity == 1)
|
||||
.ToList();
|
||||
|
||||
var workArea = plate.WorkArea();
|
||||
success = false;
|
||||
|
||||
// Phase 1: Fill multi-quantity drawings with NestEngine.
|
||||
foreach (var item in fillItems)
|
||||
{
|
||||
if (item.Quantity <= 0 || workArea.Width <= 0 || workArea.Length <= 0)
|
||||
continue;
|
||||
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
var parts = engine.FillExact(item, workArea, null, CancellationToken.None);
|
||||
|
||||
if (parts.Count > 0)
|
||||
{
|
||||
plate.Parts.AddRange(parts);
|
||||
// TODO: Compactor.Compact(parts, plate);
|
||||
item.Quantity = System.Math.Max(0, item.Quantity - parts.Count);
|
||||
success = true;
|
||||
var placedBox = parts.Cast<IBoundable>().GetBoundingBox();
|
||||
workArea = ComputeRemainderWithin(workArea, placedBox, plate.PartSpacing);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Pack single-quantity items into remaining space.
|
||||
packItems = packItems.Where(i => i.Quantity > 0).ToList();
|
||||
|
||||
if (packItems.Count > 0 && workArea.Width > 0 && workArea.Length > 0)
|
||||
{
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
var packParts = engine.PackArea(workArea, packItems, null, CancellationToken.None);
|
||||
plate.Parts.AddRange(packParts);
|
||||
|
||||
if (packParts.Count > 0)
|
||||
success = true;
|
||||
}
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
var nestParts = engine.Nest(nestItems, null, CancellationToken.None);
|
||||
plate.Parts.AddRange(nestParts);
|
||||
success = nestParts.Count > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -386,21 +345,6 @@ static class NestConsole
|
||||
return (success, sw.ElapsedMilliseconds);
|
||||
}
|
||||
|
||||
static Box ComputeRemainderWithin(Box workArea, Box usedBox, double spacing)
|
||||
{
|
||||
var hWidth = workArea.Right - usedBox.Right - spacing;
|
||||
var hStrip = hWidth > 0
|
||||
? new Box(usedBox.Right + spacing, workArea.Y, hWidth, workArea.Length)
|
||||
: Box.Empty;
|
||||
|
||||
var vHeight = workArea.Top - usedBox.Top - spacing;
|
||||
var vStrip = vHeight > 0
|
||||
? new Box(workArea.X, usedBox.Top + spacing, workArea.Width, vHeight)
|
||||
: Box.Empty;
|
||||
|
||||
return hStrip.Area() >= vStrip.Area() ? hStrip : vStrip;
|
||||
}
|
||||
|
||||
static int CheckOverlaps(Plate plate, Options options)
|
||||
{
|
||||
if (!options.CheckOverlaps || plate.Parts.Count == 0)
|
||||
|
||||
@@ -50,6 +50,88 @@ namespace OpenNest
|
||||
return new List<Part>();
|
||||
}
|
||||
|
||||
// --- Nest: multi-item strategy (virtual, side-effect-free) ---
|
||||
|
||||
public virtual List<Part> Nest(List<NestItem> items,
|
||||
IProgress<NestProgress> progress, CancellationToken token)
|
||||
{
|
||||
if (items == null || items.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
var workArea = Plate.WorkArea();
|
||||
var allParts = new List<Part>();
|
||||
|
||||
var fillItems = items
|
||||
.Where(i => i.Quantity != 1)
|
||||
.OrderBy(i => i.Priority)
|
||||
.ThenByDescending(i => i.Drawing.Area)
|
||||
.ToList();
|
||||
|
||||
var packItems = items
|
||||
.Where(i => i.Quantity == 1)
|
||||
.ToList();
|
||||
|
||||
// Phase 1: Fill multi-quantity drawings sequentially.
|
||||
foreach (var item in fillItems)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
if (item.Quantity <= 0 || workArea.Width <= 0 || workArea.Length <= 0)
|
||||
continue;
|
||||
|
||||
var parts = FillExact(
|
||||
new NestItem { Drawing = item.Drawing, Quantity = item.Quantity },
|
||||
workArea, progress, token);
|
||||
|
||||
if (parts.Count > 0)
|
||||
{
|
||||
allParts.AddRange(parts);
|
||||
item.Quantity = System.Math.Max(0, item.Quantity - parts.Count);
|
||||
var placedBox = parts.Cast<IBoundable>().GetBoundingBox();
|
||||
workArea = ComputeRemainderWithin(workArea, placedBox, Plate.PartSpacing);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Pack single-quantity items into remaining space.
|
||||
packItems = packItems.Where(i => i.Quantity > 0).ToList();
|
||||
|
||||
if (packItems.Count > 0 && workArea.Width > 0 && workArea.Length > 0
|
||||
&& !token.IsCancellationRequested)
|
||||
{
|
||||
var packParts = PackArea(workArea, packItems, progress, token);
|
||||
|
||||
if (packParts.Count > 0)
|
||||
{
|
||||
allParts.AddRange(packParts);
|
||||
|
||||
foreach (var item in packItems)
|
||||
{
|
||||
var placed = packParts.Count(p =>
|
||||
p.BaseDrawing.Name == item.Drawing.Name);
|
||||
item.Quantity = System.Math.Max(0, item.Quantity - placed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allParts;
|
||||
}
|
||||
|
||||
protected static Box ComputeRemainderWithin(Box workArea, Box usedBox, double spacing)
|
||||
{
|
||||
var hWidth = workArea.Right - usedBox.Right - spacing;
|
||||
var hStrip = hWidth > 0
|
||||
? new Box(usedBox.Right + spacing, workArea.Y, hWidth, workArea.Length)
|
||||
: Box.Empty;
|
||||
|
||||
var vHeight = workArea.Top - usedBox.Top - spacing;
|
||||
var vStrip = vHeight > 0
|
||||
? new Box(workArea.X, usedBox.Top + spacing, workArea.Width, vHeight)
|
||||
: Box.Empty;
|
||||
|
||||
return hStrip.Area() >= vStrip.Area() ? hStrip : vStrip;
|
||||
}
|
||||
|
||||
// --- FillExact (non-virtual, delegates to virtual Fill) ---
|
||||
|
||||
public List<Part> FillExact(NestItem item, Box workArea,
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace OpenNest
|
||||
/// in both bottom and left orientations, fills remnants with remaining drawings,
|
||||
/// and returns the denser result.
|
||||
/// </summary>
|
||||
public List<Part> Nest(List<NestItem> items,
|
||||
public override List<Part> Nest(List<NestItem> items,
|
||||
IProgress<NestProgress> progress, CancellationToken token)
|
||||
{
|
||||
if (items == null || items.Count == 0)
|
||||
@@ -256,23 +256,6 @@ namespace OpenNest
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the largest usable remainder within a work area after a portion has been used.
|
||||
/// Picks whichever is larger: the horizontal strip to the right, or the vertical strip above.
|
||||
/// </summary>
|
||||
private static Box ComputeRemainderWithin(Box workArea, Box usedBox, double spacing)
|
||||
{
|
||||
var hWidth = workArea.Right - usedBox.Right - spacing;
|
||||
var hStrip = hWidth > 0
|
||||
? new Box(usedBox.Right + spacing, workArea.Y, hWidth, workArea.Length)
|
||||
: Box.Empty;
|
||||
|
||||
var vHeight = workArea.Top - usedBox.Top - spacing;
|
||||
var vStrip = vHeight > 0
|
||||
? new Box(workArea.X, usedBox.Top + spacing, workArea.Width, vHeight)
|
||||
: Box.Empty;
|
||||
|
||||
return hStrip.Area() >= vStrip.Area() ? hStrip : vStrip;
|
||||
}
|
||||
// ComputeRemainderWithin inherited from NestEngineBase
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,28 +233,16 @@ namespace OpenNest.Mcp.Tools
|
||||
items.Add(new NestItem { Drawing = drawing, Quantity = qtys[i] });
|
||||
}
|
||||
|
||||
// Strategy 1: Strip nesting
|
||||
var stripEngine = new StripNestEngine(plate);
|
||||
var stripResult = stripEngine.Nest(items, null, CancellationToken.None);
|
||||
var stripScore = FillScore.Compute(stripResult, plate.WorkArea());
|
||||
|
||||
// Strategy 2: Current sequential fill
|
||||
var seqResult = SequentialFill(plate, items);
|
||||
var seqScore = FillScore.Compute(seqResult, plate.WorkArea());
|
||||
|
||||
// Pick winner and apply to plate.
|
||||
var winner = stripScore >= seqScore ? stripResult : seqResult;
|
||||
var winnerName = stripScore >= seqScore ? "strip" : "sequential";
|
||||
plate.Parts.AddRange(winner);
|
||||
var totalPlaced = winner.Count;
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
var nestParts = engine.Nest(items, null, CancellationToken.None);
|
||||
plate.Parts.AddRange(nestParts);
|
||||
var totalPlaced = nestParts.Count;
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine($"AutoNest plate {plateIndex} ({winnerName} strategy): {(totalPlaced > 0 ? "success" : "no parts placed")}");
|
||||
sb.AppendLine($"AutoNest plate {plateIndex} ({engine.Name} engine): {(totalPlaced > 0 ? "success" : "no parts placed")}");
|
||||
sb.AppendLine($" Parts placed: {totalPlaced}");
|
||||
sb.AppendLine($" Total parts: {plate.Parts.Count}");
|
||||
sb.AppendLine($" Utilization: {plate.Utilization():P1}");
|
||||
sb.AppendLine($" Strip score: {stripScore.Count} parts, density {stripScore.Density:P1}");
|
||||
sb.AppendLine($" Sequential score: {seqScore.Count} parts, density {seqScore.Density:P1}");
|
||||
|
||||
var groups = plate.Parts.GroupBy(p => p.BaseDrawing.Name);
|
||||
foreach (var group in groups)
|
||||
@@ -262,52 +250,5 @@ namespace OpenNest.Mcp.Tools
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private static List<Part> SequentialFill(Plate plate, List<NestItem> items)
|
||||
{
|
||||
var fillItems = items
|
||||
.Where(i => i.Quantity != 1)
|
||||
.OrderBy(i => i.Priority)
|
||||
.ThenByDescending(i => i.Drawing.Area)
|
||||
.ToList();
|
||||
|
||||
var workArea = plate.WorkArea();
|
||||
var allParts = new List<Part>();
|
||||
|
||||
foreach (var item in fillItems)
|
||||
{
|
||||
if (item.Quantity == 0 || workArea.Width <= 0 || workArea.Length <= 0)
|
||||
continue;
|
||||
|
||||
var engine = new DefaultNestEngine(plate);
|
||||
var parts = engine.Fill(
|
||||
new NestItem { Drawing = item.Drawing, Quantity = item.Quantity },
|
||||
workArea, null, CancellationToken.None);
|
||||
|
||||
if (parts.Count > 0)
|
||||
{
|
||||
allParts.AddRange(parts);
|
||||
var placedBox = parts.Cast<IBoundable>().GetBoundingBox();
|
||||
workArea = ComputeRemainderWithin(workArea, placedBox, plate.PartSpacing);
|
||||
}
|
||||
}
|
||||
|
||||
return allParts;
|
||||
}
|
||||
|
||||
private static Box ComputeRemainderWithin(Box workArea, Box usedBox, double spacing)
|
||||
{
|
||||
var hWidth = workArea.Right - usedBox.Right - spacing;
|
||||
var hStrip = hWidth > 0
|
||||
? new Box(usedBox.Right + spacing, workArea.Y, hWidth, workArea.Length)
|
||||
: Box.Empty;
|
||||
|
||||
var vHeight = workArea.Top - usedBox.Top - spacing;
|
||||
var vStrip = vHeight > 0
|
||||
? new Box(workArea.X, usedBox.Top + spacing, workArea.Width, vHeight)
|
||||
: Box.Empty;
|
||||
|
||||
return hStrip.Area() >= vStrip.Area() ? hStrip : vStrip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -787,104 +787,19 @@ namespace OpenNest.Forms
|
||||
|
||||
var anyPlaced = false;
|
||||
|
||||
// Strip engine: use Nest() for multi-drawing strategy.
|
||||
if (NestEngineRegistry.Create(plate) is StripNestEngine)
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
engine.PlateNumber = plateCount;
|
||||
|
||||
var nestParts = await Task.Run(() =>
|
||||
engine.Nest(remaining, progress, token));
|
||||
|
||||
activeForm.PlateView.ClearTemporaryParts();
|
||||
|
||||
if (nestParts.Count > 0 && !token.IsCancellationRequested)
|
||||
{
|
||||
var stripEngine = new StripNestEngine(plate);
|
||||
var stripParts = await Task.Run(() =>
|
||||
stripEngine.Nest(remaining, progress, token));
|
||||
|
||||
activeForm.PlateView.ClearTemporaryParts();
|
||||
|
||||
if (stripParts.Count > 0 && !token.IsCancellationRequested)
|
||||
{
|
||||
plate.Parts.AddRange(stripParts);
|
||||
activeForm.PlateView.Invalidate();
|
||||
anyPlaced = true;
|
||||
|
||||
// Deduct placed quantities.
|
||||
foreach (var item in remaining)
|
||||
{
|
||||
var placed = stripParts.Count(p =>
|
||||
p.BaseDrawing.Name == item.Drawing.Name);
|
||||
item.Quantity = System.Math.Max(0, item.Quantity - placed);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default: sequential Fill + Pack.
|
||||
var fillItems = remaining
|
||||
.Where(i => i.Quantity > 1)
|
||||
.OrderBy(i => i.Priority)
|
||||
.ThenByDescending(i => i.Drawing.Area)
|
||||
.ToList();
|
||||
|
||||
var packItems = remaining
|
||||
.Where(i => i.Quantity == 1)
|
||||
.ToList();
|
||||
|
||||
var workArea = plate.WorkArea();
|
||||
|
||||
// Phase 1: Fill each multi-quantity drawing with NestEngine.
|
||||
foreach (var item in fillItems)
|
||||
{
|
||||
if (item.Quantity <= 0 || token.IsCancellationRequested)
|
||||
continue;
|
||||
|
||||
if (workArea.Width <= 0 || workArea.Length <= 0)
|
||||
break;
|
||||
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
engine.PlateNumber = plateCount;
|
||||
|
||||
var parts = await Task.Run(() =>
|
||||
engine.FillExact(item, workArea, progress, token));
|
||||
|
||||
activeForm.PlateView.ClearTemporaryParts();
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
if (parts.Count > 0)
|
||||
{
|
||||
plate.Parts.AddRange(parts);
|
||||
// TODO: Compactor.Compact(parts, plate);
|
||||
activeForm.PlateView.Invalidate();
|
||||
anyPlaced = true;
|
||||
|
||||
item.Quantity = System.Math.Max(0, item.Quantity - parts.Count);
|
||||
|
||||
var placedBox = parts.Cast<IBoundable>().GetBoundingBox();
|
||||
workArea = ComputeRemainderWithin(workArea, placedBox, plate.PartSpacing);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Pack single-quantity items into remaining space.
|
||||
packItems = packItems.Where(i => i.Quantity > 0).ToList();
|
||||
|
||||
if (packItems.Count > 0 && workArea.Width > 0 && workArea.Length > 0
|
||||
&& !token.IsCancellationRequested)
|
||||
{
|
||||
var engine = NestEngineRegistry.Create(plate);
|
||||
var packParts = engine.PackArea(workArea, packItems, null, CancellationToken.None);
|
||||
plate.Parts.AddRange(packParts);
|
||||
var packed = packParts.Count;
|
||||
|
||||
if (packed > 0)
|
||||
{
|
||||
activeForm.PlateView.Invalidate();
|
||||
anyPlaced = true;
|
||||
|
||||
foreach (var item in packItems)
|
||||
{
|
||||
var placed = plate.Parts.Count(p =>
|
||||
p.BaseDrawing.Name == item.Drawing.Name);
|
||||
item.Quantity = System.Math.Max(0,
|
||||
item.Quantity - placed);
|
||||
}
|
||||
}
|
||||
}
|
||||
plate.Parts.AddRange(nestParts);
|
||||
activeForm.PlateView.Invalidate();
|
||||
anyPlaced = true;
|
||||
}
|
||||
|
||||
if (!anyPlaced)
|
||||
@@ -909,21 +824,6 @@ namespace OpenNest.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private static Box ComputeRemainderWithin(Box workArea, Box usedBox, double spacing)
|
||||
{
|
||||
var hWidth = workArea.Right - usedBox.Right - spacing;
|
||||
var hStrip = hWidth > 0
|
||||
? new Box(usedBox.Right + spacing, workArea.Y, hWidth, workArea.Length)
|
||||
: Box.Empty;
|
||||
|
||||
var vHeight = workArea.Top - usedBox.Top - spacing;
|
||||
var vStrip = vHeight > 0
|
||||
? new Box(workArea.X, usedBox.Top + spacing, workArea.Width, vHeight)
|
||||
: Box.Empty;
|
||||
|
||||
return hStrip.Area() >= vStrip.Area() ? hStrip : vStrip;
|
||||
}
|
||||
|
||||
private void SequenceAllPlates_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (activeForm == null)
|
||||
|
||||
Reference in New Issue
Block a user