2929from logging .handlers import WatchedFileHandler
3030import os
3131import re
32+ import time
3233
3334import grpc
3435import tqdm
@@ -372,35 +373,66 @@ def download_file(self, file_name, show_progress=True, target_dir=None):
372373 if os .path .exists (file_path ):
373374 os .remove (file_path )
374375 started = True
375- with open (file_path , mode = "ab" ) as f :
376- f .write (response .file_content )
376+
377+ try_more = 3
378+ while try_more > 0 :
379+ try :
380+ with open (file_path , mode = "ab" ) as f :
381+ f .write (response .file_content )
382+ try_more = 0
383+ except PermissionError :
384+ # intermittent "Permission Denied" error can happen
385+ if try_more <= 0 :
386+ raise
387+ try_more -= 1
388+ time .sleep (0.1 )
389+
377390 if pbar is not None :
378391 pbar .update (size )
379392 logging .info (f"Dwnloaded the file { file_name } ." )
380393 if pbar is not None :
381394 pbar .close ()
382395 return file_name
383396
384- def download_project_archive (self , archive_name , show_progress = True ):
397+ def download_project_archive (
398+ self , archive_name , include_solution_result_files = True , show_progress = True
399+ ):
385400 """Create and download the project archive.
386401
387402 Parameters
388403 ----------
389404 archive_name : str
390405 Name of the project archive to use, without the file extension.
406+ include_solution_result_files : bool, default: True
407+ Whether to include solution and result files in the archive.
391408 show_progress : bool, default: True
392409 Whether to show a progress bar during the download.
393410 """
394411 if not re .match (r"^\w+$" , archive_name ):
395412 logging .error ("archive name should contain only alphanumeric characters" )
396413 return
397414 script = f"""import os
415+ import json
416+ successful = False
398417wd = GetServerWorkingDirectory()
399418if os.path.basename(GetProjectFile()).StartsWith("wbnew."):
400419 Save(FilePath=os.path.join(wd, "{ archive_name } .wbpj"), Overwrite=True)
401- Archive(FilePath=os.path.join(wd, "{ archive_name } .wbpz"))
420+ else:
421+ Save(Overwrite=True)
422+ Archive(FilePath=os.path.join(wd, "{ archive_name } .wbpz"),
423+ IncludeSkippedFiles={ include_solution_result_files } )
424+ successful = True
425+ wb_script_result =json.dumps(successful)
402426"""
403- self .run_script_string (script )
427+ archive_created = self .run_script_string (script )
428+ if not archive_created :
429+ logging .error (
430+ (
431+ "Failed to create the project archive. "
432+ "Make sure that the solver PyAnsys sessions are closed."
433+ )
434+ )
435+ return
404436 self .download_file (archive_name + ".wbpz" , show_progress = show_progress )
405437
406438 def __python_logging (self , log_level , msg ):
@@ -490,7 +522,7 @@ def start_mechanical_server(self, system_name):
490522
491523 Examples
492524 --------
493- Start a PyMechanical session for the given system name .
525+ Start a PyMechanical session for the given system.
494526
495527 >>> from ansys.mechanical.core import connect_to_mechanical
496528 >>> server_port=wb.start_mechanical_server(system_name=mech_system_name)
@@ -505,6 +537,23 @@ def start_mechanical_server(self, system_name):
505537 )
506538 return pymech_port
507539
540+ def stop_mechanical_server (self , system_name ):
541+ """Stop the PyMechanical server for the given system in the Workbench project.
542+
543+ Parameters
544+ ----------
545+ system_name : str
546+ Name of the system in the Workbench project.
547+
548+ Examples
549+ --------
550+ Stop the PyMechanical session for the given system.
551+
552+ >>> wb.stop_mechanical_server(system_name=mech_system_name)
553+
554+ """
555+ self .run_script_string (f"""StopMechanicalServerOnSystem(SystemName="{ system_name } ")""" )
556+
508557 def start_fluent_server (self , system_name ):
509558 """Start the PyFluent server for the given system in the Workbench project.
510559
@@ -521,7 +570,7 @@ def start_fluent_server(self, system_name):
521570
522571 Examples
523572 --------
524- Start a PyFluent session for the given system name .
573+ Start a PyFluent session for the given system.
525574
526575 >>> import ansys.fluent.core as pyfluent
527576 >>> server_info_file=wb.start_fluent_server(system_name=fluent_sys_name)
@@ -540,6 +589,23 @@ def start_fluent_server(self, system_name):
540589 self .download_file (server_info_file_name , show_progress = False )
541590 return local_copy
542591
592+ def stop_fluent_server (self , system_name ):
593+ """Stop the Fluent server for the given system in the Workbench project.
594+
595+ Parameters
596+ ----------
597+ system_name : str
598+ Name of the system in the Workbench project.
599+
600+ Examples
601+ --------
602+ Stop the Fluent session for the given system.
603+
604+ >>> wb.stop_fluent_server(system_name=mech_system_name)
605+
606+ """
607+ self .run_script_string (f"""StopFluentServerOnSystem(SystemName="{ system_name } ")""" )
608+
543609 def start_sherlock_server (self , system_name ):
544610 """Start the PySherlock server for the given system in the Workbench project.
545611
@@ -555,7 +621,7 @@ def start_sherlock_server(self, system_name):
555621
556622 Examples
557623 --------
558- Start a PySherlock session for the given system name .
624+ Start a PySherlock session for the given system.
559625
560626 >>> from ansys.sherlock.core import pysherlock
561627 >>> server_port=wb.start_sherlock_server(system_name=sherlock_system_name)
@@ -571,5 +637,22 @@ def start_sherlock_server(self, system_name):
571637 )
572638 return pysherlock_port
573639
640+ def stop_sherlock_server (self , system_name ):
641+ """Stop the Sherlock server for the given system in the Workbench project.
642+
643+ Parameters
644+ ----------
645+ system_name : str
646+ Name of the system in the Workbench project.
647+
648+ Examples
649+ --------
650+ Stop the Sherlock session for the given system.
651+
652+ >>> wb.stop_sherlock_server(system_name=mech_system_name)
653+
654+ """
655+ self .run_script_string (f"""StopSherlockServerOnSystem(SystemName="{ system_name } ")""" )
656+
574657
575658__all__ = ["WorkbenchClient" ]
0 commit comments