Added Toolbox to manage tools

This commit is contained in:
AJ
2023-01-14 08:39:01 -05:00
parent 2af0c5967f
commit 2ce685bb83
5 changed files with 80 additions and 50 deletions

View File

@@ -99,6 +99,7 @@
<Compile Include="Forms\ResultsForm.Designer.cs"> <Compile Include="Forms\ResultsForm.Designer.cs">
<DependentUpon>ResultsForm.cs</DependentUpon> <DependentUpon>ResultsForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Forms\Toolbox.cs" />
<Compile Include="Helper.cs" /> <Compile Include="Helper.cs" />
<Compile Include="Models\BinInputItem.cs" /> <Compile Include="Models\BinInputItem.cs" />
<Compile Include="Models\LengthItem.cs" /> <Compile Include="Models\LengthItem.cs" />

View File

@@ -9,6 +9,7 @@ using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace CutList.Forms namespace CutList.Forms
{ {
@@ -17,6 +18,7 @@ namespace CutList.Forms
private BindingList<PartInputItem> parts; private BindingList<PartInputItem> parts;
private BindingList<BinInputItem> bins; private BindingList<BinInputItem> bins;
private string documentPath; private string documentPath;
private Toolbox toolbox;
public MainForm() public MainForm()
{ {
@@ -34,27 +36,8 @@ namespace CutList.Forms
binInputItemBindingSource.DataSource = bins; binInputItemBindingSource.DataSource = bins;
binInputItemBindingSource.ListChanged += BinInputItemBindingSource_ListChanged; binInputItemBindingSource.ListChanged += BinInputItemBindingSource_ListChanged;
LoadTools(); toolbox = new Toolbox();
} comboBox1.DataSource = toolbox.Tools;
private void LoadTools()
{
if (!File.Exists(ToolsFilePath))
{
var tools = new List<Tool>
{
new Tool { Name = "Shear", Kerf = 0.0 },
new Tool { Name = "Saw", Kerf = 0.125 }
};
SaveTools(tools);
comboBox1.DataSource = tools;
}
else
{
comboBox1.DataSource = GetTools();
}
} }
private void UpdateRunButtonState() private void UpdateRunButtonState()
@@ -166,19 +149,6 @@ namespace CutList.Forms
return name; return name;
} }
private string ToolsFilePath
{
get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data\\Tools.json"); }
}
private List<Tool> GetTools()
{
var json = File.ReadAllText(ToolsFilePath);
var list = JsonConvert.DeserializeObject<List<Tool>>(json);
return list;
}
private List<BinItem> GetItems() private List<BinItem> GetItems()
{ {
var items2 = new List<BinItem>(); var items2 = new List<BinItem>();
@@ -206,12 +176,6 @@ namespace CutList.Forms
return comboBox1.SelectedItem as Tool; return comboBox1.SelectedItem as Tool;
} }
private void SaveTools(IEnumerable<Tool> tools)
{
var json = JsonConvert.SerializeObject(tools, Formatting.Indented);
File.WriteAllText(ToolsFilePath, json);
}
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
base.OnLoad(e); base.OnLoad(e);
@@ -275,7 +239,7 @@ namespace CutList.Forms
if (tools != null) if (tools != null)
{ {
SaveTools(tools); toolbox.Save();
} }
} }
@@ -333,9 +297,7 @@ namespace CutList.Forms
[JsonIgnore] [JsonIgnore]
public string SavePath { get; set; } public string SavePath { get; set; }
public List<Tool> Tools { get; set; } public List<PartInputItem> PartsToNest { get; set; }
public List<PartInputItem> PartsToNest { get; set; }
public List<BinInputItem> StockBins { get; set; } public List<BinInputItem> StockBins { get; set; }
} }

71
CutList/Forms/Toolbox.cs Normal file
View File

@@ -0,0 +1,71 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace CutList.Forms
{
public class Toolbox
{
public Toolbox()
{
Load();
}
public List<Tool> Tools { get; set; }
public string ToolsFilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data\\Tools.json");
private void LoadDefaultTools()
{
Tools = new List<Tool>
{
new Tool { Name = "Shear", Kerf = 0.0 },
new Tool { Name = "Saw", Kerf = 0.125 },
new Tool { Name = "Channel Muncher", Kerf = 0.5 },
new Tool { Name = "Custom", Kerf = 1, AllowUserToChange = true }
};
}
/// <summary>
/// Loads the tool list from the file at ToolsFilePath
/// </summary>
public void Load()
{
try
{
if (!File.Exists(ToolsFilePath))
{
LoadDefaultTools();
Save();
}
else
{
var json = File.ReadAllText(ToolsFilePath);
var list = JsonConvert.DeserializeObject<List<Tool>>(json);
Tools = list;
}
}
catch (Exception ex)
{
throw;
}
}
public void Save()
{
try
{
if (Tools == null)
return;
var json = JsonConvert.SerializeObject(Tools, Formatting.Indented);
File.WriteAllText(ToolsFilePath, json);
}
catch (Exception ex)
{
throw;
}
}
}
}

View File

@@ -1,8 +1,4 @@
using Newtonsoft.Json; namespace CutList.Models
using SawCut;
using System;
namespace CutList.Models
{ {
public class BinInputItem : LengthItem public class BinInputItem : LengthItem
{ {

View File

@@ -42,7 +42,7 @@ namespace SawCut
/// </summary> /// </summary>
public double Utilization public double Utilization
{ {
get { return UsedLength / Length; } get { return UsedLength / Length; }
} }
public override string ToString() public override string ToString()