Get Hidden Children Component Names Example (VB.NET)
This example shows how to get the names of hidden children components.
This example also shows how to call a method that returns multiple objects
and how to cast those objects to arrays (IComponent2::GetUnloadedComponentNames).
'-----------------------------------------------------------
' 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 On
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public
Sub main()
Dim
swAssembly As AssemblyDoc
Dim
swComponent As Component2
swAssembly
= swApp.ActiveDoc
swComponent
= swAssembly.GetComponentByName("Cabinet-1")
If
swComponent.HasUnloadedComponents
Then
Dim
oPaths As Object = Nothing
Dim
oRefdConfigs As Object = Nothing
Dim
oReasons As Object = Nothing
Dim
oDocTypes As Object = Nothing
Dim
oNames As Object = Nothing
oNames
= swComponent.GetUnloadedComponentNames(oPaths,
oRefdConfigs, oReasons, oDocTypes)
Dim
Paths() As String = Nothing
Dim
RefdConfigs() As String = Nothing
Dim
Reasons() As Integer = Nothing
Dim
DocTypes() As Integer = Nothing
Dim
Names() As String = Nothing
Paths
= oPaths
RefdConfigs
= oRefdConfigs
Reasons
= oReasons
DocTypes
= oDocTypes
Names
= oNames
If
Not (IsArray(Paths) And IsArray(RefdConfigs) And IsArray(Reasons) And
IsArray(DocTypes) And IsArray(Names)) Then
MsgBox("Error:
Non-array parameter!")
Debug.Assert(False)
Exit
Sub
End
If
If
(LBound(Paths) <> LBound(RefdConfigs)) Or (LBound(Paths) <>
LBound(Reasons)) Or (LBound(Paths) <> LBound(DocTypes) Or (LBound(Paths)
<> LBound(Names))) Then
MsgBox("Error:
Array lower bounds do not match!")
Debug.Assert(False)
Exit
Sub
End
If
If
(UBound(Paths) <> UBound(RefdConfigs)) Or (UBound(Paths) <>
UBound(Reasons)) Or (UBound(Paths) <> UBound(DocTypes) Or (UBound(Paths)
<> UBound(Names))) Then
MsgBox("Error:
Array upper bounds do not match!")
Debug.Assert(False)
Exit
Sub
End
If
Dim
index As Integer
For
index = LBound(Paths) To UBound(Paths)
Dim
debugMessage As String
debugMessage
= index & ": "
Dim
eDocType As swDocumentTypes_e
eDocType
= DocTypes(index)
Select
Case eDocType
Case
swDocumentTypes_e.swDocNONE
debugMessage
= debugMessage & "The document "
Case
swDocumentTypes_e.swDocPART
debugMessage
= debugMessage & "The part "
Case
swDocumentTypes_e.swDocASSEMBLY
debugMessage
= debugMessage & "The assembly "
Case
swDocumentTypes_e.swDocDRAWING
debugMessage
= debugMessage & "The drawing "
Case
swDocumentTypes_e.swDocSDM
debugMessage
= debugMessage & "The SDM "
Case
Else
debugMessage
= debugMessage & "The document is an unknown type "
End
Select
debugMessage
= debugMessage & Names(index) & " was not loaded because
it is "
Dim
bUnloadedBecauseHidden As Boolean
bUnloadedBecauseHidden
= Reasons(index)
If
bUnloadedBecauseHidden Then
debugMessage
= debugMessage & "hidden."
Else
debugMessage
= debugMessage & "suppressed."
End
If
Debug.Print(debugMessage)
Next
End
If
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class