Retrieve selected element raises attribute error

Hello,

I’m trying to retrieve a selected element from a model and then modify its formatting.

I use the following code:

# include needed for the capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
    from simplified_api.capella import *
    
# include needed for utilities
include('workspace://Python4Capella/utilities/CapellaPlatform.py')
if False:
    from utilities.CapellaPlatform import *
    
    
# SAM_path = '/SAMSEM/SAMSEM.aird'
# aird_path = SAM_path


# retrieve the Element from the current selection and its aird model path
selected_elem = CapellaElement(CapellaPlatform.getFirstSelectedElement())
aird_path = '/' + CapellaPlatform.getModelPath(selected_elem)



# instantiate Capella model
model = CapellaModel()
model.open(aird_path)

# get the SystemEngineering and print its name
se = model.get_system_engineering()
print('Model accessed:'+ se.get_name())

# start transactions to make modifications in the model
model.start_transaction()

try:
    #do the work
    print('I am trying')
except:
    # if something went wrong rollback the transaction
    model.rollback_transaction()
else:
    # if everything is ok commit the transaction
    model.commit_transaction()
    
# model.save()

When I run this, get the following error:

org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
  File "workspace://Python4Capella/MBSEcodes/paperUtils.py", line 17, in <module>
    # sys.argv[2:] - optional - paths to prepend on sys.path
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "workspace://Python4Capella/simplified_api/capella.py", line 336, in __init__
AttributeError: Passed object is not compatible with CapellaElement: L/Python4Capella/MBSEcodes/paperUtils.py

Any help on how to solve this issue?
Also, I would appreciate any help around how to access the element’s formatting attributes, e.g., its name font size?

Regards,

When you started your script the selected element was the script itself (its workspace resource) L/Python4Capella/MBSEcodes/paperUtils.py. And you tried to wrap it to a CapellaElement Python class, each constructor has a check to make sure only compatible elements are passed to the constructor (either Python or Java objects).

To use the current selection you probably want to run you script from a popup menu using an EASE magic header at the beginning of your script:

# name                 : paperUtils.py
# script-type          : Python
# description          : paperUtils.py
# popup                : enableFor(org.polarsys.capella.core.data.capellacore.CapellaElement)

Then select an element from the Capella model in the package explorer (resource tree on the left). Right click on it and select the paperUtils.py menu. You can learn more in the tips and tricks section of the documentation. There is also some examples in the sample scripts, for instance this one.

Thanks Yvan,

I was able to create the popup menu, however, what I’m trying to do is to modify the formatting of certain element’s representation in a certain diagram. So I wanted to select the element representation form the diagram then access its formatting then modify it, let’s say modify the font size.

Any guidance on how to that?

When I right click the element representation in a diagram and select “paperUtils”, I get the following error:

org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
  File "file:///Users/ea/Library/CloudStorage/OneDrive-UNSW/Researchia/CSC_works/MBSE/MBSEcodes/paperUtils.py", line 20, in <module>
    import code as _pyease_code
                    ^^^^^^^^^^^^
  File "workspace://Python4Capella/simplified_api/capella.py", line 336, in __init__
AttributeError: Passed object is not compatible with CapellaElement: DNodeEditPart( org.eclipse.gmf.runtime.notation.impl.NodeImpl@3b8a1d56 (visible: true, type: 2001, mutable: false) )

I understand now that the selection from diagram is not a CapellaELement, but I have no clue how to access the right type?

I tried the following snippet:

specific_cls = EObject.get_class(CapellaPlatform.getFirstSelectedElement())
selected = None
if specific_cls is not None:
    selected = specific_cls(CapellaPLatform.getFirstSelectedElement())
    
print(specific_cls)

but that didn’t work :frowning:

1 Like

There is no API to do that with Python4Capella, but you can still can the Java API from your python script. In this thread you will find how to declare your menu or elements from a diagram. You can then check this thread to change the color of an element. For the layout you can check his thread.

If you change the style of a diagram element for instance its color, you will also need to mark the feature as customized, otherwise it will be reseted after a refresh of the diagram:

    representation_element.getOwnedStyle().setStrokeColor(color)
    representation_element.getOwnedStyle().getCustomFeatures().add("strokeColor")

an other example to set a workspace image and change the label position:

    style = representation_element.getOwnedStyle()
    helper = org.eclipse.sirius.diagram.ui.business.api.image.WorkspaceImageHelper()
    helper.updateStyle(style, "In-Flight Entertainment System/music.svg")
    style.setShowIcon(True)
    style.setLabelPosition(org.eclipse.sirius.diagram.LabelPosition.NODE_LITERAL)

The PyDev editor will complain that some symbols are not recognized (for instance org), but as runtime EASY will find them. You can remove this error by following this documentation.