diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index 9dc511ac9..0ac73c6ed 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -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.""" diff --git a/src/ansys/sherlock/core/model.py b/src/ansys/sherlock/core/model.py index 8dcad7bb6..7aea0a74f 100644 --- a/src/ansys/sherlock/core/model.py +++ b/src/ansys/sherlock/core/model.py @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/tests/test_model.py b/tests/test_model.py index 2c6abba8a..196d29ebd 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -1,4 +1,4 @@ -# © 2023 ANSYS, Inc. All rights reserved +# © 2023-2024 ANSYS, Inc. All rights reserved import os import platform @@ -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 @@ -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()