diff --git a/src/ansys/aedt/core/application/design.py b/src/ansys/aedt/core/application/design.py index c611232d5d4..4eff9fce17e 100644 --- a/src/ansys/aedt/core/application/design.py +++ b/src/ansys/aedt/core/application/design.py @@ -65,6 +65,7 @@ from ansys.aedt.core.generic.constants import AEDT_UNITS from ansys.aedt.core.generic.constants import unit_system from ansys.aedt.core.generic.data_handlers import variation_string_to_dict +from ansys.aedt.core.generic.file_utils import available_file_name from ansys.aedt.core.generic.file_utils import check_and_download_file from ansys.aedt.core.generic.file_utils import generate_unique_name from ansys.aedt.core.generic.file_utils import is_project_locked @@ -1255,9 +1256,9 @@ def oproject(self, proj_name=None): settings.remote_rpc_session and settings.remote_rpc_session.filemanager.pathexists(proj_name) ): if ".aedtz" in proj_name: - name = self._generate_unique_project_name() - path = os.path.dirname(proj_name) - self.odesktop.RestoreProjectArchive(proj_name, os.path.join(path, name), True, True) + p = Path(proj_name).parent + save_to_file = available_file_name(p.with_suffix(".aedt")) + self.odesktop.RestoreProjectArchive(str(save_to_file.name), str(p), True, True) time.sleep(0.5) proj_name = name[:-5] self._oproject = self.desktop_class.active_project(proj_name) diff --git a/src/ansys/aedt/core/generic/file_utils.py b/src/ansys/aedt/core/generic/file_utils.py index afce9d3c03c..c8db17e9d32 100644 --- a/src/ansys/aedt/core/generic/file_utils.py +++ b/src/ansys/aedt/core/generic/file_utils.py @@ -315,6 +315,33 @@ def generate_unique_project_name( return str(prj) +@pyaedt_function_handler() +def available_file_name(full_file_name: Union[str, Path]) -> Path: + """Provide a file name that doesn't exist. + + If the input file name exists, increment the base + name and return a valid ``Path`` object with an updated name. + + Parameters + ---------- + full_file_name : str or :class:`pathlib.Path` + File to be saved. + + Returns + ------- + class:`pathlib.Path` + Valid file name with increment suffix `"_n.ext"`. If the file doesn't + exist, the original file name will be returned as a ``Path`` object. + """ + p = Path(full_file_name) + candidate = p + n = 1 + while candidate.exists(): + candidate = candidate.with_name(f"{p.stem}_{n}{p.suffix}") + n += 1 + return candidate + + @pyaedt_function_handler(startpath="path", filepattern="file_pattern") def recursive_glob(path: Union[str, Path], file_pattern: str): """Get a list of files matching a pattern, searching recursively from a start path.