Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Renaming macro 1

Status
Not open for further replies.

VojtaR

Automotive
Apr 2, 2020
20
CZ
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:
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.
 
Replies continue below

Recommended for you

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
 
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.
 
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
 
Thank you very much LWolf!! It works exactly as I wished.
 
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
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top