Hide Table of Contents

Add Objects to Selection List Example (VBA)

This example shows how to populate the selection list with objects that have not been preselected through

the user interface.

'----------------------------------------------------------------------------
' Preconditions: Open a model that contains Sketch1 and Sketch2.
'
' Postconditions:
' 1. Inspect the Immediate Window.
' 2. The selection list containing Sketch1 is suspended.
' 3. Another selection list is populated with two objects.
' 4. The type of the last object in the selection list is reported.
' 5. The previous selection list with Sketch1 is resumed.
' 6. Sketch1 is placed in edit mode.
' ---------------------------------------------------------------------------

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim selMgr As SldWorks.SelectionMgr
Dim selData As SldWorks.SelectData
Dim featMgr As SldWorks.FeatureManager
Dim feat As SldWorks.Feature
Dim selObjs() As Object
Dim selobj As Object
Dim featCount As Long
Dim i As Integer
Dim typeName As String
Dim j As Integer
Dim numAdded As Long
Dim boolstatus As Boolean
Dim ret As Long
Dim count As Long

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set selMgr = swModel.SelectionManager
    Set selData = selMgr.CreateSelectData
    Set featMgr = swModel.FeatureManager
   

    featCount = featMgr.GetFeatureCount(True)
    Set feat = swModel.FirstFeature
    j = 0
    ReDim selObjs(j + 1)
    For i = 0 To featCount
        If Not feat Is Nothing Then
       

            typeName = feat.Name
            typeName = UCase(typeName)
            If "SKETCH" = Left(typeName, 6) Then
                Set selObjs(j) = feat
                j = j + 1
                ReDim Preserve selObjs(j + 1)
            End If
            Set feat = feat.GetNextFeature
        End If
    Next i
   

    ' Add one object to the current selection list
    boolstatus = swModel.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
   

    ' Start a new selection list
    ret = selMgr.SuspendSelectionList
    Debug.Print "The current selection list with " & ret & " object (Sketch1) is suspended."
   

    ' Add two objects to the new selection list
    numAdded = selMgr.AddSelectionListObjects((selObjs), selData)
    Debug.Print "A new selection list is started."
   

    ' Get number of objects in the new selection list (should be 2)
    count = selMgr.GetSelectedObjectCount
    Debug.Print "The selection list now contains " & count & " objects."
   

    ' Get the last object in the new selection list
    Set selobj = selMgr.GetSelectedObject6(count, -1)
    Debug.Print "The last object in the selection list is of swSelectType_e = " & selMgr.GetSelectedObjectType3(count, -1) & "."
   

    ' Go back to the previous selection list
    selMgr.ResumeSelectionList
    Debug.Print "The previous selection list is resumed."
   

    ' Get the number of objects in the selection list (should be 1)
    count = selMgr.GetSelectedObjectCount
    Debug.Print "The selection list now contains " & count & " object (Sketch1)."
   

    swModel.EditSketch
    Debug.Print "Sketch1 is in edit mode."

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:   Add Objects to Selection List 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) 2015 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.