Hide Table of Contents

Get Model Pathnames from a BOM Table Example (C#)

This example shows how to get the model pathnames from a specified row of a BOM table annotation.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Open a drawing containing one BOM table annotation named Bill of Materials1.
// 2. Modify the namespace to match the name of your C# project.
// 3. Open an Immediate Window.
//
// Postconditions: Inspect the Immediate Window for model pathnames.
// ---------------------------------------------------------------------------
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 ModelPathNames_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
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 ((swFeat.GetTypeName() == "BomFeat"))
                {
                    
Debug.Print("******************************");
                    
Debug.Print("Feature Name = " + swFeat.Name);
                    swBomFeat = (
BomFeature)swFeat.GetSpecificFeature2();
                    
if ((swFeat.Name == "Bill of Materials1"))
                    {
                        ProcessBomFeature(swApp, swModel, swBomFeat);
                    }
                }
                swFeat = (
Feature)swFeat.GetNextFeature();
            }
        }
        
public void ProcessBomFeature(SldWorks swApp, ModelDoc2 swModel, BomFeature swBomFeat)
        {
            GetPathNames(swApp, swBomFeat);
        }

        
public void GetPathNames(SldWorks swApp, BomFeature swBomFeature)
        {
            
String[] vConfigurations = null;
            
string strConfiguration = null;
            
Object vVisibility = null;
            
int lIdx = 0;
            
int lNumConfigurations = 0;
            
int lNumRow = 0;
            
int lNumColumn = 0;
            
int lRow = 0;
            
Object[] swBOMTableAnnotationArray = null;
            
BomTableAnnotation swBOMTableAnnotation = default(BomTableAnnotation);
            
TableAnnotation swTableAnnotation = default(TableAnnotation);
            
ModelDoc2 swDocument = default(ModelDoc2);
            
AssemblyDoc swAssembly = default(AssemblyDoc);
            
string strDocumentName = null;
            
int lStartRow = 0;
            
bool bGetVisible = false;
            
String[] vModelPathNames = null;
            
string strItemNumber = "";
            
string strPartNumber = "";
            
object vModelPathName = null;
            
string strModelPathName = null;

            strDocumentName = swBomFeature.GetReferencedModelName();
            swDocument = (
ModelDoc2)swApp.GetOpenDocumentByName(strDocumentName);
            
Debug.Print("Referenced model = " + strDocumentName);
            swAssembly = (
AssemblyDoc)swDocument;

            swBOMTableAnnotationArray = (
Object[])swBomFeature.GetTableAnnotations();
            
// Get the only BOM table annotation
            swBOMTableAnnotation = (BomTableAnnotation)swBOMTableAnnotationArray[0];
            swTableAnnotation = (
TableAnnotation)swBOMTableAnnotation;
            lNumRow = swTableAnnotation.RowCount;
            lNumColumn = swTableAnnotation.ColumnCount;

            lStartRow = 1;

            
if ((!(swTableAnnotation.TitleVisible == false)))
            {
                lStartRow = 2;
            }

            bGetVisible =
false;
            
Debug.Print("# configurations = " + swBomFeature.GetConfigurationCount(bGetVisible));
            vConfigurations = (
String[])swBomFeature.GetConfigurations(bGetVisible, ref vVisibility);

            
if (((vConfigurations != null)))
            {
                lNumConfigurations = vConfigurations.GetUpperBound(0) - vConfigurations.GetLowerBound(0) + 1;
                
for (lIdx = 0; lIdx <= (lNumConfigurations - 1); lIdx++)
                {
                    strConfiguration = vConfigurations[lIdx];
                    
Debug.Print("");
                    
Debug.Print("Configuration: " + strConfiguration);

                    
for (lRow = lStartRow; lRow <= (lNumRow - 1); lRow++)
                    {
                        
Debug.Print("  row = " + (lRow - lStartRow + 1));
                        vModelPathNames = (
String[])swBOMTableAnnotation.GetModelPathNames(lRow, out strItemNumber, out strPartNumber);

                        
Debug.Print("    item number = " + strItemNumber);
                        
Debug.Print("    part number = " + strPartNumber);

                        
if (((vModelPathNames != null)))
                        {
                            
Debug.Print("    # models contributing to row = " + swBOMTableAnnotation.GetModelPathNamesCount(lRow));
                            
foreach (String vModelPathName_loopVariable in vModelPathNames)
                            {
                                vModelPathName = vModelPathName_loopVariable;
                                strModelPathName = (
String)vModelPathName;
                                
Debug.Print("       " + strModelPathName);
                            }
                        }
                        
else
                        {
                            
Debug.Print("    # models contributing to row = 0");
                            
Debug.Assert(false);
                        }
                    }
                }
            }
        }


        
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 Model Pathnames from a BOM Table 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) 2018 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.