Simplify SysML Script Generation for CATIA MagicDraw
The OpenAPI can be confusing, but use your IDE to your advantage
I approach most tasks related to systems engineering and SysML models computationally, and have come to appreciate CATIA MagicDraw's OpenAPI. The learning curve for OpenAPI is steep, so for many casual users writing scripts, it can be daunting. I created the following approach that leverages your favorite IDE (and gradle) to simplify script generation.
The scripting and plugin framework for 3DS (Dassault) Magicdraw / Cameo is one of its most powerful features. There are several languages that can be used to script Magicdraw, all of which use the underlying Java virtual machine (JVM). In order to use the API that Magicdraw offers, known as the OpenAPI, you can use the Javadocs reference.
For example, consider the ostensibly easy task of printing the number of Elements in a model to the log window. You could refer to the OpenAPI Javadocs, available at https://jdocs.nomagic.com/2022x/allclasses.html:
This would give you information of a static method Application.getInstance() that gives you the singleton Application instance, which you can navigate to the Project class using the getProject() method. Using the byScope methods of Finder, you could get all elements and a count. This requires a fair bit of knowledge of the OpenAPI, and the built in code editor does not have tooltip support. You will also have to perform your own import statements, which can be tedious.
From Tools → Macros → Create Macro:
Note that I am using the Groovy language. Groovy happens to be the most convenient and powerful scripting language for those familiar with Java, but other languages would operate the same way.
Alternatively, if you have a preferred IDE, you can make scripting and navigating the OpenAPI much simpler! I prefer IntelliJ, but Eclipse would be similar.
A Simpler Scripting Approach:
From IntelliJ, create a Groovy project, and use Gradle as a build system. (Technical digression - maven could work as well, but maven really discourages local jar references. Gradle is quite happy with them.)
Next, we need to include all the Magicdraw jars (Java code libraries), so the IDE understands how to work with the OpenAPI. Add the following to your dependencies section in the build.gradle file. Your path may vary, so be sure to use your own path to the /lib directory in the Cameo/Magicdraw folder:
implementation fileTree(dir: 'C:/Program Files/CEA/Cameo Enterprise Architecture 2021/lib', include: ['*.jar'])
Next, create a groovy file:
Now, as you start typing “Application.getIns”, hit control-space (auto-complete):
It will now recommend the correct method, and add the import.
import com.nomagic.magicdraw.core.Application
Application.getInstance()
Continuing to the Finder methods, which can be confusing:
What does find(project) return? Mouse over to get help. (it returns a Collection<Element>):
Continuing, can you spot the code error? The built-in Magicdraw code editor gives cryptic errors, and only when run, not syntactic and static analysis like an IDE. IntelliJ highlighted the error - the log method takes a String, while I provided an int:
My final code:
Now, to add the IDE generated script to Magicdraw, I simply point Magicdraw to the appropriate groovy file, from Tools → Macro → Create Macro. Notice that you don’t have to move it out of the IDE created directory, so you can do iterative development both in IntelliJ and Magicdraw. Be sure to set the language, as the tool will not auto-infer from the groovy extension:
This demonstrates how to leverage the navigation and static debugging power of your IDE to generate Magicdraw scripts. Note that this doesn’t help with dynamic debugging (the running code), as that gets more complex, and Dassault has guides for that. This approach allows for quicker and easier generation of simple scripts by importing Magicdraw libraries into your IDE using Gradle, then auto-completion using Intellij features.
Hope this helps!