Added ItemExtractor
This commit is contained in:
@@ -86,6 +86,7 @@
|
||||
<Compile Include="Bounds.cs" />
|
||||
<Compile Include="DrawingInfo.cs" />
|
||||
<Compile Include="Extensions.cs" />
|
||||
<Compile Include="ItemExtractor.cs" />
|
||||
<Compile Include="Forms\ViewFlipDeciderComboboxItem.cs" />
|
||||
<Compile Include="Item.cs" />
|
||||
<Compile Include="IViewFlipDecider.cs" />
|
||||
|
||||
@@ -254,12 +254,17 @@ namespace ExportDXF.Forms
|
||||
if (worker.CancellationPending)
|
||||
return;
|
||||
|
||||
var itemExtractor = new BomItemExtractor(bom);
|
||||
itemExtractor.SkipHiddenRows = true;
|
||||
|
||||
Print(bom.BomFeature.Name);
|
||||
Print("Fetching components...");
|
||||
items.AddRange(GetItems(bom));
|
||||
Print($"Found {items.Count} component(s)");
|
||||
Print($"Fetching components from {bom.BomFeature.Name}");
|
||||
|
||||
var bomItems = itemExtractor.GetItems();
|
||||
items.AddRange(bomItems);
|
||||
}
|
||||
|
||||
Print($"Found {items.Count} component(s)");
|
||||
ExportToDXF(items);
|
||||
}
|
||||
|
||||
@@ -290,7 +295,10 @@ namespace ExportDXF.Forms
|
||||
{
|
||||
Print("Fetching components...");
|
||||
|
||||
var items = GetItems(assembly, false);
|
||||
var itemExtractor = new AssemblyItemExtractor(assembly);
|
||||
itemExtractor.TopLevelOnly = false;
|
||||
|
||||
var items = itemExtractor.GetItems();
|
||||
|
||||
Print("Found " + items.Count);
|
||||
Print("");
|
||||
@@ -318,6 +326,9 @@ namespace ExportDXF.Forms
|
||||
if (worker.CancellationPending)
|
||||
return;
|
||||
|
||||
if (item.Component == null)
|
||||
continue;
|
||||
|
||||
var fileName = GetFileName(item);
|
||||
var savepath = Path.Combine(savePath, fileName + ".dxf");
|
||||
|
||||
@@ -658,126 +669,6 @@ namespace ExportDXF.Forms
|
||||
1) as DrawingDoc;
|
||||
}
|
||||
|
||||
private List<Item> GetItems(BomTableAnnotation bom)
|
||||
{
|
||||
var items = new List<Item>();
|
||||
var table = bom as TableAnnotation;
|
||||
var itemNoColumnIndex = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_ItemNumber);
|
||||
|
||||
if (itemNoColumnIndex == -1)
|
||||
{
|
||||
Print("Error: Item number column not found.");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 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 isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly;
|
||||
|
||||
for (int rowIndex = 0; rowIndex < table.RowCount; rowIndex++)
|
||||
{
|
||||
//if (table.RowHidden[rowIndex] == true)
|
||||
// continue;
|
||||
|
||||
var bomComponents = isBOMPartsOnly ?
|
||||
((Array)bom.GetComponents2(rowIndex, bom.BomFeature.Configuration))?.Cast<Component2>() :
|
||||
((Array)bom.GetComponents(rowIndex))?.Cast<Component2>();
|
||||
|
||||
if (bomComponents == null)
|
||||
continue;
|
||||
|
||||
var distinctComponents = bomComponents
|
||||
.GroupBy(c => c.ReferencedConfiguration)
|
||||
.Select(group => group.First())
|
||||
.ToList();
|
||||
|
||||
foreach (var component in bomComponents)
|
||||
{
|
||||
if (component.IsSuppressed())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var qtyString = table.DisplayedText[rowIndex, qtyColumnIndex];
|
||||
int qty = 0;
|
||||
|
||||
int.TryParse(qtyString, out qty);
|
||||
|
||||
items.Add(new Item
|
||||
{
|
||||
PartName = table.DisplayedText[rowIndex, partNoColumnIndex],
|
||||
Quantity = qty,
|
||||
Description = table.DisplayedText[rowIndex, descriptionColumnIndex],
|
||||
ItemNo = table.DisplayedText[rowIndex, itemNoColumnIndex],
|
||||
Component = component
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
private List<Item> GetItems(AssemblyDoc assembly, bool topLevel)
|
||||
{
|
||||
var list = new List<Item>();
|
||||
|
||||
assembly.ResolveAllLightWeightComponents(false);
|
||||
|
||||
var assemblyComponents = ((Array)assembly.GetComponents(topLevel))
|
||||
.Cast<Component2>()
|
||||
.Where(c => !c.IsHidden(true));
|
||||
|
||||
var componentGroups = assemblyComponents
|
||||
.GroupBy(c => c.GetTitle() + c.ReferencedConfiguration);
|
||||
|
||||
foreach (var group in componentGroups)
|
||||
{
|
||||
var component = group.First();
|
||||
|
||||
var model = component.GetModelDoc2() as ModelDoc2;
|
||||
|
||||
if (model == null)
|
||||
continue;
|
||||
|
||||
var n1 = Path.GetFileNameWithoutExtension(component.GetTitle());
|
||||
var name = component.ReferencedConfiguration.ToLower() == "default" ? n1 : string.Format("{0} [{1}]", n1, component.ReferencedConfiguration);
|
||||
|
||||
list.Add(new Item
|
||||
{
|
||||
PartName = name,
|
||||
Quantity = group.Count(),
|
||||
Component = component
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static string DrawingTemplatePath
|
||||
{
|
||||
get { return Path.Combine(Application.StartupPath, "Templates", "Blank.drwdot"); }
|
||||
|
||||
198
ExportDXF/ItemExtractor.cs
Normal file
198
ExportDXF/ItemExtractor.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using SolidWorks.Interop.sldworks;
|
||||
using SolidWorks.Interop.swconst;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ExportDXF
|
||||
{
|
||||
public interface ItemExtractor
|
||||
{
|
||||
List<Item> GetItems();
|
||||
}
|
||||
|
||||
public class BomItemExtractor : ItemExtractor
|
||||
{
|
||||
private BomTableAnnotation bom;
|
||||
private BomColumnIndices columnIndices;
|
||||
|
||||
public BomItemExtractor(BomTableAnnotation bom)
|
||||
{
|
||||
this.bom = bom;
|
||||
columnIndices = new BomColumnIndices();
|
||||
}
|
||||
|
||||
public bool SkipHiddenRows { get; set; }
|
||||
|
||||
private void FindColumnIndices()
|
||||
{
|
||||
if (columnIndices == null)
|
||||
columnIndices = new BomColumnIndices();
|
||||
|
||||
var table = bom as TableAnnotation;
|
||||
|
||||
columnIndices.Description = table.IndexOfColumnTitle("Description");
|
||||
columnIndices.ItemNumber = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_ItemNumber);
|
||||
columnIndices.PartNumber = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_PartNumber);
|
||||
columnIndices.Quantity = table.IndexOfColumnType(swTableColumnTypes_e.swBomTableColumnType_Quantity);
|
||||
|
||||
if (columnIndices.PartNumber == -1)
|
||||
throw new Exception("Part number column not found.");
|
||||
|
||||
if (columnIndices.ItemNumber == -1)
|
||||
throw new Exception("Item number column not found.");
|
||||
|
||||
if (columnIndices.Description == -1)
|
||||
throw new Exception("Description column not found.");
|
||||
|
||||
if (columnIndices.Quantity == -1)
|
||||
throw new Exception("Quantity column not found.");
|
||||
}
|
||||
|
||||
private Item GetItem(int rowIndex)
|
||||
{
|
||||
var item = new Item();
|
||||
var table = bom as TableAnnotation;
|
||||
|
||||
if (columnIndices.ItemNumber != -1)
|
||||
{
|
||||
item.ItemNo = table.DisplayedText[rowIndex, columnIndices.ItemNumber];
|
||||
}
|
||||
|
||||
if (columnIndices.PartNumber != -1)
|
||||
{
|
||||
item.PartName = table.DisplayedText[rowIndex, columnIndices.PartNumber];
|
||||
}
|
||||
|
||||
if (columnIndices.Description != -1)
|
||||
{
|
||||
item.Description = table.DisplayedText[rowIndex, columnIndices.Description];
|
||||
}
|
||||
|
||||
if (columnIndices.Quantity != -1)
|
||||
{
|
||||
var qtyString = table.DisplayedText[rowIndex, columnIndices.Quantity];
|
||||
|
||||
int qty = 0;
|
||||
int.TryParse(qtyString, out qty);
|
||||
|
||||
item.Quantity = qty;
|
||||
}
|
||||
|
||||
var isBOMPartsOnly = bom.BomFeature.TableType == (int)swBomType_e.swBomType_PartsOnly;
|
||||
|
||||
List<Component2> components;
|
||||
|
||||
if (isBOMPartsOnly)
|
||||
{
|
||||
components = ((Array)bom.GetComponents2(rowIndex, bom.BomFeature.Configuration))?.Cast<Component2>().ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
components = ((Array)bom.GetComponents(rowIndex))?.Cast<Component2>().ToList();
|
||||
}
|
||||
|
||||
if (components != null)
|
||||
{
|
||||
foreach (var component in components)
|
||||
{
|
||||
if (component.IsSuppressed())
|
||||
continue;
|
||||
|
||||
item.Component = component;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
public List<Item> GetItems()
|
||||
{
|
||||
FindColumnIndices();
|
||||
|
||||
var items = new List<Item>();
|
||||
var table = bom as TableAnnotation;
|
||||
|
||||
for (int rowIndex = 1; rowIndex < table.RowCount; rowIndex++)
|
||||
{
|
||||
var isRowHidden = table.RowHidden[rowIndex];
|
||||
|
||||
if (isRowHidden && SkipHiddenRows)
|
||||
continue;
|
||||
|
||||
var item = GetItem(rowIndex);
|
||||
items.Add(item);
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
||||
public class AssemblyItemExtractor : ItemExtractor
|
||||
{
|
||||
private AssemblyDoc assembly;
|
||||
|
||||
public AssemblyItemExtractor(AssemblyDoc assembly)
|
||||
{
|
||||
this.assembly = assembly;
|
||||
}
|
||||
|
||||
public bool TopLevelOnly { get; set; }
|
||||
|
||||
private string GetComponentName(Component2 component)
|
||||
{
|
||||
var filepath = component.GetTitle();
|
||||
var filename = Path.GetFileNameWithoutExtension(filepath);
|
||||
var isDefaultConfig = component.ReferencedConfiguration.ToLower() == "default";
|
||||
|
||||
return isDefaultConfig ? filename : $"{filename} [{component.ReferencedConfiguration}]";
|
||||
}
|
||||
|
||||
public List<Item> GetItems()
|
||||
{
|
||||
var list = new List<Item>();
|
||||
|
||||
assembly.ResolveAllLightWeightComponents(false);
|
||||
|
||||
var assemblyComponents = ((Array)assembly.GetComponents(TopLevelOnly))
|
||||
.Cast<Component2>()
|
||||
.Where(c => !c.IsHidden(true));
|
||||
|
||||
var componentGroups = assemblyComponents
|
||||
.GroupBy(c => c.GetTitle() + c.ReferencedConfiguration);
|
||||
|
||||
foreach (var group in componentGroups)
|
||||
{
|
||||
var component = group.First();
|
||||
var model = component.GetModelDoc2() as ModelDoc2;
|
||||
|
||||
if (model == null)
|
||||
continue;
|
||||
|
||||
var name = GetComponentName(component);
|
||||
|
||||
list.Add(new Item
|
||||
{
|
||||
PartName = name,
|
||||
Quantity = group.Count(),
|
||||
Component = component
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
public class BomColumnIndices
|
||||
{
|
||||
public int ItemNumber { get; set; } = -1;
|
||||
|
||||
public int Quantity { get; set; } = -1;
|
||||
|
||||
public int Description { get; set; } = -1;
|
||||
|
||||
public int PartNumber { get; set; } = -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user