×
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!

*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

Renaming macro

Renaming macro

Renaming macro

(OP)
Hello,

I have a macro which add a prefix to everything I select (Geometrical sets, bodies, sketches, lines, outputs, surface features, parameters etc...)

Would someone be able to modify it, so it have the same function + when I run it again on already renamed features, it will change its prefix. I do not mind to use a special sign, for "ending" the prefix, so it recognizes, that it was already renamed and it will change only its prefix. e.g. Body.1 (first rename)--> Hello__Body.1 (second rename)--> WOW__Body.1

Macro looks like this:

CODE --> CATScript

Sub CATMain()

Dim PartDocument1 As Document
Set PartDocument1 = CATIA.ActiveDocument
Dim Part1 As Part
Set Part1 = PartDocument1.Part
Dim Parameters1 As Parameters
Set Parameters1 = Part1.Parameters
Dim Parameter1 As Parameter
Dim oSel as selection

On Error Resume Next
Set objSel = PartDocument1.Selection
if Err.Number<>0 then
	msgbox("error")
	
	objSel=oSel.SelectElement2(inputobject, "You have to select Apropriate Geometrical set for rename function", true)
	on Error Goto 0
end if

gsname= objSel.item(1).value.name
objSel.Search ("'Generative Shape Design'.,sel")
DIM Text AS STRING
Dim vstup as string
Dim nazev as string
vstup=inputbox("Please input text before parametrs name:")	 
	For i = 1 to objSel.Count

			Text=""

		Text= vstup&"_"&objSel.Item(i).value.name
		objSel.Item(i).value.name =Text
	next
objSel.Search (gsname)

objSel.Search ("CATKnowledgeSearch.InternalParameter,sel")

For i = 1 to objSel.Count

		
		Text=""
		pozice=InStrRev(objSel.Item(i).value.name, "\",-1)
		nazev= right(objSel.Item(i).value.name,len(objSel.Item(i).value.name)-pozice) 
		Text=Left(objSel.Item(i).value.name, pozice-2) 

		pozice2 = InStrRev(Text, "\",-1)
		Text=Mid(Text, pozice2+1 ,Len(Text)-16-pozice2 ) 

'		msgbox("text: "&text)
'		msgbox("nazev: "&nazev)
		Text=vstup & "_" & nazev
'		msgbox(text)
		objSel.Item(i).value.rename (Text)



Next
End Sub 

Thank you for your inputs.

RE: Renaming macro

why don't you try it yourself? You have a code, do you understand how it works?
So you want to check if there is a special sign in your name already. google for Instr function
if so (google if loop), you want to split out the prefix that is already there. google for Split function
Done!

regards,
LWolf

RE: Renaming macro

(OP)
The point is, that I have not created the code and I do not understand it. Colleague who created it, left and no one else understands it, that is why I asked here. If I would be able to try it by myself, I would. With hints you gave me, I might be able to google all that, but I do not know the basics of this.

RE: Renaming macro

This will rename the selected features that do not contain "_" in their names. If "_" is present, the part before it will be stripped away.
i.e this code assumes "_" is not part of the original naming convention.

Dim Text As String
Dim TextArray
Dim vstup As String
Dim nazev As String
vstup = InputBox("Please input text before parameters name:")
For i = 1 To objSel.Count
If InStr(objSel.item(i).Value.name, "_") = 0 Then
Text = ""
Text = vstup & "_" & objSel.item(i).Value.name
objSel.item(i).Value.name = Text
Else
TextArray = Split(objSel.item(i).Value.name, "_")
Text = vstup & "_" & TextArray(UBound(TextArray))
objSel.item(i).Value.name = Text
End If
Next

regards,
LWolf

RE: Renaming macro

(OP)
Thank you very much LWolf!! It works exactly as I wished.

RE: Renaming macro

(OP)
As I said it worked exactly as I wanted, but...
When I tried to rename parameters it did not work... so I have modified the macro (somehow) and now it is supper slow, but still works. Could you please suggest how to do it in a way, that it will not be so slow?
Current code:
Sub CATMain()

Dim PartDocument1 As Document
Set PartDocument1 = CATIA.ActiveDocument
Dim Part1 As Part
Set Part1 = PartDocument1.Part
Dim Parameters1 As Parameters
Set Parameters1 = Part1.Parameters
Dim Parameter1 As Parameter
Dim oSel as selection

On Error Resume Next
Set objSel = PartDocument1.Selection
if Err.Number<>0 then
msgbox("error")

objSel=oSel.SelectElement2(inputobject, "You have to select Apropriate Geometrical set for rename function", true)
on Error Goto 0
end if

gsname= objSel.item(1).value.name
objSel.Search ("'Generative Shape Design'.,sel")
DIM Text AS STRING
Dim TextArray
Dim vstup as string
Dim nazev as string
vstup = InputBox("Please input text before parameters name:")
For i = 1 To objSel.Count
If InStr(objSel.item(i).Value.name, "_ _") = 0 Then
Text = ""
Text = vstup & "_ _" & objSel.item(i).Value.name
objSel.item(i).Value.name = Text
Else
TextArray = Split(objSel.item(i).Value.name, "_ _")
Text = vstup & "_ _" & TextArray(UBound(TextArray))
objSel.item(i).Value.name = Text
End If
Next
objSel.Search (gsname)

objSel.Search ("CATKnowledgeSearch.InternalParameter,sel")

For i = 1 to objSel.Count

If InStr(objSel.item(i).Value.name, "__") = 0 Then
Text=""
pozice=InStrRev(objSel.Item(i).value.name, "\",-1)
nazev= right(objSel.Item(i).value.name,len(objSel.Item(i).value.name)-pozice)
Text=Left(objSel.Item(i).value.name, pozice-2)

pozice2 = InStrRev(Text, "\",-1)
Text=Mid(Text, pozice2+1 ,Len(Text)-16-pozice2 )

Text=vstup & "__" & nazev
objSel.Item(i).value.rename (Text)
Else
Text=""
pozice=InStrRev(objSel.Item(i).value.name, "__",-1)
nazev= right(objSel.Item(i).value.name,len(objSel.Item(i).value.name)-pozice)
Text=Left(objSel.Item(i).value.name, pozice-2)

pozice2 = InStrRev(Text, "__",-1)
Text=Mid(Text, pozice2+1 ,Len(Text)-16-pozice2 )

Text=vstup & "_" & nazev
objSel.Item(i).value.rename (Text)

End if
Next
End Sub

RE: Renaming macro

this is what I used: (there is a distinction between local and global parameter names, you should be aware of that... however, I chose the "sloppy" way i.e. removed most of your code)
b.t.w, there is an error in your code in specifying the selection, since the inputobject is not defined anywhere. It works because you have error-handling that lets this pass)
I added error-handling; basically if it is not a feature where the .name works, do the .Rename approach (which works for parameters)
Anyways

Sub CATMain()

Dim PartDocument1 As Document
Set PartDocument1 = CATIA.ActiveDocument
Dim oSel As Selection
Set objSel = PartDocument1.Selection

Dim Text As String
Dim TextArray
Dim vstup As String
Dim nazev As String
vstup = InputBox("Please input text before parameters name:")
For i = 1 To objSel.Count
If InStr(objSel.item(i).Value.name, "_") = 0 Then
Text = ""
Text = vstup & "_" & objSel.item(i).Value.name
On Error Resume Next
objSel.item(i).Value.name = Text
If err.Number <> 0 Then objSel.item(i).Value.Rename Text
On Error GoTo 0
Else
TextArray = Split(objSel.item(i).Value.name, "_")
Text = vstup & "_" & TextArray(UBound(TextArray))
On Error Resume Next
objSel.item(i).Value.name = Text
If err.Number <> 0 Then objSel.item(i).Value.Rename Text
On Error GoTo 0
End If
Next

End Sub

regards,
LWolf

RE: Renaming macro

(OP)
Thank you again,

but it is not working properly...
Here is what I have edited:
Sub CATMain()

Dim PartDocument1 As Document
Set PartDocument1 = CATIA.ActiveDocument
Dim oSel As Selection
Set objSel = PartDocument1.Selection

Dim Text As String
Dim TextArray
Dim vstup As String
Dim nazev As String
gsname= objSel.item(1).value.name
objSel.Search ("'Generative Shape Design'.,sel")

vstup = InputBox("Please input text before parameters name:")
For i = 1 To objSel.Count
If InStr(objSel.item(i).Value.name, "__") = 0 Then
Text = ""
Text = vstup & "__" & objSel.item(i).Value.name
On Error Resume Next
objSel.item(i).Value.name = Text
If err.Number <> 0 Then objSel.item(i).Value.Rename Text
On Error GoTo 0
Else
TextArray = Split(objSel.item(i).Value.name, "__")
Text = vstup & "__" & TextArray(UBound(TextArray))
On Error Resume Next
objSel.item(i).Value.name = Text
If err.Number <> 0 Then objSel.item(i).Value.Rename = Text
On Error GoTo 0
End If
Next

End Sub

In red color is what I have added. And instead of one underscore _ I use two __ .
"gsname= objSel.item(1).value.name
objSel.Search ("'Generative Shape Design'.,sel")" This one I added, so when I select GS set, it will automatically select everything inside.
"=" I added, because I thought you missed that...

But still it is not renaming parameters which has the global name, only the one with local name...
e.g.:
Will be renamed
Local name: Length.1
Name: Something

Will not be renamed
Local name: Test
Name: *Some location*\Test

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


Resources

eBook - The Future of Product Development is Here
Looking to make the design and manufacturing of your products more agile? For engineering and manufacturing organizations, the need for digital transformation of product development processes just became more urgent than ever so we wanted to share an eBook that will help you build a practical roadmap for your journey. Download Now

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