Multi-bin nesting
This commit is contained in:
@@ -11,6 +11,8 @@ namespace SawCut.Nesting
|
||||
|
||||
public double Spacing { get; set; }
|
||||
|
||||
public int MaxBinCount { get; set; } = int.MaxValue;
|
||||
|
||||
private List<BinItem> Items { get; set; }
|
||||
|
||||
public Result Pack(List<BinItem> items)
|
||||
@@ -30,6 +32,16 @@ namespace SawCut.Nesting
|
||||
|
||||
result.Bins = GetBins();
|
||||
|
||||
foreach (var bin in result.Bins)
|
||||
{
|
||||
foreach (var item in bin.Items)
|
||||
{
|
||||
Items.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
result.ItemsNotUsed.AddRange(Items);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -46,8 +58,11 @@ namespace SawCut.Nesting
|
||||
if (item.Length > StockLength)
|
||||
continue;
|
||||
|
||||
best_bin = CreateBin();
|
||||
bins.Add(best_bin);
|
||||
if (bins.Count < MaxBinCount)
|
||||
{
|
||||
best_bin = CreateBin();
|
||||
bins.Add(best_bin);
|
||||
}
|
||||
}
|
||||
|
||||
best_bin.Items.Add(item);
|
||||
|
||||
@@ -11,6 +11,8 @@ namespace SawCut.Nesting
|
||||
|
||||
public double Spacing { get; set; }
|
||||
|
||||
public int MaxBinCount { get; set; } = int.MaxValue;
|
||||
|
||||
private List<BinItem> Items { get; set; }
|
||||
|
||||
public Result Pack(List<BinItem> items)
|
||||
@@ -30,6 +32,16 @@ namespace SawCut.Nesting
|
||||
|
||||
result.Bins = GetBins();
|
||||
|
||||
foreach (var bin in result.Bins)
|
||||
{
|
||||
foreach (var item in bin.Items)
|
||||
{
|
||||
Items.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
result.ItemsNotUsed.AddRange(Items);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -37,7 +49,7 @@ namespace SawCut.Nesting
|
||||
{
|
||||
var bins = new List<Bin>();
|
||||
|
||||
while (Items.Count > 0)
|
||||
while (Items.Count > 0 && bins.Count < MaxBinCount)
|
||||
{
|
||||
var bin = new Bin(StockLength);
|
||||
bin.Spacing = Spacing;
|
||||
|
||||
70
SawCut/Nesting/MultiBinEngine.cs
Normal file
70
SawCut/Nesting/MultiBinEngine.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SawCut.Nesting
|
||||
{
|
||||
public class MultiBinEngine : IEngine
|
||||
{
|
||||
public List<MultiBin> Bins { get; set; }
|
||||
|
||||
public double Spacing { get; set; }
|
||||
|
||||
public Result Pack(List<BinItem> items)
|
||||
{
|
||||
var bins = Bins
|
||||
.Where(b => b.Length > 0)
|
||||
.OrderByDescending(b => b.Priority)
|
||||
.ThenBy(b => b.Length)
|
||||
.ToList();
|
||||
|
||||
var result = new Result();
|
||||
var remainingItems = new List<BinItem>(items);
|
||||
|
||||
foreach (var bin in bins)
|
||||
{
|
||||
var e = new Engine2();
|
||||
e.MaxBinCount = bin.Quantity;
|
||||
e.StockLength = bin.Length;
|
||||
e.Spacing = Spacing;
|
||||
var r = e.Pack(remainingItems);
|
||||
|
||||
result.Bins.AddRange(r.Bins);
|
||||
remainingItems = r.ItemsNotUsed;
|
||||
}
|
||||
|
||||
result.ItemsNotUsed = remainingItems;
|
||||
|
||||
// sanity check
|
||||
|
||||
//var itemsNested = result.Bins.SelectMany(b => b.Items).ToList();
|
||||
//var lengths = items.Select(i => i.Length).Distinct();
|
||||
|
||||
//foreach (var length in lengths)
|
||||
//{
|
||||
// var nameLengthGroups1 = items.Where(i => i.Length == length).GroupBy(i => i.Name).ToList();
|
||||
// var nameLengthGroups2 = itemsNested.Where(i => i.Length == length).GroupBy(i => i.Name).ToList();
|
||||
|
||||
// foreach (var group in nameLengthGroups1)
|
||||
// {
|
||||
// var name = group.Key;
|
||||
|
||||
// var nestedWithSameName = nameLengthGroups2.FirstOrDefault(g => g.Key == group.Key);
|
||||
|
||||
// if (group.Count() != nestedWithSameName.Count())
|
||||
// {
|
||||
// throw new Exception("Failed sanity check.");
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (nameLengthGroups1.Count() != nameLengthGroups2.Count())
|
||||
// {
|
||||
// throw new Exception("Failed sanity check.");
|
||||
// }
|
||||
//}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace SawCut.Nesting
|
||||
public Result()
|
||||
{
|
||||
ItemsNotUsed = new List<BinItem>();
|
||||
Bins = new List<Bin>();
|
||||
}
|
||||
|
||||
public List<BinItem> ItemsNotUsed { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user