14 Apr, 2019

Walking CATIA’s Trees with C-Sharp

In the post on CATIA Automation APIs, I described the languages one can use to automate CATIA. In this post, we’ll learn how to walk a CATIA tree using C-sharp. I assume that the readers is somewhat familiar with Object Oriented Programming concepts.

API CHM (Compiled HTML) Document

Before we start, let’s take how to find the methods we need in the C-Sharp/VBA API. Navigate to the following directory C:\Program Files\Dassault Systemes\B421_Cloud\win_b64\code\bin and locate the automation documentation file DSYAutomation.chm. If you’re opening it for the first time, you’ll land on the Foundation Object Model Map page, shown below:

The left section of the chm file viewer includes two tabs, Contents and Search; and the right section includes the documentation. I usually start at the Objects and Collections folder (on the left side) to locate objects and methods I need for my automation.

Note that the above API is provided for the VBA language, which is almost exactly the same for C-Sharp (there isn’t a C-Sharp document). The main difference between the two language is syntax and some language specific calls. The rest is quite the same. The same EKL VPM data model is true for VBA/C-Sharp, but with a few differences. For example:
– EKL’s ProductOccurrence is called VPMOccurrence
– The root of an assembly can be of type VPMReference (same like EKL) or of the type VPMRootOccurrence (different from EKL’s ProductOccurrence).
– Representations in EKL are either of type VPMRepReference or VPMRepInstance. Here, there is an additional type: VPMRepOccurrence.
– EKL works within an Editor object. Here, you can query the CATIA application object for its Editors collection, then work within an Editor object.

Make A Console Application

First, you’ll need to download Visual Studio, I am using the Community Version. Once downloaded, make a console application (project) as per the video below:

In future post, I’ll show how to make a WPF application following the MVVM pattern.

Load The Required Dlls

Next, we need to add CATIA’s Dlls to the project’s references. Follow the steps in the video to find the ones we need. Note that in the video I get an error message, ignore it.

Get The CATIA Application Object

Now that the Dlls are added, lets get the CATIA application object. Note that CATIA must be running in order for us to access it.

Get The Root Of The Active Editor

From the CATIA application, we can get the root of the current editor as follows.

Get The Children Under The Root

Now let’s get the children under the root. Note that here we will get the first level children only.

Getting all nested children requires recursion! This is how we can do it.

Here is the full program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using INFITF;
using System.Runtime.InteropServices;
using VPMEditorContextIDL;
using ProductStructureClientIDL;

namespace TreeWalker
{
    class Program
    {
        static void Main(string[] args)
        {
            INFITF.Application CAT = (INFITF.Application)Marshal.GetActiveObject("CATIA.Application");
            var name = CAT.FullName;

            Editor editor = CAT.ActiveEditor;
            PLMProductService service =(PLMProductService) editor.GetService("PLMProductService");

            VPMRootOccurrence root = service.RootOccurrence;
            var children = GetChildren(root);
            foreach (var item in children)
            {
                Console.WriteLine(item.InstanceOccurrenceOf.get_Name());
            }


            Console.Read();
        }
        static List<VPMOccurrence> GetChildren(VPMRootOccurrence root)
        {
            var items = new List<VPMOccurrence>();
            for (int i = 1; i <= root.Occurrences.Count; i++)
            {
                items.Add(root.Occurrences.Item(i));
                GetNestedChildren(root.Occurrences.Item(i), items);
            }
            return items;
        }
        static void GetNestedChildren(VPMOccurrence parent, List<VPMOccurrence> items)
        {
            for (int i = 1; i <= parent.Occurrences.Count; i++)
            {
                items.Add(parent.Occurrences.Item(i));
                GetNestedChildren(parent.Occurrences.Item(i), items);
            }
        }
    }
}

Here is a zip file of the Visual Studio Project! If you have CATIA installed on the C drive, the project should be able to find the dlls on its own. Here is the Repo!

And here is the DSYAutomation CHM

Hits: 249

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.

2 thoughts on “Walking CATIA’s Trees with C-Sharp”

  1. Hey Maher,

    Thanks for the post! I was going through and look at the documentation and while helpful, I’m having a hard time understanding how to connect some of the dots.

    For example; If i want to rename or append the name of the part (which in the above example is the instance title i understand under properties?) how would i find that method and the inputs for it? I tried a few different methods that i found but i wasn’t sure what inputs they were requiring? here is what i have attempted:
    – item.InstanceOccurrenceOf.set_Name(
    – item.InstanceOccurrenceOf.SetAttributeValue(
    – item.set_Name(

    This could also be some fuzziness on my understanding of C#.

    Any help would be greatly appreciated. Thanks!

    1. Hey Nathan, thanks.
      I would have guessed that set_Name would work on an instance. If not, try

      mystance.SetAttribute(“V_Name”, TheName)

      Note that V_Name is different from the Title.

      I’ll make a post on looking attributes names. You basically pick them from EKLs language browser.
      Let me know if this gets you going
      Maher

Leave a Reply

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