Get Parent Component Example (VBA)
This example shows how to get the parent component.
'---------------------------------------
'
' Preconditions:
' (1)
Assembly document is open.
' (2)
Assembly component is selected.
'
' Postconditions: None
'
'---------------------------------------
Option Explicit
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swConfigMgr As
SldWorks.ConfigurationManager
Dim
swConf As
SldWorks.Configuration
Dim
swComp As
SldWorks.Component2
Dim
swParentComp As
SldWorks.Component2
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swComp = swSelMgr.GetSelectedObjectsComponent2(1)
'
Returns NULL if selected component is a top-level component
Set
swParentComp = swComp.GetParent
If
Nothing Is swParentComp Then
Set
swConfigMgr = swModel.ConfigurationManager
Set
swConf = swConfigMgr.ActiveConfiguration
Set
swParentComp = swConf.GetRootComponent
End
If
Debug.Assert
Not swParentComp Is Nothing
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" Component
=
" & swComp.Name2 &
" [" & swComp.GetPathName
& "]"
Debug.Print
" ParentComp
= " & swParentComp.Name2
& " [" & swParentComp.GetPathName
& "]"
End Sub
'---------------------------------------