Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

How to rename Catia Bodies with Unique names

Status
Not open for further replies.

nainh

Automotive
Nov 9, 2022
10
0
0
MY
CatiaQuestion_RenameBodiesUniquely_hszqz5.png


Hi mates, may I know how can I rename a Bodies in a Part with unique names? As attached in the picture, I have multiple bodies with the same name (D14-d4)that I want to rename uniquely as D14-d4.1, D14-d4.2, D14-d4.3,...respectively.

I have tried searching for many renaming macro but I've yet to find one that does what I'm trying to do here. Appreciate if someone can point me to the right direction.
 
Replies continue below

Recommended for you

I would perform following steps:[ul]
[li]set up a dictionary with unique body names[/li]
[li]search for each entry of the dictionary[/li]
[li]for all hits, loop and rename[/li]
[li]step to next [/li]
[/ul]

regards,
LWolf
 
I have this to rename the bodies uniquely. For example, I have 5 bodies named A & 3 bodies named 3A, then they will be renamed as: A.1, A.2, A.3, A.4, A.5, 3A.1, 3A.2, 3A.3.
The code can run without error, but nothing happens[sad]. Did I do something wrong here?

Sub RenameBodiesWithCounts()
Dim doc As Document
Set doc = CATIA.ActiveDocument

If doc Is Nothing Then
MsgBox "No active document found. Please open a CATIA Part document.", vbExclamation
Exit Sub
End If

Dim part As Part
Set part = doc.Part

Dim bodies As Bodies
Set bodies = part.Bodies

Dim bodyNames As Object
Set bodyNames = CreateObject("Scripting.Dictionary")

Dim body As Body
For Each body In bodies
Dim name As String
name = body.Name

If bodyNames.Exists(name) Then
bodyNames(name) = bodyNames(name) + 1
name = name & "." & bodyNames(name)
body.Name = name
Else
bodyNames(name) = 1
End If
Next

MsgBox "Bodies renamed with counts.", vbInformation
End Sub
 
your code just completes step one... bodynames dictionary contains now all the unique names
Code:
Sub CATMain()
Dim doc As Document
Set doc = CATIA.ActiveDocument
If doc Is Nothing Then
    MsgBox "No active document found. Please open a CATIA Part document.", vbExclamation
    Exit Sub
End If

Dim part As part
Set part = doc.part
Dim bodies As bodies
Set bodies = part.bodies
Dim bodyNames As Object
Set bodyNames = CreateObject("Scripting.Dictionary")

Dim body As body
For Each body In bodies
    Dim name As String
    name = body.name
    
    If Not bodyNames.Exists(name) Then
        bodyNames.Add name, 1
    End If
Next

End Sub
use each entry of the dictionary to complete the other steps :)

regards,
LWolf
 

Working code:
Code:
Sub CATMain()
Dim doc As Document
Set doc = CATIA.ActiveDocument
If doc Is Nothing Then
    MsgBox "No active document found. Please open a CATIA Part document.", vbExclamation
    Exit Sub
End If

Dim part As part
Set part = doc.part
Dim bodies As bodies
Set bodies = part.bodies
Dim bodyNames As Object
Set bodyNames = CreateObject("Scripting.Dictionary")

Dim body As body
For Each body In bodies
    Dim name As String
    name = body.name
    
    If Not bodyNames.Exists(name) Then
        bodyNames.Add name, body
    End If
Next
Dim i As Integer
Dim sel, selString, dictKeys()
Set sel = CATIA.ActiveDocument.Selection

For i = 1 To bodyNames.Count
    dictKeys = bodyNames.keys
    selString = "(Name=" & dictKeys(i - 1) & " & 'Part Design'.Body);all"
    sel.Search selString
    If sel.Count > 1 Then
        For k = 1 To sel.Count
            sel.item(k).Value.name = sel.item(k).Value.name & "." & k
        Next
    End If
Next
End Sub

regards,
LWolf
 
Status
Not open for further replies.
Back
Top