Hello,
I am new in VBA and try to modify a code that change the instance name, in order that this code also change every catia properties of each parts, products ... in the complete tree. In fact I want that this instance rename code also change component DescriptionRef and Nomenclature with UserRefProperties that exists in each components.
I have the following code found on internet :
In fact the code works, but only for the products inside the complete tree (red line were added). It doesn't work for the parts inside this one... WHY ??? But if I only put some text between "...", it works for every components inside the tree. i don't understand, I need some help please. Thank you.
I am new in VBA and try to modify a code that change the instance name, in order that this code also change every catia properties of each parts, products ... in the complete tree. In fact I want that this instance rename code also change component DescriptionRef and Nomenclature with UserRefProperties that exists in each components.
I have the following code found on internet :
Code:
[COLOR=#4E9A06]'********************************************************
'By MarkAF, some code borrowed from forums
'********************************************************[/color]
Public oList As Variant
Option Explicit
Sub CATMain()
On Error Resume Next
[COLOR=#4E9A06]'Declarations[/color]
Dim oTopDoc As Document
Dim oTopProd As ProductDocument
Dim oCurrentProd As Product
Dim n As Integer
[COLOR=#4E9A06]'Check if the active document is an assembly, else exit[/color]
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
Set oCurrentProd = oTopDoc.Product
Set oList = CreateObject("Scripting.dictionary")
CATIA.StatusBar = "Working On" & " " & oCurrentProd.Name
Call RenameSingleLevel(oCurrentProd) [COLOR=#4E9A06]'Call the subroutine, it is a recursive loop[/color]
CATIA.StatusBar = "Done"
End Sub
Private Sub RenameSingleLevel(ByRef oCurrentProd As Product)
On Error Resume Next
[COLOR=#4E9A06]'More declarations[/color]
Dim ItemToRename As Product
Dim ToRenamePartNumber As String
Dim ToRenamePartDescription As String
Dim NumberOfItems As Long
Dim RenameArray(2000) As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Set oCurrentProd = oCurrentProd.ReferenceProduct [COLOR=#4E9A06]'You have to work with the "ReferenceProduct" object[/color]NumberOfItems = oCurrentProd.Products.Count
[COLOR=#4E9A06]'Run through this loop once, to set everything to a dummy name, to avoid naming conflicts[/color]
For i = 1 To NumberOfItems [COLOR=#4E9A06]'Cycle through the assembly's children[/color]
Set ItemToRename = oCurrentProd.Products.Item(i) [COLOR=#4E9A06]'Declare which item we are working on[/color]
ToRenamePartNumber = ItemToRename.PartNumber [COLOR=#4E9A06] 'Get the Part Number[/color]
If InStr(ToRenamePartNumber, "-_") <> 0 Then [COLOR=#4E9A06]'Check for KT #'s, should exist only in CGRs[/color]
ToRenamePartNumber = Left(ToRenamePartNumber, (InStr(ToRenamePartNumber, "-_") - 1))
End If
RenameArray(i) = ToRenamePartNumber [COLOR=#4E9A06]'Building the list of part names for the numbering loop[/color]
k = 0 [COLOR=#4E9A06]'Numbering Loop[/color]
For j = 1 To i [COLOR=#4E9A06]'This loop checks and sets the instance number[/color]
If RenameArray(j) = ToRenamePartNumber Then
k = k + 1
End If
Next
CATIA.StatusBar = ItemToRename.Name & " > " & ToRenamePartNumber & "." & k
ItemToRename.Name = ToRenamePartNumber & "TEMP." & k [COLOR=#4E9A06]'Set the new instance name, to a TEMP dummy value[/color]
Next
[COLOR=#4E9A06]'Run through this loop to set the name finally, then the recursion call[/color]
For i = 1 To NumberOfItems
Set ItemToRename = oCurrentProd.Products.Item(i)
ToRenamePartNumber = ItemToRename.PartNumber [COLOR=#4E9A06]'Toggle these two lines for testing[/color]
RenameArray(i) = ToRenamePartNumber
[COLOR=#EF2929]ItemToRename.DescriptionInst = ItemToRename.UserRefProperties.Item("Designation").Value
ItemToRename.Nomenclature = ItemToRename.UserRefProperties.Item("codeGPAO").Value
oCurrentProd.DescriptionRef = oCurrentProd.UserRefProperties.Item("Designation").Value
oCurrentProd.Nomenclature = oCurrentProd.UserRefProperties.Item("codeGPAO").Value[/color]
[COLOR=#4E9A06]'ItemToRename.DescriptionInst = "CC1"
'ItemToRename.Nomenclature = "BB1"
'oCurrentProd.DescriptionRef = "CC2"
'oCurrentProd.Nomenclature = "BB2"[/color]
k = 0
For j = 1 To i
If RenameArray(j) = ToRenamePartNumber Then
k = k + 1
End If
Next
CATIA.StatusBar = ItemToRename.Name & " > " & ToRenamePartNumber & "." & k
ItemToRename.Name = ToRenamePartNumber & "." & k [COLOR=#4E9A06]'Set the new instance name final[/color]
If ItemToRename.Products.Count <> 0 Then [COLOR=#4E9A06]'Recursive Call[/color]
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 Sub
In fact the code works, but only for the products inside the complete tree (red line were added). It doesn't work for the parts inside this one... WHY ??? But if I only put some text between "...", it works for every components inside the tree. i don't understand, I need some help please. Thank you.