From f9fca98fe6b52fc4d0264ccd93bfebe97fb77f75 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 26 Apr 2018 20:45:52 -0400 Subject: [PATCH] Update file prefix when active document changes. --- ExportDXF/DrawingInfo.cs | 49 ++++++ ExportDXF/ExportDXF.csproj | 1 + ExportDXF/Forms/MainForm.Designer.cs | 37 ++-- ExportDXF/Forms/MainForm.cs | 244 ++++++++++++++------------- 4 files changed, 198 insertions(+), 133 deletions(-) create mode 100644 ExportDXF/DrawingInfo.cs diff --git a/ExportDXF/DrawingInfo.cs b/ExportDXF/DrawingInfo.cs new file mode 100644 index 0000000..5f44f16 --- /dev/null +++ b/ExportDXF/DrawingInfo.cs @@ -0,0 +1,49 @@ +using System.Text.RegularExpressions; + +namespace ExportDXF +{ + public class DrawingInfo + { + public string JobNo { get; set; } + + public string DrawingNo { get; set; } + + public string Source { get; set; } + + public override string ToString() + { + return string.Format("{0} {1}", JobNo, DrawingNo); + } + + public override bool Equals(object obj) + { + if (obj == null) + return false; + + return obj.ToString() == ToString(); + } + + public override int GetHashCode() + { + return ToString().GetHashCode(); + } + + public static DrawingInfo Parse(string input) + { + const string pattern = @"(?[34]\d{3})\s?(?[ABEP]\d+)"; + + var match = Regex.Match(input, pattern); + + if (match.Success == false) + return null; + + var dwg = new DrawingInfo(); + + dwg.JobNo = match.Groups["jobNo"].Value; + dwg.DrawingNo = match.Groups["dwgNo"].Value; + dwg.Source = input; + + return dwg; + } + } +} diff --git a/ExportDXF/ExportDXF.csproj b/ExportDXF/ExportDXF.csproj index 5a56240..53283bd 100644 --- a/ExportDXF/ExportDXF.csproj +++ b/ExportDXF/ExportDXF.csproj @@ -87,6 +87,7 @@ + diff --git a/ExportDXF/Forms/MainForm.Designer.cs b/ExportDXF/Forms/MainForm.Designer.cs index c63518b..c907cb6 100644 --- a/ExportDXF/Forms/MainForm.Designer.cs +++ b/ExportDXF/Forms/MainForm.Designer.cs @@ -28,24 +28,25 @@ /// private void InitializeComponent() { - this.textBox1 = new System.Windows.Forms.TextBox(); + this.activeDocTitleBox = new System.Windows.Forms.TextBox(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); - this.textBox2 = new System.Windows.Forms.TextBox(); + this.prefixTextBox = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // - this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + this.activeDocTitleBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.textBox1.BackColor = System.Drawing.Color.White; - this.textBox1.Location = new System.Drawing.Point(130, 13); - this.textBox1.Name = "textBox1"; - this.textBox1.ReadOnly = true; - this.textBox1.Size = new System.Drawing.Size(400, 25); - this.textBox1.TabIndex = 2; + this.activeDocTitleBox.BackColor = System.Drawing.Color.White; + this.activeDocTitleBox.Location = new System.Drawing.Point(130, 13); + this.activeDocTitleBox.Name = "textBox1"; + this.activeDocTitleBox.ReadOnly = true; + this.activeDocTitleBox.Size = new System.Drawing.Size(400, 25); + this.activeDocTitleBox.TabIndex = 2; + this.activeDocTitleBox.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // richTextBox1 // @@ -80,12 +81,12 @@ // // textBox2 // - this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + this.prefixTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.textBox2.Location = new System.Drawing.Point(130, 44); - this.textBox2.Name = "textBox2"; - this.textBox2.Size = new System.Drawing.Size(400, 25); - this.textBox2.TabIndex = 2; + this.prefixTextBox.Location = new System.Drawing.Point(130, 44); + this.prefixTextBox.Name = "textBox2"; + this.prefixTextBox.Size = new System.Drawing.Size(400, 25); + this.prefixTextBox.TabIndex = 2; // // button1 // @@ -107,8 +108,8 @@ this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.richTextBox1); - this.Controls.Add(this.textBox2); - this.Controls.Add(this.textBox1); + this.Controls.Add(this.prefixTextBox); + this.Controls.Add(this.activeDocTitleBox); this.Controls.Add(this.button1); this.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); @@ -123,11 +124,11 @@ #endregion private System.Windows.Forms.Button button1; - private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox activeDocTitleBox; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.TextBox prefixTextBox; } } diff --git a/ExportDXF/Forms/MainForm.cs b/ExportDXF/Forms/MainForm.cs index 29936b7..6b1c19e 100644 --- a/ExportDXF/Forms/MainForm.cs +++ b/ExportDXF/Forms/MainForm.cs @@ -169,11 +169,10 @@ namespace ExportDXF.Forms private void SetActiveDocName() { var model = sldWorks.ActiveDoc as ModelDoc2; + activeDocTitleBox.Text = model == null ? "" : model.GetTitle(); + } - textBox1.Text = model != null ? model.GetTitle() : ""; - } - - private void DetermineModelTypeAndExportToDXF(ModelDoc2 model) + private void DetermineModelTypeAndExportToDXF(ModelDoc2 model) { if (model is PartDoc) { @@ -206,7 +205,7 @@ namespace ExportDXF.Forms Print("Found " + bomTables.Count); Print(""); - var items = new List(); + var items = new List(); foreach (var bom in bomTables) { @@ -215,18 +214,18 @@ namespace ExportDXF.Forms Print(bom.BomFeature.Name); Print("Fetching components..."); - Print("Found " + items.Count); + Print("Found " + items.Count); - items.AddRange(GetItems(bom)); + items.AddRange(GetItems(bom)); } - Print("Found " + items.Count + " total"); - ExportToDXF(items); - } + Print("Found " + items.Count + " total"); + ExportToDXF(items); + } - private void ExportToDXF(PartDoc part) + private void ExportToDXF(PartDoc part) { - var prefix = textBox2.Text; + var prefix = prefixTextBox.Text; var model = part as ModelDoc2; var dir = UserSelectFolder(); @@ -262,9 +261,9 @@ namespace ExportDXF.Forms private void ExportToDXF(IEnumerable items) { var savePath = UserSelectFolder(); - var prefix = textBox2.Text; + var prefix = prefixTextBox.Text; - if (savePath == null) + if (savePath == null) { Print("Canceled\n", Color.Red); return; @@ -279,65 +278,65 @@ namespace ExportDXF.Forms if (worker.CancellationPending) break; - item.ItemNo = prefix + item.ItemNo; + item.ItemNo = prefix + item.ItemNo; - var fileName = item.ItemNo + ".dxf"; + var fileName = item.ItemNo + ".dxf"; var savepath = Path.Combine(savePath, fileName); - var model = item.Component.GetModelDoc2() as ModelDoc2; - var part = model as PartDoc; + var model = item.Component.GetModelDoc2() as ModelDoc2; + var part = model as PartDoc; - if (part == null) - { - Print(item.ItemNo + " - skipped, not a part document"); - continue; - } + if (part == null) + { + Print(item.ItemNo + " - skipped, not a part document"); + continue; + } - var config = item.Component.ReferencedConfiguration; + var config = item.Component.ReferencedConfiguration; - var sheetMetal = model.GetFeatureByTypeName("SheetMetal"); - var sheetMetalData = sheetMetal?.GetDefinition() as SheetMetalFeatureData; + var sheetMetal = model.GetFeatureByTypeName("SheetMetal"); + var sheetMetalData = sheetMetal?.GetDefinition() as SheetMetalFeatureData; - if (sheetMetalData != null) - { - item.Thickness = sheetMetalData.Thickness.FromSldWorks(); - item.KFactor = sheetMetalData.KFactor; - } + if (sheetMetalData != null) + { + item.Thickness = sheetMetalData.Thickness.FromSldWorks(); + item.KFactor = sheetMetalData.KFactor; + } - if (item.Description == null) - item.Description = model.Extension.CustomPropertyManager[config].Get("Description"); + if (item.Description == null) + item.Description = model.Extension.CustomPropertyManager[config].Get("Description"); - if (item.Description == null) - item.Description = model.Extension.CustomPropertyManager[""].Get("Description"); + if (item.Description == null) + item.Description = model.Extension.CustomPropertyManager[""].Get("Description"); - var db = string.Empty; + var db = string.Empty; - item.Material = part.GetMaterialPropertyName2(config, out db); + item.Material = part.GetMaterialPropertyName2(config, out db); - if (part == null) + if (part == null) continue; SavePartToDXF(part, config, savepath); Application.DoEvents(); } - try - { - var bomFile = Path.Combine(savePath, "BOM.xlsx"); - CreateBOMExcelFile(bomFile, items.ToList()); - } - catch (Exception ex) - { - Print(ex.Message, Color.Red); - } - } + try + { + var bomFile = Path.Combine(savePath, "BOM.xlsx"); + CreateBOMExcelFile(bomFile, items.ToList()); + } + catch (Exception ex) + { + Print(ex.Message, Color.Red); + } + } - private string ChangePathExtension(string fullpath, string newExtension) - { - var dir = Path.GetDirectoryName(fullpath); - var name = Path.GetFileNameWithoutExtension(fullpath); + private string ChangePathExtension(string fullpath, string newExtension) + { + var dir = Path.GetDirectoryName(fullpath); + var name = Path.GetFileNameWithoutExtension(fullpath); - return Path.Combine(dir, name + newExtension); - } + return Path.Combine(dir, name + newExtension); + } private bool SavePartToDXF(PartDoc part, string savePath) { @@ -397,42 +396,42 @@ namespace ExportDXF.Forms } } - private void CreateBOMExcelFile(string filepath, IList items) - { - var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "BomTemplate.xlsx"); + private void CreateBOMExcelFile(string filepath, IList items) + { + var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "BomTemplate.xlsx"); - File.Copy(templatePath, filepath, true); + File.Copy(templatePath, filepath, true); - var newFile = new FileInfo(filepath); + var newFile = new FileInfo(filepath); - using (var pkg = new ExcelPackage(newFile)) - { - var workbook = pkg.Workbook; - var partsSheet = workbook.Worksheets["Parts"]; + using (var pkg = new ExcelPackage(newFile)) + { + var workbook = pkg.Workbook; + var partsSheet = workbook.Worksheets["Parts"]; - for (int i = 0; i < items.Count; i++) - { - var item = items[i]; - var row = i + 2; + for (int i = 0; i < items.Count; i++) + { + var item = items[i]; + var row = i + 2; - partsSheet.Cells[row, 1].Value = item.ItemNo; - partsSheet.Cells[row, 2].Value = item.Quantity; - partsSheet.Cells[row, 3].Value = item.Description; - partsSheet.Cells[row, 4].Value = item.PartNo; + partsSheet.Cells[row, 1].Value = item.ItemNo; + partsSheet.Cells[row, 2].Value = item.Quantity; + partsSheet.Cells[row, 3].Value = item.Description; + partsSheet.Cells[row, 4].Value = item.PartNo; - if (item.Thickness > 0) - partsSheet.Cells[row, 5].Value = item.Thickness; + if (item.Thickness > 0) + partsSheet.Cells[row, 5].Value = item.Thickness; - partsSheet.Cells[row, 6].Value = item.Material; + partsSheet.Cells[row, 6].Value = item.Material; - if (item.KFactor > 0) - partsSheet.Cells[row, 7].Value = item.KFactor; - } + if (item.KFactor > 0) + partsSheet.Cells[row, 7].Value = item.KFactor; + } - workbook.Calculate(); - pkg.Save(); - } - } + workbook.Calculate(); + pkg.Save(); + } + } private string UserSelectFolder() { @@ -449,7 +448,7 @@ namespace ExportDXF.Forms return path; } - private bool ShouldFlipView(SolidWorks.Interop.sldworks.View view) + private bool ShouldFlipView(SolidWorks.Interop.sldworks.View view) { return viewFlipDecider.ShouldFlip(view); } @@ -491,28 +490,28 @@ namespace ExportDXF.Forms Print("Item numbers are in the " + Helper.GetNumWithSuffix(itemNoColumnIndex + 1) + " column."); } - var qtyColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_Quantity); - if (qtyColumnIndex == -1) - { - Print("Error: Quantity column not found."); - return null; - } + var qtyColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_Quantity); + if (qtyColumnIndex == -1) + { + Print("Error: Quantity column not found."); + return null; + } - var descriptionColumnIndex = table.IndexOfColumnTitle("Description"); - if (descriptionColumnIndex == -1) - { - Print("Error: Description column not found."); - return null; - } + var descriptionColumnIndex = table.IndexOfColumnTitle("Description"); + if (descriptionColumnIndex == -1) + { + Print("Error: Description column not found."); + return null; + } - var partNoColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_PartNumber); - if (partNoColumnIndex == -1) - { - Print("Error: Part number column not found."); - return null; - } + var partNoColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_PartNumber); + if (partNoColumnIndex == -1) + { + Print("Error: Part number column not found."); + return null; + } - var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly; + var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly; for (int rowIndex = 0; rowIndex < table.RowCount; rowIndex++) { @@ -535,7 +534,7 @@ namespace ExportDXF.Forms if (distinctComponents.Count() > 1) { - throw new NotImplementedException(); + throw new NotImplementedException(); //foreach (var comp in distinctComponents) //{ @@ -548,12 +547,12 @@ namespace ExportDXF.Forms } else { - items.Add(new Item - { - PartNo = table.DisplayedText[rowIndex, partNoColumnIndex], - Quantity = int.Parse(table.DisplayedText[rowIndex, qtyColumnIndex]), - Description = table.DisplayedText[rowIndex, descriptionColumnIndex], - ItemNo = table.DisplayedText[rowIndex, itemNoColumnIndex].PadLeft(2, '0'), + items.Add(new Item + { + PartNo = table.DisplayedText[rowIndex, partNoColumnIndex], + Quantity = int.Parse(table.DisplayedText[rowIndex, qtyColumnIndex]), + Description = table.DisplayedText[rowIndex, descriptionColumnIndex], + ItemNo = table.DisplayedText[rowIndex, itemNoColumnIndex].PadLeft(2, '0'), Component = distinctComponents.First() }); } @@ -579,18 +578,18 @@ namespace ExportDXF.Forms { var component = group.First(); - var model = component.GetModelDoc2() as ModelDoc2; + var model = component.GetModelDoc2() as ModelDoc2; - if (model == null) - continue; + if (model == null) + continue; var name = component.ReferencedConfiguration.ToLower() == "default" ? component.GetTitle() : string.Format("{0} [{1}]", component.GetTitle(), component.ReferencedConfiguration); - list.Add(new Item + list.Add(new Item { - ItemNo = name, + ItemNo = name, PartNo = name, Quantity = group.Count(), Component = component @@ -604,7 +603,22 @@ namespace ExportDXF.Forms { get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); } } - } - + private void textBox1_TextChanged(object sender, EventArgs e) + { + var model = sldWorks.ActiveDoc as ModelDoc2; + var isDrawing = model is DrawingDoc; + + if (!isDrawing) + return; + + var drawingInfo = DrawingInfo.Parse(activeDocTitleBox.Text); + + if (drawingInfo == null) + return; + + prefixTextBox.Text = string.Format("{0} {1} PT", drawingInfo.JobNo, drawingInfo.DrawingNo); + prefixTextBox.SelectionStart = prefixTextBox.Text.Length; + } + } }