Instance Renumbering
Instance Renumbering
(OP)
I have been working on a macro to change the instance names in an assembly to be the "Part Number" + sequential numbering. I already have a script that returns and array of all the part numbers (pastrPartDoc) and product numbers (pastrProdDoc). The macro is working fine, except that it doesn't change the instance name of parts within another assembly even though the part is selected. The part instance is orange and it blinks but the instance name doesn't change. Any ideas why this is?

Sub InstanceRenumber(pastrProdDoc() As String, pastrPartDoc () As String)
Dim q As Integer
For q = 0 To UBound(pastrProdDoc) 'loop through each unique product
Dim selection1 As Selection
Set selection1 = CATIA.ActiveDocument.Selection
Dim sProd As String
sProd = pastrProdDoc(q)
Dim p As Integer
For p = 0 To UBound(pastrPartDoc) 'loop through each unique part
Dim sPart As String
sPart = pastrPartDoc(p)
selection1.Search "CATAsmSearch.Product.PartNumber=" & sProd & ",all"
Do While selection1.Count > 1 'remove multiple instances of product from selection
selection1.Remove2 (2)
Loop
selection1.Search "CATAsmSearch.Part.PartNumber=" & sPart & ",sel"
Dim iInstance As Integer
For iInstance = 1 To selection1.Count 'loop through all sParts within assembly
Dim newName As String
newName = selection1.Item2(iInstance).Value.PartNumber & "." & iInstance
selection1.Item2(iInstance).Value.Name = newName 'not working for parts under sub products
Next 'iInstance
Next 'p
Next 'q
End Sub

Sub InstanceRenumber(pastrProdDoc() As String, pastrPartDoc () As String)
Dim q As Integer
For q = 0 To UBound(pastrProdDoc) 'loop through each unique product
Dim selection1 As Selection
Set selection1 = CATIA.ActiveDocument.Selection
Dim sProd As String
sProd = pastrProdDoc(q)
Dim p As Integer
For p = 0 To UBound(pastrPartDoc) 'loop through each unique part
Dim sPart As String
sPart = pastrPartDoc(p)
selection1.Search "CATAsmSearch.Product.PartNumber=" & sProd & ",all"
Do While selection1.Count > 1 'remove multiple instances of product from selection
selection1.Remove2 (2)
Loop
selection1.Search "CATAsmSearch.Part.PartNumber=" & sPart & ",sel"
Dim iInstance As Integer
For iInstance = 1 To selection1.Count 'loop through all sParts within assembly
Dim newName As String
newName = selection1.Item2(iInstance).Value.PartNumber & "." & iInstance
selection1.Item2(iInstance).Value.Name = newName 'not working for parts under sub products
Next 'iInstance
Next 'p
Next 'q
End Sub





RE: Instance Renumbering
in my recursive sub i pass the product and check the product.products
indocti discant et ament meminisse periti
RE: Instance Renumbering
Take a look at my code on this thread:
thread560-365400: [Macro Catia ] Rename all instances and references
Drew Mumaw
www.textsketcher.com
www.drewmumaw.com
RE: Instance Renumbering
Sub InstanceRename()
Dim Documents As Documents
Set Documents = CATIA.Documents
Dim Item As Object
For Each Item In Documents
If Right(Item.Name, 10) = "CATProduct" Then
Dim CurrentProduct As Products
Set CurrentProduct = Item.Product.Products
Dim i As Integer
For i = 1 To CurrentProduct.Count
Dim CurrentPartNumber As String
CurrentPartNumber = CurrentProduct.Item(i).PartNumber
Dim k As Integer
k = 1
Dim j As Integer
For j = 1 To CurrentProduct.Count
Dim CurrentLine As String
CurrentLine = CurrentProduct.Item(j).PartNumber
If CurrentLine = CurrentPartNumber Then
CurrentProduct.Item(j).Name = CurrentPartNumber & "." & k
k = k + 1
End If
Next
Next
End If
Next
End Sub
RE: Instance Renumbering
I'd use thread560-365400: [Macro Catia ] Rename all instances and references because it's been tried and tested. Also, it initially renames all instances to something temporary to avoid any errors in renaming.
Drew Mumaw
www.textsketcher.com
www.drewmumaw.com