Hide Table of Contents

Get Component Name From Selected Entity Example (VBA)

This example shows how to get the name of a component to possibly use with a future call to IModelDocExtension::SelectByID2, when selectively opening the assembly document and specific components using ISldWorks::OpenDoc7 and IDocumentSpecification, etc. This example also shows how to set and get a component reference.

'--------------------------------------------------------------------
' Preconditions:
' 1. Open an assembly document.
' 2. Select an entity (face, edge, vertex, or loop) on any
'    component in the graphics area.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Adds a component reference to the component to which the
'    entity belongs.
' 2. Examine the Immediate window.
' 3. Locate the component to which the component reference was added
'    in the FeatureManager design tree. If necessary, use the scrollbar
'    at the bottom of the FeatureManager design tree to see the component
'    reference.
'--------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelectionMgr As SldWorks.SelectionMgr
Dim swEntity As SldWorks.Entity
Dim swComponent As SldWorks.Component2
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc    
    ' Get the selected entity (i.e., face, edge, vertex, or loop)
    ' and get the name of its component
    Set swSelectionMgr = swModel.SelectionManager
    Set swEntity = swSelectionMgr.GetSelectedObject6(1, -1)
    Set swComponent = swEntity.GetComponent    
    ' Print the name of the component to which the
    ' the selected entity belongs
    Debug.Print "Name of component to which the selected entity belongs: " & swComponent.GetSelectByIDString
    
    ' Set a component reference to this component
    swComponent.ComponentReference = "TestComponentReference"
    Debug.Print "Component reference added to the component to which the selected entity belongs: " & swComponent.ComponentReference
    
    ' Rebuild the assembly to see the component reference
    ' beside the name of the component in the FeatureManager design
    ' tree
    swModel.ForceRebuild3 True
End Sub


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 Component Name From Selected Entity Example (VBA)
*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) 2022 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.