One of the most often asked questions of me is how do you reorder the specification tree in CATIA V5 with a macro? I am putting together a complete tutorial about one method to do it, shown in the video below.

A couple of potential alternative methods you could try to sort the tree or reorder components are as follows:

 

Create a VBA Listbox in a userform

 

To manually choose the order of components, one method might be to create a VBA userform with two listboxes, the first containing the list of parts in the assembly and the user would click each part in the order they want with that order appearing in the second listbox. The user would then click a button and CATIA would cut and paste the parts into the order displayed in the listbox.

 

To make a listbox, start a new VBA macro and insert a new module. Copy and paste this code into the module:reorder tree macro

 

Sub CATMain()

Set prodDoc1 = CATIA.ActiveDocument

Set prod1 = prodDoc1.Product

Set prods1=prod1.Products

For i = 1 to prods1.Count

    str=Left(prods1.Item(i).Name, 5)

    UserForm1.Listbox1.AddItem str

Next

 

‘Display the userform

UserForm.Show vbModeless

End Sub

 

Next, insert a new Userform and add a listbox to it. In a VBA listbox a user can select one or more values. Run the macro and a the listbox should pop-up displaying the name of all child parts in an assembly.

Use an Array

 

To automatically reorder instances in numerical order, you might add each part name into an array, then do cut and paste operations based on that array. If you’ve joined my email newsletter one of the first tips includes a macro to scroll through every item in the tree – a good starting point for this graph tree reordering macro.

 

Export CATIA names to Excel

 

Another workaround to sort the tree could also be to export the spec tree to Microsoft Excel, reorder the list of parts, then run a macro to rebuild the assembly based on the order listed  in the Excel spreadsheet.

 

One last tip: Under Knowledgewaretypelib there is a method called “reorder” that you may want to use when copying and pasting:

 

Dim Selection1 As Selection

Set selection1 = reorder.prodDoc1.Selection

selection1.Add reorder.prod1.products.item(i)

selection1.cut

set selection2 = CATIA.ActiveDocument.Selection

selection2.Add reorder.prod1

selection2.paste

Concluding Thoughts

Thanks for reading about my ideas for a reorder spec tree macro. I hope this gives you an idea about how you might accomplish part reordering in CATIA V5 tree. If anyone else has any other suggestions I’d love to hear about them in the comments below!

UPDATE APRIL 2016: New version of the reorder spec tree macro is now available. You can either purchase as a stand alone tutorial or it comes with the All-in package. Check it out here.