Download diagrams#
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:
Download diagrams (single and batch).
Retrieve diagram information.
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.kparmodel imported into a project.
Python example#
Work with diagrams using the SAM API#
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_api_connector import SamApiConnector
29from ansys.sam.sysml2.diagrams.tools.sam_diagram_downloader import SamDiagramDownloader
30
31# Used to disable warnings
32requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
33
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
41sam_api_connector = SamApiConnector(
42 server_url="<SAM Server URL>", # Your SAM server base URL
43 token="<Token>", # Your authorization token
44 use_ssl=False, # If the server hasn't a valid SSL
45)
46
47project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
48
49project = project_manager.get_scripting_project("<Bike Project ID>")
50
51# -----------------------------------------
52# Work with diagrams
53# -----------------------------------------
54
55diagrams_info = sam_api_connector.get_diagrams_info(project._id)
56
57print(f"Found {len(diagrams_info)} diagrams.")
58
59downloader = SamDiagramDownloader(connector=sam_api_connector, project_id=project._id)
60
61# -----------------------------------------
62# Download diagrams
63# -----------------------------------------
64
65SAVE_IMAGE_PATH = "The path of the directory you want to save your diagrams into"
66
67### You can specify the file format and the filename.
68### The defaults are file_format="svg" and filename="<PackageName>_<FileFormat>_diagrams.zip"
69path = downloader.download_all_diagrams(
70 path=SAVE_IMAGE_PATH,
71 file_format="jpeg",
72 filename="download_all_diagrams_with_args.zip",
73)
74print(f"ZIP saved at: {path}\n")
75
76
77first_diagram = diagrams_info[0]
78first_diagram_id = first_diagram["diagramId"]
79
80path = downloader.download_diagram(
81 diagram_id=first_diagram_id,
82 path=SAVE_IMAGE_PATH,
83 file_format="svg",
84)
85print(f"Diagram saved at: {path}")
86
87
88for i, diagram in enumerate(diagrams_info, 1):
89 downloader.download_diagram(
90 diagram_id=diagram["diagramId"], file_format="png", path=SAVE_IMAGE_PATH + "/all"
91 )
92 print(f"> Saved diagram #{i}: {diagram['name']}")
93
94# -----------------------------------------
95# Retrieve diagram information
96# -----------------------------------------
97
98single_info = sam_api_connector.get_single_diagram_info(project._id, first_diagram_id)
99print(single_info["name"])
100
101for diagram in diagrams_info:
102 print("Diagram name:", diagram["name"])
Work with diagrams using the SAM API#
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_api_connector import SamApiConnector
29from ansys.sam.sysml2.diagrams.tools.sam_diagram_downloader import SamDiagramDownloader
30
31# Used to disable warnings
32requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
33
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
41sam_api_connector = SamApiConnector(
42 server_url="<SAM Server URL>", # Your SAM server base URL
43 token="<Token>", # Your authorization token
44 use_ssl=False, # If the server hasn't a valid SSL
45)
46
47project_manager = SysML2ProjectManager(connector=ansyssysml2apiconnector)
48
49project = project_manager.get_sysml_project("<Bike Project ID>")
50
51# -----------------------------------------
52# Work with diagrams
53# -----------------------------------------
54
55diagrams_info = sam_api_connector.get_diagrams_info(project._id)
56
57print(f"Found {len(diagrams_info)} diagrams.")
58
59downloader = SamDiagramDownloader(connector=sam_api_connector, project_id=project._id)
60
61# -----------------------------------------
62# Download diagrams
63# -----------------------------------------
64
65SAVE_IMAGE_PATH = "The path of the directory you want to save your diagrams into"
66
67### You can specify the file format and the filename.
68### The defaults are file_format="svg" and filename="<PackageName>_<FileFormat>_diagrams.zip"
69path = downloader.download_all_diagrams(
70 path=SAVE_IMAGE_PATH,
71 file_format="jpeg",
72 filename="download_all_diagrams_with_args.zip",
73)
74print(f"ZIP saved at: {path}\n")
75
76
77first_diagram = diagrams_info[0]
78first_diagram_id = first_diagram["diagramId"]
79
80path = downloader.download_diagram(
81 diagram_id=first_diagram_id,
82 path=SAVE_IMAGE_PATH,
83 file_format="svg",
84)
85print(f"Diagram saved at: {path}")
86
87
88for i, diagram in enumerate(diagrams_info, 1):
89 downloader.download_diagram(
90 diagram_id=diagram["diagramId"], file_format="png", path=SAVE_IMAGE_PATH + "/all"
91 )
92 print(f"> Saved diagram #{i}: {diagram['name']}")
93
94# -----------------------------------------
95# Retrieve diagram information
96# -----------------------------------------
97
98single_info = sam_api_connector.get_single_diagram_info(project._id, first_diagram_id)
99print(single_info["name"])
100
101for diagram in diagrams_info:
102 print("Diagram name:", diagram["name"])
Note
Replace placeholder values with your actual SAM configuration.
Retrieve diagram content in various formats, such as
pngandsvg.