Work with diagrams#

Warning

Functionalities for loading, downloading, and navigating diagrams are available only for projects of type SAM.

This page explains how to load, download, and navigate diagrams from your SysML v2 project using the SAM REST API.

Load diagrams#

Before interacting with diagrams, load them using the SamDiagramDownloader context manager, which requires an API connector.

Create a SamRestApiConnector:

Create SAM REST API connector#
sam_rest_api_connector = SamRestApiConnector(
    server_url="<SAM Server URL>",  # Your SAM server base URL
    token="<Token>",  # Your authorization token
    use_ssl=False,  # If the server hasn't a valid SSL
)

Load diagrams from a project and make them available for further operations like downloading:

Load diagrams using SAM diagram manager#
with SAMDiagramManager(connector=sam_rest_api_connector) as diagrams:
    diagrams.load_diagrams(model=project)

Outside the with block, the SamDiagramDownloader context manager is no longer active. This ensures proper setup and cleanup of resources when working with diagrams.

Download diagrams#

Load diagrams inside an SAMDiagramManager context before downloading them.

Also, instantiate an SamDiagramDownloader object:

Create a SAM diagram downloader#
downloader = SamDiagramDownloader(connector=sam_rest_api_connector, project_id=project._id)

Download all diagrams#

After loading diagrams, download all diagrams and save them into a ZIP archive:

Download all diagrams with custom parameters#
path = downloader.download_all_diagrams(
    path=SAVE_IMAGE_PATH,
    file_format="jpeg",
    filename="download_all_diagrams_with_args.zip",
)
print(f"ZIP saved at: {path}\n")

Default parameters

If you do not specify parameters, these defaults are used to format and name your download archive:

  • file_format: "SVG"

  • filename: "<PackageName>_<FileFormat>_diagrams.zip"

Get and download a single diagram#

Get a single diagram and download it in a given format:

Get a single diagram#
first_diagram = project.get_root_package().__diagram[0]
first_diagram_id = first_diagram._id
Download this single diagram#
path = downloader.download_diagram(
    diagram_id=first_diagram_id,
    path=SAVE_IMAGE_PATH,
    file_format="svg",
)
print(f"Diagram saved at: {path}")

Download diagrams in a loop#

Iterate through diagrams inside a specific section of your model, such as the Usage section:

Get desired diagrams#
usage_diagrams = project.get_root_package().Usage.__diagram
Download diagrams from Usage section in a loop#
for i, diagram in enumerate(usage_diagrams, 1):
    downloader.download_diagram(
        diagram_id=diagram._id, file_format="png", path=SAVE_IMAGE_PATH + "/Usage"
    )
    print(f"> Saved Usage diagram #{i}: {diagram._plane._model_element._name}")