Added option to OpenFileAfterSave
This commit is contained in:
@@ -11,6 +11,10 @@ namespace CutList.Forms
|
||||
{
|
||||
private IEnumerable<Bin> _bins;
|
||||
|
||||
private int PaddingWidthOfItemLength { get; set; }
|
||||
|
||||
public bool OpenFileAfterSave { get; set; }
|
||||
|
||||
public BinFileSaver(IEnumerable<Bin> bins)
|
||||
{
|
||||
_bins = bins ?? throw new ArgumentNullException(nameof(bins));
|
||||
@@ -21,30 +25,46 @@ namespace CutList.Forms
|
||||
using (var writer = new StreamWriter(file))
|
||||
{
|
||||
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;
|
||||
|
||||
foreach (var bin in _bins)
|
||||
{
|
||||
WriteBinSummary(writer, bin, id++, max);
|
||||
WriteBinSummary(writer, bin, id++);
|
||||
}
|
||||
}
|
||||
|
||||
Process.Start(file);
|
||||
if (OpenFileAfterSave)
|
||||
{
|
||||
OpenFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteBinSummary(StreamWriter writer, Bin bin, int id, int max)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteBinSummary(StreamWriter writer, Bin bin, int id)
|
||||
{
|
||||
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);
|
||||
WriteBinItems(writer, bin);
|
||||
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}");
|
||||
|
||||
@@ -52,7 +72,7 @@ namespace CutList.Forms
|
||||
{
|
||||
var first = group.First();
|
||||
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 pcsSingularOrPlural = count == 1 ? "pc " : "pcs";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user