Exporting Values with Python4Capella

There is an example in the Python for Capella project explaining how to get property values.
I am going to have a look at the previous item (getting values from the Mass Viewpoint)
Stephane

Here you go:

def get_mass_extension (capella_element):
capella_element_part = capella_element.get_java_object().getRepresentingParts().get(0)
extensions = capella_element_part.getOwnedExtensions()
if extensions.size() > 0 :
for ext in extensions:
if (ext.eClass().getName() == “PartMass”):
return ext
return None

And from there you can get the values by calling getValue() and getMaxValue() from the return object

Stephane Lacrampe
Obeo Canada

Thank you! That was very useful.
I am sorry to bother you again but i realized that maybe I’m doing something wrong with property values, because actually my script works but PVMT.get_p_v_value(sf, pvName) gives me always None as output .
So, to recap, I have created 3 float property values and I have applied them, on the extensions section, to some NodePC. Despite that what I obtain is:
(Node Property Value)
NodePC1 None
NodePC2 None
NodePC3 None

Also the sample script provided in capella gives me the same output (for functions this time)
I honestly don’t know what I am doing wrong, thank you for any help.

Can you provide your script and a screenshot of your model with property values?

Here it is the 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 the PVMT API

include(‘workspace://Python4Capella/simplified_api/pvmt.py’)
if False:
from simplified_api.pvmt import *

# include needed for the requirement API

include(‘workspace://Python4Capella/simplified_api/requirement.py’)
if False:
from simplified_api.requirement import *

include needed for utilities

include(‘workspace://Python4Capella/utilities/CapellaPlatform.py’)
if False:
from utilities.CapellaPlatform import *

aird_path = ‘/Toy_catapult/Toy_catapult.aird’

model = CapellaModel()
model.open(aird_path)

se = model.get_system_engineering()

allPC = se.get_all_contents_by_type(NodePC)

allPVs = [‘Value_1’,‘Value_2’,‘Value_3’,‘Value_4’,‘Value_5’]

for pc in allPC:
for pvName in PVMT.get_p_v_names(pc):
if pvName not in allPVs:
allPVs.append(pvName)

for pc in allPC:
print(pc.get_name())
for pvName in allPVs:
print(pvName,PVMT.get_p_v_value(pc, pvName))

And the model:

As you can see it’s just an easy use case.
Value_1 Value_2 and_Value_3 have been created in the system analysis while Value_4 and Value_5 in physiscal Architecture. From the mass editing view you can see how this properties are applied.
The script here is the one used to retrieve property values associated with NodePCs, The one used to get property values associated with functions is the one provided in the sample script folder (without the exportation).
The results are always of this type:

Base
Value_1 None
Value_2 None
Value_3 None
Value_4 None
Value_5 None
Spring
Value_1 None
Value_2 None
Value_3 None
Value_4 None
Value_5 None
Container
Value_1 None
Value_2 None
Value_3 None
Value_4 None
Value_5 None
Pin/Trigger
Value_1 None
Value_2 None
Value_3 None
Value_4 None
Value_5 None
Mechanism
Value_1 None
Value_2 None
Value_3 None
Value_4 None
Value_5 None

Looking at your model, your property values are not created using PVMT. So either you use PVMT, or if you want to create your own PV, then I think the script would work better if you first create a “Property Value Group” and then a property value inside it.
I am not sure, though, as I can see only a partial view of your model.

Hello,
I am opening again the discussion in this topic because with your suggestions at the end the script worked perfectly, now I am trying to do the same but with property values associated with Logical Components. So I have created a similar script but it is not printing anything and I do not know why.



Furthermore it does not makes sense to me why it is not printing at least the names of logical components, if I use the sample script for listing logical components the output in the console is this:

Another thing is that I do not know why this sample script is printing just the “aircraft” LC and not all the others LC within it (for instance “airframe”).
Anyway I should be able to print at least the property values of the “aircraft” logical component but this is not happening and nothing is printed in the console.

I have to say that now I am using Capella 6.0 while last time I used Capella 5.2

From the execution output I would say there are no LogicalComponent in the TMS_test_5 project. But by default all projects have at least one LogicalComponent… The second loop should at least print the name of the LogicalComponent even is no PV is found. So I’m not sure what is going wrong here. Maybe check the implementation of EObject.get_all_contents_by_type() and EObject.get_class().

The sample script only list the first level of LogicalComponent. To print all components you need to add a recursive function.

def list_logical_components_in_pkg(logical_component_pkg):
    for lc in logical_component_pkg.get_owned_logical_components():
        #: :type lc: LogicalComponent
        list_logical_components_in_component(lc)
        for pkg in lc.get_owned_logical_component_pkgs():
            list_logical_components_in_pkg(pkg)

def list_logical_components_in_component(logical_component):
        print(" - " + logical_component.get_name())
        if not isinstance(logical_component, LogicalActor):
            for child in logical_component.get_owned_logical_components():
                #: :type child: LogicalComponent
                list_logical_components_in_component(child)
            for pkg in logical_component.get_owned_logical_component_pkgs():
                list_logical_components_in_pkg(pkg)
    


# print the name of each LogicalComponent
list_logical_components_in_pkg(se.get_logical_architecture().get_logical_component_pkg())

Thank you for your help in listing the logical components.
Regarding EObject.get_all_contents_by_type() and EObject.get_class() I found differces between the capella.py code on github and the one implemented in Capella on my device:



As you can see the I found discrepancies in get_class and get_all_contents, so I replaced the scipts in the simplified_api and in utilities with the scripts found on github, anyway my attempt to print property values associated with logical components failed again and the output is always the same.
What should I do? Should I dowload a different version of Python4Capella?

Since you are using Capella 6.0.0, you should use Python4Capella 1.1.0. You can also make sure your model is in version 6.0.0 but I’m pretty sure Capella would complain if it was not the case.

Yes I am using Python4Capella 1.1.0 and the model is in Version 6.0.0.
I tried to use that script also with the sample “In-Flight entertainment system” model but the script output is always the same, so it is not printing at least one logical component.

Can you provide your Capella project an Python script ? I’ll try it locally see if I can figure out what’s going wrong here…

The model:
TMS_test_5.zip (210.9 KB)
The script:

include needed for the Capella modeller API

include(‘workspace://Python4Capella/simplified_api/capella.py’)
if False:
from simplified_api.capella import *

include needed for the PVMT API

include(‘workspace://Python4Capella/simplified_api/pvmt.py’)
if False:
from simplified_api.pvmt import *

include needed for utilities

include(‘workspace://Python4Capella/utilities/CapellaPlatform.py’)
if False:
from utilities.CapellaPlatform import *

include needed to read/write xlsx files

from openpyxl import *

include needed for the requirement API

include(‘workspace://Python4Capella/simplified_api/requirement.py’)
if False:
from simplified_api.requirement import *

aird_path = ‘TMS_test_5/TMS_test_5.aird’

model = CapellaModel()
model.open(aird_path)

se = model.get_system_engineering()

allLC = se.get_all_contents_by_type(LogicalComponent)

allPVs = []

print(‘start’)

for lc in allLC:
for pvName in PVMT.get_p_v_names(lc):
if pvName not in allPVs:
allPVs.append(pvName)

for lc in allLC:
print(lc.get_name())
for pvName in allPVs:
print(pvName,PVMT.get_p_v_value(lc, pvName))

print(‘end’)

Thank you very much for the help!

Sorry the copy/paste did not maintain the format so here it is the code in a text file

script.txt (1.2 KB)

I installed a new Capella 6.0.0 with the PVMT update site (not the droppins) and Python4Capella 1.1.0. I imported your project and your script and it works:

I guess you have an issue with your installation. Also I’m using Python 3.7.

Using a new Capella 6.0.0 and the latest releases of the PVMT add-on and Python for capella, and also using Python 3.9 interpreter the script finally worked, I do not why I was having troubles before but anyway thanks for the help!

1 Like

Hi, I am using this topic again because I have an issue with one of the scripts that you provided me before in this conversation.
I have used this script to extract attributes and values of requirements:
for req in se.get_all_contents_by_type(Requirement):

    # Extracts Requirement Type
    if req.get_java_object().getRequirementType() != None:
        print("Type: "+req.get_java_object().getRequirementType().getReqIFLongName())
    
    # Extracts Requirements Attributes and Values
    if req.get_java_object().getOwnedAttributes() != None:
        for att in req.get_java_object().getOwnedAttributes():
            print("- Attribute: "+att.getDefinition().getReqIFLongName()+", value: "+str(att.getValue()))

With Capella 5.2 it was working but now on Capella 6.0 and a new model this error is being printed:

					org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
					  File "workspace://test_1/test_6.py", line 99, in <module>
					    if isinstance(value, integer_types):
					  File "C:\Users\Utente\Desktop\capella test 2\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\java_gateway.py", line 1321, in __call__
					    return_value = get_return_value(
					  File "C:\Users\Utente\Desktop\capella test 2\capella\plugins\py4j-python_0.10.9.5-bnd-2odeag\src\py4j\protocol.py", line 330, in get_return_value
					    raise Py4JError(
					py4j.protocol.Py4JError: An error occurred while calling o9779.getValue. Trace:
					py4j.Py4JException: Method getValue([]) does not exist
						at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
						at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
						at py4j.Gateway.invoke(Gateway.java:274)
						at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
						at py4j.commands.CallCommand.execute(CallCommand.java:79)
						at py4j.ClientServerConnection.sendCommand(ClientServerConnection.java:244)
						at py4j.CallbackClient.sendCommand(CallbackClient.java:384)
						at py4j.CallbackClient.sendCommand(CallbackClient.java:356)
						at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:106)
						at jdk.proxy15/jdk.proxy15.$Proxy28.executeScript(Unknown Source)
						at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.internalExecute(Py4jScriptEngine.java:240)
						at org.eclipse.ease.lang.python.py4j.internal.Py4jScriptEngine.execute(Py4jScriptEngine.java:227)
						at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:189)
						at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:242)
						at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

It seems that with some of the “req.get_java_object().getOwnedAttributes()” is not possible to use getValue, I tried also get_Value and getvalue and a few other functions but it didn’t work either

I think this is related to this issue:

Yes that was the problem, thank you!

1 Like