×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to rename Catia Bodies with Unique names

How to rename Catia Bodies with Unique names

How to rename Catia Bodies with Unique names

(OP)


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.

RE: How to rename Catia Bodies with Unique names

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

regards,
LWolf

RE: How to rename Catia Bodies with Unique names

(OP)
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 happenssad. 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

RE: How to rename Catia Bodies with Unique names

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

RE: How to rename Catia Bodies with Unique names


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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close