List logical components with python4Capella in logical architecture diagram

Hi all, I am successfully importing the logical components with the sample_scripts by Python4Capella in Figure 1. However, as shown in Figure 2, we would prefer to list all elements from the logical architecture diagram ([LAB] IFE System - All Components, CEs), which means that the logical elements should also include the blue parts in the diagram.


The following scripts obviously cannot achieve this goal, and I am not sure how to modify them.

print the name of each LogicalComponent

for lc in se.get_logical_architecture().get_logical_component_pkg().get_owned_logical_components():
#: :type lc: LogicalComponent
print(" - " + lc.get_name())

You can use a nested for loop and the functions model.get_all_diagrams() and get_represented_elements() to achieve this.

diagram_name = "some diagram name"
for diag in model.get_all_diagrams():
    if diag.get_name() == diagram_name:
        for comp in diag.get_represented_elements():
            print(comp.get_name())

That will get you all items on the diagram, you can then filter with some if conditions to obtain just logical components.

1 Like

To filter LogicalComponent you can add a a if:

diagram_name = "some diagram name"
for diag in model.get_all_diagrams():
    if diag.get_name() == diagram_name:
        for comp in diag.get_represented_elements():
            if isinstance(comp, LogicalComponent):
                print(comp.get_name())

Thank you for your reply, but I still couldn’t get the desired result. After running these scripts, it still remains the same as before. Could you please explain the reason?

Thank you for your reply, but I still couldn’t get the desired result. After running these scripts, it still remains the same as before. Could you please explain the reason?

Are you using the last version of Python4Capella (1.2.0)?

I’m using Python4Capella 1.2.0 with Python 3.11 and Capella 6.1.0 and I get the following result:

The script:

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

# aird_path = '/IA/IA.aird'
aird_path = '/In-Flight Entertainment System/In-Flight Entertainment System.aird'
model = CapellaModel()
model.open(aird_path)

# gets the SystemEngineering and print its name
se = model.get_system_engineering()

diagram_name = "[LAB] IFE System - All Components, CEs"
for diag in model.get_all_diagrams():
    if diag.get_name() == diagram_name:
        for comp in diag.get_represented_elements():
            if isinstance(comp, LogicalComponent):
                print(comp.get_name())

The output in the console:

Aircraft Front Servers
Aircraft Interface
Aircraft-Specific Aircraft Interface
Core Aircraft Interface
Media Server
Streaming Server
Applications Server
Cabin Terminal
Cabin Terminal Interactions Manager
Cabin Terminal Airline-Specific Interactions Manager
Cabin Terminal Core Interactions Manager
Seat TV
Seat TV Audio Video Player
Seat TV Interactions Manager
Seat TV Core Interactions Manager
Seat TV Airline-Specific Interactions Manager
Seat TV Services Manager
Cabin Screen
1 Like

Yes, the result you obtained is what I wanted.
I’m using Python4Capella 1.2.0 with Python 3.7.4 and Capella 6.1.0.
Thank you very much. I’m not quite sure where the problem lies here. I will try again according to your suggestion.

What if you try to just print the components on the diagram to see what’s happening? so before you put your if condition, just add a print statement to view everything on the diagram, maybe there’s something not being caught.

diagram_name = "[LAB] IFE System - All Components, CEs"
for diag in model.get_all_diagrams():
    if diag.get_name() == diagram_name:
        for comp in diag.get_represented_elements():
            print(f"element: {comp}, name: {comp.get_name()})
            #if isinstance(comp, LogicalComponent):
            #    print(comp.get_name())

Thank you very much for your reply. I have tried your suggestion, but there are some errors here. In addition, according to Yvan Lussaud’s suggestion, the script ran in my Capella software without any output.

The errors in the console:
org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
File “C:\Users\onlyf\AppData\Local\Programs\Python\Python37\lib\ast.py”, line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File “”, line 55
print(f"element: {comp}, name: {comp.get_name()})
^
SyntaxError: EOL while scanning string literal

Thank you again for your suggestion. I have chosen the same Python 3.11 as you, but it still doesn’t work. Do you have any other suggestions?

Do you have any error messages, also check the error view ? Also try to comment the if filter as suggested by @Roy something might help to figure out what is going wrong in your case.

Thank you, the problem may arise below. the console does not list all diagramdiagrams.

diagram_name = “[LAB] IFE System - All Components, CEs”
for diag in model.get_all_diagrams():
print(diag.get_name())

I get why the list of diagrams is empty… This API call a Java method and its code hasn’t changed lately. So I’m not sure what can cause this.

Can you try to call the Java API see if you get any diagram:

for diag in JavaList(org.eclipse.sirius.business.api.dialect.DialectManager.INSTANCE.getAllRepresentationDescriptors(model.session),Diagram):
    print('- ', diag.get_name())

You can ignore the error on the or symbol. If you want to remove it you can follow this documentation.

1 Like

You did a great job, and I finally realized it. Thank you again.

But I’m not sure why you can implement it in Python there. If I were like you, where would I need to modify it?

That’s the same Java code that is called via the Python API… I don’t understand why it is not working in your case. But the workaround works which is great.

You could change the implementation of CapellaModel.get_all_diagrams() but I’m not sure it’s a good idea for moving to newer versions of Python4Capella without breaking your code.

Okay, great. I’m really happy to receive your help.
Below is a screenshot of the successful outcome.

1 Like