From 103bdf17a81eb04e343a947efe2f5ca8d9cfe319 Mon Sep 17 00:00:00 2001 From: Samuel Lopez <85613111+Samuelopez-ansys@users.noreply.github.com> Date: Thu, 26 Oct 2023 16:55:19 +0200 Subject: [PATCH] Add unused variables delete (#3803) --- _unittest/test_09_VariableManager.py | 20 +++++++- pyaedt/application/Design.py | 11 ++++ pyaedt/application/Variables.py | 75 ++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/_unittest/test_09_VariableManager.py b/_unittest/test_09_VariableManager.py index fc42ebe3196..1fd01c51075 100644 --- a/_unittest/test_09_VariableManager.py +++ b/_unittest/test_09_VariableManager.py @@ -435,8 +435,9 @@ def test_13_postprocessing(self): def test_14_intrinsics(self): self.aedtapp["fc"] = "Freq" assert self.aedtapp["fc"] == "Freq" - assert self.aedtapp.variable_manager.dependent_variables["fc"].numeric_value == 1.0 - assert self.aedtapp.variable_manager.dependent_variables["fc"].units == "GHz" + assert self.aedtapp.variable_manager.dependent_variables["fc"].units == self.aedtapp.odesktop.GetDefaultUnit( + "Frequency" + ) def test_15_arrays(self): self.aedtapp["arr_index"] = 0 @@ -658,3 +659,18 @@ def test_31_test_validator_float_type_invalidate(self, validation_float_input): validation_errors = generate_validation_errors(property_names, expected_settings, actual_settings) assert len(validation_errors) == 1 + + def test_32_delete_unused_variables(self): + self.aedtapp.insert_design("used_variables") + self.aedtapp["used_var"] = "1mm" + self.aedtapp["unused_var"] = "1mm" + self.aedtapp["$project_used_var"] = "1" + self.aedtapp.modeler.create_rectangle(0, ["used_var", "used_var", "used_var"], [10, 20]) + mat1 = self.aedtapp.materials.add_material("new_copper2") + mat1.permittivity = "$project_used_var" + assert self.aedtapp.variable_manager.delete_variable("unused_var") + self.aedtapp["unused_var"] = "1mm" + number_of_variables = len(self.aedtapp.variable_manager.variable_names) + assert self.aedtapp.variable_manager.delete_unused_variables() + new_number_of_variables = len(self.aedtapp.variable_manager.variable_names) + assert number_of_variables != new_number_of_variables diff --git a/pyaedt/application/Design.py b/pyaedt/application/Design.py index be6978c1e23..b83305c3c82 100644 --- a/pyaedt/application/Design.py +++ b/pyaedt/application/Design.py @@ -3196,6 +3196,17 @@ def delete_variable(self, sVarName): """ return self.variable_manager.delete_variable(sVarName) + @pyaedt_function_handler() + def delete_unused_variables(self): + """Delete design and project unused variables. + + Returns + ------- + bool + ``True`` when successful, ``False`` when failed. + """ + return self.variable_manager.delete_unused_variables() + @pyaedt_function_handler() def insert_design(self, design_name=None, solution_type=None): """Add a design of a specified type. diff --git a/pyaedt/application/Variables.py b/pyaedt/application/Variables.py index 06d91b48ab2..48257aaadcd 100644 --- a/pyaedt/application/Variables.py +++ b/pyaedt/application/Variables.py @@ -1236,6 +1236,81 @@ def delete_variable(self, var_name): return True return False + @pyaedt_function_handler() + def is_used_variable(self, var_name): + """Find if a variable is used. + + Parameters + ---------- + var_name : str + Name of the variable. + + Returns + ------- + bool + ``True`` when successful, ``False`` when failed. + + """ + used = False + # Modeler + for obj in self._app.modeler.objects.values(): + used = self._find_used_variable_history(obj.history(), var_name) + if used: + self._logger.warning("{} used in modeler.".format(var_name)) + return used + + # Material + for mat in self._app.materials.material_keys.values(): + for _, v in mat._props.items(): + if isinstance(v, str) and var_name in re.findall("[$a-zA-Z0-9_]+", v): + used = True + self._logger.warning("{} used in the material: {}.".format(var_name, mat.name)) + return used + return used + + def _find_used_variable_history(self, history, var_name): + """Find if a variable is used. + + Parameters + ---------- + history : :class:`pyaedt.modeler.cad.elements3d.BinaryTree` + Object history. + + var_name : str + Name of the variable. + + Returns + ------- + bool + ``True`` when successful, ``False`` when failed. + + """ + used = False + for _, v in history.props.items(): + if isinstance(v, str) and var_name in re.findall("[a-zA-Z0-9_]+", v): + return True + for el in history.children.values(): + used = self._find_used_variable_history(el, var_name) + if used: + return True + return used + + @pyaedt_function_handler() + def delete_unused_variables(self): + """Delete unused design and project variables. + + Returns + ------- + bool + ``True`` when successful, ``False`` when failed. + """ + var_list = self.variable_names + + for var in var_list[:]: + if not self.is_used_variable(var): + self.delete_variable(var) + return True + @pyaedt_function_handler() def _get_var_list_from_aedt(self, desktop_object): var_list = []