Hide Table of Contents

Selection Lists (VBA)

If you specify a positive, non-zero mark for an object (e.g., 1 or 2 or 3, etc.) and then specify to select only the objects with that mark, the resulting selection list contains only those objects with that mark. This selection list is independent of a selection list created by selecting all of the objects.

For example:

 

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

'

' Preconditions: The file, bracket_a.sldprt, exists.

'

' NOTE: The file, bracket_a.sldprt, is usually installed

'       when you install SolidWorks and is intended to be

'       used with one of the SolidWorks tutorials. The path  

'       to this file is installation-specific; however, the  

'       path shown in this macro is the typical installation

'       path.

'

'       If you modify the part, do NOT save the part document

'       because its intended tutorial will not work!

'

' Postcondtions: None

'

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

Option Explicit

Dim swApp                   As SldWorks.SldWorks

Dim swModel                 As SldWorks.ModelDoc2

Dim swDocSpecification      As SldWorks.DocumentSpecification

Dim swSelMgr                As SldWorks.SelectionMgr

Dim swModelExt              As SldWorks.ModelDocExtension

Dim bRet                    As Boolean

Dim lMark                   As Long

Dim lMarkedIdx              As Long

Dim lNumMarkedSelections    As Long

Dim lNumAllSelections       As Long

Dim lAllIdx                 As Long

Dim swMarkedSelectedObject  As Object

Dim swSelectedObject        As Object

 

Sub main()

Set swApp = Application.SldWorks

' Open the drawing document

Set swDocSpecification = swApp.GetOpenDocSpec("C:\Program Files\SolidWorks\SolidWorks\samples\tutorial\swutilities\bracket_a.sldprt")

Set swModel = swApp.ActiveDoc

If swModel Is Nothing Then

    Set swModel = swApp.OpenDoc7(swDocSpecification)

End If

Set swSelMgr = swModel.SelectionManager

Set swModelExt = swModel.Extension

' Select all of the extrude features; Base-Extrude and Boss-Extrude5 are marked with a value of 8

bRet = swModelExt.SelectByID2("Base-Extrude", "BODYFEATURE", 0, 0, 0, True, 8, Nothing, swSelectOptionDefault)

bRet = swModelExt.SelectByID2("Boss-Extrude3", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, swSelectOptionDefault)

bRet = swModelExt.SelectByID2("Boss-Extrude4", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, swSelectOptionDefault)

bRet = swModelExt.SelectByID2("Boss-Extrude5", "BODYFEATURE", 0, 0, 0, True, 8, Nothing, swSelectOptionDefault)

bRet = swModelExt.SelectByID2("Boss-Extrude7", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, swSelectOptionDefault)

' Look for a given mark value

    lMark = 8

    ' Get the number of marked selections

    lNumMarkedSelections = swSelMgr.GetSelectedObjectCount2(lMark)

    Debug.Print "Number of marked selections = " & lNumMarkedSelections

 

    ' Get the total number of selections

    lNumAllSelections = swSelMgr.GetSelectedObjectCount2(-1)

    Debug.Print "Number of selections        = " & lNumAllSelections

 

    Debug.Print " "

    

    For lMarkedIdx = 1 To lNumMarkedSelections

         Set swMarkedSelectedObject = swSelMgr.GetSelectedObject6(lMarkedIdx, lMark)

          For lAllIdx = 1 To lNumAllSelections

             Set swSelectedObject = swSelMgr.GetSelectedObject6(lAllIdx, -1)

             If (swMarkedSelectedObject Is swSelectedObject) Then

                ' Types must match

                Debug.Assert (swSelMgr.GetSelectedObjectType3(lMarkedIdx, lMark) = swSelMgr.GetSelectedObjectType3(lAllIdx, -1))

                Debug.Print "Number of marked selections = " & lMarkedIdx

                Debug.Print "Number of selections        = " & lAllIdx

                Debug.Print " "

              Exit For

             End If

         Next lAllIdx

         ' Has marked selected object been found in complete selection list

        Debug.Assert (lAllIdx <= lNumAllSelections)

     Next lMarkedIdx

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:   Selection Lists
*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.