AnsweredAssumed Answered

How get all top level components from assembly and all components from children assembly, if children assembly is promoted?

Question asked by Alek 77 on Apr 11, 2020
Latest reply on Apr 12, 2020 by Alek 77

How get all top level components from assembly and all components from children assembly, if children assembly is promoted?

I can get all top level components from assembly like instance.GetComponents(True) for SldWorks.AssemblyDoc. But this method does not allow me to take all the components from children assembly, if children assembly is promoted.

I maked Function, but my macro = infinity work. I think I chose the wrong way.

Please, give me advice on how to get all components + components from an promoted children assembly

My code:

Option Explicit

Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim allComponents As Variant
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim bRet As Boolean
Dim i As Integer
Global compsColl As Collection

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Set swConf = swModel.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
Debug.Print "File = " & swModel.GetPathName

Set compsColl = New Collection

'allComponents = swAssembly.GetComponents(True)
allComponents = GetAllComp(swModel)

For i = 1 To compsColl.Count
Debug.Print "Компонент (" & i & ") - " & compsColl(i).Name2
Next


Debug.Print "Ok"
Debug.Print
End Sub


Function GetAllComp(asm As SldWorks.AssemblyDoc)
Dim allComp As Variant

allComp = asm.GetComponents(True)

For i = 0 To UBound(allComp)
Dim eachComp As SldWorks.Component2
Set eachComp = allComp(i)
Debug.Print "Компонент (" & i & ") - " & eachComp.Name2

If eachComp.GetModelDoc2.GetType = swDocASSEMBLY Then

Dim swConfig As SldWorks.Configuration
Dim swConfMgr As SldWorks.ConfigurationManager
Set swConfMgr = eachComp.GetModelDoc2.ConfigurationManager
Set swConfig = swConfMgr.ActiveConfiguration

If swConfig.ChildComponentDisplayInBOM = swChildComponentInBOMOption_e.swChildComponent_Promote Then
Debug.Print "Asm Promote"

Dim compDoc As SldWorks.ModelDoc2
Set compDoc = eachComp.GetModelDoc2
Dim compAssembly As SldWorks.AssemblyDoc
Set compAssembly = compDoc

GetAllComp = GetAllComp(compAssembly)
Else
compsColl.Add eachComp
End If
Else
compsColl.Add eachComp
End If
Next i
Debug.Print
End Function

Attachments

Outcomes