×
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

Macro to Change Part Number and Instance Name to File Name

Macro to Change Part Number and Instance Name to File Name

Macro to Change Part Number and Instance Name to File Name

(OP)
Hello,

This is my first thread on this site, so any help someone can give me would be greatly appreciated. I am looking for a macro that would automatically rename a CatParts Part Number and Instance Name to match the filename. However, I need to be able to run the macro when the CatPart exists inside of a CatProduct. We are starting to use a design catalog for all of our components. We bring in a component which is stored under a generic name in the catalog and we then rename it and save it into our job folder under a more specific name. It is very time consuming to manually go in and change the Part Number and Instance Name to match the file name so we would like a macro to this automatically. Any help that someone can give me on this would be greatly appreciated. Just want to note that I have limited script writing experience.

Thank You.
Jeff

RE: Macro to Change Part Number and Instance Name to File Name

Is there more than 1 instance of any part?


If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum

RE: Macro to Change Part Number and Instance Name to File Name

(OP)
I am thinking that we will bring the CatPart into the design and then run the macro. If we need any additional instances, we typically just use the fast multi instantiation button which will automatically add the .1 to the end of the instance name based on the amount of instances. If possible we would like the macro to only rename the CatPart that is the active document in the tree. Please let me know if you need more information than this. Thank you for taking the time to try and help us.

RE: Macro to Change Part Number and Instance Name to File Name

CODE -->

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
   Call ListingNames(productDocument1.Product)


End Sub

Sub ListingNames(current_prod)
If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i))
Next i
Else
MsgBox current_prod.ReferenceProduct.Parent.Name
End If
End Sub 

This Macro will list All of Yours Parts filenames



CODE -->

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If
   Call ListingNames(productDocument1.Product)


End Sub

Sub ListingNames(current_prod)
If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call ListingNames(current_prod.Products.Item(i))
If InStr(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, ".CATProduct") > 0 Then
current_prod.Products.Item(i).PartNumber = Left(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, Len(current_prod.Products.Item(i).ReferenceProduct.Parent.Name) - 11)
End If
Next i
Else
If InStr(current_prod.ReferenceProduct.Parent.Name, ".CATPart") > 0 Then
current_prod.PartNumber = Left(current_prod.ReferenceProduct.Parent.Name, Len(current_prod.ReferenceProduct.Parent.Name) - 8)
End If
End If
End Sub 

This one will scan All CATProduct tree and rename PartNumber or ProductNumber with respect to filename

I will post Macro to rename instance names soon!

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum

RE: Macro to Change Part Number and Instance Name to File Name

(OP)
I tried to use both of these macros and they both give me an error saying as follows:
Description: Expected end of statement
Statement: Next i
Line: 21
Column: 5

Any ideas on what the problem might be?

RE: Macro to Change Part Number and Instance Name to File Name

I'm using Catia v5r21 and external VBA compiler, and it causes some differences between typical CatScripts


If You simply change

CODE -->

Next i 
to

CODE -->

Next 
it will be working.
Let me know if You have any other problems with those macros

I will post macro to change instance names on saturday

/The first one is just simply macro for testing alghoritm, the second one is the proper macro. Both of them You need run under Your assembly - it works with Active Product and its child components

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum

RE: Macro to Change Part Number and Instance Name to File Name

CODE --> CATScript

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If


   Call ListingNames(productDocument1.Product)


End Sub

Sub ListingNames(current_prod)
Dim selected()
Dim New_instance As String
If current_prod.Products.Count > 0 Then

ReDim selected(current_prod.Products.Count)
For i = 1 To current_prod.Products.Count
selected(i) = False
Next

For i = 1 To current_prod.Products.Count
instances = 1

If selected(i - 1) = False Then

For j = i To current_prod.Products.Count
If current_prod.Products.Item(j).PartNumber = current_prod.Products.Item(i).PartNumber Then
selected(j - 1) = True
New_instance=CStr(current_prod.Products.Item(i).PartNumber + "." +CStr(instances))
'current_prod.Products.Item(j).Name =New_instance
instances = instances + 1
MsgBox New_instance
End If
Next 
End If

Call ListingNames(current_prod.Products.Item(i))

Next
End If

End Sub 

CATSrcipt posted above correctly list all components form product tree (products and parts) and display proposed new instance names according to theirs PartNumbers, but line

CODE -->

current_prod.Products.Item(j).Name =New_instance 
is not working correctly
I can't find any mistakes in this line. I'm still working on it smile

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum

RE: Macro to Change Part Number and Instance Name to File Name

(OP)
I used the macro that "Scans All CATProduct tree and rename PartNumber or ProductNumber with respect to filename". It works great!!! If you can get the instance macro to work, is there a way that you can combine the two macros so that i can assign an icon to the macro and only have to push one button?

RE: Macro to Change Part Number and Instance Name to File Name

Quote:

is there a way that you can combine the two macros so that i can assign an icon to the macro and only have to push one button?



CODE --> CATScript

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

  If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
     MsgBox "Active CATIA Document is not a Product. Open a Product file and run this script again.", , msgboxtext
     Exit Sub
   End If


	Call PartNumberAsFileName(productDocument1.Product)
	Call Rename_PartName(productDocument1.Product)
End Sub


Sub PartNumberAsFileName(current_prod)

If InStr(current_prod.ReferenceProduct.Parent.Name, ".CATProduct") > 0 Then
current_prod.PartNumber = Left(current_prod.ReferenceProduct.Parent.Name, Len(current_prod.ReferenceProduct.Parent.Name) - 11)
End If

If current_prod.Products.Count > 0 Then
For i = 1 To current_prod.Products.Count
Call PartNumberAsFileName(current_prod.Products.Item(i))
If InStr(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, ".CATProduct") > 0 Then
current_prod.Products.Item(i).PartNumber = Left(current_prod.Products.Item(i).ReferenceProduct.Parent.Name, Len(current_prod.Products.Item(i).ReferenceProduct.Parent.Name) - 11)
End If
Next
Else
If InStr(current_prod.ReferenceProduct.Parent.Name, ".CATPart") > 0 Then
current_prod.PartNumber = Left(current_prod.ReferenceProduct.Parent.Name, Len(current_prod.ReferenceProduct.Parent.Name) - 8)
End If
End If
End Sub 




Sub Rename_PartName(current_prod)
Dim selected()
Dim New_instance As String
If current_prod.Products.Count > 0 Then

ReDim selected(current_prod.Products.Count)
For i = 1 To current_prod.Products.Count
selected(i) = False
Next

For i = 1 To current_prod.Products.Count
instances = 1

If selected(i - 1) = False Then

For j = i To current_prod.Products.Count
If current_prod.Products.Item(j).PartNumber = current_prod.Products.Item(i).PartNumber Then
selected(j - 1) = True
New_instance=CStr(current_prod.Products.Item(i).PartNumber + "." +CStr(instances))
current_prod.Products.Item(j).Name =New_instance
instances = instances + 1
'MsgBox New_instance
End If
Next 
End If

Call Rename_PartName(current_prod.Products.Item(i))

Next
End If

End Sub 

Is it working correctly with products with non-flat structure, eg:
Product1
-Product2
--Product3
---Part1
---Part2

?

I've spend few hours to test this macro on various types of products configurations and still have no idea, why it is not working correctly...
Maybe more experienced Catia Scripter will be able to re-write this macro

Anyway, I'm glad that I was able to solve some of Yours problems

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum

RE: Macro to Change Part Number and Instance Name to File Name

(OP)
I tested the macro today, i created a new cat product and then inserted a catpart. I saved the catpart under a different name and ran the macro, it worked perfectly. It does exactly what i was looking for except it doesn't only rename the selected catpart. Also, i brought in a new product to simulate bringing in a sub-assembly. I saved this sub-assembly under a new name, then i ran the macro and it still did exactly what i wanted except the exception listed above. Thank you for all of your help.

RE: Macro to Change Part Number and Instance Name to File Name

Quote:

it doesn't only rename the selected catpart

I Tought You'd like macro that rename all parts/products in assembly:)

Tell me more about quantity of selected catparts, are they more or less than 50% of all parts in assembly?

If You are interested in Macros in Catia (Catscripts, Catvba) feel free to send me a message. I will wrote specified macros for free and post it on eng-tips forum

RE: Macro to Change Part Number and Instance Name to File Name

Hello lukaszsz,

i think i have a similar problem like CatiaWiz, but your macro didnt really work with it.

Perhaps you can help me with this one.
I have created a Product with several Parts. They have following naming structure:
xxxxxxx_A_1_A_ProductName
xxxxxxx_0001_PartName1 (xxxxxxx_0001_PartName1)
xxxxxxx_0002_Partname2 (xxxxxxx_0002_PartName2)
....

I need to change the PartName and the Instance name of every Part to any number i want, but only the first seven digits should change. I have a macro with wich i can change the Instance name but nothing that can rename the Partname with the Instance name.

Is there anything one could do to help me?

Thanks, Rob

RE: Macro to Change Part Number and Instance Name to File Name

I'm not a regular coder for Catia and I am late with the answer ... but I hope it helps somebody.

CODE --> VBA

Option Explicit
Sub CATMain()

    'part declaration________________
    Dim catDocument As PartDocument
    Dim catPart As Part
    Set catDocument = CATIA.ActiveDocument
    Set catPart = catDocument.Part
    'other declaration_______________
    Dim HSF As HybridShapeFactory
    Dim se1 As Object
    Dim result As Variant
    Dim hBs1 As HybridBodies
    Dim hB1 As HybridBody	
    Dim i As Integer
    'asociation_____________________
    Set HSF = catPart.HybridShapeFactory    
    Set se1 = catDocument.Selection
    se1.Search "CATGmoSearch.Point,sel"

    Set hBs1 = catPart.HybridBodies
    Set hB1 = hBs1.Item("GeomSet") 'change the name with yours
    se1.Add hB1
    se1.Search "CATGmoSearch.Point,sel"
            
    If (se1.Count > 0) Then    
        For i = 1 To se1.Count
		
			'write your code

        Next
        se1.Clear                
    Else
        MsgBox prompt:="no points in GeomSet, Title:="Aleluia"
        Exit Sub
    End If

End Sub 

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