Hide Table of Contents

Get Selected Objects and Types in an Assembly Example (VBA)

This example shows how to determine what is currently selected in an assembly document.

 

'-----------------------------------------------

' Problem:

'       There are many features and selectable

'       entities within the SolidWorks software. In most cases,

'       it is obvious as to what is selected, but

'       sometimes it is not clear or it is ambiguous.

'

'       This code sample is a diagnostic tool

'       to determine what is currently selected. It shows

'       several techniques and methods to get a reference

'       to what is selected.

'

' Preconditions:

'       (1) Assembly document is open.

'       (2) Something is selected.

'

' Postconditions:

'       None

'

' NOTE: Not all features have an associated *FeatureData object.

'

'-----------------------------------------------

 

Option Explicit

 

Sub main()

    Dim swApp               As SldWorks.SldWorks

    Dim swModel             As SldWorks.ModelDoc2

    Dim swSelMgr            As SldWorks.SelectionMgr

    Dim swSelObj            As Object

    Dim swFeat              As SldWorks.Feature

    Dim swEnt               As SldWorks.Entity

    Dim swBody              As SldWorks.Body2

    Dim swSelComp           As SldWorks.Component2

    Dim swSelModel          As SldWorks.ModelDoc2

    Dim nSelType            As Long

    Dim sFeatName           As String

    Dim bRet                As Boolean

 

    ' Disables Visual Basic's implicit error on QueryInterface

    On Error Resume Next

 

    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

 

    ' Could have selected either a feature or an entity;

    ' do not try to get entity directly from feature

    ' because feature may be NULL; instead,

    ' use ISelectionManager

    Set swFeat = swSelMgr.GetSelectedObject6(1, -1)

    Set swEnt = swSelMgr.GetSelectedObject6(1, -1)

    Set swBody = swSelMgr.GetSelectedObject6(1, -1)

    Set swSelObj = swSelMgr.GetSelectedObject6(1, -1)

    Set swSelComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)

    Debug.Print "SelType      = " & swSelMgr.GetSelectedObjectType3(1, -1)

 

    If Not swFeat Is Nothing Then

        Debug.Print "Feature      = " & swFeat.Name & " [" & swFeat.GetTypeName & "]"

    End If

 

    If Not swBody Is Nothing Then

       Debug.Print "  Body selected"

    End If

 

    If swFeat Is Nothing And swEnt Is Nothing And swBody Is Nothing And Not swSelObj Is Nothing Then

        Debug.Print "  Unknown object"

    End If

 

    ' Could not get component from ISelectionManager,

    ' so try and get component through IEntity

    If swSelComp Is Nothing Then

        Set swSelComp = swEnt.GetComponent

    End If

 

    If Not swSelComp Is Nothing Then

        Set swSelModel = swSelComp.GetModelDoc2

        Debug.Print "Component name               = " & swSelComp.Name2

        Debug.Print "Model path                   = " & swSelModel.GetPathName

    End If

 

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 Selected Objects and Types 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) 2012 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.