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:
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:
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?