Separated nesting logic from UI

This commit is contained in:
AJ
2019-11-17 22:42:14 -05:00
parent e9802ce44a
commit 8d7185bda8
3 changed files with 80 additions and 57 deletions

View 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);
}
}

View File

@@ -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">

View File

@@ -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();