Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Part to Product Macro Issue 1

Status
Not open for further replies.

jzecha

Aerospace
Joined
Jan 20, 2016
Messages
236
Location
US
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.
 
take a look at this line
BodyName(i) = prt & "_" & bodies1.Item(i).name & "-" & i

add after name & "-" & i
 
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?
 
What are you writing in your code in, VBA or CATScript?
 
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

[COLOR=#4E9A06]'Search for all bodies in the part...make sure you only have a part loaded[/color]
oSelection.Search "((((CATStFreeStyleSearch.BodyFeature + CATPrtSearch.BodyFeature) + CATGmoSearch.BodyFeature) + CATSpdSearch.SpdBodyRef) + CATSpdSearch.BodyFeature),all"

[COLOR=#4E9A06]'Set up a counter to number bodies with the same name[/color]
iCounter = 1

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

Part and Inventory Search

Sponsor

Back
Top