Creation of physical ports with python4capella

Hello python4capella community! I’m trying to generate physical ports on a generic physical component, but every time I try it I get several errors. There are many predesigned scripts but none of them create several physical ports on a physical component, can you give me a hand, has anyone tried it before? Thanks in advance.

Hi Alex,

There is no Python API for this but we can call the Java API from Python:

pc = PhysicalComponent()
pp = PhysicalPort()

pc.get_java_object().getOwnedFeatures().add(pp.get_java_object())
org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(pp.get_java_object())

You will have an error on the org symbol because it’s not recognized by PyDev but will be accessible at runtime. You can remove this error.

Note: you need to make all your changes in a transaction.

1 Like

Thank you very much for your quick response! This code works fine, but I’m unable to achieve the same result that by doing it manually.

My code is:

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

aird_path = "/TEST/TEST.aird"
xlsx_path = "/Python4Capella/InputFiles/Components.xlsx"

model = CapellaModel()
model.open(aird_path)
se = model.get_system_engineering()
pc_pkg = se.get_physical_architecture().get_physical_system()

# start a transaction to modify the Capella model
model.start_transaction()

try:
    pc = NodePC()
    pc.set_name("Comp1")
    pc_pkg.get_owned_physical_components().add(pc)
    
    pp = PhysicalPort()
    pc.get_java_object().getOwnedFeatures().add(pp.get_java_object())
    org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(pp.get_java_object())
    
except:
    # if something went wrong we rollback the transaction
    model.rollback_transaction()
    raise
else:
    # if everything is ok we commit the transaction
    model.commit_transaction()

# save the Capella model
model.save()

This code creates a physical component (Node) with one physical port. My problem (using Capella 5.1) is that I’m unable to see the proper graphical representation in a [PAB], I cannot see the graphical representation of the physical port itself and the proper relation in the semantic browser. So I don’t get the same effect that doing it manually, I tried everything but I just dont get how to do it. Do you have any idea why this is happening?

Thanks in advance!

1 Like

Can you try to manually refresh your diagram ? I’m not sure if it’s enough for creating the visual representation of the port.

1 Like

Yes it worked! Thank you very much for your fast response!

1 Like