Skip to content

Commit

Permalink
Added export_aedb.
Browse files Browse the repository at this point in the history
  • Loading branch information
ansys-pwalters authored and ansnfernand committed Mar 13, 2024
1 parent 4856112 commit 53cdbd4
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/ansys/sherlock/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,18 @@ def __str__(self):
return f"Model service error: {self.message}"


class SherlockExportAEDBError(Exception):
"""Contains the error raised when an Electronics Desktop model cannot be exported."""

def __init__(self, message):
"""Initialize error message."""
self.message = message

def __str__(self):
"""Format error message."""
return f"Export AEDB error: {self.message}"


class SherlockInvalidLoadDirectionError(Exception):
"""Contains the error raised when the load direction string is invalid."""

Expand Down
79 changes: 77 additions & 2 deletions src/ansys/sherlock/core/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# © 2023 ANSYS, Inc. All rights reserved
# © 2023-2024 ANSYS, Inc. All rights reserved

"""Module containing all model generation capabilities."""
import os.path
Expand All @@ -11,7 +11,10 @@
from ansys.api.sherlock.v0 import SherlockModelService_pb2_grpc

from ansys.sherlock.core import LOG
from ansys.sherlock.core.errors import SherlockModelServiceError
from ansys.sherlock.core.errors import (
SherlockExportAEDBError,
SherlockModelServiceError,
)
from ansys.sherlock.core.grpc_stub import GrpcStub


Expand Down Expand Up @@ -298,3 +301,75 @@ def generate_trace_model(
except Exception as e:
LOG.error(str(e))
raise


def export_aedb(
self,
project_name,
cca_name,
export_file,
overwrite=True,
display_model=False,
):
r"""Export an Electronics Desktop model.
Parameters
----------
project_name : str
Name of the Sherlock project to generate the EDB model for.
cca_name : str
Name of the CCA to generate the EDB model from.
export_file : str
Directory for saving exported model to.
overwrite : bool, optional
Whether to overwrite an existing file having the same file name.
The default is ``True``.
display_model : bool, optional
Whether to launch and display the exported model in Ansys Electronics
Desktop once the export finishes. The default is ``False``.
Returns
-------
int
Status code of the response. 0 for success.
Examples
--------
>>> from ansys.sherlock.core import launcher
>>> from ansys.sherlock.core import model
>>> sherlock = launcher.launch_sherlock()
>>> sherlock.model.export_aedb(
'Tutorial Project', 'Main Board', 'c:\Temp\export.aedb',
True, False)
"""
try:
if not project_name:
raise SherlockExportAEDBError("Project name is invalid.")
if not cca_name:
raise SherlockExportAEDBError("CCA name is invalid.")
if export_file == "":
raise SherlockExportAEDBError(message="Export filepath is required.")
except Exception as e:
LOG.error(str(e))
raise e

if not self._is_connection_up():
LOG.error("There is no connection to a gRPC service.")
return

export_request = SherlockModelService_pb2.ExportAEDBRequest()
export_request.project = project_name
export_request.ccaName = cca_name
export_request.exportFile = export_file
export_request.overwrite = overwrite
export_request.displayModel = display_model

try:
return_code = self.stub.exportAEDB(export_request)
if return_code.value != 0:
raise SherlockExportAEDBError(return_code.message)

return return_code.value
except Exception as e:
LOG.error(str(e))
raise
77 changes: 75 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# © 2023 ANSYS, Inc. All rights reserved
# © 2023-2024 ANSYS, Inc. All rights reserved

import os
import platform
Expand All @@ -7,7 +7,10 @@
import grpc
import pytest

from ansys.sherlock.core.errors import SherlockModelServiceError
from ansys.sherlock.core.errors import (
SherlockExportAEDBError,
SherlockModelServiceError,
)
from ansys.sherlock.core.model import Model


Expand Down Expand Up @@ -206,6 +209,76 @@ def test_model_generate_trace_model(self):
except SherlockModelServiceError as e:
pytest.fail(str(e))

def test_model_export_aedb(self):
channel_param = "127.0.0.1:9090"
channel = grpc.insecure_channel(channel_param)
model = Model(channel)

project_name = "Tutorial Project"
cca_name = "Main Board"
export_file = "test_aedb_export"
try:
model.export_aedb(
project_name="",
cca_name=cca_name,
export_file=export_file,
overwrite=True,
display_model=False,
)
pytest.fail("No exception raised when using an invalid parameter")
except SherlockExportAEDBError as e:
assert str(e) == "Export AEDB error: Project name is invalid."

try:
model.export_aedb(
project_name=project_name,
cca_name="",
export_file=export_file,
overwrite=True,
display_model=False,
)
pytest.fail("No exception raised when using an invalid parameter")
except SherlockExportAEDBError as e:
assert str(e) == "Export AEDB error: CCA name is invalid."

try:
model.export_aedb(
project_name=project_name,
cca_name=cca_name,
export_file="",
overwrite=True,
display_model=False,
)
pytest.fail("No exception raised when using an invalid parameter")
except SherlockExportAEDBError as e:
assert str(e) == "Export AEDB error: Export filepath is required."

if model._is_connection_up():
try:
invalid_cca_name = "Invalid CCA"
model.export_aedb(
project_name=project_name,
cca_name=invalid_cca_name,
export_file=export_file,
overwrite=True,
display_model=False,
)
pytest.fail("No exception raised when using an invalid parameter")
except Exception as e:
assert type(e) == SherlockExportAEDBError

try:
result = model.export_aedb(
project_name=project_name,
cca_name=cca_name,
export_file=export_file,
overwrite=True,
display_model=False,
)
assert result == 0
except SherlockExportAEDBError as e:
pytest.fail(str(e))


if __name__ == "__main__":
unittest.main()

0 comments on commit 53cdbd4

Please sign in to comment.