Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reordering instance numbers 1

Status
Not open for further replies.

romaleee

Mechanical
May 13, 2016
37
HR
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


 
 http://files.engineering.com/getfile.aspx?folder=7b55057c-98b2-445a-b826-865bcadaf55f&file=Capture.JPG
Replies continue below

Recommended for you

As far as I know there is no direct VBA support for products reordering. Only way how to do it is to use some CUT & PASTE operations (not suitable for large assemblies and also you can break some links), or connect to "Graph tree reordering" window and simulate user. But in your code you are not doing any reordering, you just try to rename instance number, right?

Tesak
- Curved text for Catia V5
 
i did this trick way back then with my first recursive instance renaming script (sorry can't share).

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.



Eric N.
indocti discant et ament meminisse periti
 
2 passes would definitely help me and I'll try to rework mine code, but it is still not clear to me why this script refuses to change name in lines:

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.
 
Product reordering isn't really a topic of this thread. I just want someone to tell me what am I doing wrong with my scipt.
 
Try changing the Product you are passing to the Subroutine:

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.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top