Skip to content

Commit

Permalink
refactor: Avoiding components for saving selections or for performing…
Browse files Browse the repository at this point in the history
… multiple selections.
  • Loading branch information
germa89 committed Sep 20, 2024
1 parent 96343fc commit 0f9ad5b
Showing 1 changed file with 30 additions and 42 deletions.
72 changes: 30 additions & 42 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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.")

Check warning on line 2708 in src/ansys/mapdl/core/mapdl_core.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_core.py#L2707-L2708

Added lines #L2707 - L2708 were not covered by tests
else:
type__ = type_

Check warning on line 2710 in src/ansys/mapdl/core/mapdl_core.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_core.py#L2710

Added line #L2710 was not covered by tests

# 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.
Expand Down

0 comments on commit 0f9ad5b

Please sign in to comment.