Create a new element#

Make sure that you have access to a valid server and a project containing the Bike structure. If you do not have a project containing the Bike structure, perform the following steps:

  1. Download the model: Bike Model.

  2. Open the SAM editor in your browser and select the desired organization (for example, MyOrga).

  3. Select New Project > SysML V2 > Import File.

  4. Select Choose File for the File to import option.

  5. Select the bike.xmi file that you just downloaded. The project name is automatically set to bike.

  6. Click Import and wait for the project to load.

Create an attribute usage element for the bicycle frame length#

The following code shows how to create a new AttributeUsage element inside the Bike structure and assign it a value.

Note

You need to replace the organization ID, server URL, and token with your own data. For more information, see Find information.

Create a new AttributeUsage element using the Factory class#
 1# Copyright (C) 2024 - 2026 ANSYS, Inc. and/or its affiliates.
 2# SPDX-License-Identifier: MIT
 3#
 4#
 5# Permission is hereby granted, free of charge, to any person obtaining a copy
 6# of this software and associated documentation files (the "Software"), to deal
 7# in the Software without restriction, including without limitation the rights
 8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22"""Creating element example for PySAM SysML2."""
23
24import requests
25from urllib3.exceptions import InsecureRequestWarning
26
27from ansys.sam.sysml2 import AnsysSysML2APIConnector, SysML2ProjectManager
28from ansys.sam.sysml2.tools import Factory
29
30# Used to disable warnings
31requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
32
33# Create your connector for the SAM server
34ansyssysml2apiconnector = AnsysSysML2APIConnector(
35    server_url="<SAM Server URL>",  # Your SAM server base URL
36    organization_id="<Orga ID>",  # The organization ID
37    token="<Token>",  # Your authorization token
38    use_ssl=False,  # If the server hasn't a valid SSL
39)
40
41project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
42project = project_manager.get_scripting_project("<Bike Project ID>")
43
44bike = project.get_root_package().Structure.Bike
45
46factory = Factory(project, ansyssysml2apiconnector)
47
48new_bicycle_frame_length = factory.create_attribute_usage(name="length", owner=bike.frame)
49
50new_bicycle_frame_length.parse_and_set_value("60 [cm]")
51
52print(project.get_root_package().Structure.Bike.frame.length.get_value())
Create a new AttributeUsage element using the Factory class#
 1# Copyright (C) 2024 - 2026 ANSYS, Inc. and/or its affiliates.
 2# SPDX-License-Identifier: MIT
 3#
 4#
 5# Permission is hereby granted, free of charge, to any person obtaining a copy
 6# of this software and associated documentation files (the "Software"), to deal
 7# in the Software without restriction, including without limitation the rights
 8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22"""Creating element static example for PySAM SysML2."""
23
24import requests
25from urllib3.exceptions import InsecureRequestWarning
26
27from ansys.sam.sysml2 import AnsysSysML2APIConnector, SysML2ProjectManager
28from ansys.sam.sysml2.tools import Factory
29
30# Used to disable warnings
31requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
32
33# Create your connector for the SAM server
34ansyssysml2apiconnector = AnsysSysML2APIConnector(
35    server_url="<SAM Server URL>",  # Your SAM server base URL
36    organization_id="<Orga ID>",  # The organization ID
37    token="<Token>",  # Your authorization token
38    use_ssl=False,  # If the server hasn't a valid SSL
39)
40
41project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
42project = project_manager.get_sysml_project("<Bike Project ID>")
43
44bike = project.get_root_package().get("Structure").get("Bike")
45
46factory = Factory(project, ansyssysml2apiconnector)
47
48new_bicycle_frame_length = factory.create_attribute_usage(name="length", owner=bike.get("frame"))
49
50new_bicycle_frame_length.parse_and_set_value("60 [cm]")
51
52print(bike.get("frame").get("length").get_value())

You just created a new element and assigned a parsed value to it.

Note

You can also assign a value directly when creating the element, without using the set_value() or parse_and_set_value() method. 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.