Reordering instance numbers
Reordering instance numbers
(OP)
Hi guys. Could anyone please help me with a problem... This macro does not go trough and I don't know what I'm doing wrong. I'm running it on a structure attached in post. The macro does not rename Product2/Part2 (Part2.2) to (Part2-temp.4).
Thanks
Roman
Option Explicit
Sub CATMain()
Dim productDocument1 As Document
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.")
Exit Sub
End If
Call reorder_item(productDocument1.Product)
End Sub
Sub reorder_item(oproduct)
Dim i, j, k As Integer
Dim oproducts As Products
Set oproducts = oproduct.Products
For i = 1 To oproducts.Count
If oproducts.Item(i).Products.Count > 0 Then
Call reorder_item(oproducts.Item(i))
End If
k = 0
For j = 1 To i
If oproducts.Item(j).PartNumber = oproducts.Item(i).PartNumber Then
k = k + 1
End If
Next
If i <> oproducts.Count Then
For j = i + 1 To oproducts.Count
If oproducts.Item(j).PartNumber = oproducts.Item(i).PartNumber Then
If oproducts.Item(j).Name = oproducts.Item(i).PartNumber & "." & k Then
oproducts.Item(j).Name = oproducts.Item(i).PartNumber & "-temp." & j
MsgBox (oproducts.Item(j).Name & " " & j)
End If
End If
Next
End If
oproducts.Item(i).Name = oproducts.Item(i).PartNumber & "." & k
Next
End Sub
Thanks
Roman
Option Explicit
Sub CATMain()
Dim productDocument1 As Document
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.")
Exit Sub
End If
Call reorder_item(productDocument1.Product)
End Sub
Sub reorder_item(oproduct)
Dim i, j, k As Integer
Dim oproducts As Products
Set oproducts = oproduct.Products
For i = 1 To oproducts.Count
If oproducts.Item(i).Products.Count > 0 Then
Call reorder_item(oproducts.Item(i))
End If
k = 0
For j = 1 To i
If oproducts.Item(j).PartNumber = oproducts.Item(i).PartNumber Then
k = k + 1
End If
Next
If i <> oproducts.Count Then
For j = i + 1 To oproducts.Count
If oproducts.Item(j).PartNumber = oproducts.Item(i).PartNumber Then
If oproducts.Item(j).Name = oproducts.Item(i).PartNumber & "." & k Then
oproducts.Item(j).Name = oproducts.Item(i).PartNumber & "-temp." & j
MsgBox (oproducts.Item(j).Name & " " & j)
End If
End If
Next
End If
oproducts.Item(i).Name = oproducts.Item(i).PartNumber & "." & k
Next
End Sub





RE: Reordering instance numbers
Tesak
http://scripts4all.eu/txtoncurve/ - Curved text for Catia V5
RE: Reordering instance numbers
RE: Reordering instance numbers
Tesak
http://scripts4all.eu/txtoncurve/ - Curved text for Catia V5
RE: Reordering instance numbers
What I did was a two pass system:
first pass would rename all instances to part.name & "." & (i+1000000)
second pass would just remove 1000000 from all instance number
I used a Dictionary to count the instances in first pass.
I used this 2 pass system because I could not be sure that the first pass would not use a number already used but not found yet.
indocti discant et ament meminisse periti
RE: Reordering instance numbers
oproducts.Item(j).Name = oproducts.Item(i).PartNumber & "-temp." & j
MsgBox (oproducts.Item(j).Name & " " & j)
Message box shows old name though it is clearly given a new value that does not exist in any other place. If I run this script only on main product (transfer "Call reorder_item(oproducts.Item(i))" in comment), then it goes trough.
RE: Reordering instance numbers
this is nice, but not a real solution for product reordering problem.
Tesak
http://scripts4all.eu/txtoncurve/ - Curved text for Catia V5
RE: Reordering instance numbers
RE: Reordering instance numbers
If oproducts.Item(i).Products.Count > 0 Then
Call reorder_item(oproducts.Item(i).ReferenceProduct)
End If
When you are processing the instances in the Subassemblies you will need to make sure you are obtaining the Product object from the 'ReferenceProduct' property of that subassembly before parsing through the Products collection to modify the instances in that subassembly.
RE: Reordering instance numbers
Tesak
http://scripts4all.eu/txtoncurve/ - Curved text for Catia V5
RE: Reordering instance numbers