diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index 9f285e6da..1962ff4b6 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -1025,3 +1025,15 @@ def __init__(self, message): def __str__(self): """Format error message.""" return f"Export net list error: {self.message}" + + +class SherlockExportProjectError(Exception): + """Contains the error raised when a project cannot be exported.""" + + def __init__(self, message): + """Initialize error message.""" + self.message = message + + def __str__(self): + """Format error message.""" + return f"Export project error : {self.message}" diff --git a/src/ansys/sherlock/core/project.py b/src/ansys/sherlock/core/project.py index c5564d99a..f0c85320f 100644 --- a/src/ansys/sherlock/core/project.py +++ b/src/ansys/sherlock/core/project.py @@ -17,6 +17,7 @@ SherlockAddStrainMapsError, SherlockAddThermalMapsError, SherlockDeleteProjectError, + SherlockExportProjectError, SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, @@ -1693,3 +1694,102 @@ def import_project_zip_archive_single_mode( raise e return response.value + + def export_project( + self, + project_name, + export_design_files, + export_result_files, + export_archive_results, + export_user_files, + export_log_files, + export_system_data, + export_file_dir, + export_file_name, + overwrite_existing_file, + ): + """ + Export a sherlock project. + + Requires Sherlock Version: 2025 R1 + + Parameters + ---------- + project_name : str + Name of the project being exported. + export_design_files : bool + Determines if design files should be exported. + export_result_files : bool + Determines if all analysis module result files should be exported. + export_archive_results : bool + Determines if all archive result files should be exported. + export_user_files : bool + Determines if user properties and custom data files should be exported. + export_log_files : bool + Determines if Sherlock console and application log files should be exported. + export_system_data : bool + Determines if system technical data should be exported. + export_file_dir : str + Destination of export file. + export_file_name : str + Name to be given to the exported file. + overwrite_existing_file : bool + Determines if exported file will overwrite a previously existing file. + + Returns + ------- + int + Status code of the response. 0 for success. + + Examples + -------- + >>> from ansys.sherlock.core.launcher import launch_sherlock + >>> sherlock = launch_sherlock() + >>> sherlock.project.export_project("Tutorial Project", + True, + True, + True, + True, + True, + True, + "C:/Path/To/Exported/Project", + "Exported_Project", + True) + """ + try: + if project_name == "": + raise SherlockExportProjectError(message="Project name is invalid") + + if export_file_dir == "": + raise SherlockExportProjectError(message="Export directory is invalid") + + if export_file_name == "": + raise SherlockExportProjectError(message="Export file name is invalid") + + if not self._is_connection_up(): + LOG.error("There is no connection to a gRPC service.") + return + + request = SherlockProjectService_pb2.ExportProjectRequest( + project=project_name, + exportDesignFiles=export_design_files, + exportResultFiles=export_result_files, + exportArchivedResults=export_archive_results, + exportUserFiles=export_user_files, + exportLogFiles=export_log_files, + exportSystemData=export_system_data, + exportFileDirectory=export_file_dir, + exportFileName=export_file_name, + overwriteExistingFile=overwrite_existing_file, + ) + + response = self.stub.exportProject(request) + + if response.value == -1: + raise SherlockExportProjectError(message=response.message) + + except SherlockExportProjectError as e: + LOG.error(str(e)) + raise e + + return response.value diff --git a/tests/test_project.py b/tests/test_project.py index 31e66d504..14085fadb 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -14,6 +14,7 @@ SherlockAddStrainMapsError, SherlockAddThermalMapsError, SherlockDeleteProjectError, + SherlockExportProjectError, SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, @@ -66,6 +67,8 @@ def test_all(): finally: clean_up_after_add(project, project_name) + helper_test_export_project(project) + def helper_test_delete_project(project): """Test delete_project API""" @@ -2969,5 +2972,99 @@ def clean_up_after_add(project, project_name): project.delete_project(project_name) +def helper_test_export_project(project): + """Test method for export project""" + try: + result = project.export_project( + project_name="", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="/Test/Dir", + export_file_name="ExportedProject", + overwrite_existing_file=True, + ) + except SherlockExportProjectError as e: + assert str(e) == "Export project error : Project name is invalid" + + try: + result = project.export_project( + project_name="Tutorial Project", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="", + export_file_name="ExportedProject", + overwrite_existing_file=True, + ) + except SherlockExportProjectError as e: + assert str(e) == "Export project error : Export directory is invalid" + + try: + result = project.export_project( + project_name="Tutorial Project", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="/Test/Dir", + export_file_name="", + overwrite_existing_file=True, + ) + except SherlockExportProjectError as e: + assert str(e) == "Export project error : Export file name is invalid" + + if project._is_connection_up(): + try: + result = project.export_project( + project_name="", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="/Test/Dir", + export_file_name="ExportedProject", + overwrite_existing_file=True, + ) + pytest.fail("No exception raised when using an invalid parameter") + except Exception as e: + assert type(e) == SherlockExportProjectError + this_dir = os.path.dirname(os.path.realpath(__file__)) + output_file_name = "ExportedProject" + try: + result = project.export_project( + project_name="Tutorial Project", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir=this_dir, + export_file_name=output_file_name, + overwrite_existing_file=True, + ) + assert result == 0 + + # Clean up file + output_file = os.path.join(this_dir, output_file_name) + if os.path.exists(output_file): + os.remove(output_file) + else: + pytest.fail("Failed to generate export file.") + except SherlockExportProjectError as e: + pytest.fail(str(e)) + + if __name__ == "__main__": test_all()