Moved nesting to SawCut library
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
|
||||
namespace CutToLength
|
||||
{
|
||||
public static class ArchUnits
|
||||
{
|
||||
public static double ParseToInches(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
return 0;
|
||||
|
||||
var sb = new StringBuilder(input.Trim().ToLower());
|
||||
|
||||
// replace all units with their equivelant symbols
|
||||
sb.Replace("ft", "'");
|
||||
sb.Replace("feet", "'");
|
||||
sb.Replace("foot", "'");
|
||||
sb.Replace("inches", "\"");
|
||||
sb.Replace("inch", "\"");
|
||||
sb.Replace("in", "\"");
|
||||
|
||||
var regex = new Regex("^(?<Feet>\\d+\\.?\\d*\\s*')?\\s*(?<Inches>\\d+\\.?\\d*\\s*\")?$");
|
||||
|
||||
// input manipulation is done, put the value back
|
||||
input = Fraction.ReplaceFractionsWithDecimals(sb.ToString());
|
||||
|
||||
var match2 = regex.Match(input);
|
||||
|
||||
if (!match2.Success)
|
||||
throw new Exception("Input is not in a valid format.");
|
||||
|
||||
var feet = match2.Groups["Feet"];
|
||||
var inches = match2.Groups["Inches"];
|
||||
|
||||
var totalInches = 0.0;
|
||||
|
||||
if (feet.Success)
|
||||
{
|
||||
var x = double.Parse(feet.Value.Remove(feet.Length - 1));
|
||||
totalInches += x * 12;
|
||||
}
|
||||
|
||||
if (inches.Success)
|
||||
{
|
||||
var x = double.Parse(inches.Value.Remove(inches.Length - 1));
|
||||
totalInches += x;
|
||||
}
|
||||
|
||||
return Math.Round(totalInches, 8);
|
||||
}
|
||||
|
||||
public static double ParseToFeet(string input)
|
||||
{
|
||||
var inches = ParseToInches(input);
|
||||
return Math.Round(inches / 12.0, 8);
|
||||
}
|
||||
|
||||
public static string FormatFromInches(double totalInches)
|
||||
{
|
||||
var feet = Math.Floor(totalInches / 12.0);
|
||||
var inches = totalInches - (feet * 12.0);
|
||||
|
||||
if (feet > 0)
|
||||
{
|
||||
return $"{feet}' {inches}\"";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{inches}\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace CutToLength
|
||||
{
|
||||
public class Bin
|
||||
{
|
||||
public List<BinItem> Items;
|
||||
|
||||
public Bin(double length)
|
||||
{
|
||||
Items = new List<BinItem>();
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public double Spacing { get; set; }
|
||||
|
||||
public double Length { get; set; }
|
||||
|
||||
public double UsedLength
|
||||
{
|
||||
get
|
||||
{
|
||||
return Math.Round(Items.Sum(i => i.Length) + Spacing * Items.Count, 8);
|
||||
}
|
||||
}
|
||||
|
||||
public double RemainingLength
|
||||
{
|
||||
get { return Math.Round(Length - UsedLength, 8); }
|
||||
}
|
||||
|
||||
public double Utilization
|
||||
{
|
||||
get { return UsedLength / Length; }
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var totalLength = Math.Round(Length, 4);
|
||||
var remainingLength = Math.Round(RemainingLength, 4);
|
||||
var utilitation = Math.Round(Utilization * 100, 2);
|
||||
|
||||
return $"Length: {totalLength}\", {remainingLength}\" remaining, {Items.Count} items, {utilitation}% utilization";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace CutToLength
|
||||
{
|
||||
public class BinItem
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public double Length { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Drawing;
|
||||
using SawCut;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Windows.Forms;
|
||||
|
||||
|
||||
@@ -83,14 +83,9 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ArchUnits.cs" />
|
||||
<Compile Include="Nesting\BestFitEngine.cs" />
|
||||
<Compile Include="Bin.cs" />
|
||||
<Compile Include="BinItem.cs" />
|
||||
<Compile Include="Controls\BinLayoutView.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Fraction.cs" />
|
||||
<Compile Include="Forms\MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
@@ -103,13 +98,10 @@
|
||||
<Compile Include="Forms\ResultsForm.Designer.cs">
|
||||
<DependentUpon>ResultsForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Nesting\Engine2.cs" />
|
||||
<Compile Include="Nesting\IEngine.cs" />
|
||||
<Compile Include="Nesting\Result.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Tool.cs" />
|
||||
<Compile Include="UIItem.cs" />
|
||||
<Compile Include="Models\UIItem.cs" />
|
||||
<EmbeddedResource Include="Forms\MainForm.resx">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
@@ -168,6 +160,12 @@
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SawCut\SawCut.csproj">
|
||||
<Project>{3D873FF0-6930-4BCE-A5A9-DA5C20354DEE}</Project>
|
||||
<Name>SawCut</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using CutToLength.Nesting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json;
|
||||
using SawCut;
|
||||
using SawCut.Nesting;
|
||||
using SimpleExpressionEvaluator;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
116
CutToLength/Forms/ResultsForm.Designer.cs
generated
116
CutToLength/Forms/ResultsForm.Designer.cs
generated
@@ -29,23 +29,24 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.components = new System.ComponentModel.Container();
|
||||
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
|
||||
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
|
||||
this.dataGridView1 = new System.Windows.Forms.DataGridView();
|
||||
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
|
||||
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
|
||||
this.dataGridView2 = new System.Windows.Forms.DataGridView();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.spacingDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.lengthDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.usedLengthDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.remainingLengthDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.utilizationDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.binBindingSource = new System.Windows.Forms.BindingSource(this.components);
|
||||
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
|
||||
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
|
||||
this.dataGridView2 = new System.Windows.Forms.DataGridView();
|
||||
this.binLayoutView1 = new CutToLength.Controls.BinLayoutView();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.uIItemBindingSource = new System.Windows.Forms.BindingSource(this.components);
|
||||
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
|
||||
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.binBindingSource)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
|
||||
this.splitContainer1.Panel1.SuspendLayout();
|
||||
this.splitContainer1.Panel2.SuspendLayout();
|
||||
@@ -55,7 +56,6 @@
|
||||
this.splitContainer2.Panel2.SuspendLayout();
|
||||
this.splitContainer2.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.binBindingSource)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.uIItemBindingSource)).BeginInit();
|
||||
this.menuStrip1.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
@@ -84,6 +84,46 @@
|
||||
this.dataGridView1.TabIndex = 0;
|
||||
this.dataGridView1.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_RowEnter);
|
||||
//
|
||||
// spacingDataGridViewTextBoxColumn
|
||||
//
|
||||
this.spacingDataGridViewTextBoxColumn.DataPropertyName = "Spacing";
|
||||
this.spacingDataGridViewTextBoxColumn.HeaderText = "Spacing";
|
||||
this.spacingDataGridViewTextBoxColumn.Name = "spacingDataGridViewTextBoxColumn";
|
||||
//
|
||||
// lengthDataGridViewTextBoxColumn
|
||||
//
|
||||
this.lengthDataGridViewTextBoxColumn.DataPropertyName = "Length";
|
||||
this.lengthDataGridViewTextBoxColumn.HeaderText = "Length";
|
||||
this.lengthDataGridViewTextBoxColumn.Name = "lengthDataGridViewTextBoxColumn";
|
||||
//
|
||||
// usedLengthDataGridViewTextBoxColumn
|
||||
//
|
||||
this.usedLengthDataGridViewTextBoxColumn.DataPropertyName = "UsedLength";
|
||||
this.usedLengthDataGridViewTextBoxColumn.HeaderText = "Used Length";
|
||||
this.usedLengthDataGridViewTextBoxColumn.Name = "usedLengthDataGridViewTextBoxColumn";
|
||||
this.usedLengthDataGridViewTextBoxColumn.ReadOnly = true;
|
||||
//
|
||||
// remainingLengthDataGridViewTextBoxColumn
|
||||
//
|
||||
this.remainingLengthDataGridViewTextBoxColumn.DataPropertyName = "RemainingLength";
|
||||
this.remainingLengthDataGridViewTextBoxColumn.HeaderText = "Remaining Length";
|
||||
this.remainingLengthDataGridViewTextBoxColumn.Name = "remainingLengthDataGridViewTextBoxColumn";
|
||||
this.remainingLengthDataGridViewTextBoxColumn.ReadOnly = true;
|
||||
this.remainingLengthDataGridViewTextBoxColumn.Width = 150;
|
||||
//
|
||||
// utilizationDataGridViewTextBoxColumn
|
||||
//
|
||||
this.utilizationDataGridViewTextBoxColumn.DataPropertyName = "Utilization";
|
||||
dataGridViewCellStyle1.Format = "P2";
|
||||
this.utilizationDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle1;
|
||||
this.utilizationDataGridViewTextBoxColumn.HeaderText = "Utilization";
|
||||
this.utilizationDataGridViewTextBoxColumn.Name = "utilizationDataGridViewTextBoxColumn";
|
||||
this.utilizationDataGridViewTextBoxColumn.ReadOnly = true;
|
||||
//
|
||||
// binBindingSource
|
||||
//
|
||||
this.binBindingSource.DataSource = typeof(SawCut.Bin);
|
||||
//
|
||||
// splitContainer1
|
||||
//
|
||||
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
@@ -136,6 +176,17 @@
|
||||
this.dataGridView2.Size = new System.Drawing.Size(276, 216);
|
||||
this.dataGridView2.TabIndex = 1;
|
||||
//
|
||||
// binLayoutView1
|
||||
//
|
||||
this.binLayoutView1.BackColor = System.Drawing.Color.White;
|
||||
this.binLayoutView1.Bin = null;
|
||||
this.binLayoutView1.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.binLayoutView1.Location = new System.Drawing.Point(0, 0);
|
||||
this.binLayoutView1.Name = "binLayoutView1";
|
||||
this.binLayoutView1.Size = new System.Drawing.Size(714, 216);
|
||||
this.binLayoutView1.TabIndex = 1;
|
||||
this.binLayoutView1.Text = "class11";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.BackColor = System.Drawing.Color.LightSlateGray;
|
||||
@@ -149,57 +200,6 @@
|
||||
this.label1.Text = "Items";
|
||||
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
|
||||
//
|
||||
// spacingDataGridViewTextBoxColumn
|
||||
//
|
||||
this.spacingDataGridViewTextBoxColumn.DataPropertyName = "Spacing";
|
||||
this.spacingDataGridViewTextBoxColumn.HeaderText = "Spacing";
|
||||
this.spacingDataGridViewTextBoxColumn.Name = "spacingDataGridViewTextBoxColumn";
|
||||
//
|
||||
// lengthDataGridViewTextBoxColumn
|
||||
//
|
||||
this.lengthDataGridViewTextBoxColumn.DataPropertyName = "Length";
|
||||
this.lengthDataGridViewTextBoxColumn.HeaderText = "Length";
|
||||
this.lengthDataGridViewTextBoxColumn.Name = "lengthDataGridViewTextBoxColumn";
|
||||
//
|
||||
// usedLengthDataGridViewTextBoxColumn
|
||||
//
|
||||
this.usedLengthDataGridViewTextBoxColumn.DataPropertyName = "UsedLength";
|
||||
this.usedLengthDataGridViewTextBoxColumn.HeaderText = "Used Length";
|
||||
this.usedLengthDataGridViewTextBoxColumn.Name = "usedLengthDataGridViewTextBoxColumn";
|
||||
this.usedLengthDataGridViewTextBoxColumn.ReadOnly = true;
|
||||
//
|
||||
// remainingLengthDataGridViewTextBoxColumn
|
||||
//
|
||||
this.remainingLengthDataGridViewTextBoxColumn.DataPropertyName = "RemainingLength";
|
||||
this.remainingLengthDataGridViewTextBoxColumn.HeaderText = "Remaining Length";
|
||||
this.remainingLengthDataGridViewTextBoxColumn.Name = "remainingLengthDataGridViewTextBoxColumn";
|
||||
this.remainingLengthDataGridViewTextBoxColumn.ReadOnly = true;
|
||||
this.remainingLengthDataGridViewTextBoxColumn.Width = 150;
|
||||
//
|
||||
// utilizationDataGridViewTextBoxColumn
|
||||
//
|
||||
this.utilizationDataGridViewTextBoxColumn.DataPropertyName = "Utilization";
|
||||
dataGridViewCellStyle5.Format = "P2";
|
||||
this.utilizationDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle5;
|
||||
this.utilizationDataGridViewTextBoxColumn.HeaderText = "Utilization";
|
||||
this.utilizationDataGridViewTextBoxColumn.Name = "utilizationDataGridViewTextBoxColumn";
|
||||
this.utilizationDataGridViewTextBoxColumn.ReadOnly = true;
|
||||
//
|
||||
// binBindingSource
|
||||
//
|
||||
this.binBindingSource.DataSource = typeof(CutToLength.Bin);
|
||||
//
|
||||
// binLayoutView1
|
||||
//
|
||||
this.binLayoutView1.BackColor = System.Drawing.Color.White;
|
||||
this.binLayoutView1.Bin = null;
|
||||
this.binLayoutView1.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.binLayoutView1.Location = new System.Drawing.Point(0, 0);
|
||||
this.binLayoutView1.Name = "binLayoutView1";
|
||||
this.binLayoutView1.Size = new System.Drawing.Size(714, 216);
|
||||
this.binLayoutView1.TabIndex = 1;
|
||||
this.binLayoutView1.Text = "class11";
|
||||
//
|
||||
// uIItemBindingSource
|
||||
//
|
||||
this.uIItemBindingSource.DataSource = typeof(CutToLength.UIItem);
|
||||
@@ -234,6 +234,7 @@
|
||||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
|
||||
this.Text = "Results";
|
||||
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.binBindingSource)).EndInit();
|
||||
this.splitContainer1.Panel1.ResumeLayout(false);
|
||||
this.splitContainer1.Panel2.ResumeLayout(false);
|
||||
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
|
||||
@@ -243,7 +244,6 @@
|
||||
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
|
||||
this.splitContainer2.ResumeLayout(false);
|
||||
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.binBindingSource)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.uIItemBindingSource)).EndInit();
|
||||
this.menuStrip1.ResumeLayout(false);
|
||||
this.menuStrip1.PerformLayout();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SawCut;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
|
||||
@@ -120,13 +120,10 @@
|
||||
<metadata name="binBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="binBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="uIItemBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>165, 17</value>
|
||||
</metadata>
|
||||
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>560, 17</value>
|
||||
<value>330, 17</value>
|
||||
</metadata>
|
||||
</root>
|
||||
@@ -1,85 +0,0 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace CutToLength
|
||||
{
|
||||
public static class Fraction
|
||||
{
|
||||
public static readonly Regex FractionRegex = new Regex(@"((?<WholeNum>\d+)(\ |-))?(?<Fraction>\d+\/\d+)");
|
||||
|
||||
public static bool IsValid(string s)
|
||||
{
|
||||
return FractionRegex.IsMatch(s);
|
||||
}
|
||||
|
||||
public static double Parse(string s)
|
||||
{
|
||||
var match = FractionRegex.Match(s);
|
||||
|
||||
if (!match.Success)
|
||||
throw new Exception("Invalid format.");
|
||||
|
||||
var value = 0.0;
|
||||
|
||||
var wholeNumGroup = match.Groups["WholeNum"];
|
||||
var fractionGroup = match.Groups["Fraction"];
|
||||
|
||||
if (wholeNumGroup.Success)
|
||||
{
|
||||
value = double.Parse(wholeNumGroup.Value);
|
||||
}
|
||||
|
||||
if (fractionGroup.Success)
|
||||
{
|
||||
var parts = fractionGroup.Value.Split('/');
|
||||
var numerator = double.Parse(parts[0]);
|
||||
var denominator = double.Parse(parts[1]);
|
||||
|
||||
value += Math.Round(numerator / denominator, 8);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static string ReplaceFractionsWithDecimals(string input)
|
||||
{
|
||||
var sb = new StringBuilder(input);
|
||||
|
||||
// find all matches and sort by descending index number to avoid
|
||||
// changing all previous index numbers when the fraction is replaced
|
||||
// with the decimal equivalent.
|
||||
var fractionMatches = FractionRegex.Matches(sb.ToString())
|
||||
.Cast<Match>()
|
||||
.OrderByDescending(m => m.Index);
|
||||
|
||||
foreach (var fractionMatch in fractionMatches)
|
||||
{
|
||||
// convert the fraction to a decimal value
|
||||
var decimalValue = Parse(fractionMatch.Value);
|
||||
|
||||
// remove the fraction and insert the decimal value in its place.
|
||||
sb.Remove(fractionMatch.Index, fractionMatch.Length);
|
||||
sb.Insert(fractionMatch.Index, decimalValue);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static bool TryParse(string s, out double fraction)
|
||||
{
|
||||
try
|
||||
{
|
||||
fraction = Parse(s);
|
||||
}
|
||||
catch
|
||||
{
|
||||
fraction = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Newtonsoft.Json;
|
||||
using SawCut;
|
||||
using System;
|
||||
|
||||
namespace CutToLength
|
||||
@@ -1,91 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace CutToLength.Nesting
|
||||
{
|
||||
public class BestFitEngine : IEngine
|
||||
{
|
||||
public double StockLength { get; set; }
|
||||
|
||||
public double Spacing { get; set; }
|
||||
|
||||
private List<BinItem> Items { get; set; }
|
||||
|
||||
public Result Pack(List<BinItem> items)
|
||||
{
|
||||
if (StockLength <= 0)
|
||||
throw new Exception("Stock length must be greater than 0");
|
||||
|
||||
Items = items.OrderByDescending(i => i.Length).ToList();
|
||||
|
||||
var result = new Result();
|
||||
result.ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList();
|
||||
|
||||
foreach (var item in result.ItemsNotUsed)
|
||||
{
|
||||
Items.Remove(item);
|
||||
}
|
||||
|
||||
result.Bins = GetBins();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Bin> GetBins()
|
||||
{
|
||||
var bins = new List<Bin>();
|
||||
|
||||
foreach (var item in Items)
|
||||
{
|
||||
Bin best_bin;
|
||||
|
||||
if (!FindBin(bins.ToArray(), item.Length, out best_bin))
|
||||
{
|
||||
if (item.Length > StockLength)
|
||||
continue;
|
||||
|
||||
best_bin = CreateBin();
|
||||
bins.Add(best_bin);
|
||||
}
|
||||
|
||||
best_bin.Items.Add(item);
|
||||
}
|
||||
|
||||
return bins
|
||||
.OrderByDescending(b => b.Utilization)
|
||||
.ThenBy(b => b.Items.Count)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private Bin CreateBin()
|
||||
{
|
||||
var length = StockLength;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,146 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
|
||||
namespace CutToLength.Nesting
|
||||
{
|
||||
public class Engine2 : IEngine
|
||||
{
|
||||
public double StockLength { get; set; }
|
||||
|
||||
public double Spacing { get; set; }
|
||||
|
||||
private List<BinItem> Items { get; set; }
|
||||
|
||||
public Result Pack(List<BinItem> items)
|
||||
{
|
||||
if (StockLength <= 0)
|
||||
throw new Exception("Stock length must be greater than 0");
|
||||
|
||||
Items = items.OrderByDescending(i => i.Length).ToList();
|
||||
|
||||
var result = new Result();
|
||||
result.ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList();
|
||||
|
||||
foreach (var item in result.ItemsNotUsed)
|
||||
{
|
||||
Items.Remove(item);
|
||||
}
|
||||
|
||||
result.Bins = GetBins();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Bin> GetBins()
|
||||
{
|
||||
var bins = new List<Bin>();
|
||||
|
||||
while (Items.Count > 0)
|
||||
{
|
||||
var bin = new Bin(StockLength);
|
||||
bin.Spacing = Spacing;
|
||||
|
||||
FillBin(bin);
|
||||
|
||||
int count = 0;
|
||||
|
||||
while (TryImprovePacking(bin))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
bins.Add(bin);
|
||||
}
|
||||
|
||||
return bins
|
||||
.OrderByDescending(b => b.Utilization)
|
||||
.ThenBy(b => b.Items.Count)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private void FillBin(Bin bin)
|
||||
{
|
||||
for (int i = 0; i < Items.Count; i++)
|
||||
{
|
||||
var item = Items[i];
|
||||
|
||||
if (bin.RemainingLength >= item.Length)
|
||||
bin.Items.Add(item);
|
||||
}
|
||||
|
||||
foreach (var item in bin.Items)
|
||||
{
|
||||
Items.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryImprovePacking(Bin bin)
|
||||
{
|
||||
if (bin.Items.Count == 0)
|
||||
return false;
|
||||
|
||||
if (Items.Count < 2)
|
||||
return false;
|
||||
|
||||
var lengthGroups = bin.Items
|
||||
.OrderByDescending(i => i.Length)
|
||||
.GroupBy(i => i.Length)
|
||||
.Skip(1);
|
||||
|
||||
var minItemLength = Items.Min(i => i.Length);
|
||||
|
||||
foreach (var group in lengthGroups)
|
||||
{
|
||||
var minRemainingLength = bin.RemainingLength;
|
||||
|
||||
var firstItem = group.First();
|
||||
bin.Items.Remove(firstItem);
|
||||
|
||||
for (int i = 0; i < Items.Count; i++)
|
||||
{
|
||||
var item1 = Items[i];
|
||||
|
||||
if (Items[i].Length > bin.RemainingLength)
|
||||
continue;
|
||||
|
||||
var bin2 = new Bin(bin.RemainingLength);
|
||||
bin2.Spacing = bin.Spacing;
|
||||
bin2.Items.Add(item1);
|
||||
|
||||
for (int j = i + 1; j < Items.Count; j++)
|
||||
{
|
||||
if (bin2.RemainingLength < minItemLength)
|
||||
break;
|
||||
|
||||
var item2 = Items[j];
|
||||
|
||||
if (item2.Length > bin2.RemainingLength)
|
||||
continue;
|
||||
|
||||
bin2.Items.Add(item2);
|
||||
}
|
||||
|
||||
if (bin2.RemainingLength < minRemainingLength)
|
||||
{
|
||||
Items.Add(firstItem);
|
||||
bin.Items.AddRange(bin2.Items);
|
||||
|
||||
foreach (var item in bin2.Items)
|
||||
{
|
||||
Items.Remove(item);
|
||||
}
|
||||
|
||||
// improvement made
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bin.Items.Add(firstItem);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CutToLength.Nesting
|
||||
{
|
||||
public interface IEngine
|
||||
{
|
||||
Result Pack(List<BinItem> items);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CutToLength.Nesting
|
||||
{
|
||||
public class Result
|
||||
{
|
||||
public Result()
|
||||
{
|
||||
ItemsNotUsed = new List<BinItem>();
|
||||
}
|
||||
|
||||
public List<BinItem> ItemsNotUsed { get; set; }
|
||||
|
||||
public List<Bin> Bins { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user