Hide Table of Contents

Get External References (C#)

This example shows how to get a list of the external references for a selected component, selected feature, or document.

//----------------------------------------------------------
//  Preconditions:
//  1. Open an assembly or part document.
//  2. Select:
//     * a component in an assembly document.
//       - or -
//     * a feature in an assembly or part document.
//       - or -
//     * Nothing in either type of document.
//
// Postconditions: Examine the Immediate window
// to see the name of the part or assembly document
// and the external references for the selected
// component, feature, or document.
//----------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;

namespace ListExternalFileReferencesCSharp.csproj
{

    
partial class SolidWorksMacro
    {


        
public void Main()
        {
            ModelDoc2 swModel =
default(ModelDoc2);
            ModelDocExtension swModDocExt =
default(ModelDocExtension);
            SelectionMgr swSelMgr =
default(SelectionMgr);
            Feature swFeat =
default(Feature);
            Component2 swComp =
default(Component2);
            
object vModelPathName = null;
            
object vComponentPathName = null;
            
object vFeature = null;
            
object vDataType = null;
            
object vStatus = null;
            
object vRefEntity = null;
            
object vFeatComp = null;
            
int nConfigOpt = 0;
            
string sConfigName = null;
            
int nRefCount = 0;
            
int nSelType = 0;
            
int i = 0;

            swModel = (ModelDoc2)swApp.ActiveDoc;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;

            swModDocExt = (ModelDocExtension)swModel.Extension;
            nSelType = swSelMgr.GetSelectedObjectType3(1, -1);

            
switch (nSelType)
            {

                
// Selected component in an assembly document
                case (int)swSelectType_e.swSelCOMPONENTS:
                    swComp = (Component2)swSelMgr.GetSelectedObjectsComponent3(1, -1);
                    nRefCount = swComp.ListExternalFileReferencesCount();
                    swComp.ListExternalFileReferences2(
out vModelPathName, out vComponentPathName, out vFeature, out vDataType, out vStatus, out vRefEntity, out vFeatComp, out nConfigOpt, out sConfigName);

                    swModel = (ModelDoc2)swComp.GetModelDoc2();

                    
break;
                
// Selected feature in a part or assembly document
                case (int)swSelectType_e.swSelBODYFEATURES:
                
case (int)swSelectType_e.swSelSKETCHES:
                    swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1);
                    nRefCount = swFeat.ListExternalFileReferencesCount();
                    swFeat.ListExternalFileReferences2(
out vModelPathName, out vComponentPathName, out vFeature, out vDataType, out vStatus, out vRefEntity, out vFeatComp, out nConfigOpt, out sConfigName);

                    
break;

                
// Part document only
                default:
                    nRefCount = swModDocExt.ListExternalFileReferencesCount();
                    swModDocExt.ListExternalFileReferences(
out vModelPathName, out vComponentPathName, out vFeature, out vDataType, out vStatus, out vRefEntity, out vFeatComp, out nConfigOpt, out sConfigName);

                    
break;
            }

            
Debug.Print("Model name = " + swModel.GetPathName());
            
Debug.Print("    Reference count        = " + System.Convert.ToString(nRefCount));

            
if (nRefCount >= 1)
            {
                
object[] ModelPathName = new object[nRefCount - 1];
                
object[] ComponentPathName = new object[nRefCount - 1];
                
object[] Feature = new object[nRefCount - 1];
                
object[] DataType = new object[nRefCount - 1];
                
int[] Status = new int[nRefCount - 1];
                
object[] RefEntity = new object[nRefCount - 1];
                
object[] FeatComp = new object[nRefCount - 1];

                ModelPathName = (
object[])vModelPathName;
                ComponentPathName = (
object[])vComponentPathName;
                Feature = (
object[])vFeature;
                DataType = (
object[])vDataType;
                Status = (
int[])vStatus;
                RefEntity = (
object[])vRefEntity;
                FeatComp = (
object[])vFeatComp;

                
Debug.Print("");
                
for (i = 0; i <= nRefCount - 1; i++)
                {
                    
Debug.Print("    Model path + name      = " + ModelPathName[i]);
                    
Debug.Print("    Component path + name  = " + ComponentPathName[i]);
                    
Debug.Print("    Feature                = " + Feature[i]);
                    
Debug.Print("    Data type              = " + DataType[i]);
                    
Debug.Print("    Status                 = " + System.Convert.ToString(Status[i]));
                    
Debug.Print("    Reference entity       = " + RefEntity[i]);
                    
Debug.Print("    Feature component      = " + FeatComp[i]);
                    
Debug.Print("    Config option          = " + nConfigOpt);
                    
Debug.Print("    Config name            = " + sConfigName);
                    
Debug.Print(" ");
                }
            }
        }



        
/// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>

        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 External References (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) 2013 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.