fix: resolve infinite loop in multi-plate autonest and wire into console

- Change while(true) to bounded for-loop (max 100 plates) in MainForm
- Use Drawing.Name comparison instead of reference equality for quantity deduction
- Add Math.Max(0, ...) guard to prevent negative quantities
- Tune SA parameters for faster convergence (cooling=0.995, minTemp=0.1, maxNoImprove=500)
- Add --autonest flag to OpenNest.Console for CLI-based NFP autonesting

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 08:29:02 -04:00
parent 3f3b07ef5d
commit 2632b3dbf7
3 changed files with 46 additions and 12 deletions
+36 -4
View File
@@ -20,6 +20,7 @@ var checkOverlaps = false;
var noSave = false; var noSave = false;
var noLog = false; var noLog = false;
var keepParts = false; var keepParts = false;
var autoNest = false;
for (var i = 0; i < args.Length; i++) for (var i = 0; i < args.Length; i++)
{ {
@@ -60,6 +61,9 @@ for (var i = 0; i < args.Length; i++)
case "--keep-parts": case "--keep-parts":
keepParts = true; keepParts = true;
break; break;
case "--autonest":
autoNest = true;
break;
case "--help": case "--help":
case "-h": case "-h":
PrintUsage(); PrintUsage();
@@ -145,11 +149,38 @@ else
Console.WriteLine("---"); Console.WriteLine("---");
// Run fill. // Run fill or autonest.
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
var engine = new NestEngine(plate); bool success;
var item = new NestItem { Drawing = drawing, Quantity = quantity };
var success = engine.Fill(item); if (autoNest)
{
// AutoNest: use all drawings (or specific drawing if --drawing given).
var nestItems = new List<NestItem>();
if (drawingName != null)
{
nestItems.Add(new NestItem { Drawing = drawing, Quantity = quantity > 0 ? quantity : 1 });
}
else
{
foreach (var d in nest.Drawings)
nestItems.Add(new NestItem { Drawing = d, Quantity = quantity > 0 ? quantity : 1 });
}
Console.WriteLine($"AutoNest: {nestItems.Count} drawing(s), {nestItems.Sum(i => i.Quantity)} total parts");
var parts = NestEngine.AutoNest(nestItems, plate);
plate.Parts.AddRange(parts);
success = parts.Count > 0;
}
else
{
var engine = new NestEngine(plate);
var item = new NestItem { Drawing = drawing, Quantity = quantity };
success = engine.Fill(item);
}
sw.Stop(); sw.Stop();
// Check overlaps. // Check overlaps.
@@ -208,6 +239,7 @@ void PrintUsage()
Console.Error.WriteLine(" --spacing <value> Override part spacing"); Console.Error.WriteLine(" --spacing <value> Override part spacing");
Console.Error.WriteLine(" --size <WxH> Override plate size (e.g. 120x60)"); Console.Error.WriteLine(" --size <WxH> Override plate size (e.g. 120x60)");
Console.Error.WriteLine(" --output <path> Output nest file path (default: <input>-result.zip)"); Console.Error.WriteLine(" --output <path> Output nest file path (default: <input>-result.zip)");
Console.Error.WriteLine(" --autonest Use NFP-based mixed-part autonesting instead of linear fill");
Console.Error.WriteLine(" --keep-parts Don't clear existing parts before filling"); Console.Error.WriteLine(" --keep-parts Don't clear existing parts before filling");
Console.Error.WriteLine(" --check-overlaps Run overlap detection after fill (exit code 1 if found)"); Console.Error.WriteLine(" --check-overlaps Run overlap detection after fill (exit code 1 if found)");
Console.Error.WriteLine(" --no-save Skip saving output file"); Console.Error.WriteLine(" --no-save Skip saving output file");
+3 -3
View File
@@ -13,9 +13,9 @@ namespace OpenNest
/// </summary> /// </summary>
public class SimulatedAnnealing : INestOptimizer public class SimulatedAnnealing : INestOptimizer
{ {
private const double DefaultCoolingRate = 0.997; private const double DefaultCoolingRate = 0.995;
private const double DefaultMinTemperature = 0.01; private const double DefaultMinTemperature = 0.1;
private const int DefaultMaxNoImprovement = 2000; private const int DefaultMaxNoImprovement = 500;
public NestResult Optimize(List<NestItem> items, Box workArea, NfpCache cache, public NestResult Optimize(List<NestItem> items, Box workArea, NfpCache cache,
Dictionary<int, List<double>> candidateRotations, Dictionary<int, List<double>> candidateRotations,
+7 -5
View File
@@ -680,8 +680,9 @@ namespace OpenNest.Forms
return; return;
var items = form.GetNestItems(); var items = form.GetNestItems();
var maxPlates = 100;
while (true) for (var plateCount = 0; plateCount < maxPlates; plateCount++)
{ {
var remaining = items.Where(i => i.Quantity > 0).ToList(); var remaining = items.Where(i => i.Quantity > 0).ToList();
@@ -698,14 +699,15 @@ namespace OpenNest.Forms
break; break;
plate.Parts.AddRange(parts); plate.Parts.AddRange(parts);
activeForm.Nest.UpdateDrawingQuantities();
// Reduce remaining quantities by how many were placed per drawing. // Deduct placed quantities using Drawing.Name to avoid reference issues.
foreach (var item in remaining) foreach (var item in remaining)
{ {
var placed = parts.Count(p => p.BaseDrawing == item.Drawing); var placed = parts.Count(p => p.BaseDrawing.Name == item.Drawing.Name);
item.Quantity -= placed; item.Quantity = System.Math.Max(0, item.Quantity - placed);
} }
activeForm.Nest.UpdateDrawingQuantities();
} }
} }