Logical component generation using python4 capella

Hi all,
I am using the .add() method on a logical component object to update the logical architecture of a (currently empty) Capella model. I am successfully adding the logical components to the Capella model and these appear in the Capella model when I open the project. However these logical components (or coffee machine in my example below) do not have any entries in their “representing parts” or “Typed Elements” properties, nor do they appear in their parents “contained parts” or “sub logical components” properties.
As a result, the logical components I add to the model in this way can’t be added to any Logical Architecture Diagrams [LAB]. Is there a workaround for this?

This is a stripped-down version of the script I am using that should recreate the issue:

# include needed for the Capella modeller API
include('workspace://Python4Capella/simplified_api/capella.py')
if False:
    from simplified_api.capella import *

ws = {'Coffee_Machine'}


model = capella.CapellaModel()
model.open('/blank_project/blank_project.aird')

# gets the SystemEngineering
se = model.get_system_engineering()

# get the root logical component
la_ls = se.get_logical_architecture().get_logical_system()

# start a transaction to modify the Capella model
model.start_transaction()
try:
    # create physical components with the list of names in the components list
    for component in ws:
        # create a logical component
        lc = LogicalComponent()
        #set its name
        print("* Create a new logical component with name " + component)
        lc.set_name(component)
        #add the new PhysicalComponent
        la_ls.get_owned_logical_components().add(lc)
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()

Thanks for any help with this

I suspect that instead of using:

la_ls = se.get_logical_architecture().get_logical_system()

You should use:

la_ls = se.get_logical_architecture().get_logical_component_pkg()

Stephane Lacrampe
ObeoSoft Canada

Hi Stephane,
thanks for the help, I have made that change but sadly no luck. Are there any available functions for specifically defining typed elements?

my current code is:

# 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 *

ws = {'Coffee_Machine'}


model = CapellaModel()
model.open('/blank_project/blank_project.aird')

# gets the SystemEngineering
se = model.get_system_engineering()

# get the root logical component
la_ls = se.get_logical_architecture().get_logical_component_pkg()

# start a transaction to modify the Capella model
model.start_transaction()
try:
    # create physical components with the list of names in the components list
    for component in ws:
        # create a logical component
        lc = LogicalComponent()
        #set its name
        print("* Create a new logical component with name " + component)
        lc.set_name(component)
        #add the new PhysicalComponent
        la_ls.get_owned_logical_components().add(lc)
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()
# get the rootlogicalComponent
la_ls = se.get_logical_architecture().get_logical_system()
for lc in la_ls.get_owned_logical_components():
    print(f'logical components owned by the logical system: {lc.get_name()}')

There is no API to manipulate Part in the Python API. You can use the Java API to do something like:

java_part = create_e_object_from_e_classifier(get_e_classifier("http://www.polarsys.org/capella/core/cs/" + capella_version(), "Part"))
java_part.setType(lc.get_java_object())
la_ls.get_java_object().getOwnedParts().add(java_part)

Hi YvanLussaud,
thanks for your help, where should I import the create_e_object_from_e_classifier function from (I don’t seem to be able to see it in the python4capella packages)?
Or is this something I need to define myself?

Yes sorry, it should be:

java_part = create_e_object("http://www.polarsys.org/capella/core/cs/" + capella_version(), "Part")

Great thanks very much, that all works now for me