Instance Renaming Macro
Instance Renaming Macro
(OP)
I have used a bunch of macros to rename the instances of my parts to the part number.
I have ran into two big issues:
1.I have found macros that will rename every instance no matter how many different levels of products there are, but they fail when they run into an instance name that already exists further down the tree. Like the shown code:
2.I have also found a macro shown below that has code written into it that prevents it from failing when running into an instance name that is already in use.
Can someone help me combine these two codes together?
I tried, but have quickly gotten lost in the complexity of the second macro.
If this macro exists somewhere else, could someone please point me in the right direction?
I have ran into two big issues:
1.I have found macros that will rename every instance no matter how many different levels of products there are, but they fail when they run into an instance name that already exists further down the tree. Like the shown code:
CODE --> CATScript
Sub CATMain() Dim documentlist Dim MainProduct As Product Dim MainDocument As ProductDocument Set documentlist = CATIA.Documents Set MainDocument = CATIA.ActiveDocument Set oTopProduct = MainDocument.Product If (InStr(MainDocument.Name, "CATProduct") <> 0) Then Call RenameSingleLevelProduct(oTopProduct) Else MsgBox "Active document should be a Product" Exit Sub End If End Sub Sub RenameSingleLevelProduct(oTopProduct) Dim ItemToRename As Product Dim ItemToRenamePartNumber As String Dim lNumberOfItems As Long Dim myArray(4000) As String Dim i, j, k As Integer lNumberOfItems = oTopProduct.Products.Count For i = 1 to lNumberOfItems myArray(i) = "" Next For i = 1 to lNumberOfItems Set ItemToRename = oTopProduct.Products.Item(i) k = 0 'Rename Instance ItemToRenamePartNumber = ItemToRename.PartNumber myArray(i) = ItemToRenamePartNumber For j = 1 to i If myArray(j) = ItemToRenamePartNumber Then k = k + 1 End If Next ItemToRename.Name = ItemToRenamePartNumber & "." & k If (ItemToRename.Products.Count <> 0) Then Call RenameSingleLevelProduct(ItemToRename.ReferenceProduct) End If Next End Sub
2.I have also found a macro shown below that has code written into it that prevents it from failing when running into an instance name that is already in use.
CODE --> CATScript
Option Explicit
Dim oTopProductDoc As ProductDocument
Dim oTopProduct As Product
Dim ItemToRename As Product
Dim ItemToRenamePartNumber As String
Dim oDictionary 'Dictionary Object
Dim lNumberOfItems As Long
Dim i As Integer
Dim CatchError As Integer
Sub CATMain()
Set oTopProductDoc = CATIA.ActiveDocument
CatchError = 0
'Document type check
If (InStrRev(oTopProductDoc.Name, ".CATProduct", -1) = 0) Then
MsgBox "Active Document must be a CATProduct"
Exit Sub
End If
Set oTopProduct = oTopProductDoc.Product 'The top product containing the items to rename
Set oDictionary = CreateObject("Scripting.Dictionary")
lNumberOfItems = oTopProduct.Products.Count
For i = 1 to lNumberOfItems
Set ItemToRename = oTopProduct.Products.Item(i)
'~ ItemToRenamePartNumber = ItemToRename.PartNumber
ItemToRenamePartNumber = ItemToRename.PartNumber
'See if the item is already in list, in that case increment the suffix number by 1 before renaming
If oDictionary.Exists(ItemToRenamePartNumber) Then
oDictionary.Item(ItemToRenamePartNumber) = oDictionary.Item(ItemToRenamePartNumber)+1
Else
oDictionary.Add (ItemToRenamePartNumber) ,"1"
End If
'Trap errors because of pre-existing Instance names
On Error Resume Next
'Rename the items instance name like this: Part Number + "." + suffix number
ItemToRename.Name = ItemToRenamePartNumber & "." & oDictionary.Item(ItemToRenamePartNumber)
If Err.Number <> 0 Then
CatchError = 1
End If
Next
'If there was an error, then repeat the rename to fix it.
If CatchError = 1 Then
oDictionary.RemoveAll
RepeatRename()
End If
Set oDict1 = Nothing
End Sub
Sub RepeatRename()
For i = 1 to lNumberOfItems
Set ItemToRename = oTopProduct.Products.Item(i)
ItemToRenamePartNumber = ItemToRename.PartNumber
If oDictionary.Exists(ItemToRenamePartNumber) Then
oDictionary.Item(ItemToRenamePartNumber) = oDictionary.Item(ItemToRenamePartNumber)+1
Else
oDictionary.Add (ItemToRenamePartNumber) ,"1"
End If
ItemToRename.Name = ItemToRenamePartNumber & "." & oDictionary.Item(ItemToRenamePartNumber)
Next
End Sub Can someone help me combine these two codes together?
I tried, but have quickly gotten lost in the complexity of the second macro.
If this macro exists somewhere else, could someone please point me in the right direction?





RE: Instance Renaming Macro
CODE --> CATVBA
Public oList As Variant Option Explicit Sub CATMain() On Error Resume Next 'Declarations Dim oTopDoc As Document Dim oTopProd As ProductDocument Dim oCurrentProd As Product Dim n As Integer 'Check if the active document is an assembly, else exit Set oTopDoc = CATIA.ActiveDocument If oTopDoc Is Nothing Then MsgBox "Must have an assembly open" Exit Sub End If If Right(oTopDoc.Name, 7) <> "Product" Then MsgBox "Active document should be a product" Exit Sub End If 'CATIA.RefreshDisplay = False Set oCurrentProd = oTopDoc.Product Set oList = CreateObject("Scripting.dictionary") CATIA.StatusBar = "Working On" & " " & oCurrentProd.Name Call RenameSingleLevel(oCurrentProd) 'Call the subroutine, it is a recursive loop 'CATIA.RefreshDisplay = True CATIA.StatusBar = "Done" End Sub Private Sub RenameSingleLevel(ByRef oCurrentProd As Product) On Error Resume Next 'More declarations Dim ItemToRename As Product Dim ToRenamePartNumber As String Dim lNumberOfItems As Long Dim RenameArray(2000) As String Dim i As Integer Dim j As Integer Dim k As Integer Set oCurrentProd = oCurrentProd.ReferenceProduct 'You have to work with the "ReferenceProduct" object lNumberOfItems = oCurrentProd.Products.Count 'For i = 1 To lNumberOfItems 'Clear out the rename array ' RenameArray(i) = "" 'Don't know if this is necessary 'Next 'Run through this loop once, to set everything to a dummy name, to avoid naming conflicts For i = 1 To lNumberOfItems 'Cycle through the assembly's children Set ItemToRename = oCurrentProd.Products.Item(i) 'Declare which item we are working on ToRenamePartNumber = ItemToRename.PartNumber 'Get the Part Number 'ToRenamePartNumber = ItemToRename.DescriptionRef 'Toggle these two lines for testing RenameArray(i) = ToRenamePartNumber 'Building the list of part names for the numbering loop k = 0 'Numbering Loop For j = 1 To i 'This loop checks and sets the instance number If RenameArray(j) = ToRenamePartNumber Then k = k + 1 End If Next CATIA.StatusBar = ItemToRename.Name & " > " & ToRenamePartNumber & "." & k 'MsgBox ItemToRename.Name & " / " & ToRenamePartNumber & "." & k 'This line is for testing only ItemToRename.Name = ToRenamePartNumber & "TEMP." & k 'Set the new instance name, to a TEMP dummy value Next 'Run through this loop to set the name finally, then the recursion call For i = 1 To lNumberOfItems Set ItemToRename = oCurrentProd.Products.Item(i) ToRenamePartNumber = ItemToRename.PartNumber 'Toggle these two lines for testing 'ToRenamePartNumber = ItemToRename.DescriptionRef 'Toggle these two lines for testing RenameArray(i) = ToRenamePartNumber k = 0 For j = 1 To i If RenameArray(j) = ToRenamePartNumber Then k = k + 1 End If Next CATIA.StatusBar = ItemToRename.Name & " > " & ToRenamePartNumber & "." & k 'MsgBox ItemToRename.Name & " / " & ToRenamePartNumber & "." & k 'For testing ItemToRename.Name = ToRenamePartNumber & "." & k 'Set the new instance name final If ItemToRename.Products.Count <> 0 Then 'Recursive Call for version 0.1.2 If oList.exists(ItemToRename.PartNumber) Then GoTo Finish If ItemToRename.PartNumber = ItemToRename.ReferenceProduct.Parent.Product.PartNumber Then oList.Add ItemToRename.PartNumber, 1 Call RenameSingleLevel(ItemToRename) End If Finish: Next End SubRE: Instance Renaming Macro
I appreciate the help, now I have to figure out how to convert this to CATScript.
Why does everyone seem to prefer VBA over CATScript?
I am very new to coding and have run into VBA code responses to all my queries.
RE: Instance Renaming Macro
Even shorter summary: CATScript runs faster and lighter, but CATVBA has more power and more options available.
RE: Instance Renaming Macro
Your response was pretty much what I figured with CATScript vs VBA.
VBA is more powerfull and CATScript is faster.
RE: Instance Renaming Macro
Take a look at my macro here:
http://www.eng-tips.com/viewthread.cfm?qid=365400
Drew Mumaw
www.textsketcher.com
www.drewmumaw.com
RE: Instance Renaming Macro
RE: Instance Renaming Macro
Forgot to add...so you cannot use the second code in CATScript because it utilizes a dictionary.
RE: Instance Renaming Macro
Examples for Scripting.Dictionary
And for collections you can use CreateObject("System.Collections.ArrayList")
Not all companies has vba installed....and then you have to handle CATScripts and catvbs...
Regards
Fernando
https://picasaweb.google.com/102257836106335725208 - Romania
https://picasaweb.google.com/103462806772634246699... - EU
RE: Instance Renaming Macro
PRT-00004256(PRT-00004256.1.1.2)
So i want to change that for my name file that it is: CEL14086020 but every time i try to run the macro it crash because the .1.1.2. i try your macro but donsn't work eader. do you have any solution for that? thank you for your help.