15 Apr, 2019

Walking CATIA’s Trees With VBA

In a previous post, Walking CATIA’s Trees with C-Sharp, I showed how one can traverse a CATIA tree with C-Sharp using an external Windows Application. Here is the same example with VBA.

Here are a few differences:
– I am running within a CATIA session.
– I did not need to add references to Dlls.
– The syntax is different (but the APIs are the same).

Here is the VBA code

Sub CATMain()
    'the code here is structured similar to the c-sharp example.
    'This is the same API, it is just being called from VBA'

    'define variables'
    Dim editor As editor
    Dim root As VPMRootOccurrence
    Dim service As PLMProductService

    'get the active editor and from it get the PLMProductService to find the root.'
    Set editor = CATIA.ActiveEditor
    Set service = editor.GetService("PLMProductService")
    Set root = service.RootOccurrence

    'define a collection data structure'
    Dim children As Collection

    'get the chidlren
    Set children = GetChildren(root)

    Dim child As VPMOccurrence
    'loop over each child and print the name in a message box'
    For Each child In children
        MsgBox "Child Name Is " & child.Name

End Sub

'function to take a root and return a collection'
Function GetChildren(root As VPMRootOccurrence) As Collection

    Dim col As Collection
    Set col = New Collection

    'loop over nested occurrences in root'
    Dim i As Integer
    For i = 1 To root.Occurrences.Count
        'add occurrence to list'
        col.Add root.Occurrences.Item(i)

        'call recursive function to get all nested occurrences
        'inside current item and add it to the collection'
        GetNestedChildren root.Occurrences.Item(i), col

    Set GetChildren = col

End Function

'recursive function'
Sub GetNestedChildren(parent As VPMOccurrence, col As Collection)

    Dim i As Integer
    For i = 1 To parent.Occurrences.Count
        'add current occurence to collection'
        col.Add parent.Occurrences.Item(i)
        'call the method on the current occurrence'
        GetNestedChildren parent.Occurrences.Item(i), col

End Sub

How Does C-Sharp Compare?

You can do the same with C-Sharp using VSTA, which is like VBA, requires no setup; or with C-Sharp as an external project/application (with setup). That said, developing macros in VSTA means that code will live in side CATIA, just like VBA. So it’s a matter of which language is preferred. I find C-Sharp much easier to wtite with than VBA. The main benefit here for using either language is that code lives within a CATIA session. This means that users can run multiple sessions and run macros within each as needed.

C-Sharp external applications can only target one session at a time. Eitherway, I personally prefer C-Sharp (whether inside CATIA or as an external Visual Studio Project with the additional setup). C-Sharp is a modern language. It has far more advanced utilities and a huge number of nuget packages. It also supports a wider range of user interface elements.

How Does EKl Compare?

EKl does the same in a fewer lines of code. EKL code lives inside CATIA as well, but it has the least overhead and setup time. You can just write code.

Here is the code:

let root(ProductOccurrence)
root = GetEditorRootOccurrences("ProductOccurrence").GetItem(1)

let products(List)
products = root.Query("ProductOccurrence", "")

products.Apply("Feature", "Message( x.Name)")
Tags: ,

About : Maher Elkhaldi

Maher Elkhaldi is a senior applications engineer at Tesla Motors. He founded the 3DXAutomation blog to help make knowledge of programming CATIA easier to find, and contribute to the open-source community.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.