Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/3887.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refactor: do not import ansys.tools.visualizer by default when importing ansys.mapdl.core
1 change: 1 addition & 0 deletions doc/changelog.d/3897.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hold the bc settings per plotter instance
2 changes: 1 addition & 1 deletion doc/source/api/plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Various PyMAPDL specific plotting commands.
.. autosummary::
:toctree: _autosummary

plotting.MapdlPlotter
plotting.visualizer.MapdlPlotter
plotting.MapdlTheme
Mapdl.aplot
Mapdl.eplot
Expand Down
4 changes: 0 additions & 4 deletions src/ansys/mapdl/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#
import logging
import os
import sys
from typing import Dict, List, Tuple
from warnings import warn

Expand Down Expand Up @@ -135,6 +134,3 @@
get_mapdl_path,
save_ansys_path,
)

if _HAS_VISUALIZER:
from ansys.tools.visualization_interface import Plotter
21 changes: 8 additions & 13 deletions src/ansys/mapdl/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""Module for helper functions"""

from functools import namedtuple
import importlib
import importlib.util
import os
import sys
from warnings import warn
Expand All @@ -34,14 +34,15 @@
def is_installed(package_name: str) -> bool:
"""Check if a package is installed"""
package_name = package_name.replace("-", ".")

try:
importlib.import_module(package_name)

return True
except ModuleNotFoundError: # pragma: no cover
package_spec = importlib.util.find_spec(package_name)
if package_spec is None: # pragma: no cover
LOG.debug(f"The module '{package_name}' is not installed.")
return False
except ModuleNotFoundError:

Check warning on line 42 in src/ansys/mapdl/core/helpers.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/helpers.py#L42

Added line #L42 was not covered by tests
LOG.debug(f"The module '{package_name}' is not installed.")
return False
return True


def get_python_version() -> namedtuple:
Expand Down Expand Up @@ -94,13 +95,7 @@

def run_every_import() -> None:
# Run every time we import PyMAPDL
from ansys.mapdl.core import _HAS_VISUALIZER, RUNNING_TESTS

# Apply custom theme
if _HAS_VISUALIZER:
from ansys.mapdl.core.plotting.theme import _apply_default_theme

_apply_default_theme()
from ansys.mapdl.core import RUNNING_TESTS

# In case we want to do something specific for testing.
if RUNNING_TESTS: # pragma: no cover
Expand Down
1 change: 0 additions & 1 deletion src/ansys/mapdl/core/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ class GraphicsBackend(Enum):

if _HAS_VISUALIZER:
from ansys.mapdl.core.plotting.theme import MapdlTheme # noqa: F401
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter # noqa: F401
40 changes: 32 additions & 8 deletions src/ansys/mapdl/core/plotting/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"""Module for the MapdlPlotter class."""
from collections import OrderedDict
from typing import Any, Dict, Iterable, Optional, Union
from typing import Any, Callable, Dict, Iterable, Optional, Union

from ansys.tools.visualization_interface import Plotter
from ansys.tools.visualization_interface.backends.pyvista import PyVistaBackendInterface
Expand All @@ -43,12 +43,22 @@
)
from ansys.mapdl.core.plotting.theme import MapdlTheme

_FIRST_USE_RUN = False

if _HAS_VISUALIZER:
import pyvista as pv

from ansys.mapdl.core.plotting.plotting_defaults import DefaultSymbol

BC_plot_settings = DefaultSymbol()
def _first_use():
# Run first time we use the visualizer
global _FIRST_USE_RUN
if _FIRST_USE_RUN is True:
return
if _HAS_VISUALIZER:
from ansys.mapdl.core.plotting.theme import _apply_default_theme

_apply_default_theme()
_FIRST_USE_RUN = True


class MapdlPlotterBackend(PyVistaBackendInterface):
Expand Down Expand Up @@ -115,6 +125,7 @@ def __init__(
self, use_trame: bool = False, theme: pv.Plotter.theme = None, **plotter_kwargs
):
"""Initialize the ``MapdlPlotter`` class."""
_first_use()
self._backend = MapdlPlotterBackend(use_trame=use_trame, **plotter_kwargs)
super().__init__(backend=self._backend)
self._theme = theme
Expand All @@ -124,6 +135,19 @@ def __init__(
self._notebook = None
self._savefig = None
self._title = None
self._bc_settings = None

@property
def bc_settings(self) -> Callable:
"""Get the boundary condition settings object."""
if self._bc_settings is None:
self._make_bc_settings()
return self._bc_settings

def _make_bc_settings(self) -> None:
from ansys.mapdl.core.plotting.plotting_defaults import DefaultSymbol

self._bc_settings = DefaultSymbol()

def _bc_labels_checker(self, bc_labels):
"""Make sure we have allowed parameters and data types for ``bc_labels``"""
Expand Down Expand Up @@ -587,13 +611,13 @@ def bc_nodes_plot(
orient=False,
scale="scale",
# tolerance=0.05,
geom=BC_plot_settings(each_label)["glyph"],
geom=self.bc_settings(each_label)["glyph"],
)
name_ = f"{each_label}"
self.scene.add_mesh(
glyphs,
# name_filter=None,
color=BC_plot_settings(each_label)["color"],
color=self.bc_settings(each_label)["color"],
style="surface",
# style='wireframe',
# line_width=3,
Expand Down Expand Up @@ -644,11 +668,11 @@ def bc_nodes_plot(
# something it can be seen properly in the legend
label_ = value[1]
if "U" in label_:
value = [BC_plot_settings("UY")["glyph"], label_, value[2]]
value = [self.bc_settings("UY")["glyph"], label_, value[2]]
elif "F" in label_:
value = [BC_plot_settings("FX")["glyph"], label_, value[2]]
value = [self.bc_settings("FX")["glyph"], label_, value[2]]
else:
value = [BC_plot_settings(label_)["glyph"], label_, value[2]]
value = [self.bc_settings(label_)["glyph"], label_, value[2]]

if symbol == value[1]:
sorted_dict[key] = value
Expand Down
8 changes: 4 additions & 4 deletions src/ansys/mapdl/core/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@

import numpy as np

from ansys.mapdl.core import _HAS_VISUALIZER
from ansys.mapdl.core.errors import MapdlRuntimeError
from ansys.mapdl.core.misc import requires_package, supress_logging

if _HAS_VISUALIZER:
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter

COMPONENT_STRESS_TYPE = ["X", "Y", "Z", "XY", "YZ", "XZ"]
PRINCIPAL_TYPE = ["1", "2", "3"]
STRESS_TYPES = ["X", "Y", "Z", "XY", "YZ", "XZ", "1", "2", "3", "INT", "EQV"]
Expand Down Expand Up @@ -633,6 +629,8 @@ def _plot_point_scalars(self, scalars, show_node_numbering=False, **kwargs):
"exist within the result file."
)

from ansys.mapdl.core.plotting.visualizer import MapdlPlotter

with self._mapdl.save_selection:
mask = self.selected_nodes
nodes_ids = self._mapdl.get_array("NODE", item1="NLIST")
Expand Down Expand Up @@ -679,6 +677,8 @@ def _plot_cell_scalars(self, scalars, show_elem_numbering=False, **kwargs):
"exist within the result file."
)

from ansys.mapdl.core.plotting.visualizer import MapdlPlotter

with self._mapdl.save_selection:
# Select nodes to avoid segfault
self._mapdl.nsle("s", "all")
Expand Down
1 change: 0 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ def test_detach_examples_submodule():

assert 'ansys.mapdl.core' in sys.modules, 'PyMAPDL is not loaded!'
assert 'ansys.mapdl.core.examples' not in sys.modules, 'Examples is loaded!'
assert 'requests' in sys.modules, 'Requests is loaded!'

from ansys.mapdl.core.examples import vmfiles

Expand Down