Add Objects to Selection List Example (VB.NET)
This example shows how to populate the selection list with objects that have
not been pre-selected through the user interface.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Open a model that contains Sketch1 and Sketch2.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Suspends the selection list containing Sketch1.
' 2. Populates another selection list with two objects.
' 3. Gets the type of the last object in the selection list.
' 4. Resumes the previous selection list with Sketch1.
' 5. Places Sketch1 in edit mode.
' 6. Inspect the Immediate window and graphics area.
' ---------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Partial Class SolidWorksMacro
Dim swModel As ModelDoc2
Dim selMgr As SelectionMgr
Dim selData As SelectData
Dim featMgr As FeatureManager
Dim feat As Feature
Dim selObjs(1) As Feature
Dim arrObjIn(1) As DispatchWrapper
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()
swModel = swApp.ActiveDoc
selMgr = swModel.SelectionManager
selData = selMgr.CreateSelectData
featMgr = swModel.FeatureManager
featCount = featMgr.GetFeatureCount(True)
feat = swModel.FirstFeature
j = 0
For i = 0 To featCount
If Not feat Is Nothing Then
typeName = feat.Name
typeName = UCase(typeName)
If "SKETCH" = Left(typeName, 6) Then
selObjs(j) = feat
arrObjIn(j) = New DispatchWrapper(selObjs(j))
j = j + 1
End If
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(arrObjIn, 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
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
Public swApp As SldWorks
End Class