Only Show Selected Components Example (VBA)
This example shows how to only show the selected components in an assembly.
'----------------------------------------------------
'
' Preconditions:
' (1)
Assembly is open.
' (2)
One or more components are selected either in the
' graphics
area or in the FeatureManager design tree.
'
' Postconditions: All components, except selected components,
are hidden.
'
' NOTES:
' (1)
If a subassembly is selected, all child components (parts
' and
sub-subassemblies, are shown.
' (2)
If a sub-subassembly is selected, all parents are recursively
' shown
to the top-level assembly.
'
'-----------------------------------------------------
Option Explicit
Public Enum swComponentVisibilityState_e
swComponentHidden
= 0
swComponentVisible
= 1
End Enum
Function IsCompInSelList _
( _
swSelCompArr()
As SldWorks.Component2, _
swComp
As SldWorks.Component2 _
) As Boolean
Dim
vSelCompArr As
Variant
Dim
vSelComp As
Variant
vSelCompArr
= swSelCompArr
For
Each vSelComp In vSelCompArr
If
vSelComp Is swComp Then
IsCompInSelList
= True
Exit
Function
End
If
Next
vSelComp
IsCompInSelList
= False
End Function
Sub ShowParentComp _
( _
swApp
As SldWorks.SldWorks, _
swModel
As SldWorks.ModelDoc2, _
swComp
As SldWorks.Component2 _
)
Dim
swParentComp As
SldWorks.Component2
Set
swParentComp = swComp.GetParent
If
Not swParentComp Is Nothing Then
'
This is a component in a subassembly, so have to set
'
subassembly to be visible because subassembly's visibility
'
overrides component's visibility; that is, show parent
swParentComp.Visible = swComponentVisible
'
Show grandparent; that is, recurse up assembly tree
ShowParentComp
swApp, swModel, swParentComp
End
If
End Sub
Sub ShowComp _
( _
swApp
As SldWorks.SldWorks, _
swModel
As SldWorks.ModelDoc2, _
swComp
As SldWorks.Component2 _
)
Dim
vChildArr As
Variant
Dim
vChild As
Variant
Dim
swChildComp As
SldWorks.Component2
'
Show this component
swComp.Visible = swComponentVisible
vChildArr
= swComp.GetChildren
For
Each vChild In vChildArr
Set
swChildComp = vChild
'
Show all children
swChildComp.Visible = swComponentVisible
'
Show all grandchildren; that is, recurse down assembly tree
ShowComp
swApp, swModel, swChildComp
Next
vChild
End Sub
Sub HideAllComp _
( _
swApp
As SldWorks.SldWorks, _
swModel
As SldWorks.ModelDoc2, _
swComp
As SldWorks.Component2 _
)
Dim
vChildArr As
Variant
Dim
vChild As
Variant
Dim
swChildComp As
SldWorks.Component2
'
Hide this component
swComp.Visible = swComponentHidden
vChildArr
= swComp.GetChildren
For
Each vChild In vChildArr
Set
swChildComp = vChild
'
Hide each child component; that is, recurse down assembly tree
HideAllComp
swApp, swModel, swChildComp
Next
vChild
End Sub
Function GetSelCompList _
( _
swApp
As SldWorks.SldWorks, _
swModel
As SldWorks.ModelDoc2 _
) As SldWorks.Component2()
Dim
swSelCompArr() As
SldWorks.Component2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swComp As
SldWorks.Component2
Dim
nSelCount As
Long
Dim
i As
Long
ReDim
swSelCompArr(0)
Set
swSelMgr = swModel.SelectionManager
nSelCount
= swSelMgr.GetSelectedObjectCount:
Debug.Assert nSelCount >= 1
For
i = 1 To nSelCount
Set
swComp = swSelMgr.GetSelectedObjectsComponent2(i)
If
Not swComp Is Nothing Then
Set
swSelCompArr(UBound(swSelCompArr)) = swComp
ReDim
Preserve swSelCompArr(UBound(swSelCompArr) + 1)
End
If
Next
i
Debug.Assert
UBound(swSelCompArr) > 0
ReDim
Preserve swSelCompArr(UBound(swSelCompArr) - 1)
GetSelCompList
= swSelCompArr
End Function
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swDocExt As
SldWorks.ModelDocExtension
Dim
swFeatMgr As
SldWorks.FeatureManager
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swConfigMgr As
SldWorks.ConfigurationManager
Dim
swConfig As
SldWorks.Configuration
Dim
swRootComp As
SldWorks.Component2
Dim
swSelCompArr() As
SldWorks.Component2
Dim
vSelCompArr As
Variant
Dim
vSelComp As
Variant
Dim
swSelComp As
SldWorks.Component2
Dim
nSelCount As
Long
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swDocExt = swModel.Extension
Set
swFeatMgr = swModel.FeatureManager
Set
swAssy = swModel
Set
swConfigMgr = swModel.ConfigurationManager
Set
swConfig = swConfigMgr.ActiveConfiguration
Set
swRootComp = swConfig.GetRootComponent
'
Temporarily disable FeatureManager design tree updates to increase performance
swFeatMgr.EnableFeatureTree = False
swSelCompArr
= GetSelCompList(swApp, swModel)
vSelCompArr
= swSelCompArr
HideAllComp
swApp, swModel, swRootComp
For
Each vSelComp In vSelCompArr
Set
swSelComp = vSelComp
ShowComp
swApp, swModel, swSelComp
ShowParentComp
swApp, swModel, swSelComp
Next
vSelComp
'
Restore selected components
nSelCount
= swDocExt.MultiSelect(vSelCompArr,
True, Nothing): Debug.Assert UBound(vSelCompArr) + 1 = nSelCount
swFeatMgr.EnableFeatureTree = True
End Sub