From 0f9ad5bb6fb9d6a045c923cf500517a46d78dfb5 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Fri, 20 Sep 2024 21:02:31 +0200 Subject: [PATCH] refactor: Avoiding components for saving selections or for performing multiple selections. --- src/ansys/mapdl/core/mapdl_core.py | 72 +++++++++++++----------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index b3ddc6ddfb..961f73ef65 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -1393,47 +1393,40 @@ class _save_selection: def __init__(self, parent): self._parent = weakref.ref(parent) - self.selection_sets = [] - self.selection_sets_comps = [] + self.selection = [] def __enter__(self): self._parent()._log.debug("Entering saving selection context") - - selection_set_name = random_string(10) - self.selection_sets.append(selection_set_name) - self.selection_sets_comps.append(self._parent().components.names) - mapdl = self._parent() - prev_ier = mapdl.ignore_errors - mapdl.ignore_errors = True - for entity in ["kp", "lines", "area", "volu", "node", "elem"]: - mapdl.cm(f"_{selection_set_name}_{entity}_", f"{entity}", mute=True) - mapdl.ignore_errors = prev_ier + # Storing components + selection = { + "cmsel": mapdl.components.names, + # "components_type": mapdl.components.types, + "nsel": mapdl.mesh.nnum, + "esel": mapdl.mesh.enum, + "ksel": mapdl.geometry.knum, + "lsel": mapdl.geometry.lnum, + "asel": mapdl.geometry.anum, + "vsel": mapdl.geometry.vnum, + } + + self.selection.append(selection) def __exit__(self, *args): self._parent()._log.debug("Exiting saving selection context") - last_selection_name = self.selection_sets.pop() - last_selection_cmps = self.selection_sets_comps.pop() - + selection = self.selection.pop() mapdl = self._parent() - # Restore first the components, and later entities; so the entities - # selection can be overwritten but the components still activated. - - # Mute to avoid getting issues when the component wasn't created in - # first place because there was no entities. - self._parent().components.select(last_selection_cmps, mute=True) + cmps = selection.pop("cmsel") - # probably this is redundant - prev_ier = mapdl.ignore_errors - mapdl.ignore_errors = True - for entity in ["kp", "lines", "area", "volu", "node", "elem"]: - cmp_name = f"_{last_selection_name}_{entity}_" - mapdl.cmsel("s", cmp_name, f"{entity}", mute=True) - mapdl.cmdele(cmp_name) + if cmps: + mapdl.components.select(cmps) - mapdl.ignore_errors = prev_ier + for select_cmd, ids in selection.items(): + if ids.size > 0: + func = getattr(mapdl, select_cmd) + func(vmin=ids) class _chain_commands: """Store MAPDL commands and send one chained command.""" @@ -2706,22 +2699,17 @@ def _perform_entity_list_selection( self, entity, selection_function, type_, item, comp, vmin, kabs ): """Select entities using CM, and the supplied selection function.""" - self.cm(f"__temp_{entity}s__", f"{entity}") # Saving previous selection - # Getting new selection for id_, each_ in enumerate(vmin): - selection_function( - self, "S" if id_ == 0 else "A", item, comp, each_, "", "", kabs - ) - - self.cm(f"__temp_{entity}s_1__", f"{entity}") - - self.cmsel("S", f"__temp_{entity}s__") - self.cmsel(type_, f"__temp_{entity}s_1__") + if type_ == "S" or not type_: + type__ = "S" if id_ == 0 else "A" + # R is an issue, because first iteration will clean up the rest. + elif type_ == "R": + raise NotImplementedError("Mode R is not supported.") + else: + type__ = type_ - # Cleaning - self.cmdele(f"__temp_{entity}s__") - self.cmdele(f"__temp_{entity}s_1__") + selection_function(self, type__, item, comp, each_, "", "", kabs) def _raise_errors(self, text): # to make sure the following error messages are caught even if a breakline is in between.