Get Hidden Children Component Names Example (VBA)
This example shows how to get the names of the hidden components in
an assembly document.
'-----------------------------------------------------------
' Preconditions:
' 1. In SolidWorks, click File > Open,
and browse to
' <SolidWorks_install_dir>\samples\tutorial\routing-pipes.
' 2. Select finalskid.sldasm, select
the Quick view/Selective
' open check
box, and click Open.
'
' NOTE: If
the path to the Design Library (click Tools
> Options >
' File Locations
and select Design Library in Show folders for)
' does
not exist, then multiple dialogs might be displayed
' informing
you that SolidWorks is unable to locate
' some
components. Click No or OK to close these dialogs.
'
' 3. Expand all nodes in the FeatureManager design tree,
' select
all upper-level components except Cabinet<1>(Default),
' and
click Open Selected.
' 4. Click OK.
' 5. Run the macro and look at the results in the Immediate
' Window
in the IDE.
'
' NOTE:
Only the selected components are loaded. The
' Cabinet<1>(Default)
component and its
' children
components (cabinet-1, door-1,
' and
door-2)are not loaded and are
not visible
' in
the SolidWorks graphics area. See the
' SolidWorks
Help for details about selectively
' opening
assembly documents.
'
' Postconditions: The Cabinet<1>(Default)
component and its
' children
components (cabinet-1, door-1,
' and
door-2) are not loaded.
'
' IMPORTANT: Because
this assembly document is used in
' a SolidWorks online tutorial, when you close the assembly
' document do not save any changes, if prompted.
'-----------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
Set
swApp = Application.SldWorks
Dim
swAssembly As SldWorks.AssemblyDoc
Set
swAssembly = swApp.ActiveDoc
Dim
swComponent As SldWorks.Component2
Set
swComponent = swAssembly.GetComponentByName("Cabinet-1")
If
swComponent.HasUnloadedComponents
Then
Dim
vPaths As Variant
Dim
vRefdConfigs As Variant
Dim
vReasons As Variant
Dim
vDocTypes As Variant
Dim
vNames As Variant
vNames
= swComponent.GetUnloadedComponentNames(vPaths,
vRefdConfigs, vReasons, vDocTypes)
If
IsEmpty(vPaths) Or IsEmpty(vRefdConfigs) Or IsEmpty(vReasons) Or IsEmpty(vDocTypes)
Or IsEmpty(vNames) Then
MsgBox
"Error: Empty VARIANT parameter!"
Debug.Assert
False
Exit
Sub
End
If
If
Not (IsArray(vPaths) And IsArray(vRefdConfigs) And IsArray(vReasons) And
IsArray(vDocTypes) And IsArray(vNames)) Then
MsgBox
"Error: Non-array VARIANT parameter!"
Debug.Assert
False
Exit
Sub
End
If
If
(LBound(vPaths) <> LBound(vRefdConfigs)) Or (LBound(vPaths) <>
LBound(vReasons)) Or (LBound(vPaths) <> LBound(vDocTypes)) Or (LBound(vPaths)
<> LBound(vNames)) Then
MsgBox
"Error: Array lower bounds do not match!"
Debug.Assert
False
Exit
Sub
End
If
If
(UBound(vPaths) <> UBound(vRefdConfigs)) Or (UBound(vPaths) <>
UBound(vReasons)) Or (UBound(vPaths) <> UBound(vDocTypes)) Or (UBound(vPaths)
<> UBound(vNames)) Then
MsgBox
"Error: Array upper bounds do not match!"
Debug.Assert
False
Exit
Sub
End
If
Dim
index As Integer
For
index = LBound(vNames) To UBound(vNames)
Dim
debugMessage As String
debugMessage
= index & ": "
Dim
eDocType As swDocumentTypes_e
eDocType
= vDocTypes(index)
Select
Case eDocType
Case
swDocNONE
debugMessage
= debugMessage & "The document "
Case
swDocPART
debugMessage
= debugMessage & "The part "
Case
swDocASSEMBLY
debugMessage
= debugMessage & "The assembly "
Case
swDocDRAWING
debugMessage
= debugMessage & "The drawing "
Case
swDocSDM
debugMessage
= debugMessage & "The SDM "
Case
Else
debugMessage
= debugMessage & "The document of unknown type "
End
Select
debugMessage
= debugMessage & vNames(index) & " was not loaded because
it is "
Dim
bUnloadedBecauseHidden As Boolean
bUnloadedBecauseHidden
= vReasons(index)
If
bUnloadedBecauseHidden Then
debugMessage
= debugMessage & "hidden. "
Else
debugMessage
= debugMessage & "suppressed."
End
If
Debug.Print
debugMessage
Next
End
If
End Sub