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
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
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
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 SubThis 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 SubThis 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
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
If You simply change
CODE -->
CODE -->
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 SubCATSrcipt 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 -->
I can't find any mistakes in this line. I'm still working on it
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
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 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 SubIs 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
RE: Macro to Change Part Number and Instance Name to File Name
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
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
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