Change/add PropertyValue values via Script

Hi,

I’m currently trying to set property values of a specific component based off of an excel spreadsheet.
I currently extract all components in the model and all property value groups + values associated with each component. However, I make edits to the spreadsheet and due to the sheer number of changes, it’s impractical to go through using the project explorer to search for the specific component and change the property values 1 by 1. I’ve looked at the “get_p_v_value()” function from the PVMT API and somewhat reverse engineered the script to allow me to set values but it’s not behaving the way it’s intended - which I suspect is because I’m only changing the top level of information.

The code I’ve written below is able to change the value assigned to each property value somewhat successfully.

for x in se.get_physical_architecture().get_all_contents_by_type(PhysicalComponent):
# Prints the component name
print(x.get_name())
# Checks the owned property value groups of each component
for test in x.get_java_object().getOwnedPropertyValueGroups():
# Checks each owne property value for each property value group
for a in test.getOwnedPropertyValues():
#if the name of the property value is allocation2
if a.getName() == “allocation2”:
# if the value is none try to set the name’s value to test1
if a.getValue() is None:

                try:
                    a.getValue().setName("test1")
                except:
                    pass
            # else set it to test2
            else:
                try:
                    a.getValue().setName("test2")
                except:
                    pass
        # for all other property values, set the name to test3
        else:
            try:
                a.getValue().setName("test3")
            except:
                pass

image

Running this script will change the value to test1, test2 or test3 almost randomly and sometimes even set all values of allocation1, allocation2, allocation3 to test3 ignoring the logic I’ve set. Is there another method I should look into in order to correctly change the property values as I only want to change the property value if the name == allocation2? Is it also therefore possible to assign known property value groups to components based on a script too?

1 Like

Hi Roy,

To do this I used the script below, which seems to work for me. The main differences are the names of some functions (like get_value instead of getValue).

for comp in se.get_physical_architecture().get_all_contents_by_type(PhysicalComponent):
print(comp.get_name())

for group in comp.get_owned_property_value_groups():
    print(group.get_name())
    for prop in group.get_owned_property_values():
        
        if prop.get_name() == "allocation2":
            
            if prop.get_value() is None:
                try:
                    prop.set_value("test1")
                except:
                    pass
            else:
                try:
                    prop.set_value("test2")
                except:
                    pass
        else:
            try:
                prop.set_value("test3")
            except:
                pass

What I can’t really figure out, though, is why running your script randomly sets the values of the properties.

Also, regarding your second question on a script to assign known property value groups to components, I don’t really know. Maybe it is possible to modify the groups, scopes, rules etc. using Python, but I would need to take a further look at that before I am able to reply.

All the best,

Martin
ObeoSoft Canada

Hi Martin,

I managed to write the actual value by taking the actual enumeration property value. I assume I should be able write values in a similar fashion to other components/elements.

But thank you for the response!