From 40a502d829fb95fa378b0deec0cc434f48a51cb4 Mon Sep 17 00:00:00 2001 From: aj Date: Wed, 25 Jan 2017 09:36:55 -0500 Subject: [PATCH] Find item number column using solidworks columntype property. --- ExportDXF/Forms/MainForm.Designer.cs | 8 +-- ExportDXF/Forms/MainForm.cs | 86 +++++++++++----------------- 2 files changed, 36 insertions(+), 58 deletions(-) diff --git a/ExportDXF/Forms/MainForm.Designer.cs b/ExportDXF/Forms/MainForm.Designer.cs index 664362c..ab1ae90 100644 --- a/ExportDXF/Forms/MainForm.Designer.cs +++ b/ExportDXF/Forms/MainForm.Designer.cs @@ -57,7 +57,7 @@ this.richTextBox1.Location = new System.Drawing.Point(12, 83); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.ReadOnly = true; - this.richTextBox1.Size = new System.Drawing.Size(570, 181); + this.richTextBox1.Size = new System.Drawing.Size(570, 259); this.richTextBox1.TabIndex = 3; this.richTextBox1.Text = ""; // @@ -113,11 +113,11 @@ this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // - // Form1 + // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(594, 276); + this.ClientSize = new System.Drawing.Size(594, 354); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.richTextBox1); @@ -128,7 +128,7 @@ 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); this.MaximizeBox = false; - this.Name = "Form1"; + this.Name = "MainForm"; this.Text = "ExportDXF"; this.ResumeLayout(false); this.PerformLayout(); diff --git a/ExportDXF/Forms/MainForm.cs b/ExportDXF/Forms/MainForm.cs index 42758f6..917a8c7 100644 --- a/ExportDXF/Forms/MainForm.cs +++ b/ExportDXF/Forms/MainForm.cs @@ -269,56 +269,6 @@ namespace ExportDXF.Forms } } - private static int? FindItemNumberColumn(TableAnnotation table) - { - try - { - if (table.RowCount == 0 || table.ColumnCount == 0) - return null; - - var consecutiveNumberCountPerColumn = new int[table.ColumnCount]; - - for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex) - { - for (int rowIndex = 0; rowIndex < table.RowCount - 1; ++rowIndex) - { - var currentRowValue = table.Text[rowIndex, columnIndex]; - var nextRowValue = table.Text[rowIndex + 1, columnIndex]; - - int currentRowNum; - int nextRowNum; - - if (currentRowValue == null || !int.TryParse(currentRowValue, out currentRowNum)) - continue; // because currentRowValue is not a number - - if (nextRowValue == null || !int.TryParse(nextRowValue, out nextRowNum)) - continue; // because nextRowValue is not a number - - if (currentRowNum == (nextRowNum - 1)) - consecutiveNumberCountPerColumn[columnIndex]++; - } - } - - int index = 0; - int max = consecutiveNumberCountPerColumn[0]; - - for (int i = 1; i < consecutiveNumberCountPerColumn.Length; ++i) - { - if (consecutiveNumberCountPerColumn[i] > max) - { - index = i; - max = consecutiveNumberCountPerColumn[i]; - } - } - - return index; - } - catch - { - return null; - } - } - private bool SavePartToDXF(PartDoc part, string savePath) { var partModel = part as ModelDoc2; @@ -459,16 +409,16 @@ namespace ExportDXF.Forms var table = bom as TableAnnotation; - var itemNumColumnFound = FindItemNumberColumn(bom as TableAnnotation); + var itemNoColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_ItemNumber); - if (itemNumColumnFound == null) + if (itemNoColumnIndex == -1) { Print("Error: Item number column not found."); return null; } else { - Print("Item numbers are in the " + Helper.GetNumWithSuffix(itemNumColumnFound.Value + 1) + " column."); + Print("Item numbers are in the " + Helper.GetNumWithSuffix(itemNoColumnIndex + 1) + " column."); } var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly; @@ -489,7 +439,7 @@ namespace ExportDXF.Forms .GroupBy(c => c.ReferencedConfiguration) .Select(group => group.First()); - var itemNumber = table.Text[rowIndex, itemNumColumnFound.Value].PadLeft(2, '0'); + var itemNumber = table.Text[rowIndex, itemNoColumnIndex].PadLeft(2, '0'); var rev = 'A'; if (distinctComponents.Count() > 1) @@ -674,6 +624,34 @@ namespace ExportDXF.Forms default: return i.ToString() + "th"; } } + + public static int IndexOfColumnType(this TableAnnotation table, swTableColumnTypes_e columnType) + { + for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex) + { + var currentColumnType = (swTableColumnTypes_e)table.GetColumnType(columnIndex); + + if (currentColumnType == columnType) + return columnIndex; + } + + return -1; + } + + public static int IndexOfColumnTitle(this TableAnnotation table, string columnTitle) + { + var lowercaseColumnTitle = columnTitle.ToLower(); + + for (int columnIndex = 0; columnIndex < table.ColumnCount; ++columnIndex) + { + var currentColumnType = table.GetColumnTitle(columnIndex); + + if (currentColumnType.ToLower() == lowercaseColumnTitle) + return columnIndex; + } + + return -1; + } } public class Item