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()
    Dim editor As editor
    Dim root As VPMRootOccurrence
    Dim service As PLMProductService

    Set editor = CATIA.ActiveEditor
    Set service = editor.GetService("PLMProductService")
    Set root = service.RootOccurrence

    Dim children As Collection
    Set children = GetChildren(root)

    Dim child As VPMOccurrence

    For Each child In children
        MsgBox "Child Name Is " & child.Name

End Sub

Function GetChildren(root As VPMRootOccurrence) As Collection

    Dim col As Collection
    Set col = New Collection

    Dim i As Integer
    For i = 1 To root.Occurrences.Count

        col.Add root.Occurrences.Item(i)
        GetNestedChildren root.Occurrences.Item(i), col

    Set GetChildren = col

End Function

Sub GetNestedChildren(parent As VPMOccurrence, col As Collection)

    Dim i As Integer
    For i = 1 To parent.Occurrences.Count
        col.Add parent.Occurrences.Item(i)
        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)")

Hits: 198

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.