Hide Table of Contents

Get Components in Each BOM Table Row Example (C#)

This example shows how to get the components in each row of a BOM table annotation.

//-----------------------------------------------------------------------------
// Preconditions:
// 1. Open install_dir\samples\tutorial\assemblyvisualize\food_processor.sldasm.
// 2. Create a drawing from the assembly.
// 3. Select Insert > Tables > Bill of Materials.
// 4. Select BOM Type > Parts only.
// 5. Select Part Configuration Grouping >
//           Display configurations of the same part as separate items.
// 6. Click the green check mark to accept the settings.
// 7. Ensure the namespace matches the name of your C# project.
//
// Postconditions: Inspect the Immediate window.
//
// NOTE: Because the model is used elsewhere, do not save changes to it.
//-----------------------------------------------------------------------------


using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace GetComponentsCount2_CSharp.csproj
{
    
partial class SolidWorksMacro
    {


        
public void ProcessTableAnn(SldWorks swApp, ModelDoc2 swModel, TableAnnotation swTableAnn, string ConfigName)
        {
            
int nNumRow = 0;
            
int J = 0;
            
int I = 0;
            
string ItemNumber = null;
            
string PartNumber = null;

            
Debug.Print("   Table Title        " + swTableAnn.Title);

            nNumRow = swTableAnn.RowCount;

            
BomTableAnnotation swBOMTableAnn = default(BomTableAnnotation);
            swBOMTableAnn = (
BomTableAnnotation)swTableAnn;

            
for (J = 0; J <= nNumRow - 1; J++)
            {
                
Debug.Print("   Row Number " + J + " Component Count  : " + swBOMTableAnn.GetComponentsCount2(J, ConfigName, out ItemNumber, out PartNumber));
                
Debug.Print("       Item Number  : " + ItemNumber);
                
Debug.Print("       Part Number  : " + PartNumber);

                
object[] vPtArr = null;
                
Component2 swComp = null;
                
object pt = null;

                vPtArr = (
object[])swBOMTableAnn.GetComponents2(J, ConfigName);

                
if (((vPtArr != null)))
                {
                    
for (I = 0; I <= vPtArr.GetUpperBound(0); I++)
                    {
                        pt = vPtArr[I];
                        swComp = (
Component2)pt;
                        
if ((swComp != null))
                        {
                            
Debug.Print("           Component Name :" + swComp.Name2 + "      Configuration Name : " + swComp.ReferencedConfiguration);
                            
Debug.Print("           Component Path :" + swComp.GetPathName());
                        }
                        
else
                        {
                            
Debug.Print("  Could not get component.");
                        }
                    }
                }
            }
        }



        
public void ProcessBomFeature(SldWorks swApp, ModelDoc2 swModel, BomFeature swBomFeat)
        {
            
Feature swFeat = default(Feature);
            
object[] vTableArr = null;
            
object vTable = null;
            
string[] vConfigArray = null;
            
object vConfig = null;
            
string ConfigName = null;
            
TableAnnotation swTable = default(TableAnnotation);
            
object visibility = null;

            swFeat = swBomFeat.GetFeature();
            vTableArr = (
object[])swBomFeat.GetTableAnnotations();

            
foreach (TableAnnotation vTable_loopVariable in vTableArr)
            {
                vTable = vTable_loopVariable;
                swTable = (
TableAnnotation)vTable;
                vConfigArray = (
string[])swBomFeat.GetConfigurations(true, ref visibility);
                
foreach (object vConfig_loopVariable in vConfigArray)
                {
                    vConfig = vConfig_loopVariable;
                    ConfigName = (
string)vConfig;
                    
Debug.Print("-------------------------------------------------------");
                    
Debug.Print(" Component for Configuration : " + ConfigName);
                    ProcessTableAnn(swApp, swModel, swTable, ConfigName);
                }
            }

        }

        
public void Main()
        {
            
ModelDoc2 swModel = default(ModelDoc2);
            
DrawingDoc swDraw = default(DrawingDoc);
            
Feature swFeat = default(Feature);
            
BomFeature swBomFeat = default(BomFeature);

            swModel = (
ModelDoc2)swApp.ActiveDoc;
            swDraw = (
DrawingDoc)swModel;
            swFeat = (
Feature)swModel.FirstFeature();

            
while ((swFeat != null))
            {
                
if ("BomFeat" == swFeat.GetTypeName())
                {
                    
Debug.Print("******************************");
                    
Debug.Print("Feature Name : " + swFeat.Name);

                    swBomFeat = (
BomFeature)swFeat.GetSpecificFeature2();

                    ProcessBomFeature(swApp, swModel, swBomFeat);
                }
                swFeat = (
Feature)swFeat.GetNextFeature();
            }
        }


        
public SldWorks swApp;

    }
}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Components in Each BOM Table Row Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.