Download diagrams and create new elements#
This example shows how to work with the Bike model in a SysML v2 project using the
Ansys SAM API. It explains how to perform these tasks:
Load and download diagrams.
Navigate and save diagram content.
Note
If you have never used PySAM SysML2 before, start with one of these simpler examples to understand how this library works:
Prerequisites#
Ensure that you meet these prerequisites:
A running SAM server instance.
A valid organization ID, project ID, and token.
The
bike.xmimodel imported into a project.
Python example#
Work with diagrams using SAMDiagramManager#
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"""Download diagrams example for PySAM SysML2."""
23
24import requests
25from urllib3.exceptions import InsecureRequestWarning
26
27from ansys.sam.sysml2 import AnsysSysML2APIConnector, SysML2ProjectManager
28from ansys.sam.sysml2.diagrams.api.sam_rest_api_connector import SamRestApiConnector
29from ansys.sam.sysml2.diagrams.sam_diagram_manager import SAMDiagramManager
30from ansys.sam.sysml2.diagrams.tools.sam_diagram_downloader import SamDiagramDownloader
31
32# Used to disable warnings
33requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
34
35ansyssysml2apiconnector = AnsysSysML2APIConnector(
36 server_url="<SAM Server URL>", # Your SAM server base URL
37 organization_id="<Orga ID>", # The organization ID
38 token="<Token>", # Your authorization token
39 use_ssl=False, # If the server hasn't a valid SSL
40)
41
42sam_rest_api_connector = SamRestApiConnector(
43 server_url="<SAM Server URL>", # Your SAM server base URL
44 token="<Token>", # Your authorization token
45 use_ssl=False, # If the server hasn't a valid SSL
46)
47
48project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
49
50project = project_manager.get_scripting_project("<Bike Project ID>")
51
52# -----------------------------------------
53# Work with diagrams
54# -----------------------------------------
55
56with SAMDiagramManager(connector=sam_rest_api_connector) as diagrams:
57 diagrams.load_diagrams(model=project)
58
59print(f"Loaded {len(project.get_root_package().__diagram)} diagrams.")
60
61downloader = SamDiagramDownloader(connector=sam_rest_api_connector, project_id=project._id)
62
63# -----------------------------------------
64# Download diagrams
65# -----------------------------------------
66
67SAVE_IMAGE_PATH = "The path of the directory you want to save your diagrams into"
68
69### You can specify the file format and the filename.
70### The defaults are file_format="svg" and filename="<PackageName>_<FileFormat>_diagrams.zip"
71path = downloader.download_all_diagrams(
72 path=SAVE_IMAGE_PATH,
73 file_format="jpeg",
74 filename="download_all_diagrams_with_args.zip",
75)
76print(f"ZIP saved at: {path}\n")
77
78
79first_diagram = project.get_root_package().__diagram[0]
80first_diagram_id = first_diagram._id
81
82path = downloader.download_diagram(
83 diagram_id=first_diagram_id,
84 path=SAVE_IMAGE_PATH,
85 file_format="svg",
86)
87print(f"Diagram saved at: {path}")
88
89
90usage_diagrams = project.get_root_package().Usage.__diagram
91for i, diagram in enumerate(usage_diagrams, 1):
92 downloader.download_diagram(
93 diagram_id=diagram._id, file_format="png", path=SAVE_IMAGE_PATH + "/Usage"
94 )
95 print(f"> Saved Usage diagram #{i}: {diagram._plane._model_element._name}")
96
97# -----------------------------------------
98# Navigate through diagrams
99# -----------------------------------------
100
101print(first_diagram._plane._model_element._name)
102
103for diagram in usage_diagrams:
104 print("Diagram name:", diagram._name)
Work with diagrams using SAMDiagramManager#
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"""Download diagrams 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.diagrams.api.sam_rest_api_connector import SamRestApiConnector
29from ansys.sam.sysml2.diagrams.sam_diagram_manager import SAMDiagramManager
30from ansys.sam.sysml2.diagrams.tools.sam_diagram_downloader import SamDiagramDownloader
31
32# Used to disable warnings
33requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
34
35ansyssysml2apiconnector = AnsysSysML2APIConnector(
36 server_url="<SAM Server URL>", # Your SAM server base URL
37 organization_id="<Orga ID>", # The organization ID
38 token="<Token>", # Your authorization token
39 use_ssl=False, # If the server hasn't a valid SSL
40)
41
42sam_rest_api_connector = SamRestApiConnector(
43 server_url="<SAM Server URL>", # Your SAM server base URL
44 token="<Token>", # Your authorization token
45 use_ssl=False, # If the server hasn't a valid SSL
46)
47
48project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
49
50project = project_manager.get_sysml_project("<Bike Project ID>")
51
52# -----------------------------------------
53# Work with diagrams
54# -----------------------------------------
55
56with SAMDiagramManager(connector=sam_rest_api_connector) as diagrams:
57 diagrams.load_diagrams(model=project)
58
59print(f"Loaded {len(project.get_root_package().__diagram)} diagrams.")
60
61downloader = SamDiagramDownloader(connector=sam_rest_api_connector, project_id=project._id)
62
63# -----------------------------------------
64# Download diagrams
65# -----------------------------------------
66
67SAVE_IMAGE_PATH = "The path of the directory you want to save your diagrams into"
68
69### You can specify the file format and the filename.
70### The defaults are file_format="svg" and filename="<PackageName>_<FileFormat>_diagrams.zip"
71path = downloader.download_all_diagrams(
72 path=SAVE_IMAGE_PATH,
73 file_format="jpeg",
74 filename="download_all_diagrams_with_args.zip",
75)
76print(f"ZIP saved at: {path}\n")
77
78
79first_diagram = project.get_root_package().__diagram[0]
80first_diagram_id = first_diagram._id
81
82path = downloader.download_diagram(
83 diagram_id=first_diagram_id,
84 path=SAVE_IMAGE_PATH,
85 file_format="svg",
86)
87print(f"Diagram saved at: {path}")
88
89
90usage_diagrams = project.get_root_package().get("Usage").__diagram
91for i, diagram in enumerate(usage_diagrams, 1):
92 downloader.download_diagram(
93 diagram_id=diagram._id, file_format="png", path=SAVE_IMAGE_PATH + "/Usage"
94 )
95 print(f"> Saved Usage diagram #{i}: {diagram._plane._model_element._name}")
96
97# -----------------------------------------
98# Navigate through diagrams
99# -----------------------------------------
100
101print(first_diagram._plane._model_element._name)
102
103for diagram in usage_diagrams:
104 print("Diagram name:", diagram._name)
Note
Replace placeholder values with your actual SAM configuration.
Always use the
SAMDiagramManagerclass within a context manager (withstatement).Retrieve diagram content in various formats, such as
pngandsvg.