From a1ad16bc527c4ea20fce1967c6caead14c2d27fa Mon Sep 17 00:00:00 2001 From: AJ Date: Thu, 2 Jan 2025 23:21:34 -0500 Subject: [PATCH] Document --- CutList/CutList.csproj | 1 + CutList/Forms/Document.cs | 77 +++++++ CutList/Forms/MainForm.Designer.cs | 262 +++++++++++----------- CutList/Forms/MainForm.cs | 325 ++++++++++++++-------------- SawCut/Nesting/AdvancedFitEngine.cs | 12 +- 5 files changed, 383 insertions(+), 294 deletions(-) create mode 100644 CutList/Forms/Document.cs diff --git a/CutList/CutList.csproj b/CutList/CutList.csproj index 573ace6..ff8f2c1 100644 --- a/CutList/CutList.csproj +++ b/CutList/CutList.csproj @@ -88,6 +88,7 @@ + Form diff --git a/CutList/Forms/Document.cs b/CutList/Forms/Document.cs new file mode 100644 index 0000000..e44ef5f --- /dev/null +++ b/CutList/Forms/Document.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows.Forms; +using System.Xml.Linq; +using CutList.Models; +using Newtonsoft.Json; + +namespace CutList.Forms +{ + public class Document + { + public Document() + { + PartsToNest = new List(); + StockBins = new List(); + } + + [JsonIgnore] + public string LastFilePath { get; private set; } + + public List PartsToNest { get; set; } + + public List StockBins { get; set; } + + public Tool Tool { get; set; } + + public void Save(string filePath) + { + try + { + var json = JsonConvert.SerializeObject(this, Formatting.Indented); + File.WriteAllText(filePath, json); + LastFilePath = filePath; + } + catch (Exception ex) + { + MessageBox.Show($"Failed to save file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + public static Document Load(string filePath) + { + try + { + var json = File.ReadAllText(filePath); + var document = JsonConvert.DeserializeObject(json); + document.LastFilePath = filePath; + return document; + } + catch (Exception ex) + { + MessageBox.Show($"Failed to load file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } + } + + public bool Validate(out string validationMessage) + { + if (PartsToNest == null || !PartsToNest.Any()) + { + validationMessage = "No parts to nest."; + return false; + } + + if (StockBins == null || !StockBins.Any()) + { + validationMessage = "No stock bins available."; + return false; + } + + validationMessage = string.Empty; + return true; + } + } +} \ No newline at end of file diff --git a/CutList/Forms/MainForm.Designer.cs b/CutList/Forms/MainForm.Designer.cs index 507398d..6af3bcb 100644 --- a/CutList/Forms/MainForm.Designer.cs +++ b/CutList/Forms/MainForm.Designer.cs @@ -29,43 +29,43 @@ private void InitializeComponent() { this.components = new System.ComponentModel.Container(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.nameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.lengthDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.quantityDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.TotalLength = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.itemBindingSource = new System.Windows.Forms.BindingSource(this.components); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); - this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); - this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); + this.newDocumentButton = new System.Windows.Forms.ToolStripButton(); + this.openFileButton = new System.Windows.Forms.ToolStripButton(); this.saveButton = new System.Windows.Forms.ToolStripButton(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.runButton = new System.Windows.Forms.ToolStripButton(); - this.comboBox1 = new System.Windows.Forms.ComboBox(); - this.textBox1 = new System.Windows.Forms.TextBox(); - this.label3 = new System.Windows.Forms.Label(); - this.label4 = new System.Windows.Forms.Label(); + this.loadExampleDataButton = new System.Windows.Forms.ToolStripButton(); + this.cutMethodComboBox = new System.Windows.Forms.ComboBox(); + this.cutWidthTextBox = new System.Windows.Forms.TextBox(); + this.cutMethodLabel = new System.Windows.Forms.Label(); + this.cutWidthLabel = new System.Windows.Forms.Label(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage2 = new System.Windows.Forms.TabPage(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.dataGridView2 = new System.Windows.Forms.DataGridView(); - this.TotalLengthString = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.toolStripButton3 = new System.Windows.Forms.ToolStripButton(); - this.nameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.lengthDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.quantityDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.itemBindingSource = new System.Windows.Forms.BindingSource(this.components); this.lengthInputValueDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.quantityDataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.TotalLengthString = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.priorityDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.binInputItemBindingSource = new System.Windows.Forms.BindingSource(this.components); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.itemBindingSource)).BeginInit(); this.toolStrip1.SuspendLayout(); this.tabControl1.SuspendLayout(); this.tabPage2.SuspendLayout(); this.tabPage1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.itemBindingSource)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.binInputItemBindingSource)).BeginInit(); this.SuspendLayout(); // @@ -98,6 +98,31 @@ this.dataGridView1.CellValidated += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit); this.dataGridView1.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView1_DataError); // + // nameDataGridViewTextBoxColumn + // + this.nameDataGridViewTextBoxColumn.DataPropertyName = "Name"; + this.nameDataGridViewTextBoxColumn.HeaderText = "Name"; + this.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn"; + this.nameDataGridViewTextBoxColumn.Width = 200; + // + // lengthDataGridViewTextBoxColumn + // + this.lengthDataGridViewTextBoxColumn.DataPropertyName = "LengthInputValue"; + dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; + this.lengthDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle1; + this.lengthDataGridViewTextBoxColumn.HeaderText = "Length"; + this.lengthDataGridViewTextBoxColumn.Name = "lengthDataGridViewTextBoxColumn"; + this.lengthDataGridViewTextBoxColumn.Width = 120; + // + // quantityDataGridViewTextBoxColumn + // + this.quantityDataGridViewTextBoxColumn.DataPropertyName = "Quantity"; + dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; + this.quantityDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle2; + this.quantityDataGridViewTextBoxColumn.HeaderText = "Qty"; + this.quantityDataGridViewTextBoxColumn.Name = "quantityDataGridViewTextBoxColumn"; + this.quantityDataGridViewTextBoxColumn.Width = 50; + // // TotalLength // this.TotalLength.DataPropertyName = "TotalLengthString"; @@ -110,15 +135,19 @@ this.TotalLength.ReadOnly = true; this.TotalLength.Width = 150; // + // itemBindingSource + // + this.itemBindingSource.DataSource = typeof(CutList.Models.PartInputItem); + // // toolStrip1 // this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripButton2, - this.toolStripButton1, + this.newDocumentButton, + this.openFileButton, this.saveButton, this.toolStripSeparator1, this.runButton, - this.toolStripButton3}); + this.loadExampleDataButton}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.Size = new System.Drawing.Size(880, 39); @@ -127,27 +156,27 @@ // // toolStripButton2 // - this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; - this.toolStripButton2.Image = global::CutList.Properties.Resources.gnome_document_new; - this.toolStripButton2.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; - this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta; - this.toolStripButton2.Name = "toolStripButton2"; - this.toolStripButton2.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); - this.toolStripButton2.Size = new System.Drawing.Size(46, 36); - this.toolStripButton2.Text = "Open"; - this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click); + this.newDocumentButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; + this.newDocumentButton.Image = global::CutList.Properties.Resources.gnome_document_new; + this.newDocumentButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; + this.newDocumentButton.ImageTransparentColor = System.Drawing.Color.Magenta; + this.newDocumentButton.Name = "newDocumentButton"; + this.newDocumentButton.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.newDocumentButton.Size = new System.Drawing.Size(46, 36); + this.newDocumentButton.Text = "New"; + this.newDocumentButton.Click += new System.EventHandler(this.newDocumentButton_Click); // // toolStripButton1 // - this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; - this.toolStripButton1.Image = global::CutList.Properties.Resources.Open_Folder_32; - this.toolStripButton1.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; - this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta; - this.toolStripButton1.Name = "toolStripButton1"; - this.toolStripButton1.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); - this.toolStripButton1.Size = new System.Drawing.Size(46, 36); - this.toolStripButton1.Text = "Open"; - this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click); + this.openFileButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; + this.openFileButton.Image = global::CutList.Properties.Resources.Open_Folder_32; + this.openFileButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; + this.openFileButton.ImageTransparentColor = System.Drawing.Color.Magenta; + this.openFileButton.Name = "openFileButton"; + this.openFileButton.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.openFileButton.Size = new System.Drawing.Size(46, 36); + this.openFileButton.Text = "Open"; + this.openFileButton.Click += new System.EventHandler(this.openFileButton_Click); // // saveButton // @@ -178,45 +207,59 @@ this.runButton.Text = "Run"; this.runButton.Click += new System.EventHandler(this.runButton_Click); // + // toolStripButton3 + // + this.loadExampleDataButton.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right; + this.loadExampleDataButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; + this.loadExampleDataButton.ForeColor = System.Drawing.Color.DimGray; + this.loadExampleDataButton.Image = global::CutList.Properties.Resources.Circled_Play_32; + this.loadExampleDataButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; + this.loadExampleDataButton.ImageTransparentColor = System.Drawing.Color.Magenta; + this.loadExampleDataButton.Name = "toolStripButton3"; + this.loadExampleDataButton.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.loadExampleDataButton.Size = new System.Drawing.Size(121, 36); + this.loadExampleDataButton.Text = "Load Example Data"; + this.loadExampleDataButton.Click += new System.EventHandler(this.loadExampleDataButton_Click); + // // comboBox1 // - this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBox1.FormattingEnabled = true; - this.comboBox1.Location = new System.Drawing.Point(107, 22); - this.comboBox1.Name = "comboBox1"; - this.comboBox1.Size = new System.Drawing.Size(184, 25); - this.comboBox1.TabIndex = 7; - this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); + this.cutMethodComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.cutMethodComboBox.FormattingEnabled = true; + this.cutMethodComboBox.Location = new System.Drawing.Point(107, 22); + this.cutMethodComboBox.Name = "comboBox1"; + this.cutMethodComboBox.Size = new System.Drawing.Size(184, 25); + this.cutMethodComboBox.TabIndex = 7; + this.cutMethodComboBox.SelectedIndexChanged += new System.EventHandler(this.cutMethodComboBox_SelectedIndexChanged); // // textBox1 // - this.textBox1.Location = new System.Drawing.Point(107, 53); - this.textBox1.Name = "textBox1"; - this.textBox1.Size = new System.Drawing.Size(184, 25); - this.textBox1.TabIndex = 9; - this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); + this.cutWidthTextBox.Location = new System.Drawing.Point(107, 53); + this.cutWidthTextBox.Name = "textBox1"; + this.cutWidthTextBox.Size = new System.Drawing.Size(184, 25); + this.cutWidthTextBox.TabIndex = 9; + this.cutWidthTextBox.TextChanged += new System.EventHandler(this.cutWidthTextBox_TextChanged); // // label3 // - this.label3.AutoSize = true; - this.label3.Font = new System.Drawing.Font("Segoe UI Semibold", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.label3.ForeColor = System.Drawing.Color.Blue; - this.label3.Location = new System.Drawing.Point(20, 25); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(81, 17); - this.label3.TabIndex = 6; - this.label3.Text = "Cut method"; + this.cutMethodLabel.AutoSize = true; + this.cutMethodLabel.Font = new System.Drawing.Font("Segoe UI Semibold", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.cutMethodLabel.ForeColor = System.Drawing.Color.Blue; + this.cutMethodLabel.Location = new System.Drawing.Point(20, 25); + this.cutMethodLabel.Name = "label3"; + this.cutMethodLabel.Size = new System.Drawing.Size(81, 17); + this.cutMethodLabel.TabIndex = 6; + this.cutMethodLabel.Text = "Cut method"; // // label4 // - this.label4.AutoSize = true; - this.label4.Font = new System.Drawing.Font("Segoe UI Semibold", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.label4.ForeColor = System.Drawing.Color.Blue; - this.label4.Location = new System.Drawing.Point(34, 56); - this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(67, 17); - this.label4.TabIndex = 8; - this.label4.Text = "Cut width"; + this.cutWidthLabel.AutoSize = true; + this.cutWidthLabel.Font = new System.Drawing.Font("Segoe UI Semibold", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.cutWidthLabel.ForeColor = System.Drawing.Color.Blue; + this.cutWidthLabel.Location = new System.Drawing.Point(34, 56); + this.cutWidthLabel.Name = "label4"; + this.cutWidthLabel.Size = new System.Drawing.Size(67, 17); + this.cutWidthLabel.TabIndex = 8; + this.cutWidthLabel.Text = "Cut width"; // // tabControl1 // @@ -246,14 +289,14 @@ // tabPage1 // this.tabPage1.Controls.Add(this.dataGridView2); - this.tabPage1.Controls.Add(this.textBox1); - this.tabPage1.Controls.Add(this.comboBox1); - this.tabPage1.Controls.Add(this.label4); - this.tabPage1.Controls.Add(this.label3); - this.tabPage1.Location = new System.Drawing.Point(4, 26); + this.tabPage1.Controls.Add(this.cutWidthTextBox); + this.tabPage1.Controls.Add(this.cutMethodComboBox); + this.tabPage1.Controls.Add(this.cutWidthLabel); + this.tabPage1.Controls.Add(this.cutMethodLabel); + this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = "tabPage1"; this.tabPage1.Padding = new System.Windows.Forms.Padding(3); - this.tabPage1.Size = new System.Drawing.Size(848, 465); + this.tabPage1.Size = new System.Drawing.Size(848, 469); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "STOCK LENGTHS"; this.tabPage1.UseVisualStyleBackColor = true; @@ -281,62 +324,10 @@ this.dataGridView2.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single; this.dataGridView2.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing; this.dataGridView2.RowTemplate.Height = 26; - this.dataGridView2.Size = new System.Drawing.Size(836, 331); + this.dataGridView2.Size = new System.Drawing.Size(836, 327); this.dataGridView2.TabIndex = 12; - this.dataGridView2.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellContentClick); this.dataGridView2.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellEndEdit); // - // TotalLengthString - // - this.TotalLengthString.DataPropertyName = "TotalLengthString"; - dataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Info; - this.TotalLengthString.DefaultCellStyle = dataGridViewCellStyle4; - this.TotalLengthString.HeaderText = "Total Length"; - this.TotalLengthString.Name = "TotalLengthString"; - this.TotalLengthString.ReadOnly = true; - // - // toolStripButton3 - // - this.toolStripButton3.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right; - this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.toolStripButton3.Image = global::CutList.Properties.Resources.Circled_Play_32; - this.toolStripButton3.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None; - this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta; - this.toolStripButton3.Name = "toolStripButton3"; - this.toolStripButton3.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); - this.toolStripButton3.Size = new System.Drawing.Size(121, 36); - this.toolStripButton3.Text = "Load Example Data"; - this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton3_Click); - // - // nameDataGridViewTextBoxColumn - // - this.nameDataGridViewTextBoxColumn.DataPropertyName = "Name"; - this.nameDataGridViewTextBoxColumn.HeaderText = "Name"; - this.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn"; - this.nameDataGridViewTextBoxColumn.Width = 200; - // - // lengthDataGridViewTextBoxColumn - // - this.lengthDataGridViewTextBoxColumn.DataPropertyName = "LengthInputValue"; - dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; - this.lengthDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle1; - this.lengthDataGridViewTextBoxColumn.HeaderText = "Length"; - this.lengthDataGridViewTextBoxColumn.Name = "lengthDataGridViewTextBoxColumn"; - this.lengthDataGridViewTextBoxColumn.Width = 120; - // - // quantityDataGridViewTextBoxColumn - // - this.quantityDataGridViewTextBoxColumn.DataPropertyName = "Quantity"; - dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; - this.quantityDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle2; - this.quantityDataGridViewTextBoxColumn.HeaderText = "Qty"; - this.quantityDataGridViewTextBoxColumn.Name = "quantityDataGridViewTextBoxColumn"; - this.quantityDataGridViewTextBoxColumn.Width = 50; - // - // itemBindingSource - // - this.itemBindingSource.DataSource = typeof(CutList.Models.PartInputItem); - // // lengthInputValueDataGridViewTextBoxColumn // this.lengthInputValueDataGridViewTextBoxColumn.DataPropertyName = "LengthInputValue"; @@ -349,6 +340,15 @@ this.quantityDataGridViewTextBoxColumn1.HeaderText = "Quantity"; this.quantityDataGridViewTextBoxColumn1.Name = "quantityDataGridViewTextBoxColumn1"; // + // TotalLengthString + // + this.TotalLengthString.DataPropertyName = "TotalLengthString"; + dataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Info; + this.TotalLengthString.DefaultCellStyle = dataGridViewCellStyle4; + this.TotalLengthString.HeaderText = "Total Length"; + this.TotalLengthString.Name = "TotalLengthString"; + this.TotalLengthString.ReadOnly = true; + // // priorityDataGridViewTextBoxColumn // this.priorityDataGridViewTextBoxColumn.DataPropertyName = "Priority"; @@ -372,6 +372,7 @@ this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Cut List"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.itemBindingSource)).EndInit(); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.tabControl1.ResumeLayout(false); @@ -379,7 +380,6 @@ this.tabPage1.ResumeLayout(false); this.tabPage1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.itemBindingSource)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.binInputItemBindingSource)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -390,14 +390,14 @@ private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.BindingSource itemBindingSource; private System.Windows.Forms.ToolStrip toolStrip1; - private System.Windows.Forms.ToolStripButton toolStripButton1; + private System.Windows.Forms.ToolStripButton openFileButton; private System.Windows.Forms.ToolStripButton saveButton; private System.Windows.Forms.ToolStripButton runButton; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; - private System.Windows.Forms.ComboBox comboBox1; - private System.Windows.Forms.TextBox textBox1; - private System.Windows.Forms.Label label3; - private System.Windows.Forms.Label label4; + private System.Windows.Forms.ComboBox cutMethodComboBox; + private System.Windows.Forms.TextBox cutWidthTextBox; + private System.Windows.Forms.Label cutMethodLabel; + private System.Windows.Forms.Label cutWidthLabel; private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.TabPage tabPage2; @@ -411,8 +411,8 @@ private System.Windows.Forms.DataGridViewTextBoxColumn lengthDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn quantityDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn TotalLength; - private System.Windows.Forms.ToolStripButton toolStripButton2; - private System.Windows.Forms.ToolStripButton toolStripButton3; + private System.Windows.Forms.ToolStripButton newDocumentButton; + private System.Windows.Forms.ToolStripButton loadExampleDataButton; } } diff --git a/CutList/Forms/MainForm.cs b/CutList/Forms/MainForm.cs index fb0a35d..e967f9a 100644 --- a/CutList/Forms/MainForm.cs +++ b/CutList/Forms/MainForm.cs @@ -15,18 +15,17 @@ namespace CutList.Forms { public partial class MainForm : Form { + private static readonly Random random = new Random(); + private BindingList parts; private BindingList bins; - private string documentPath; private Toolbox toolbox; + private Document currentDocument; public MainForm() { InitializeComponent(); - parts = new BindingList(); - bins = new BindingList(); - dataGridView1.DrawRowNumbers(); dataGridView2.DrawRowNumbers(); @@ -37,7 +36,17 @@ namespace CutList.Forms binInputItemBindingSource.ListChanged += BinInputItemBindingSource_ListChanged; toolbox = new Toolbox(); - comboBox1.DataSource = toolbox.Tools; + cutMethodComboBox.DataSource = toolbox.Tools; + + currentDocument = new Document(); + +#if DEBUG + loadExampleDataButton.Visible = true; +#else + loadExampleDataButton.Visible = false; +#endif + + LoadDocumentData(); } private void UpdateRunButtonState() @@ -71,37 +80,64 @@ namespace CutList.Forms private void Open() { - var openFileDialog = new OpenFileDialog(); - openFileDialog.Multiselect = true; - openFileDialog.Filter = "Json File|*.json"; - - if (openFileDialog.ShowDialog() == DialogResult.OK) + try { - documentPath = openFileDialog.FileName; - var data = File.ReadAllText(documentPath); - parts = JsonConvert.DeserializeObject>(data); + var openFileDialog = new OpenFileDialog + { + Multiselect = false, + Filter = "Json File|*.json" + }; - dataGridView1.ClearSelection(); - itemBindingSource.DataSource = parts; - UpdateRunButtonState(); + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + currentDocument = Document.Load(openFileDialog.FileName); + + if (currentDocument == null) + return; + + LoadDocumentData(); + UpdateRunButtonState(); + } } + catch (Exception ex) + { + MessageBox.Show($"Failed to load file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void LoadDocumentData() + { + parts = new BindingList(currentDocument.PartsToNest); + bins = new BindingList(currentDocument.StockBins); + + itemBindingSource.DataSource = parts; + binInputItemBindingSource.DataSource = bins; + } + + private void DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) + { + dataGridView1.AutoResizeColumns(); + dataGridView2.AutoResizeColumns(); } private void Save() { - var itemsToSave = parts; + if (!currentDocument.Validate(out string validationMessage)) + { + MessageBox.Show(validationMessage, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } - if (dataGridView1.Rows[parts.Count - 1].IsNewRow == true) - itemsToSave.RemoveAt(itemsToSave.Count - 1); - - var json = JsonConvert.SerializeObject(itemsToSave, Formatting.Indented); - - var saveFileDialog = new SaveFileDialog(); - saveFileDialog.FileName = documentPath == null ? null : Path.GetFileName(documentPath); - saveFileDialog.Filter = "Json File|*.json"; + var saveFileDialog = new SaveFileDialog + { + FileName = currentDocument.LastFilePath == null ? "NewDocument.json" : Path.GetFileName(currentDocument.LastFilePath), + Filter = "Json File|*.json" + }; if (saveFileDialog.ShowDialog() == DialogResult.OK) - File.WriteAllText(saveFileDialog.FileName, json); + { + currentDocument.Save(saveFileDialog.FileName); + } } private void Run() @@ -137,9 +173,6 @@ namespace CutList.Forms private string GetResultsSaveName() { - if (documentPath != null) - return Path.GetFileNameWithoutExtension(documentPath); - var today = DateTime.Today; var year = today.Year.ToString(); var month = today.Month.ToString().PadLeft(2, '0'); @@ -173,117 +206,12 @@ namespace CutList.Forms public Tool GetSelectedTool() { - return comboBox1.SelectedItem as Tool; + return cutMethodComboBox.SelectedItem as Tool; } - protected override void OnLoad(EventArgs e) + private double GetRandomLength(double min, double max) { - base.OnLoad(e); - UpdateRunButtonState(); - } - - private void toolStripButton1_Click(object sender, EventArgs e) - { - Open(); - } - - private void saveButton_Click(object sender, EventArgs e) - { - Save(); - } - - private void runButton_Click(object sender, EventArgs e) - { - Run(); - } - - private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) - { - var tool = comboBox1.SelectedItem as Tool; - - if (tool == null) - return; - - textBox1.Text = tool.Kerf.ToString(); - - if (tool.AllowUserToChange) - { - textBox1.ReadOnly = false; - textBox1.BackColor = Color.White; - } - else - { - textBox1.ReadOnly = true; - textBox1.BackColor = SystemColors.Info; - } - } - - private void textBox1_TextChanged(object sender, EventArgs e) - { - double value; - - if (!double.TryParse(textBox1.Text, out value)) - return; - - var tool = comboBox1.SelectedItem as Tool; - - if (tool == null) - return; - - if (!tool.AllowUserToChange) - return; - - tool.Kerf = value; - - var tools = comboBox1.DataSource as List; - - if (tools != null) - { - toolbox.Save(); - } - } - - private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) - { - if (e.ColumnIndex == lengthDataGridViewTextBoxColumn.Index) - dataGridView1.Refresh(); - } - - private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) - { - dataGridView1.Rows[e.RowIndex].ErrorText = e.Exception.InnerException?.Message; - e.ThrowException = false; - } - - private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e) - { - } - - private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) - { - if (e.ColumnIndex == lengthInputValueDataGridViewTextBoxColumn.Index) - dataGridView2.Refresh(); - } - - private void BinInputItemBindingSource_ListChanged(object sender, ListChangedEventArgs e) - { - UpdateRunButtonState(); - } - - private void ItemBindingSource_ListChanged(object sender, ListChangedEventArgs e) - { - UpdateRunButtonState(); - } - - private void toolStripButton2_Click(object sender, EventArgs e) - { - documentPath = null; - parts = new BindingList(); - bins = new BindingList(); - - itemBindingSource.DataSource = parts; - binInputItemBindingSource.DataSource = bins; - UpdateRunButtonState(); + return Math.Round(random.NextDouble() * (max - min) + min, 2); } private void LoadExampleData(bool clearCurrentData = true) @@ -319,14 +247,112 @@ namespace CutList.Forms }); } - private static readonly Random random = new Random(); - - private double GetRandomLength(double min, double max) + protected override void OnLoad(EventArgs e) { - return Math.Round(random.NextDouble() * (max - min) + min, 2); + base.OnLoad(e); + UpdateRunButtonState(); } - private void toolStripButton3_Click(object sender, EventArgs e) + private void openFileButton_Click(object sender, EventArgs e) + { + Open(); + } + + private void saveButton_Click(object sender, EventArgs e) + { + Save(); + } + + private void runButton_Click(object sender, EventArgs e) + { + Run(); + } + + private void cutMethodComboBox_SelectedIndexChanged(object sender, EventArgs e) + { + var tool = cutMethodComboBox.SelectedItem as Tool; + + if (tool == null) + return; + + cutWidthTextBox.Text = tool.Kerf.ToString(); + + if (tool.AllowUserToChange) + { + cutWidthTextBox.ReadOnly = false; + cutWidthTextBox.BackColor = Color.White; + } + else + { + cutWidthTextBox.ReadOnly = true; + cutWidthTextBox.BackColor = SystemColors.Info; + } + } + + private void cutWidthTextBox_TextChanged(object sender, EventArgs e) + { + double value; + + if (!double.TryParse(cutWidthTextBox.Text, out value)) + return; + + var tool = cutMethodComboBox.SelectedItem as Tool; + + if (tool == null) + return; + + if (!tool.AllowUserToChange) + return; + + tool.Kerf = value; + + var tools = cutMethodComboBox.DataSource as List; + + if (tools != null) + { + toolbox.Save(); + } + } + + private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) + { + if (e.ColumnIndex == lengthDataGridViewTextBoxColumn.Index) + dataGridView1.Refresh(); + } + + private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) + { + dataGridView1.Rows[e.RowIndex].ErrorText = e.Exception.InnerException?.Message; + e.ThrowException = false; + } + + private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) + { + if (e.ColumnIndex == lengthInputValueDataGridViewTextBoxColumn.Index) + dataGridView2.Refresh(); + } + + private void BinInputItemBindingSource_ListChanged(object sender, ListChangedEventArgs e) + { + UpdateRunButtonState(); + } + + private void ItemBindingSource_ListChanged(object sender, ListChangedEventArgs e) + { + UpdateRunButtonState(); + } + + private void newDocumentButton_Click(object sender, EventArgs e) + { + parts = new BindingList(); + bins = new BindingList(); + + itemBindingSource.DataSource = parts; + binInputItemBindingSource.DataSource = bins; + UpdateRunButtonState(); + } + + private void loadExampleDataButton_Click(object sender, EventArgs e) { var clearData = true; @@ -343,19 +369,4 @@ namespace CutList.Forms LoadExampleData(clearData); } } - - public class Document - { - public Document() - { - - } - - [JsonIgnore] - public string SavePath { get; set; } - - public List PartsToNest { get; set; } - - public List StockBins { get; set; } - } } \ No newline at end of file diff --git a/SawCut/Nesting/AdvancedFitEngine.cs b/SawCut/Nesting/AdvancedFitEngine.cs index ae5ce7b..5642687 100644 --- a/SawCut/Nesting/AdvancedFitEngine.cs +++ b/SawCut/Nesting/AdvancedFitEngine.cs @@ -24,14 +24,14 @@ namespace SawCut.Nesting Items = items.OrderByDescending(i => i.Length).ToList(); - var result = new Result(); - result.ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList(); - - foreach (var item in result.ItemsNotUsed) + var result = new Result { - Items.Remove(item); - } + ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList() + }; + Items.RemoveAll(item => result.ItemsNotUsed.Contains(item)); + + result.ItemsNotUsed = Items.Where(i => i.Length > StockLength).ToList(); result.Bins = GetBins(); foreach (var bin in result.Bins)