diff --git a/ExportDXF/ExportDXF.csproj b/ExportDXF/ExportDXF.csproj index d4e26b7..3ab8ae4 100644 --- a/ExportDXF/ExportDXF.csproj +++ b/ExportDXF/ExportDXF.csproj @@ -86,6 +86,7 @@ + diff --git a/ExportDXF/Forms/MainForm.cs b/ExportDXF/Forms/MainForm.cs index 0d8337f..7fde2f0 100644 --- a/ExportDXF/Forms/MainForm.cs +++ b/ExportDXF/Forms/MainForm.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 GetItems(BomTableAnnotation bom) - { - var items = new List(); - 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() : - ((Array)bom.GetComponents(rowIndex))?.Cast(); - - 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 GetItems(AssemblyDoc assembly, bool topLevel) - { - var list = new List(); - - assembly.ResolveAllLightWeightComponents(false); - - var assemblyComponents = ((Array)assembly.GetComponents(topLevel)) - .Cast() - .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"); } diff --git a/ExportDXF/ItemExtractor.cs b/ExportDXF/ItemExtractor.cs new file mode 100644 index 0000000..c331d75 --- /dev/null +++ b/ExportDXF/ItemExtractor.cs @@ -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 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 components; + + if (isBOMPartsOnly) + { + components = ((Array)bom.GetComponents2(rowIndex, bom.BomFeature.Configuration))?.Cast().ToList(); + } + else + { + components = ((Array)bom.GetComponents(rowIndex))?.Cast().ToList(); + } + + if (components != null) + { + foreach (var component in components) + { + if (component.IsSuppressed()) + continue; + + item.Component = component; + break; + } + } + + return item; + } + + public List GetItems() + { + FindColumnIndices(); + + var items = new List(); + 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 GetItems() + { + var list = new List(); + + assembly.ResolveAllLightWeightComponents(false); + + var assemblyComponents = ((Array)assembly.GetComponents(TopLevelOnly)) + .Cast() + .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; + } +} \ No newline at end of file