Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Re-Order CATIA Tree

Re-Order CATIA Tree

Re-Order CATIA Tree


I am working on a macro to re-order the product tree. I first developed a macro that cuts and pastes the each of the components in the right order. This worked great but has the issue of breaking any drawings that are linked to the components. As far as I know the only way to re-order the tree without breaking links is using the "Graph tree Reorder" command. I have had some success pushing keystrokes (sendkeys) into this command to perform the required re-ordering but I am running into an issue as the model gets larger allowing time for the command to open prior to sending keys is proving difficult. I have tried using "Sub Sleep Lib "kernel32"", Application.Wait (Now + TimeValue("00:00:10")), sending the program into a loop for a set period of time, and CATIA.RefreshDisplay = True. All of these pause the macro, but the command doesn't open while the code is paused. Any ideas?

Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10
'Declare sleep
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub CATOrder()

Dim selectProducts As Products
Dim Item As Product
Dim i, d, m, k, g As String
Dim prodCnt, pn, currentPos, position, cntr, bs As Integer
Dim continue As Boolean
Dim pastPos() As Integer
Dim timer As Double

On Error GoTo 2
Set selectProducts = CATIA.ActiveDocument.Selection.Item2(1).Value.Products

'Open Excel
Dim Excel As Object
Set Excel = CreateObject("Excel.Application")
Excel.Visible = True

Excel.Cells(1, 1) = "Part Number"
Excel.Cells(1, 2) = "Detail Number"
Excel.Cells(1, 3) = "Instance Name"
Excel.Cells(1, 4) = "ORDER"

Excel.Cells(1, 1).Font.Bold = True
Excel.Cells(1, 2).Font.Bold = True
Excel.Cells(1, 3).Font.Bold = True
Excel.Cells(1, 4).Font.Bold = True
Excel.Cells(1, 4).Interior.ColorIndex = 15
Excel.Cells(1, 1).Borders.LineStyle = xlContinuous
Excel.Cells(1, 2).Borders.LineStyle = xlContinuous
Excel.Cells(1, 3).Borders.LineStyle = xlContinuous
Excel.Cells(1, 3).Borders.LineStyle = xlContinuous

prodCnt = selectProducts.Count
On Error Resume Next
'Input values into excel
i = 1
g = 2
Do While i < prodCnt + 1

Set Item = selectProducts.Item(i)

pn = InStr(Item.PartNumber, "_")
pn = Right(Item.PartNumber, Len(Item.PartNumber) - pn)
pn = Left(pn, Len(pn) - 2)

Excel.Cells(g, 1) = Item.PartNumber
Excel.Cells(g, 2) = pn
Excel.Cells(g, 3) = Item.Name
Excel.Cells(g, 4) = i

Excel.Cells(g, 1).Borders.LineStyle = xlContinuous
Excel.Cells(g, 2).Borders.LineStyle = xlContinuous
Excel.Cells(g, 3).Borders.LineStyle = xlContinuous
Excel.Cells(g, 4).Borders.LineStyle = xlContinuous
Excel.Cells(g, 4).Interior.ColorIndex = 15

g = g + 1
i = i + 1

'Sort values in Excel
continue = MsgBox("Sort Excel then press 'OK'.", vbOKCancel)

If continue = vbCancel Then
End If

CATIA.RefreshDisplay = True
CATIA.StartCommand "Graph tree Reordering"
Sleep 1000

'Application.Wait (Now + TimeValue("00:00:10"))

'timer = Now + 0.0000115
'Do While Now < timer
'bs = bs + 1

mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 50
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True

Excel.Cells(1, 5) = "i"
Excel.Cells(1, 6) = "position"
Excel.Cells(1, 7) = "d"
Excel.Cells(1, 8) = "m"
Excel.Cells(1, 9) = "cntr"

'Sort tree with Graph tree Reordering
i = 1
Do While i < prodCnt + 1

Excel.Cells(i + 1, 5) = i

position = Excel.Cells(i + 1, 4)

k = i - 2
cntr = 0
Do While k >= 0
If pastPos(k) > position Then
cntr = cntr + 1
End If
k = k - 1

ReDim Preserve pastPos(i - 1)
pastPos(i - 1) = position

d = position + cntr - i + 1

If i = 1 Then
d = d - 1
End If

m = position + cntr - i

Excel.Cells(i + 1, 9) = cntr
Excel.Cells(i + 1, 6) = position
Excel.Cells(i + 1, 7) = d

Do While d > 0
Call arrowDwn
d = d - 1

Call tabUp

Excel.Cells(i + 1, 8) = m

Do While m > 0
Call moveUp
m = m - 1

Call tabDwn
i = i + 1
Sleep 200

closeExcel = MsgBox("Would you like to close Excel?", vbYesNo)
If closeExcel = vbYes Then

Close Excel
Excel.ActiveWorkbook.Close SaveChanges:=False
End If

GoTo 1

MsgBox "Select a product before running macro."

Excel.ActiveWorkbook.Close SaveChanges:=False

End Sub
Function moveUp()

Application.SendKeys "~"

End Function
Function arrowDwn()

Application.SendKeys "{DOWN}"

End Function
Function tabUp()

Application.SendKeys "{TAB}"

End Function
Function tabDwn()

Application.SendKeys "+{TAB}"

End Function

RE: Re-Order CATIA Tree

Wow. Such a convoluted solution to something Dassault should provide out of the box. I've always found the graph tree reordering function to be lacking in CATIA. But I'm not sure I would trust send keys for this.

Drew Mumaw

RE: Re-Order CATIA Tree

Yes, this is a very convoluted solution, but there isn't any other way. I have gotten it to work great on a small product but it fails on a larger product due to the delay from the command opening. Probably due to the time required to gather all of the instance names.

Can't use V6 so that doesn't really help. It's nice that they added this functionality though.

RE: Re-Order CATIA Tree

yea, that's the problem with send keys. you never know when the previous code has executed. not much control. i've found it unreliable.

Drew Mumaw

RE: Re-Order CATIA Tree

You can also try SmartSort application:


It is doing exactly what you are after but much faster and more reliable way through Windows Automation.

Main Features:
  • Works in Visualization mode or Design mode
  • Perfect for large assemblies (no need to switch to Design mode)
  • Constraint links are preserved
  • No CUT & PASTE operations and no broken links
  • No components renaming
  • It uses CATIA native Graph tree reordering feature

http://scripts4all.eu/txtoncurve/ - Text along a curve for Catia V5

RE: Re-Order CATIA Tree


Here is an idea if you hit that wall :)

Of course, the solution is not the "cut&paste" one.


RE: Re-Order CATIA Tree


Yes, it will soon be available. It is in testing now.


RE: Re-Order CATIA Tree

I tried SmartSort. It's really a useful tool for Catia sort, Thanks for Tesak's sharing. I recommand this tool to everybody.

RE: Re-Order CATIA Tree

Hey llbby, the only way that we found to run SmartSort from CATIA is to run it with a batch file. Basically you have to input a path to a folder and the code writes a batch file then runs it. Here is a snip of the code that we use to do this:

Sub CATMain()

If CATIA.GetWorkbenchId = "Drw" And CATIA.GetWorkbenchId = "DrwBG" Then
MsgBox "Not for drawings.", , "Error"
End If

If Len(Dir("C:\Temp\")) = 0 Then
MkDir ("C:\Temp")
End If

runPath = Dir("C:\Temp\SmartSortLoc.txt")
If runPath <> "" Then
Open "C:\Temp\SmartSortLoc.txt" For Input As #1
Line Input #1, runPath
Close #1
End If

If Dir(runPath & "\") = "" Or runPath = "" Then
runPath = ""
runPath = InputBox("Please input the path to your SmartSort folder e.g. [C:\Users\user.name\Desktop\SmartSort]", "Path")
If runPath = "" Then End
Open "C:\Temp\SmartSortLoc.txt" For Output As #1
Print #1, runPath
Close #1
GoTo Check
End If

Dim apps() As Variant
apps() = AllRunningApps
g = 0
For i = 1 To UBound(apps, 2)
If apps(0, i) = "CNEXT.exe" Then
If g = 0 Then
g = 1
MsgBox "More than one instance of CATIA is running. Please close the other instance of CATIA and try again.", , "Error"
End If
End If

If CATIA.GetWorkbenchId <> "Assembly" Then
CATIA.StartWorkbench ("Assembly")
End If

Open runPath & "\AutoPlay.bat" For Output As #1
Print #1, "start /d " & Chr(34) & runPath & Chr(34) & " SmartSort.exe"
Close #1

If Dir(runPath & "\SmartSort.exe") <> "SmartSort.exe" Then
MsgBox "SmartSort.exe was not found. Please make sure that the application is named correctly.", , "Error"
End If

Call Shell(runPath & "\AutoPlay.bat", 1)

End Sub

Public Function AllRunningApps() As Variant
Dim strComputer As String
Dim objServices As Object, objProcessSet As Object, Process As Object
Dim oDic As Object, a() As Variant

Set oDic = CreateObject("Scripting.Dictionary")

strComputer = "."

Set objServices = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set objProcessSet = objServices.ExecQuery("SELECT Name, ProcessID FROM Win32_Process", , 48)

ReDim a(1, 0)
a(0, 0) = ""
For Each Process In objProcessSet
If a(0, 0) = "" Then
a(0, 0) = Process.Properties_("Name").Value
a(1, 0) = Process.Properties_("ProcessID").Value
ReDim Preserve a(1, UBound(a, 2) + 1)
a(0, UBound(a, 2)) = Process.Properties_("Name").Value
a(1, UBound(a, 2)) = Process.Properties_("ProcessID").Value
End If

AllRunningApps = a()
End Function

RE: Re-Order CATIA Tree

hi cilici,
it is a great tool. well done.
uhm do you have that kind of tool of renaming every part number or instance name? or just deleting part number or instance name?


RE: Re-Order CATIA Tree

Hi everyone,

Here is my sorter that I created. It can sort based on Boeing detail number (which is what we do here mostly) or by instance name. I have also included the source file so you can add your own sorting algorithm (you just need to sort the values in the right box).

RE: Re-Order CATIA Tree

good job! thanks for sharing

Eric N.
indocti discant et ament meminisse periti

RE: Re-Order CATIA Tree


Here is an updated version of Reorder.


It now manages the situations when instances' names are identical except the case (e.g. Part1(Part1.1) versus Part1(PART1.1)).

Please note that the tool is free of use commercially or not.

Any feedback (especially the negative ones) will be appreciated (you can find my email in the file properties).


RE: Re-Order CATIA Tree

Hi ! everyone , Cilici thanks you for sharing it with us.

I-m having a problem using the last reorder version.

I place all parts on the order I want, it starts to run but at the en the next message apears

Could you know what is this happening _

RE: Re-Order CATIA Tree


I had similar queries from people running on slow computers. Having the same assembly reordered on a more powerful computer was fine.

Now, this message is a warning and the differences between expected and actual result should be minimum therefore you could manually do the missing moves in the tree.

I know that this behavior is not what I intended when developing this app and it should do what was programmed to do but there can be so many particularities in terms of software and hardware.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close