Lister tous les composants de l'assemblage actif et les composants des sous assemblages

bonjour,

je souhaite lister tous les composant de l'assemblage actif sous la forme

composant1-1@assemblageracine

composant1-2@assemblageracine

ssassemblage1-1@assemblageracine/composant2-1@ssassemblage1

etc...

le but étant d'injecter par la suite le résultat dans la macro ci-dessus suivant d'autres paramètres.

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("1052616-1@1052602/2102935-1@1052616", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.HideComponent2
Part.ClearSelection2 True
End Sub

merci

Bonjours, ceci devrait te convenir j'ai un peu nettoyé la macro de l'aide, http://help.solidworks.com/2018/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.icomponent2~getchildren.html

 

Option Explicit

Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
    Dim vChildComp As Variant
    Dim swChildComp As SldWorks.Component2
    Dim swCompConfig As SldWorks.Configuration
    Dim sPadStr As String
    Dim i As Long
    For i = 0 To nLevel - 1
        sPadStr = sPadStr + "  "
    Next i
    vChildComp = swComp.GetChildren
    For i = 0 To UBound(vChildComp)
        Set swChildComp = vChildComp(i)
        Debug.Print sPadStr & "+" & swChildComp.Name2 & " "

        TraverseComponent swChildComp, nLevel + 1
    Next i
End Sub

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAssy As SldWorks.AssemblyDoc
    Dim swConf As SldWorks.Configuration
    Dim swRootComp As SldWorks.Component2
    Dim nStart As Long
    Dim bRet As Boolean
    Dim fileName As String
    Dim errors As Long
    Dim warnings As Long
    Set swApp = CreateObject("SldWorks.Application")
    ' Open assembly
    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\smartcomponents\stepped_shaft.sldasm"
    Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swConf = swModel.GetActiveConfiguration
    Set swRootComp = swConf.GetRootComponent3(True)
    nStart = Timer
    Debug.Print "File = " & swModel.GetPathName

    TraverseComponent swRootComp, 1
    
    Debug.Print ""
    Debug.Print "Time = " & Timer - nStart & " seconds"
End Sub
 

2 « J'aime »

ça marche bien merci mandragore