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:
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!
Reorder Specification Tree Tutorial
This step-by-step tutorial will show you how to rearrange the order of the components in the specification tree using a VBA macro. This method will use cut and paste but the constraint links will be preserved. In this tutorial, you’ll also learn how to:
-create a userform with listboxes, labels, and command buttons
- make a listbox not selectable
-keep constraints when reordering by cutting and pasting
- automatically sort the tree from A to Z or Z to A
The general steps this tutorial will follow are:
1. Create a new CATProduct or open an existing assembly file in CATIA V5
2. Setup a new CATVBA program
3. Create a Userform
4. Write the code for each of the command buttons
5. Write the reorder tree code
6. Test
I want this!https://dsadsadsadsadsa.gumroad.com/l/reorderspectree
Did you find any way to reorder the spec tree without cutting and pasting? This method causes problems (e.g. when constraints are applied).
You can reorder the tree with VBA or CAA. I’ve seen it done before I just haven’t had the time to write it out for myself yet.
Hello,
Even i want an option to reorder my Specification tree which consists of more than 5000 parts. I can not cut paste parts as they are linked with drawing. Is there any option for easy reordering ???
It can be done. We’ve been working on a catvba to reorder the tree and preserve all the links. It will be available to download soon!
Have you had any luck with this alternate solution to the cut and paste. Am trying to do the same thing, however, if I cut and paste y loose the links between parts, i know that the graph tree reordering (manual operation) works great. It doesn’t brake links! The question is, how does it do it? As when I try to record the operation I get no code 🙁 Any suggestions?
Have you had any luck with this alternate solution to the cut and paste. Am trying to do the same thing, however, if I cut and paste y loose the links between parts, i know that the graph tree reordering (manual operation) works great. It doesn’t brake links! The question is, how does it do it? As when I try to record the operation I get no code 🙁 Any suggestions
Hi Francisco. Yes, we created a macro that will reorder the tree and keep the constraints. There are still come issues with contextual links we are still working on. Will let you know when there is an update. Thanks,
Emmett
Hi Emmett, how are you doing?
Have you already had the issues solved regarding macro that will reorder the tree and keep the constraints? Please, let us know.
Thank you in advance.
Long time i’m trying to find some solution for this.
Best what i found was this win api but i never try it:
http://www.grozeaion.com/catia/47-catia-v5/vb-vba/97-reorder-components
Hope that u will make something, tree reordering macro will be very usefull.
Thanks!
Toni
Hi,Toni! I’m doing something with this. Could tell me you method to deal with this? Do you use the cut and paste method of selection?
New version of the spec tree reorder is up and it does include how to keep the assembly constraints intact. https://www.scripting4v5.com/reorder-spec-tree-macro
The way to keep link with Assembly constraint is simple like below.
‘——————- Keep assembly constraint when cutting & pasting ————————
Dim asmConstraintSettingAtt1 ‘As AsmConstraintSettingAtt
Set asmConstraintSettingAtt1 = CATIA.SettingControllers.Item(“CATAsmConstraintSettingCtrl”)
Dim Value_Org As Long ‘ Will recover original value after reorder.
Value_Org = asmConstraintSettingAtt1.PasteComponentMode
asmConstraintSettingAtt1.PasteComponentMode = catPasteWithCstOnCopyAndCut
asmConstraintSettingAtt1.SaveRepository
‘——————————————————————————————–
‘ reorder
Call Reorder_Spec_Tree
‘——————- ‘ recover original value ——————–
asmConstraintSettingAtt1.PasteComponentMode = Value_Org
asmConstraintSettingAtt1.SaveRepository
‘——————————————————————————————–
But the main problem is if the product has a drawing.
If reorder by macro then it’s broken.
Does anybody know the method without CUT/PASTE ?
Otherwise keep link with product and drawing?
By using an array as suggested by Emmett – created as System.Collections.ArrayList – I have stored the desired order of geometrical sets in it.
Now I can’t make cut/paste/delete operations on the starting geometrical sets because they have some children (features) somewhere in my CatPart file.
So, is it possible to assign/replace each item in the starting hybrid body collection with the respective item contained in the ordered array?
How can I do that?
Thanks in advance. Cheers guys!
Any solution to this? Reordering the tree will cause pink constraints in the drawings since are not “up to date”. Changing the cut/paste setting does not fix this.
do the reorder first , after this drawing 🙂