×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

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!
  • Students Click Here

*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

Jobs

Part to Product Macro Issue

Part to Product Macro Issue

Part to Product Macro Issue

(OP)
I currently have a CATScript Macro that creates a Product from a Catia Part with multiple bodies.
When i run this macro, and it runs into a body that has a name that already exists it throws a pup up box stating:
The item xxxx already exists.
Do you want it to be overwritten?

I have a bunch of parts that have hundreds of bodies like this.

Is there a macro that renames duplicate part bodies?
All I need the macro to do is add a -1, -2, or -3 on duplicate part bodies before i run the Part to Product macro.
Example: Starting Name: XXX, Duplicate Names: XXX-1, XXX-2, and XXX-3.

RE: Part to Product Macro Issue

take a look at this line
BodyName(i) = prt & "_" & bodies1.Item(i).name & "-" & i

add after name & "-" & i

RE: Part to Product Macro Issue

(OP)
That is amazing, you sir are a lifesaver. It is amazing how such a small change can yield such a big different result.
This saved me multiple hours of work renaming bodies.
Only complaint I have is when i have the following parts:
XXX
XXX
XXX
YYY
YYY
It gives me this as a result:
XXX_1
XXX_2
XXX_3
YYY_4
YYY_5
When I would rather have:
XXX_1
XXX_2
XXX_3
YYY_1
YYY_2

Any idea how to possibly accommodate this?

RE: Part to Product Macro Issue

What are you writing in your code in, VBA or CATScript?

RE: Part to Product Macro Issue

(OP)
CATScript.

RE: Part to Product Macro Issue

JeniaL's suggestion will be very fast because it just renumbers all the bodies without any comparison. Because you want a comparison, it can slow your macro down considerably.

I didn't test the following code, but if you rename something, I would put a unique string in the new name so you can detect if the body was already renamed and skip it...to speed up the macro. I used _# as my unique string, but if you find there are bodies already with that string, you may need something more complex so you can guarantee there are no bodies named that.

CODE -->

Dim oBody 'As Body
Dim oSelection 'As Selection
oSelection.Clear

'Search for all bodies in the part...make sure you only have a part loaded
oSelection.Search "((((CATStFreeStyleSearch.BodyFeature + CATPrtSearch.BodyFeature) + CATGmoSearch.BodyFeature) + CATSpdSearch.SpdBodyRef) + CATSpdSearch.BodyFeature),all"

'Set up a counter to number bodies with the same name
iCounter = 1

If oSelection.Count > 0 then
For i = 1 to oSelection.Count
If Instr(oSelection.Item(i).Value.Name, "_#")=0 Then '_# is the unique string I chose...you may need something more elaborate
Set oBody = oSelection.Item(i).value 'Set the body to compare all the other bodies to For j = i+1 to oSelection.Count 'Loop through all the bodies after the one you set above
If Instr(oSelection.Item(j).Value.Name, "_#")=0 Then 'If it doesnt have your unique string in the name already, that means it hasn't been renamed
If oSelection.Item(j).Value.Name = oBody.name then 'If the names are the same
oSelection.Item(j).Value.Name = oSelection.Item(j).Value.Name & "_#" & iCounter 'Add the number on the end of the name iCounter = iCounter + 1
End if
End if
Next
Next
End if
End if

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!


Resources