BinFileSaver
This commit is contained in:
@@ -86,6 +86,7 @@
|
||||
<Compile Include="Controls\BinLayoutView.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Forms\BinFileSaver.cs" />
|
||||
<Compile Include="Forms\DataGridViewExtensions.cs" />
|
||||
<Compile Include="Forms\MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
|
||||
63
CutList/Forms/BinFileSaver.cs
Normal file
63
CutList/Forms/BinFileSaver.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using SawCut;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace CutList.Forms
|
||||
{
|
||||
public class BinFileSaver
|
||||
{
|
||||
private IEnumerable<Bin> _bins;
|
||||
|
||||
public BinFileSaver(IEnumerable<Bin> bins)
|
||||
{
|
||||
_bins = bins ?? throw new ArgumentNullException(nameof(bins));
|
||||
}
|
||||
|
||||
public void SaveBinsToFile(string file)
|
||||
{
|
||||
using (var writer = new StreamWriter(file))
|
||||
{
|
||||
writer.AutoFlush = true;
|
||||
var max = _bins.Max(b => b.Items.Max(i => SawCut.Helper.ConvertToMixedFraction(i.Length).Length));
|
||||
var id = 1;
|
||||
|
||||
foreach (var bin in _bins)
|
||||
{
|
||||
WriteBinSummary(writer, bin, id++, max);
|
||||
}
|
||||
}
|
||||
|
||||
Process.Start(file);
|
||||
}
|
||||
|
||||
private void WriteBinSummary(StreamWriter writer, Bin bin, int id, int max)
|
||||
{
|
||||
var totalLength = SawCut.Helper.ConvertToMixedFraction(bin.Length);
|
||||
var remainingLength = SawCut.Helper.ConvertToMixedFraction(bin.RemainingLength);
|
||||
var utilization = Math.Round(bin.Utilization * 100, 2);
|
||||
|
||||
writer.WriteLine($"{id}. Length: {totalLength}, {remainingLength} remaining, {bin.Items.Count} items, {utilization}% utilization");
|
||||
WriteBinItems(writer, bin, max);
|
||||
writer.WriteLine("---------------------------------------------------------------------");
|
||||
}
|
||||
|
||||
private void WriteBinItems(StreamWriter writer, Bin bin, int max)
|
||||
{
|
||||
var groups = bin.Items.GroupBy(i => $"{i.Name} {i.Length}");
|
||||
|
||||
foreach (var group in groups)
|
||||
{
|
||||
var first = group.First();
|
||||
var count = group.Count();
|
||||
var length = SawCut.Helper.ConvertToMixedFraction(first.Length).PadLeft(max);
|
||||
var name = first.Name;
|
||||
var pcsSingularOrPlural = count == 1 ? "pc " : "pcs";
|
||||
|
||||
writer.WriteLine($" {count}{pcsSingularOrPlural} @ {length} LG Tag: {name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ namespace CutList.Forms
|
||||
public partial class ResultsForm : Form
|
||||
{
|
||||
private string filename;
|
||||
|
||||
public ResultsForm(string filename)
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -40,58 +41,25 @@ namespace CutList.Forms
|
||||
}
|
||||
|
||||
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Save(string filepath)
|
||||
{
|
||||
var writer = new BinFileSaver(Bins);
|
||||
writer.SaveBinsToFile(filepath);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var s = new SaveFileDialog();
|
||||
|
||||
s.FileName = filename;
|
||||
s.Filter = "Text File|*.txt";
|
||||
|
||||
if (s.ShowDialog() != DialogResult.OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SaveBins(s.FileName);
|
||||
}
|
||||
|
||||
private void SaveBins(string file)
|
||||
{
|
||||
var writer = new StreamWriter(file);
|
||||
writer.AutoFlush = true;
|
||||
|
||||
var max = Bins.Max(b => b.Items.Max(i => SawCut.Helper.ConvertToMixedFraction(i.Length).Length));
|
||||
var id = 1;
|
||||
|
||||
foreach (var bin in Bins)
|
||||
{
|
||||
var totalLength = SawCut.Helper.ConvertToMixedFraction(bin.Length);
|
||||
var remainingLength = SawCut.Helper.ConvertToMixedFraction(bin.RemainingLength);
|
||||
var utilitation = Math.Round(bin.Utilization * 100, 2);
|
||||
|
||||
var binDescription = $"{id++}. Length: {{totalLength}}, {remainingLength} remaining, {bin.Items.Count} items, {utilitation}% utilization";
|
||||
writer.WriteLine(binDescription);
|
||||
|
||||
var groups = bin.Items.GroupBy(i => $"{i.Name} {i.Length}");
|
||||
|
||||
foreach (var group in groups)
|
||||
{
|
||||
var first = group.First();
|
||||
var count = group.Count();
|
||||
var length = SawCut.Helper.ConvertToMixedFraction(first.Length).PadLeft(max);
|
||||
var name = first.Name;
|
||||
|
||||
var pcsSingularOrPlural = count == 1 ? "pc " : "pcs";
|
||||
|
||||
writer.WriteLine($" {count}{pcsSingularOrPlural} @ {length} LG Tag: {name}");
|
||||
}
|
||||
|
||||
writer.WriteLine("---------------------------------------------------------------------");
|
||||
//writer.WriteLine();
|
||||
}
|
||||
|
||||
writer.Close();
|
||||
|
||||
Process.Start(file);
|
||||
if (s.ShowDialog() == DialogResult.OK)
|
||||
Save(s.FileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user