Create new element in the model using capella-groovy scripts

Hello everyone,

I’m looking for a way to create a model element using groovy scripts (with FDorner capella-groovy addon).
If I take the following screenshot as example

image

I want to create a physical node called “Cabin screen” that will be a descendant of the physical element “Cabin”. Is there a way to do it ?

Thanks a lot,
Christian KOUMLAH MBEY

Of course this is possible. But to understand components, you have to understand Parts. Unfortunately, nobody really understands parts, but we keep trying :). So: first deselect all filters in the Capella Project Explorer: Quick Filters->Deselect All Filters.

Now you will see that for each Component, in your case “Cabin”, there will be an additional Object next to it, labeled “Cabin : Cabin”. This is the Cabin Part. When you create a Component, you must also make sure that that Part object next to is also created properly. Maybe Capella does it automatically, but maybe it doesn’t work if you do it in Groovy. So, how to actually create the Component:

First you find the parent component, parent. And then:

PhysicalComponent myNewComponent = PaFactory.eINSTANCE.createPhysicalComponent()
parent.getOwnedPhysicalComponents.add(myNewComponent).

If that doesn’t trigger creation of the Part, you create it in a similar fashion:
Part p = CsFactory.eINSTANCE.createPart();
p.setType(myNewComponent)
parent.getOwnedFeatures().add(part);

Something similar to this should do the trick. Feel free to DM me again if you need more help.

On the MANIFEST.MF of the groovy script plugin, you can also add dependency towards
org.polarsys.capella.core.model.helpers plugin.

Then use CapellaElementExt.creationService like:

PhysicalArchitecture pa = PhysicalArchitecture.find()
PhysicalComponent myNewComponent = PaFactory.eINSTANCE.createPhysicalComponent()
((PhysicalComponent)pa.getSystem()).getOwnedPhysicalComponents().add(myNewComponent);
CapellaElementExt.creationService(myNewComponent);

This will create the relative Part and set a name to the component.
(helpers can be found here

Philippe

I have always envisioned a transaction listener to handle the creation service calls, so you only create and hook up the element and the rest is automatic. Sometimes there is need to create the element without the part, or don’t set the name etc. In in such cases, we could use a custom transaction option to skip the call to creation service, e.g. OPTION_AUTOCREATE_PART=false, OPTION_AUTONAME=false. The method is I think TransactionalCommandStack.execute(Command, Map options).