Get Feature Type and Name Example (VBA)
This example shows how to get the feature type and name of the interactively
selected feature for use with IModelDocExtension::SelectByID2.
'----------------------------
' Preconditions:
' 1. Open:
'<SolidWorks_install_dir>\samples\tutorial\cosmosfloxpress\ball valve\ball_valve.sldasm
' 2. Expand any component in the FeatureManager design tree
' and
select one of its features.
'
' Postconditions: The selected feature's type and name
are
' printed
to the Immediate window. Examine the Immediate window to
' verify
the results.
'
' NOTE: Because this assembly document is used
' by
a SolidWorks online tutorial, do not
' save
any changes when closing the document.
'------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeat As SldWorks.Feature
Dim featName As String, featType As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swModelDocExt = swModel.Extension
' Get the selected feature
Set swFeat = swSelMgr.GetSelectedObject6(1,
-1)
swModel.ClearSelection2
True
' Get the feature's type and name
featName = swFeat.GetNameForSelection(featType)
swModelDocExt.SelectByID2
featName, featType, 0, 0, 0, True, 0, Nothing, 0
' Print the feature's type and name
' to the Immediate window
Debug.Print "Feature type: " & featType
Debug.Print "Feature name: " & featName
End Sub