Simplify SAM project initialization#

This example shows how to use the simplified AnsysSysML2Project / AnsysScriptingProject class to work with a SysML v2 project on SAM. This approach reduces boilerplate code by automatically initializing all necessary connectors. It explains how to perform these tasks:

  • Simplify project initialization with a single class.

  • Download diagrams (single and batch).

  • Create new elements.

  • Navigate through project diagrams.

  • Retrieve diagram information.

Note

The AnsysSysML2Project / AnsysScriptingProject class is specifically designed for SAM projects and automatically handles the initialization of these classes:

Prerequisites for simplified SAM#

Ensure that you meet these prerequisites:

  • A running SAM server instance.

  • A valid organization ID, project ID, and token.

  • The bike.xmi model imported into your project.

Simplified Python example#

Simplified example using AnsysScriptingProject for SAM projects#
 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"""Ansys SysML2 project example for PySAM SysML2."""
23
24import requests
25from urllib3.exceptions import InsecureRequestWarning
26
27from ansys.sam.sysml2.tools.ansys_scripting_project import AnsysScriptingProject
28
29requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
30
31project = AnsysScriptingProject(
32    server_url="<SAM Server URL>",  # Your SAM server base URL
33    organization_id="<Orga ID>",  # The organization ID
34    token="<Token>",  # Your authorization token
35    use_ssl=False,  # If the server hasn't a valid SSL
36    project_id="<Bike Project ID>",  # The project ID
37)
38
39diagrams_status = project.is_diagrams_available()
40print(f"Diagrams Status : {'Available' if diagrams_status else 'Unavailable'}", end="\n\n")
41
42SAVE_IMAGE_PATH = "The path of the directory you want to save your diagrams into"
43first_diagram = project.get_root_package().__diagram[0]
44first_diagram_id = first_diagram._id
45
46# -----------------------------------------
47# Download ZIP file containing diagrams
48# -----------------------------------------
49
50response = project.download_all_diagrams(path=SAVE_IMAGE_PATH, file_format="png", filename="png")
51print(f"> ZIP saved at: {response}\n")
52
53# -----------------------------------------
54# Download single diagram
55# -----------------------------------------
56
57path = project.download_diagram(
58    diagram_id=first_diagram_id,
59    filename="CUSTOM_FILENAME",
60    file_format="png",
61    path=SAVE_IMAGE_PATH,
62)
63print(f"> Diagram saved at: {path}\n")
64
65usage_diagrams = project.get_root_package().Usage.__diagram
66for i, diagram in enumerate(usage_diagrams, 1):
67    project.download_diagram(
68        diagram_id=diagram._id, file_format="png", path=SAVE_IMAGE_PATH + "/Usage"
69    )
70    print(f"> Saved Usage diagram #{i}: {diagram._plane._model_element._name}\n")
71
72# -----------------------------------------
73# Navigate through diagrams
74# -----------------------------------------
75
76print(first_diagram._plane._model_element._name, end="\n")
77
78for diagram in project.get_root_package().Usage.__diagram:
79    print("Diagram name:", diagram._name, end="\n")
80
81# -----------------------------------------
82# Create element
83# -----------------------------------------
84
85project.factory.create_attribute_usage(name="NewAttr")
86
87# -----------------------------------------
88# Get diagrams information
89# -----------------------------------------
90
91print(project.get_project_diagrams_info(), end="\n\n")
92
93print(project.get_single_diagram_info(first_diagram_id))
Simplified example using AnsysSysML2Project for SAM projects#
 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"""Ansys SysML2 project static example for PySAM SysML2."""
23
24import requests
25from urllib3.exceptions import InsecureRequestWarning
26
27from ansys.sam.sysml2.tools.ansys_sysml2_project import AnsysSysML2Project
28
29requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
30
31project = AnsysSysML2Project(
32    server_url="<SAM Server URL>",  # Your SAM server base URL
33    organization_id="<Orga ID>",  # The organization ID
34    token="<Token>",  # Your authorization token
35    use_ssl=False,  # If the server hasn't a valid SSL
36    project_id="<Bike Project ID>",  # The project ID
37)
38
39diagrams_status = project.is_diagrams_available()
40print(f"Diagrams Status : {'Available' if diagrams_status else 'Unavailable'}", end="\n\n")
41
42SAVE_IMAGE_PATH = "The path of the directory you want to save your diagrams into"
43first_diagram = project.get_root_package().__diagram[0]
44first_diagram_id = first_diagram._id
45
46# -----------------------------------------
47# Download ZIP file containing diagrams
48# -----------------------------------------
49
50response = project.download_all_diagrams(path=SAVE_IMAGE_PATH, file_format="png", filename="png")
51print(f"> ZIP saved at: {response}\n")
52
53# -----------------------------------------
54# Download single diagram
55# -----------------------------------------
56
57path = project.download_diagram(
58    diagram_id=first_diagram_id,
59    filename="CUSTOM_FILENAME",
60    file_format="png",
61    path=SAVE_IMAGE_PATH,
62)
63print(f"> Diagram saved at: {path}\n")
64
65usage_diagrams = project.get_root_package().get("Usage").__diagram
66for i, diagram in enumerate(usage_diagrams, 1):
67    project.download_diagram(
68        diagram_id=diagram._id, file_format="png", path=SAVE_IMAGE_PATH + "/Usage"
69    )
70    print(f"> Saved Usage diagram #{i}: {diagram._plane._model_element._name}\n")
71
72# -----------------------------------------
73# Navigate through diagrams
74# -----------------------------------------
75
76print(first_diagram._plane._model_element._name, end="\n")
77
78for diagram in project.get_root_package().get("Usage").__diagram:
79    print("Diagram name:", diagram._name, end="\n")
80
81# -----------------------------------------
82# Create element
83# -----------------------------------------
84
85project.factory.create_attribute_usage(name="NewAttr")
86
87# -----------------------------------------
88# Get diagrams information
89# -----------------------------------------
90
91print(project.get_project_diagrams_info(), end="\n\n")
92
93print(project.get_single_diagram_info(first_diagram_id))

Key advantages#

Compared to the traditional approach described in Download diagrams and create new elements, using the simplified AnsysSysML2Project / AnsysScriptingProject class offers these advantages:

  • Single initialization: One class automatically handles all connectors.

  • Built-in diagram management: Removes the need to manually create and manage the SAMDiagramManager class.

  • Streamlined API: Provides direct access to project operations without managing multiple connector instances.

  • Integrated features: Combines project management, diagram operations, and element creation in one interface.

Note

  • Replace placeholder values with your actual SAM configuration.

  • The AnsysSysML2Project / AnsysScriptingProject class automatically manages diagram loading and the connector lifecycle.

  • The class supports all diagram formats (png, svg, jpeg) for downloads.