Separated nesting logic from UI
This commit is contained in:
73
CutToLength/BestFitEngine.cs
Normal file
73
CutToLength/BestFitEngine.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace CutToLength
|
||||
{
|
||||
public class BestFitEngine : IEngine
|
||||
{
|
||||
public double StockLength { get; set; }
|
||||
|
||||
public Tool CutTool { get; set; }
|
||||
|
||||
public List<Bin> GetResults(List<BinItem> items)
|
||||
{
|
||||
var items2 = items.OrderByDescending(i => i.Length);
|
||||
var bins = new List<Bin>();
|
||||
var length = StockLength;
|
||||
|
||||
foreach (var item in items2)
|
||||
{
|
||||
Bin best_bin;
|
||||
|
||||
if (!FindBin(bins.ToArray(), item.Length, out best_bin))
|
||||
{
|
||||
if (item.Length > length)
|
||||
continue;
|
||||
|
||||
best_bin = CreateBin();
|
||||
bins.Add(best_bin);
|
||||
}
|
||||
|
||||
best_bin.Items.Add(item);
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
|
||||
private Bin CreateBin()
|
||||
{
|
||||
var length = StockLength;
|
||||
var spacing = CutTool.Kerf;
|
||||
|
||||
return new Bin(length)
|
||||
{
|
||||
Spacing = spacing
|
||||
};
|
||||
}
|
||||
|
||||
private static bool FindBin(IEnumerable<Bin> bins, double length, out Bin found)
|
||||
{
|
||||
found = null;
|
||||
|
||||
foreach (var bin in bins)
|
||||
{
|
||||
if (bin.RemainingLength < length)
|
||||
continue;
|
||||
|
||||
if (found == null)
|
||||
found = bin;
|
||||
|
||||
if (bin.RemainingLength < found.RemainingLength)
|
||||
found = bin;
|
||||
}
|
||||
|
||||
return (found != null);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IEngine
|
||||
{
|
||||
List<Bin> GetResults(List<BinItem> items);
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BestFitEngine.cs" />
|
||||
<Compile Include="Bin.cs" />
|
||||
<Compile Include="BinItem.cs" />
|
||||
<Compile Include="BinLayoutView.cs">
|
||||
|
||||
@@ -3,7 +3,6 @@ using SimpleExpressionEvaluator;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
@@ -129,7 +128,12 @@ namespace CutToLength
|
||||
|
||||
private void Run()
|
||||
{
|
||||
var bins = GetResults();
|
||||
var engine = new BestFitEngine();
|
||||
engine.CutTool = GetSelectedTool();
|
||||
engine.StockLength = StockLengthInches;
|
||||
|
||||
var items = GetItems();
|
||||
var bins = engine.GetResults(items);
|
||||
|
||||
var form = new ResultsForm();
|
||||
form.Bins = bins;
|
||||
@@ -144,31 +148,6 @@ namespace CutToLength
|
||||
//}
|
||||
}
|
||||
|
||||
private List<Bin> GetResults()
|
||||
{
|
||||
var items2 = GetItems().OrderByDescending(i => i.Length);
|
||||
var bins = new List<Bin>();
|
||||
var length = StockLengthInches;
|
||||
|
||||
foreach (var item in items2)
|
||||
{
|
||||
Bin best_bin;
|
||||
|
||||
if (!FindBin(bins.ToArray(), item.Length, out best_bin))
|
||||
{
|
||||
if (item.Length > length)
|
||||
continue;
|
||||
|
||||
best_bin = CreateBin();
|
||||
bins.Add(best_bin);
|
||||
}
|
||||
|
||||
best_bin.Items.Add(item);
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
|
||||
private List<BinItem> GetItems()
|
||||
{
|
||||
var items2 = new List<BinItem>();
|
||||
@@ -191,17 +170,6 @@ namespace CutToLength
|
||||
return items2;
|
||||
}
|
||||
|
||||
private Bin CreateBin()
|
||||
{
|
||||
var length = StockLengthInches;
|
||||
var spacing = GetSelectedTool().Kerf;
|
||||
|
||||
return new Bin(length)
|
||||
{
|
||||
Spacing = spacing
|
||||
};
|
||||
}
|
||||
|
||||
public Tool GetSelectedTool()
|
||||
{
|
||||
return comboBox1.SelectedItem as Tool;
|
||||
@@ -235,25 +203,6 @@ namespace CutToLength
|
||||
Process.Start(file);
|
||||
}
|
||||
|
||||
private static bool FindBin(IEnumerable<Bin> bins, double length, out Bin found)
|
||||
{
|
||||
found = null;
|
||||
|
||||
foreach (var bin in bins)
|
||||
{
|
||||
if (bin.RemainingLength < length)
|
||||
continue;
|
||||
|
||||
if (found == null)
|
||||
found = bin;
|
||||
|
||||
if (bin.RemainingLength < found.RemainingLength)
|
||||
found = bin;
|
||||
}
|
||||
|
||||
return (found != null);
|
||||
}
|
||||
|
||||
private void toolStripButton1_Click(object sender, EventArgs e)
|
||||
{
|
||||
Open();
|
||||
|
||||
Reference in New Issue
Block a user