Select All Faces of Component Example (VBA)
This example shows how to select all of the faces of the selected component.
'------------------------------------------------
' Preconditions:
' (1)
Assembly is open.
' (2)
Assembly is fully resolved.
' (3)
Something is selected.
' (4)
Selected item belongs to a component part.
'
' Postconditions:
' (1)
Initial entity is deselected.
' (2)
All faces in component part are selected.
'
'----------------------------------------------
Option Explicit
Public Enum swBodyType_e
swSolidBody
= 0
swSheetBody
= 1
swWireBody
= 2
swMinimumBody
= 3
swGeneralBody
= 4
swEmptyBody
= 5
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swSelData As
SldWorks.SelectData
Dim
swComp As
SldWorks.Component2
Dim
swBody As
SldWorks.body2
Dim
vBody As
Variant
Dim
vBodyArr As
Variant
Dim
swFace As
SldWorks.face2
Dim
swEnt As
SldWorks.entity
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swSelData = swSelMgr.CreateSelectData
Set
swComp = swSelMgr.GetSelectedObjectsComponent(1)
'
Return number of bodies
vBodyArr
= swComp.GetBodies2(swSolidBody)
'
Probably a subassembly selected
If
IsEmpty(vBodyArr) Then Exit Sub
'
Debugging only
Debug.Print
"Assy = " & swModel.GetPathName
Debug.Print
" Comp
= " & swComp.Name2
Debug.Print
" Path
=
" & swComp.GetPathName
Debug.Print
" Bodies
= "
& UBound(vBodyArr) + 1
swModel.ClearSelection2 True
For
Each vBody In vBodyArr
Set
swBody = vBody
Set
swFace = swBody.GetFirstFace
Do
While Not swFace Is Nothing
Set
swEnt = swFace
bRet
= swEnt.Select4(True, swSelData): Debug.Assert bRet
Set
swFace = swFace.GetNextFace
Loop
Next
End Sub