Get Hidden Components Filenames Example (VBA)
This example shows how to get the filenames of the components hidden
in an assembly.
'-----------------------------------------------------------
' Preconditions:
' 1. In SolidWorks, click File >
Open, and browse to
' <SolidWorks_install_dir>\samples\tutorial\routing-pipes.
' 2. Select ball valve with flanges.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. Select Ball Valve-1 in the FeatureManager
design tree
' 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. Components
' not
selected are not loaded and not visible in the
' SolidWorks
graphics area. See the SolidWorks
' Help
for details about selectively opening
' assembly
documents.
'
' Postconditions: Both slip on weld flange components
are hidden.
'
' NOTE: 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
If
swAssembly.HasUnloadedComponents
Then
Dim
vPaths As Variant
Dim
vRefdConfigs As Variant
Dim
vReasons As Variant
Dim
vDocTypes As Variant
Dim
vNames As Variant
vNames
= swAssembly.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 & vPaths(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