Rename all components in an assembly, even those included in a subassembly

All right 

well I don't have the option to display the name of the file which seems weird to me...


capture.png

It's hidden in "Show component names". The component name is the same as the record name.

Otherwise, you should look in the general options at the external reference level if the option "Update component names when documents are replaced" is checked.

1 Like

Great, this option of external references was not checked :)!  

I've been testing it on several assemblies since this morning, and I still have a sorry question^^ 

Are there cases where the rename function doesn't work? 

Because here I have two parts that are not renamed, yet I pass the program in step by step it passes on them and triggers the line: errorsRename = swModel.Extension.RenameDocument(newName)

But don't rename it. Do you have an idea? 

Have a good weekend

Hello 

after intensive use of the macro. I notice that depending on the configuration of my parts I often have an error because we have both languages (default and default). How can we get past this error and take both cases into consideration? 

Thanks in advance, 

Have a nice day 

Hello

I hadn't seen the first question.

For the renaming problem, you have to look at what the ErrorsRename variable returns and see in the help what this error corresponds to: 

 http://help.solidworks.com/2020/english/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swRenameDocumentError_e.html

For the rest, you have to add an additional check as in the code below:

Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
    Dim vChildComp As Variant
    Dim swChildComp As Component2
    Dim i As Long
    Dim swModelChild As SldWorks.ModelDoc2
    Dim swCustProp As CustomPropertyManager
    Dim val As String
    Dim valout 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("Défaut") 'mettre le nom de la configuration comprenant la propriété
        If swCustProp Is Nothing Then
            Set swCustProp = swModelChild.Extension.CustomPropertyManager("Default")
        End If
        If Not swCustProp Is Nothing Then
        status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
        If valout <> "" Then
            newName = NomParent & "-" & "000" & j
            errorsRename = swModel.Extension.RenameDocument(newName)
            swChildComp.Name2 = newName
            Debug.Print swModelChild.GetTitle & " : " & j & " - " & errorsRename
            j = j + 1
        End If
        End If
    Next i
End Sub

 

Yes I had thought about it but already at the "swCust set.... " as it can't find the configuration it crashes and I don't know how to do it. 

For the first question, it's actually that the part already existed in the explorer windows but was not in the assembly.

Hello

I think it comes from something else. Personally, I don't have any problems with the operation even if the configuration doesn't exist.

swCustProp remains empty and therefore there is no renaming or anything else on the file concerned.

Has the code been embedded as-is in your macro or has it been modified to integrate into your code?

Hello Cyril, 

I post all my code. I put two loops because if I have already renamed my pieces it didn't work so I rename them first with a counter (1,2,3,4...) then I rename them with the ruler. 

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
Sun j As Long
Sun h As Long
Dim ParentName As String
Dim errorsRename As Long
Dim status As Boolean
Dim warnings As Long
Save 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
ParentName = 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
    Sun i As Long
    
    Dim status2 As Boolean
    Dim val1 As String
    Dim valout1 As String
    
    vChildComp = swComp.GetChildren
    For i = 0 TB 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") ' put the name of the configuration including the
        If Not swCustProp Is Nothing Then
            status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
            status2 = swCustProp.Get4("HARDWARE", 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 TB 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") ' set the name of the configuration including the
        If Not swCustProp Is Nothing Then
            status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
            status2 = swCustProp.Get4("HARDWARE", False, val1, valout1)
            
            If valout <> "" Or valout1 <> "" Then
                newName = ParentName & "-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 = d + 1
            End If
        End If
    Next i
End Sub

 

I haven't checked it yet, but in the first approach, I think the problem comes from the second loop.

The swChild variable is probably flushed after the first run is passed.

Hello Cyril, 

swChild is not reset to nothing, I don't understand how it would be empty.

Hello

Personally, I have no problem with this macro. As it stands, I don't see what's blocking (I have files that don't have any named configuration like the ones targeted and it goes on without any problem).

What is the error message being reported?

Hello 

I found a solution with GetActiveConfiguration so I take all the configurations into account no translation problems. 

Hello Cyril, 

I did some more tests and I have an example that I attach in a ZIP file. 

I have the impression that it doesn't go over all the files of the assembly and I really don't understand why... In the end, my holidays were not :D!  

Thank you for your help in advance, 

Have a nice day 

Vincent 


1500000-0000.zip
renaming.swp