Exporting Values with Python4Capella

I have created in Capella a few requirements and I have also created my own requirement type (with two attribute definitions: Value and Description), I have then tried to export these requirements through Python4Capella sample script: Export_list_of_Requirements_to_xlsx.py
I have seen that in the excel file the script writes just:
Req id, Req Text, Linked Elements (incoming links), Linked Elements (outgoing links).
Is there any way to export also the Value and the Description created as Attribute definitions in the Requirement type? This in order to have numerical values or strings in excel cells.
I have tried to modify the Export script and others scripts provided in the simplified_api to obtain those values but is not working because I have not found how to get to those data through Python4Capella.
In a similar way I have tried, unsuccessfully, to export the informations about the mass viewpoint (Value and Max Value), so i wanted to know if there is the possibility to export these informations.
Thank you!

Here is what should work for extracting Types and Values from your Requirements (req is a Requirement object):
# 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()))

Stephane Lacrampe
Obeo Canada

Thank you, now is working properly for requirements.
And regarding “part mass” informations, available with basic mass viewpoint, how do I retrieve those data? (for instance Value and Max Value)
Thank you for yor time

I have also applied a loop to an Exchange Scenario diagram and I have applied float property values to this loop, how do I get to these property values through 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.