Write data to your model#

Note

To avoid performance issues, don’t forget to use Transaction mode. See the bottom of this page for details.

Update a feature value#

You have two functions for updating the value of a feature:

Function set_value()#

The set_value() function supports all primitive types:

>>> myFeature.set_value(True)
>>> myFeature.get_value()
True
>>> myFeature.set_value(10)
>>> myFeature.get_value()
10
>>> myFeature.set_value("Hello")
>>> myFeature.get_value()
Hello
>>> myFeature.set_value(10.5)
>>> myFeature.get_value()
10.5

The model updates after you set all values to ensure accuracy.

Function parse_and_set_value()#

The parse_and_set_value() function handles more complex expressions:

>>> myFeature.parse_and_set_value("10 [m]")
>>> myFeature.get_value()
(10, "m")
>>> myFeature.parse_and_set_value("2 + 10 [kg]")
>>> myFeature.get_value()
Exception UnsupportedValueExpression raised

Create new elements#

Use the Factory class to create new elements in your model.

Tip

For a comprehensive example, see Create a new element.

Create a Factory instance#
factory = Factory(project, ansyssysml2apiconnector)

Use the create_<element_type>() method to create a new model element. Provide the element type and any number of keyword arguments representing its attributes:

new_attribute_usage = factory.create_attribute_usage(
    name="new_attribute_usage",
)

This creates a new AttributeUsage element at the root of your project. The create_<element_type>() method returns the newly created element.

Create a new AttributeUsage element with owner#
new_bicycle_frame_length = factory.create_attribute_usage(name="length", owner=bike.frame)

new_bicycle_frame_length.parse_and_set_value("60 [cm]")

This creates a new AttributeUsage element with the specified attributes inside the Bike frame.

Note

The list of accepted attributes depends on the element type you are creating. For example, name, owner, shortName, and others are defined by the metamodel.

You can also assign a value directly when creating the element. There are two ways:

  • value=... for simple values (such as numbers).

  • expression="..." for values with units or expressions.

new_bicycle_frame_length_with_value = factory.create_attribute_usage(
    name="lengthWithValue",
    owner=bike.frame,
    value=60
)

new_bicycle_frame_length_with_expression = factory.create_attribute_usage(
    name="lengthWithExpression",
    owner=bike.frame,
    expression="60 [cm]"
)
new_bicycle_frame_length_with_value = factory.create_attribute_usage(
    name="lengthWithValue",
    owner=bike.get("frame"),
    value=60
)

new_bicycle_frame_length_with_expression = factory.create_attribute_usage(
    name="lengthWithExpression",
    owner=bike.get("frame"),
    expression="60 [cm]"
)

This lets you set values directly at creation time, depending on your data format.

Update attributes directly#

Update element properties directly using simple assignment. This is useful for quickly changing properties like names.

>>> my_attribute = factory.create_attribute_usage(name="OriginalName")
>>> my_attribute._name = "New Name"
New Name
>>> my_attribute = factory.create_attribute_usage(name="OriginalName")
>>> my_attribute.name = "New Name"
New Name

Moving elements#

Use append() - on the owned element property - to move an element to a different container. The element is automatically removed from its current container.

new_container._owned_element.append(element_to_move)
new_container.owned_element.append(element_to_move)

Removing elements#

If you use the remove() method of the owned element property, the element is deleted from the model.
Please use this with caution: if a diagram displays this removed element, the diagram displays errors.

Transaction mode#

When you perform write operations, the model is updated after each operation to ensure accuracy. However, if you want to perform multiple write operations without intermediate updates, you can use the transaction mode. In transaction mode, the model updates only after you complete all your operations. This can improve performance when making multiple changes to the model, but be aware that the model does not reflect any changes until you exit the transaction mode.

my_bike_project.start_transactional_mode()

bike.frontWheel.rim.weight.parse_and_set_value("0.5 [kg]")
bike.rearWheel.rim.weight.parse_and_set_value("0.8 [kg]")

my_bike_project.stop_transactional_mode()
my_bike_project.start_transactional_mode()

bike.get("frontWheel").get("rim").get("weight").parse_and_set_value("0.5 [kg]")
bike.get("rearWheel").get("rim").get("weight").parse_and_set_value("0.8 [kg]")

my_bike_project.stop_transactional_mode()
Previous step

Read your model

Read your model
Next step

Work with diagrams

Work with diagrams