Continue to Site

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!

Rename CATIA V5 Product and Parts 2

Status
Not open for further replies.

ghizzo

Mechanical
Nov 27, 2015
10
Hi to everyone.

I've a problem, i need to rename an assembly (and all sub-assembly and parts) changing only a the first part of the name.

EX :

all my files start with a code of 7 numbers, i need to change only this 7 numbers in all files

1234567_aaa.catproduct
1234567_bbb_catpart
1234567_ccc_catproduct
1234567_ddd_catpart

must become

7654321_aaa.catproduct
7654321_bbb_catpart
7654321_ccc_catproduct
7654321_ddd_catpart


There's a way (macro ??) to speed up the saving of these files ?

Thank you !
 
Replies continue below

Recommended for you

yes you can make a macro. That do the job easy.

Eric N.
indocti discant et ament meminisse periti
 
If you only mean the operating system filenames need changed, you can even just use the good old DOS window.

You would still need to manually edit the part and product properties inside Catia though.

I use DOS to do somethings in milliseconds that Windows can't do at all.

It's class.

Here's the procedure, but be careful. If you don't know what you're doing you can wipe out your PC in a nanosecond.

Go to the START menu

Select Run

Type in command

A black DOS window should appear.

You then type in the following command, but obviously editing it to suit where you have stored the files.

My example will have the files located a few folders deep from the root of the hard drive C

I purposely use only maximum 8 character folder names since DOS only allowed 8 characters.

(There is a way to use long names but it involves more complicated filename typing using squiggles etc).


REN C:\TESTFOLD\MYFILES\1234567*.* 7654321*.*

That will do them all in one hit.

Just note there is a space between 1234567*.* and 7654321*.*

Just type EXIT when you want to leave the DOS window


You can also freely use the ? wildcard to represent a single character, as opposed to the * which represents any group of characters.

I'm amazed Windows has no features like this, or if it has, I have never heard of them.

P.S. If in doubt at all what you're doing, practice on a trash PC that you don't mind eliminating :)



 
beware of catia links management if you rename outside of catia process

Eric N.
indocti discant et ament meminisse periti
 
@itsmyjob

I tried to make a macro, but i'm not very good with these kind of programming.
Could someone help me ?


@MRSSPOCK @itsmyjob

I have many link between files, i must do the process IN catia, I need to mantain those links

Thanks to everyone !
 
We need to know more about your file structure. Are the parts and products all inside of a product?
Can the script be in VBA or CATScript?

If you can use VBA (alt+f11 to open the VBA editor) you can probably use something like the following. I did not test it, so I would try it on a test structure. There may be other properties you want to rename...this is just an idea based on what little I know about the issue. It doesn't account for all the error states of a part number.

Code:
Sub CATMain()
	Dim oDocument as Document
	Set oDocument = Catia.activedocument

	Dim oRootProduct as product
	Dim oProduct as product

	Dim oSelection as Selection
	Set oSelection = oDocument.selection
	Dim oSelectionLB
	Set oSelectionLB = oSelection

	Dim sOldNumber as String
	Dim sNewNumber as String

	Dim aSelectionFilter(0) = "Product"

	If TypeName (oDocument) = "Product" then
		set oRootProduct = oDocument.Product
		oSelection.Clear
		sMessage = "Select a part to renumber"
		MsgBox sMessage
		oSelectionLB.SelectElement2 aSelectionFilter, sMessage, True
		If oSelection.Count = 0 then
			Msgbox "Macro Canceled by user"
			exit sub
		Else
			sOldNumber = oSelection.Item(1).Value.Nomenclature
			oSelection.Clear
			sNewNumber = "A"
			i=0
			Do until Len(sNewNumber) = 7 and IsNumeric(sNewNumber) = True and Val(sNewNumber) = Int(sNewNumber)
				sNewNumber = InputBox("Type new 7 digit number)
				i=i+1
				If i = 5 then
					Msgbox "You can only enter a 7 digit number, exit macro"
					end sub
				end if
			Loop 
			For i=1 to oRootProduct.products.Count
				Set oProduct = oRootProduct.Products.Item(i)
				If oProduct.PartNumber = sOldNumber then
					oProduct.Nomenclature = replace(oProduct.Nomenclature, sOldNumber, sNewNumber)
					oProduct.PartNumber = replace(oProduct.PartNumber, sOldNumber, sNewNumber)
					oProduct.Name = replace(oProduct.name, sOldNumber, sNewNumber)
				end if
			Next
		End if
	Else
		MsgBox "Product not loaded, exit macro"
		end sub
	End if

End Sub
 
@lardman363

Hi,
Yes, i can use the VBA

All inside the product there are many parts and products, it's a very big assembly so i need a macro that rename all the files in my assembly

1234567_aaa.catproduct
1234567_bbb_catpart​
1234567_ccc_catproduct​
1234567_ddd_catpart​
1234567_eee_catproduct​
1234567_fff_catpart​
1234567_ggg_catproduct​
1234567_hhh_catpart​
1234567_iii_catpart​

This is an example where all "1234567" must renamed in "7654321" without changing the 3 letters after the names

Thanks
 
Load your assembly in CATIA and run this from VBA:

Code:
Sub CATMain()
	Dim oDocument as Document
	Set oDocument = Catia.activedocument

	Dim oRootProduct as product
	Dim oProduct as product

	Dim oSelection as Selection
	Set oSelection = oDocument.selection

	Dim sOldNumber as String
	Dim sNewNumber as String

	If TypeName (oDocument) = "Product" then
		oSelection.Clear
		set oRootProduct = oDocument.Product
		sOldNumber = Left(oRootProduct.Nomenclature,7)
		
		sNewNumber = "A"
		i=0
		Do until Len(sNewNumber) = 7 and IsNumeric(sNewNumber) = True and Val(sNewNumber) = Int(sNewNumber)
			sNewNumber = InputBox("Type new 7 digit number)
			i=i+1
			If i = 5 then
				Msgbox "You can only enter a 7 digit number, exit macro"
				exit sub
			end if
		Loop
		
		oSelection.Clear
		oSelection.Search ("type=Product,all")
		
		For i=1 to oSelection.Count
			Set oProduct = oSelection.Item(i).Value
			If instr(oProduct.PartNumber, sOldNumber) <> 0 then
				oProduct.Nomenclature = replace(oProduct.Nomenclature, sOldNumber, sNewNumber)
				oProduct.PartNumber = replace(oProduct.PartNumber, sOldNumber, sNewNumber)
				oProduct.Name = replace(oProduct.name, sOldNumber, sNewNumber)
			end if
		Next
		oSelection.Clear
		MsgBox "Macro Complete"
	Else
		MsgBox "Product not loaded, exit macro"
		exit sub
	End if

End Sub
 
@lardman363

I run your script on CATIA in the "first" product, but it doesn't work
I see only the message "product not loaded, exit macro" .. Why ?
it seems that does not recognize the product ..

thank you so much
 
Sorry, it should say
If TypeName (oDocument) = "ProductDocument" then
 
ok ..
Now the problem is in the line

"Do until Len(sNewNumber) = 7 and IsNumeric(sNewNumber) = True and Val(sNewNumber) = Int(sNewNumber)"
 
What is the error?
Since it is looking for a number, maybe use
Code:
sNewNumber = "1"
 
@lardman363

The error is "type Mismatch"

and is in the line "Do until Len(sNewNumber) = 7 and IsNumeric(sNewNumber) = True and Val(sNewNumber) = Int(sNewNumber)"

I try to change the value of sNewNumber from "A" to "1", but it doestn't work

Thank You
 
Type mismatch means a variable is one type but the code is expecting another. The only variable in that line is sNewNumber, it is a string. Int is looking for a number.

Code:
Do until Len(sNewNumber) = 7 and IsNumeric(sNewNumber) = True and Val(sNewNumber) = Int([b]Val[/b](sNewNumber))
 
Perfect !

Now the macro run, but it doesn't rename, right ?
 

Use below macro in CATVba....
for using this macro, follow below steps
1) first create new assembly from old assembly by using File->new from
2) run this macro...
3) In first input box , "enter string to be replaced" ..... as in your case it is "1234567"
4) In second input box, "enter replaced string".............. as in your case it is "7654321"
5) Use file save management to save all child parts and assembly at new location




Sub CATMain()

Dim root As Document
Set root = CATIA.ActiveDocument

Dim docs As Documents
Set docs = CATIA.Documents

If (root.Path <> "") Then
MsgBox "Please Use This macro on Unsaved Product"
Exit Sub
End If

If (TypeName(root) <> "ProductDocument") Then
MsgBox "Please open Assembly File"
Exit Sub
End If


Dim FstInp, SndInp As String
FstInp = InputBox("Please Enter String to be Replaced")

SndInp = InputBox("Please Enter String to Replace")

If FstInp <> "" And SndInp <> "" Then
root.Product.PartNumber = Replace(LCase(root.Product.PartNumber), LCase(FstInp), LCase(SndInp))
Call ALLTREE(root, FstInp, SndInp) ' callin procedure
Else
MsgBox "Input not reached correctly"
Exit Sub
End If

End Sub

Sub ALLTREE(Froot As ProductDocument, FstStr, SndStr)

Dim objFPrd As Product
Set objFPrd = Froot.Product

Dim objFPrds As Products
Set objFPrds = objFPrd.Products

For i = 1 To objFPrds.Count

Dim objFIns As Product
Set objFIns = objFPrds.Item(i)

objFIns.Name = Replace(LCase(objFIns.Name), LCase(FstStr), LCase(SndStr))
objFIns.PartNumber = Replace(LCase(objFIns.PartNumber), LCase(FstStr), LCase(SndStr))

If (TypeName(objFIns.ReferenceProduct.Parent) = "ProductDocument") Then

Call ALLTREE(objFIns.ReferenceProduct.Parent, FstStr, SndStr) 'if instance is product then calling the procedure

End If
Next
End Sub


 
Hi
This code worke But in replace "ABC" & "abc" is same!!!!
Please edite your code.
Very Thanks.
 
this forum is a good place to get some help, not a place where to get work for free.
if you want to contribute and bring better stuff to the community I suggest you do your work and share it.

so pretty please, do some work, and thanks for sharing.

(sorry if I sound rude, I did not finish my morning coffee yet)


Eric N.
indocti discant et ament meminisse periti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor