From 07fb9811ccbed95c3530faef72d1ed258e60839c Mon Sep 17 00:00:00 2001 From: Nayane Fernandes <143632290+ansnfernand@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:29:59 +0200 Subject: [PATCH 1/2] New API: export_all_test_points() --- src/ansys/sherlock/core/errors.py | 17 ++++++ src/ansys/sherlock/core/layer.py | 97 +++++++++++++++++++++++++++++++ tests/test_layer.py | 73 +++++++++++++++++++++++ 3 files changed, 187 insertions(+) diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index d4a3cc144..508afe2ec 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -1107,3 +1107,20 @@ def str_itr(self): assert self.error_array is None return [f"Update test fixtures by file error: {self.message}"] + + +class SherlockExportAllTestPoints(Exception): + """Contains the errors raised when test points cannot be exported.""" + + def __init__(self, message=None, error_array=None): + """Initialize error message.""" + self.message = message + self.error_array = error_array + + def str_itr(self): + """Create list of error messages.""" + if self.message is None: + return [f"Export test points error: {error}" for error in self.error_array] + + assert self.error_array is None + return [f"Export test points error: {self.message}"] diff --git a/src/ansys/sherlock/core/layer.py b/src/ansys/sherlock/core/layer.py index b922a886f..090a88805 100644 --- a/src/ansys/sherlock/core/layer.py +++ b/src/ansys/sherlock/core/layer.py @@ -22,6 +22,7 @@ SherlockDeleteAllICTFixturesError, SherlockDeleteAllMountPointsError, SherlockDeleteAllTestPointsError, + SherlockExportAllTestPoints, SherlockUpdateMountPointsByFileError, SherlockUpdateTestFixturesByFileError, SherlockUpdateTestPointsByFileError, @@ -643,3 +644,99 @@ def update_test_fixtures_by_file( for error in e.str_itr(): LOG.error(error) raise e + + def export_all_test_points( + self, + project, + cca_name, + export_file, + length_units="DEFAULT", + displacement_units="DEFAULT", + force_units="DEFAULT", + ): + """Export the test point properties for a CCA. + + Parameters + ---------- + project : str + Name of the Sherlock project. + cca_name : str + Name of the CCA. + export_file : str + Full path for the CSV file to export the parts list to. + length_units : str, optional + Length units to use when exporting the test points. + The default is ``DEFAULT``. + displacement_units : str, optional + Displacement units to use when exporting the test points. + The default is ``DEFAULT``. + force_units : str, optional + Force units to use when exporting the test points. + The default is ``DEFAULT``. + + Returns + ------- + int + Status code of the response. 0 for success. + + Examples + -------- + >>> from ansys.sherlock.core.launcher import launch_sherlock + >>> sherlock = launch_sherlock() + >>> sherlock.project.import_odb_archive( + "ODB++ Tutorial.tgz", + True, + True, + True, + True, + project="Tutorial Project", + cca_name="Card", + ) + >>> sherlock.layer.export_all_test_points( + "Tutorial Project", + "Card", + "TestPointsExport.csv", + "DEFAULT", + "DEFAULT", + "DEFAULT", + ) + """ + try: + if project == "": + raise SherlockExportAllTestPoints(message="Project name is invalid.") + if cca_name == "": + raise SherlockExportAllTestPoints(message="CCA name is invalid.") + if export_file == "": + raise SherlockExportAllTestPoints(message="File path is required.") + + if not self._is_connection_up(): + LOG.error("There is no connection to a gRPC service.") + return + + request = SherlockLayerService_pb2.ExportAllTestPointsRequest( + project=project, + ccaName=cca_name, + filePath=export_file, + lengthUnits=length_units, + displacementUnits=displacement_units, + forceUnits=force_units, + ) + + response = self.stub.exportAllTestPoints(request) + + return_code = response.returnCode + + if return_code.value == -1: + if return_code.message == "": + raise SherlockExportAllTestPoints(error_array=response.updateError) + + raise SherlockExportAllTestPoints(message=return_code.message) + + else: + LOG.info(return_code.message) + return return_code.value + + except SherlockExportAllTestPoints as e: + for error in e.str_itr(): + LOG.error(error) + raise e diff --git a/tests/test_layer.py b/tests/test_layer.py index 095326f81..147cd2e16 100644 --- a/tests/test_layer.py +++ b/tests/test_layer.py @@ -1,4 +1,7 @@ # © 2023-2024 ANSYS, Inc. All rights reserved +import os +import platform +import time import uuid import grpc @@ -9,6 +12,7 @@ SherlockDeleteAllICTFixturesError, SherlockDeleteAllMountPointsError, SherlockDeleteAllTestPointsError, + SherlockExportAllTestPoints, SherlockUpdateMountPointsByFileError, SherlockUpdateTestFixturesByFileError, SherlockUpdateTestPointsByFileError, @@ -30,6 +34,7 @@ def test_all(): helper_test_add_potting_region(layer) helper_test_update_test_fixtures_by_file(layer) helper_test_update_test_points_by_file(layer) + helper_test_export_all_test_points(layer) def helper_test_add_potting_region(layer): @@ -524,5 +529,73 @@ def helper_test_update_test_fixtures_by_file(layer): assert type(e) == SherlockUpdateTestFixturesByFileError +def helper_test_export_all_test_points(layer): + """Tests export_all_test_points API.""" + try: + layer.export_all_test_points( + "", + "Main Board", + "Test Points.csv", + ) + pytest.fail("No exception raised when using an invalid parameter") + except SherlockExportAllTestPoints as e: + assert e.str_itr()[0] == "Export test points error: Project name is invalid." + + try: + layer.export_all_test_points( + "Tutorial Project", + "", + "Test Points.csv", + ) + pytest.fail("No exception raised when using an invalid parameter") + except SherlockExportAllTestPoints as e: + assert e.str_itr()[0] == "Export test points error: CCA name is invalid." + + try: + layer.export_all_test_points( + "Tutorial Project", + "Main Board", + "", + ) + pytest.fail("No exception raised when using an invalid parameter") + except SherlockExportAllTestPoints as e: + assert e.str_itr()[0] == "Export test points error: File path is required." + + if layer._is_connection_up(): + if platform.system() == "Windows": + temp_dir = os.environ.get("TEMP", "C:\\TEMP") + else: + temp_dir = os.environ.get("TEMP", "/tmp") + test_points_file = os.path.join(temp_dir, "test_points.csv") + + try: + result = layer.export_all_test_points( + "Tutorial Project", + "Main Board", + test_points_file, + ) + + assert os.path.exists(test_points_file) + time.sleep(0.5) + assert result == 0 + except Exception as e: + pytest.fail(e.message) + finally: + try: + os.remove(test_points_file) + except Exception as e: + print(str(e)) + + try: + layer.export_all_test_points( + "Tutorial Project", + "Invalid CCA", + test_points_file, + ) + pytest.fail("No exception raised when using an invalid parameter") + except Exception as e: + assert type(e) == SherlockExportAllTestPoints + + if __name__ == "__main__": test_all() From abbbb3f88d1d0a599df25482186b0b2ac60426c5 Mon Sep 17 00:00:00 2001 From: Nayane Fernandes <143632290+ansnfernand@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:50:58 +0200 Subject: [PATCH 2/2] Update after feedback --- src/ansys/sherlock/core/layer.py | 15 ++++----------- tests/test_layer.py | 7 ------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/ansys/sherlock/core/layer.py b/src/ansys/sherlock/core/layer.py index 090a88805..1d28af11a 100644 --- a/src/ansys/sherlock/core/layer.py +++ b/src/ansys/sherlock/core/layer.py @@ -722,19 +722,12 @@ def export_all_test_points( forceUnits=force_units, ) - response = self.stub.exportAllTestPoints(request) + return_code = self.stub.exportAllTestPoints(request) - return_code = response.returnCode - - if return_code.value == -1: - if return_code.message == "": - raise SherlockExportAllTestPoints(error_array=response.updateError) + if return_code.value != 0: + raise SherlockExportAllTestPoints(error_array=return_code.message) - raise SherlockExportAllTestPoints(message=return_code.message) - - else: - LOG.info(return_code.message) - return return_code.value + return return_code.value except SherlockExportAllTestPoints as e: for error in e.str_itr(): diff --git a/tests/test_layer.py b/tests/test_layer.py index 147cd2e16..116667213 100644 --- a/tests/test_layer.py +++ b/tests/test_layer.py @@ -1,7 +1,6 @@ # © 2023-2024 ANSYS, Inc. All rights reserved import os import platform -import time import uuid import grpc @@ -576,15 +575,9 @@ def helper_test_export_all_test_points(layer): ) assert os.path.exists(test_points_file) - time.sleep(0.5) assert result == 0 except Exception as e: pytest.fail(e.message) - finally: - try: - os.remove(test_points_file) - except Exception as e: - print(str(e)) try: layer.export_all_test_points(