From 90ae89802a4b80b1adf4f444205cde0c4077be11 Mon Sep 17 00:00:00 2001 From: AJ Date: Mon, 9 Dec 2024 11:15:08 -0500 Subject: [PATCH] BinFileSaver --- CutList/CutList.csproj | 1 + CutList/Forms/BinFileSaver.cs | 63 +++++++++++++++++++++++++++++++++++ CutList/Forms/ResultsForm.cs | 60 ++++++++------------------------- 3 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 CutList/Forms/BinFileSaver.cs diff --git a/CutList/CutList.csproj b/CutList/CutList.csproj index 8298152..573ace6 100644 --- a/CutList/CutList.csproj +++ b/CutList/CutList.csproj @@ -86,6 +86,7 @@ Component + Form diff --git a/CutList/Forms/BinFileSaver.cs b/CutList/Forms/BinFileSaver.cs new file mode 100644 index 0000000..e913779 --- /dev/null +++ b/CutList/Forms/BinFileSaver.cs @@ -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 _bins; + + public BinFileSaver(IEnumerable 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}"); + } + } + } +} \ No newline at end of file diff --git a/CutList/Forms/ResultsForm.cs b/CutList/Forms/ResultsForm.cs index 2d53af3..afdfb4b 100644 --- a/CutList/Forms/ResultsForm.cs +++ b/CutList/Forms/ResultsForm.cs @@ -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); } } } \ No newline at end of file