Find item number column using solidworks columntype property.

This commit is contained in:
aj
2017-01-25 09:36:55 -05:00
parent 3b0af13689
commit 40a502d829
2 changed files with 36 additions and 58 deletions

View File

@@ -57,7 +57,7 @@
this.richTextBox1.Location = new System.Drawing.Point(12, 83); this.richTextBox1.Location = new System.Drawing.Point(12, 83);
this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true; 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.TabIndex = 3;
this.richTextBox1.Text = ""; this.richTextBox1.Text = "";
// //
@@ -113,11 +113,11 @@
this.button1.UseVisualStyleBackColor = true; this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); this.button1.Click += new System.EventHandler(this.button1_Click);
// //
// Form1 // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.label2);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Controls.Add(this.richTextBox1); 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.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.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false; this.MaximizeBox = false;
this.Name = "Form1"; this.Name = "MainForm";
this.Text = "ExportDXF"; this.Text = "ExportDXF";
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View File

@@ -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) private bool SavePartToDXF(PartDoc part, string savePath)
{ {
var partModel = part as ModelDoc2; var partModel = part as ModelDoc2;
@@ -459,16 +409,16 @@ namespace ExportDXF.Forms
var table = bom as TableAnnotation; 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."); Print("Error: Item number column not found.");
return null; return null;
} }
else 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; var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly;
@@ -489,7 +439,7 @@ namespace ExportDXF.Forms
.GroupBy(c => c.ReferencedConfiguration) .GroupBy(c => c.ReferencedConfiguration)
.Select(group => group.First()); .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'; var rev = 'A';
if (distinctComponents.Count() > 1) if (distinctComponents.Count() > 1)
@@ -674,6 +624,34 @@ namespace ExportDXF.Forms
default: return i.ToString() + "th"; 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 public class Item