Donc si j'ai bien compris sa donnerai
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vDepend As Variant
Dim bRet As Boolean
Dim i As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
vDepend = swApp.GetDocumentDependencies2("PièceRechercher-1", True, True, False)
If IsEmpty(vDepend) Then
Debug.Print " No dependencies."
Exit Sub
else
boolstatus = "ce que je veux faire de mon assemblage
End If
For i = 0 To (UBound(vDepend) - 1) / 2
Debug.Print " " + vDepend(2 * i) + " --> " + vDepend(2 * i + 1)
Next i
End Sub
Le bRet sert a quoi ? J'imagine que le les trois dernière ligne (For i ...) sont pour descendre dans les différent niveau de l'arbre.
Par exemple oui l'important est au niveau de la zone
For i = 0 To (UBound(vDepend) - 1) / 2 If vDepend(2 * i) = "blade shaft" Then MsgBox "blade shaft present dans l'assemblage" Next i
ou tu peut glisser comme la un controle sur le nom de la piece
Option Explicit Sub main() ' Name of unopened document Const sDefaultName As String = "c:\program files\solidworks corp\solidworks\samples\tutorial\advdrawings\98food processor.sldasm" Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim sDocName As String Dim vDepend As Variant Dim bRet As Boolean Dim i As Long Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc If Not swModel Is Nothing Then sDocName = swModel.GetPathName Else sDocName = sDefaultName End If vDepend = swApp.GetDocumentDependencies2(sDocName, True, True, False) 'Debug.Print sDocName If IsEmpty(vDepend) Then Exit Sub End If For i = 0 To (UBound(vDepend) - 1) / 2 If vDepend(2 * i) = "blade shaft" Then MsgBox "blade shaft present dans l'assemblage" Next i End Sub
En version simplifier et avec une prise en charge de plusieur piece a cherché
Option Explicit Sub main() Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim vDepend As Variant Dim bRet As Boolean Dim i As Long Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc If swModel Is Nothing Then Exit Sub vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False) If IsEmpty(vDepend) Then Exit Sub Dim Ref As Variant For i = 0 To (UBound(vDepend) - 1) / 2 For Each Ref In Array("drive shaft", "drive shaft pin") If vDepend(2 * i) = Ref Then MsgBox Ref & " est present dans l'assemblage." Next Ref Next i End Sub
La ligne à modifie est la suivante
For Each Ref In Array("drive shaft", "drive shaft pin")
ou drive shaft et drive shaft pin son les nom de pieces a recherché tu peu en mettre autant que tu veut tant que tu respece l'exriture ("X", "Y", "Z", "etc")
Parfait j'ai réussis a faire ce que je voulais, j'ai généralisé la fonction suivant ce que je veux faire sa donne :
Dim swApp As Object
Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc If GetCompInAss("Pièce2") = True Then
'si pièce présente alors : boolstatus = Part.Extension.SelectByID2("Pièce2-1@Assemblage", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0) Part.EditDelete 'Suppression End If End Sub
Function GetCompInAss(DocCible As Variant) Dim vDepend As Variant Dim bRet As Boolean Dim i As Long Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False) If IsEmpty(vDepend) Then Exit Function
For i = 0 To (UBound(vDepend) - 1) / 2 If vDepend(2 * i) = DocCible Then GetCompInAss = True End If Debug.Print " " + vDepend(2 * i) + " --> " + vDepend(2 * i + 1) Next i
End Function
Dernières questions, le bRet sert a quoi ? Je suis obliger de repointer le document dans la fonction on ne peu pas éviter ce genre de redondance ?
Si vous avez d'autre subjection pour simplifié le codeje suis prenneur.
Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long
Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc If GetCompInAss("Pièce2") = True Then 'si pièce présente alors : boolstatus = Part.Extension.SelectByID2("Pièce2-1@Assemblage", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0) Part.EditDelete 'Suppression End If End Sub
Function GetCompInAss(DocCible As Variant) As Boolean Dim vDepend As Variant Dim i As Long Dim swModel As SldWorks.ModelDoc2 Set swModel = swApp.ActiveDoc vDepend = swApp.GetDocumentDependencies2(swModel.GetPathName, True, True, False) If IsEmpty(vDepend) Then Exit Function For i = 0 To (UBound(vDepend) - 1) / 2 If vDepend(2 * i) = DocCible Then GetCompInAss = True Next i End Function