Bonjour Cyril,
je poste tout mon code. J'ai mis deux boucles car si j'ai deja renommé mes pièces cela ne fonctionnait pas donc je les renomme en premier avec un compteur ( 1,2,3,4...) puis je les renomme avec la règle.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swRootComp As SldWorks.Component2
Dim Children As Variant
Dim swChild As SldWorks.Component2
Dim SwSelData As SldWorks.SelectData
Dim ChildCount As Long
Dim oldName As String
Dim newName As String
Dim j As Long
Dim h As Long
Dim NomParent As String
Dim errorsRename As Long
Dim status As Boolean
Dim warnings As Long
Dim errorsSave As Long
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Dim bool As Boolean
Dim val As String
Dim valout As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
NomParent = Left(swModel.GetTitle, 7)
Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True)
j = 1
h = 1
TraverseComponent swRootComp, 1
swModel.ForceRebuild3 True
status = swModel.Save3(swSaveAsOptions_SaveReferenced, errorsSave, warnings)
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As Component2
Dim swModelChild As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
Dim i As Long
Dim status2 As Boolean
Dim val1 As String
Dim valout1 As String
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
swChildComp.Select4 False, SwSelData, False
Set swModelChild = swChildComp.GetModelDoc2
Set swCustProp = swModelChild.Extension.CustomPropertyManager("Default") 'mettre le nom de la configuration comprenant la propriété
If Not swCustProp Is Nothing Then
status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
status2 = swCustProp.Get4("QUINCAILLERIE", False, val1, valout1)
If valout <> "" Or valout1 <> "" Then
newName = h
errorsRename = swModel.Extension.RenameDocument(newName)
Debug.Print swModelChild.GetTitle & " : " & h & " - " & errorsRename
h = h + 1
End If
End If
Next i
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
swChildComp.Select4 False, SwSelData, False
Set swModelChild = swChildComp.GetModelDoc2
Set swCustProp = swModelChild.Extension.CustomPropertyManager("Défaut") 'mettre le nom de la configuration comprenant la propriété
If Not swCustProp Is Nothing Then
status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
status2 = swCustProp.Get4("QUINCAILLERIE", False, val1, valout1)
If valout <> "" Or valout1 <> "" Then
newName = NomParent & "-000" & j
If Len(newName) > 12 Then
newName = Left(newName, 8) & Right(newName, 4)
End If
errorsRename = swModel.Extension.RenameDocument(newName)
Debug.Print swModelChild.GetTitle & " : " & j & " - " & errorsRename
j = j + 1
End If
End If
Next i
End Sub