Hi All,
I am trying to use Python4Capella to script the adding of new PhysicalComponents to a project as well as apply a pre-existing PropertyValueGroup. However after reading the forums, in particular this thread, I am still having issues that I don’t understand and can’t resolve. I have included as much of the code as I can (rather than snippets) to provide the complete context that I am using as I don’t seem to be able to reverse engineer it correctly from the existing threads on the forum.
The purpose of this example code it to build a simple, but complete ‘Hello World’ example. I have used a blank new Capella project and a single python script.
I have re-used the function to find the correct PropertyValueGroup definition from this thread in this example.
The code executes, however, I am left with a PropertyValueGroup under component that does not have the expected name, neither does the PropertyValue. The component also does not have the expected name.
I then added another string PropertyValue to the definition, saved the project and selected the ‘Global appy properties’ button. This resulted in the Property group I thought the script was adding to the new component being added to the new component rather that the additional PropertyValue being added to the PropertyValueGroup the script had added. Please see Figure 3 for the updated PropertyValueGroup Definition and Figure 4 for the actual result. I have also included Figure 5 for the desired result. Any help would be greatly appreciated.
Please see the screenshots and code blocks below:
Code Block 1 - The existing 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 utilities
include('workspace://Python4Capella/utilities/CapellaPlatform.py')
if False:
from utilities.CapellaPlatform import *
def get_property_value_group(capellaModel, propertyValuePkgName, propertyValueGroupName):
project = capellaModel.get_system_engineering().get_java_object().eContainer()
for pv_package in project.getOwnedPropertyValuePkgs().get(0).getOwnedPropertyValuePkgs():
if pv_package.getName() == propertyValuePkgName:
for pv_group in pv_package.getOwnedPropertyValueGroups():
print(pv_group)
if pv_group.getName() == propertyValueGroupName:
return PropertyValueGroup(pv_group)
# Initial steps, including getting the model
aird_path = "/TestProj/TestProj.aird"
model = CapellaModel()
model.open(aird_path)
# gets the SystemEngineering and the Physical System
se = model.get_system_engineering()
PhySystem = se.get_physical_architecture().get_physical_component_pkg().get_owned_physical_components().get(0)
# Test Script
try:
model.start_transaction()
#test to find the PVG
project = se.get_java_object().eContainer()
propertyGroupToApply = get_property_value_group(model, 'Domain', 'Extension')
pvg=PropertyValueGroup()
pvg.get_java_object().getAppliedPropertyValueGroups().add(propertyGroupToApply.get_java_object())
# also took the first one
for propertyValueToApply in propertyGroupToApply.get_owned_property_values():
pv = PropertyValue()
pv.get_java_object().getAppliedPropertyValues().add(propertyValueToApply.get_java_object())
pvg.get_owned_property_values().add(pv)
pvg.set_name("Domain.Extension")
pc = PhysicalComponent()
pc.set_name("A New Test Cmp")
PhySystem.get_owned_physical_components().add(pc)
org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(pc.get_java_object())
pc.get_owned_property_value_groups().add(pvg)
pc.get_java_object().getAppliedPropertyValueGroups().add(pvg.get_java_object())
pc.get_java_object().setNature(get_enum_literal("http://www.polarsys.org/capella/core/pa/" + capella_version(), "PhysicalComponentNature", "BEHAVIOR"))
pvg.get_java_object().getAppliedPropertyValueGroups().add(propertyGroupToApply.get_java_object())
org.polarsys.capella.core.model.helpers.CapellaElementExt.creationService(pvg.get_java_object())
pvg.get_java_object().getAppliedPropertyValueGroups().add(propertyGroupToApply.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()