Added option to OpenFileAfterSave
This commit is contained in:
@@ -11,6 +11,10 @@ namespace CutList.Forms
|
|||||||
{
|
{
|
||||||
private IEnumerable<Bin> _bins;
|
private IEnumerable<Bin> _bins;
|
||||||
|
|
||||||
|
private int PaddingWidthOfItemLength { get; set; }
|
||||||
|
|
||||||
|
public bool OpenFileAfterSave { get; set; }
|
||||||
|
|
||||||
public BinFileSaver(IEnumerable<Bin> bins)
|
public BinFileSaver(IEnumerable<Bin> bins)
|
||||||
{
|
{
|
||||||
_bins = bins ?? throw new ArgumentNullException(nameof(bins));
|
_bins = bins ?? throw new ArgumentNullException(nameof(bins));
|
||||||
@@ -21,30 +25,46 @@ namespace CutList.Forms
|
|||||||
using (var writer = new StreamWriter(file))
|
using (var writer = new StreamWriter(file))
|
||||||
{
|
{
|
||||||
writer.AutoFlush = true;
|
writer.AutoFlush = true;
|
||||||
var max = _bins.Max(b => b.Items.Max(i => SawCut.Helper.ConvertToMixedFraction(i.Length).Length));
|
PaddingWidthOfItemLength = _bins.Max(b => b.Items.Max(i => SawCut.Helper.ConvertToMixedFraction(i.Length).Length));
|
||||||
var id = 1;
|
var id = 1;
|
||||||
|
|
||||||
foreach (var bin in _bins)
|
foreach (var bin in _bins)
|
||||||
{
|
{
|
||||||
WriteBinSummary(writer, bin, id++, max);
|
WriteBinSummary(writer, bin, id++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (OpenFileAfterSave)
|
||||||
|
{
|
||||||
|
OpenFile(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OpenFile(string file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Process.Start("notepad.exe", file);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Notepad is not installed, so just open the file
|
||||||
Process.Start(file);
|
Process.Start(file);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void WriteBinSummary(StreamWriter writer, Bin bin, int id, int max)
|
private void WriteBinSummary(StreamWriter writer, Bin bin, int id)
|
||||||
{
|
{
|
||||||
var totalLength = SawCut.Helper.ConvertToMixedFraction(bin.Length);
|
var totalLength = SawCut.Helper.ConvertToMixedFraction(bin.Length);
|
||||||
var remainingLength = SawCut.Helper.ConvertToMixedFraction(bin.RemainingLength);
|
var remainingLength = SawCut.Helper.ConvertToMixedFraction(bin.RemainingLength);
|
||||||
var utilization = Math.Round(bin.Utilization * 100, 2);
|
var utilization = Math.Round(bin.Utilization * 100, 2);
|
||||||
|
|
||||||
writer.WriteLine($"{id}. Length: {totalLength}, {remainingLength} remaining, {bin.Items.Count} items, {utilization}% utilization");
|
writer.WriteLine($"{id}. Length: {totalLength}, {remainingLength} remaining, {bin.Items.Count} items, {utilization}% utilization");
|
||||||
WriteBinItems(writer, bin, max);
|
WriteBinItems(writer, bin);
|
||||||
writer.WriteLine("---------------------------------------------------------------------");
|
writer.WriteLine("---------------------------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteBinItems(StreamWriter writer, Bin bin, int max)
|
private void WriteBinItems(StreamWriter writer, Bin bin)
|
||||||
{
|
{
|
||||||
var groups = bin.Items.GroupBy(i => $"{i.Name} {i.Length}");
|
var groups = bin.Items.GroupBy(i => $"{i.Name} {i.Length}");
|
||||||
|
|
||||||
@@ -52,7 +72,7 @@ namespace CutList.Forms
|
|||||||
{
|
{
|
||||||
var first = group.First();
|
var first = group.First();
|
||||||
var count = group.Count();
|
var count = group.Count();
|
||||||
var length = SawCut.Helper.ConvertToMixedFraction(first.Length).PadLeft(max);
|
var length = SawCut.Helper.ConvertToMixedFraction(first.Length).PadLeft(PaddingWidthOfItemLength);
|
||||||
var name = first.Name;
|
var name = first.Name;
|
||||||
var pcsSingularOrPlural = count == 1 ? "pc " : "pcs";
|
var pcsSingularOrPlural = count == 1 ? "pc " : "pcs";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user