Generate ExchangeItem programmatically

Hello,

how can an ExchangeItem be generated with Py4C in an InterfacePackage?

import_pkg is an InterfacePackage e.g. in the SA-Level.
name shall be the name of the new EI.

This one is not working:

ei = ExchangeItem()

import_pkg.getOwnedExchangeItems().add(ei.get_java_object())

org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(ei.get_java_object())

ei.set_name(name)

Error-Message:
‘InterfacePkg’ object has no attribute ‘getOwnedExchangeItems’

The method name “zip” from the module “__EASE_MOD_org_eclipse_ease_modules_platform_ResourcesModule” can not be wrapped because it’s name is reserved

(Capella 7.0.1, Py4C 1.4.0)

Hello!

you can use the method get_owned_exchange_items() :
(bad) example code:

se = model.get_system_engineering()
data = se.get_logical_architecture().get_interface_pkg()

...

for item in my_interfaces[(source_name,target_name)]:
            itf_item = ExchangeItem()
            itf_item.set_name(item)
            
            exists = False
            
            for exchange_item in data.get_owned_exchange_items():
                if exchange_item.get_name() == item:
                    itf_item = exchange_item
                    exists = True
            
            if not exists:
                data.get_owned_exchange_items().add(itf_item)
1 Like

Yes you should use the get_owned_exchange_items() method. To be more precise, you are calling a Java method on a Python object. That’s why Python complains about the method that doesn’t exist. To navigate between the Java and Python world, you can have a look at the tips and tricks from the documentation.

Thank you both very much - my script now runs smoothly :slight_smile:

Except in the end there is still this error twice in the ErrorLog:
Plug-in: org.eclipse.ease.lang.python

The method name “zip” from the module “__EASE_MOD_org_eclipse_ease_modules_platform_ResourcesModule” can not be wrapped because it’s name is reserved


What ist its meaning? Is it of importance?

You can ignore this message. Are you using Python for Capella 1.4.0 ?

yes, with Capella 7.0.1

Related Question:
I want to generate the ExchangeItems in an InterfacePkg.

Is it possible to show the popup menue item only when the mouse-right-click is done on an InterfacePkg?

# popup: enableFor(org.polarsys.capella.core.data.capellacore.CapellaElement)

This magic header entry shows the menue on all Capella-Elements…

OK, I think I missed something or it might be dependent on the Python version… but in any case you can ignore it.

Yes you need to use the Java class qualified name. You can get it by selecting the element and using the Sirius interpreter view with the following AQL expression:

aql:self.eClass().instanceClassName

or in the M2Doc interpreteur view:

selection.eClass().instanceClassName

This should output:

org.polarsys.capella.core.data.cs.InterfacePkg

yes, that is what I tried
# popup : enableFor(org.polarsys.capella.core.data.cs.InterfacePkg)

but the popup menue item still shows up on every Capella Item.
Function, Capability, even on an aird where no other of the sample scripts popups are shown.

The error-message may be triggered by include line in my script…

# include needed for the Capella modeller API

include('workspace://Python4Capella/simplified_api/capella.py')

if False:

    from simplified_api.capella import *

I know EASE magic header can be affected by the order of the lines in it (check the order against this script for instance). You might also want to check if you have other script with the same header that apply on other types of object (after a copy for instance).

I have inserted an empty line with “#” and a comment line “# This Script…“ after the popup - now it ist working…
That is really a magic header ;-> Thx"!

# name  : Import ExchangeItems from Excel
# image : platform:/plugin/org.polarsys.capella.core.sirius.analysis/icons/full/obj16/ExchangeItemFlow.gif
# script-type : Python
# description : Imports ExchangeItems from Excel ('Import_Informations/Structure_Informations.xlsx')
# popup       : enableFor(org.polarsys.capella.core.data.cs.InterfacePkg)
#
# This Script ...
1 Like