Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend glyphs improvements #2902

Merged
merged 23 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
34 changes: 25 additions & 9 deletions src/ansys/mapdl/core/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
"FY",
"FZ",
"AMPS",
"CHRGS",
"CHRG",
# "FLUX",
"CSGZ",
] # TODO: Add moments MX, MY, MZ
FIELDS = {
"MECHANICAL": ["UX", "UY", "UZ", "FX", "FY", "FZ"],
"THERMAL": ["TEMP", "HEAT"],
"ELECTRICAL": ["VOLT", "CHRGS", "AMPS"],
"ELECTRICAL": ["VOLT", "CHRG", "AMPS"],
}

FIELDS_ORDERED_LABELS = FIELDS["MECHANICAL"].copy()
Expand Down Expand Up @@ -668,7 +668,7 @@ def general_plotter(

* **'electrical'**
To plot the following electrical boundary conditions:
'VOLT', 'CHRGS', and 'AMPS'.
'VOLT', 'CHRG', and 'AMPS'.

Defaults to all the allowed boundary conditions present
in the responses of :func:`ansys.mapdl.core.Mapdl.dlist`
Expand Down Expand Up @@ -745,7 +745,7 @@ def general_plotter(
+------------+--------------------------------------+
| THERMAL | ["TEMP", "HEAT"] |
+------------+--------------------------------------+
| ELECTRICAL | ["VOLT", "CHRGS", "AMPS"] |
| ELECTRICAL | ["VOLT", "CHRG", "AMPS"] |
+------------+--------------------------------------+

Examples
Expand Down Expand Up @@ -903,6 +903,9 @@ def bc_plotter(
else:
bc_labels = _bc_labels_default(mapdl)

if not bc_labels:
return pl

if bc_target:
bc_target = _bc_target_checker(bc_target)
else:
Expand All @@ -919,10 +922,11 @@ def bc_plotter(
bc_glyph_size = get_bounding_box(mapdl.mesh.nodes)
bc_glyph_size = bc_glyph_size[bc_glyph_size != 0]

ratio = 0.075 # Because a glyph of 1 is too big.
if bc_glyph_size.size != 0:
bc_glyph_size = bc_glyph_size.mean() * 0.75 / 10
bc_glyph_size = bc_glyph_size.mean() * ratio
else: # Case were there is only one node
bc_glyph_size = 1
bc_glyph_size = ratio

if not isinstance(bc_glyph_size, (int, float)):
raise ValueError("The 'bc_glyph_size' parameter can be only an int or float.")
Expand Down Expand Up @@ -1035,6 +1039,11 @@ def bc_nodes_plotter(
pcloud["labels"],
shape_opacity=0.25,
font_size=bc_labels_font_size,
# There is a conflict here. See
# To do not hide the labels, even when the underlying nodes
# are hidden, we set "always_visible"
always_visible=True,
show_points=False, # to not have node duplicity
)

if plot_bc_legend:
Expand All @@ -1045,16 +1054,23 @@ def bc_nodes_plotter(
# sorting the keys
for symbol in FIELDS_ORDERED_LABELS:
for key, value in labels_.items():
# taking advantage and overriding the legend glyph with 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]]
elif "F" in label_:
value = [BC_plot_settings("FX")["glyph"], label_, value[2]]
elif label_ in FIELDS["ELECTRICAL"]:
value = [BC_plot_settings("VOLT")["glyph"], label_, value[2]]

if symbol == value[1]:
sorted_dict[key] = value

# moving the not added labels (just in case)
for key, value in labels_.items():
if value[1] not in FIELDS_ORDERED_LABELS:
if label_ not in FIELDS_ORDERED_LABELS:
sorted_dict[key] = value

assert sorted_dict == labels_

# overwriting labels
pl.renderer._labels = sorted_dict
pl.add_legend(bcolor=None)
Expand Down
192 changes: 102 additions & 90 deletions src/ansys/mapdl/core/plotting_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,48 @@
# SOFTWARE.

import pyvista as pv

# Symbols for constrains
from pyvista.core import _vtk_core as _vtk
from pyvista.core.utilities import translate
from pyvista.core.utilities.helpers import wrap
from pyvista.core.utilities.misc import no_new_attr

# I dont want to have to fix a very recent lower bound for pyvista.
# Hence I'm copying what I need from there.
# copied from pyvista source code:
# https://github.com/pyvista/pyvista/blob/35396c2e7645a6b57ad30d25ac1893f2141aab95/pyvista/core/utilities/geometric_sources.py#L2254


@no_new_attr
class ArrowSource(_vtk.vtkArrowSource):
def __init__(
self,
tip_length=0.25,
tip_radius=0.1,
tip_resolution=20,
shaft_radius=0.05,
shaft_resolution=20,
):
"""Initialize source."""
self.SetTipLength(tip_length)
self.SetTipRadius(tip_radius)
self.SetTipResolution(tip_resolution)
self.SetShaftResolution(shaft_resolution)
self.SetShaftRadius(shaft_radius)

@property
def output(self):
"""Get the output data object for a port on this algorithm.

Returns
-------
pyvista.PolyData
Plane mesh.
"""
self.Update()
return wrap(self.GetOutput())


# Symbols for constrains
class DefaultSymbol:
# Class to store the symbols because of https://github.com/ansys/pymapdl/issues/2872
# To solve that issue, we avoid having to load the pyvista attributes when the module is load.
Expand All @@ -41,109 +79,83 @@ def __call__(self, name):
return getattr(self, name)

def _set_configuration(self):
# Setting the
self.TEMP = {
"color": "orange",
"glyph": pv.Sphere(center=(0, 0, 0), radius=0.5),
}
self._point = pv.Sphere(center=(0, 0, 0), radius=0.5)
self._cube = pv.Cube(center=(0, 0, 0), x_length=1, y_length=1, z_length=1)

def _basic_arrow(
start=(0.0, 0.0, 0.0),
direction=(1.0, 0.0, 0.0),
tip_length=0.5,
tip_radius=0.25,
tip_resolution=20,
shaft_radius=0.05,
shaft_resolution=20,
invert=True,
):
arrow = ArrowSource(
tip_length=tip_length,
tip_radius=tip_radius,
tip_resolution=tip_resolution,
shaft_radius=shaft_radius,
shaft_resolution=shaft_resolution,
)
arrow.SetInvert(invert)
surf = arrow.output

self.HEAT = {
"color": "red",
"glyph": pv.Arrow(
start=(-1, 0, 0),
direction=(1, 0, 0),
tip_length=1,
tip_radius=0.5,
scale=1.0,
),
}
translate(surf, start, direction)
return surf

self.UX = {
"color": "red",
"glyph": pv.Arrow(
start=(0, -1, 0),
direction=(0, 1, 0),
tip_length=1,
tip_radius=0.5,
scale=1.0,
),
}
def _arrow(*args, **kwargs):
return _basic_arrow(*args, **kwargs)

self.UY = {
"color": "green",
"glyph": pv.Arrow(
start=(0, 0, -1),
direction=(0, 0, 1),
def _cone(start=(0, 0, 0), direction=None):
return _basic_arrow(
start=start,
direction=direction,
tip_length=1,
tip_radius=0.5,
scale=1.0,
),
}
)

self.UZ = {
"color": "blue",
"glyph": pv.Arrow(
start=(-1, 0, 0),
direction=(1, 0, 0),
tip_length=0.5,
tip_radius=0.25,
scale=1.0,
),
self.TEMP = {
"color": "orange",
"glyph": self._point,
}

self.VOLT = {
"color": "yellow",
"glyph": pv.Arrow(
start=(0, -1, 0),
direction=(0, 1, 0),
tip_length=0.5,
tip_radius=0.25,
scale=1.0,
),
}
self.HEAT = {"color": "red", "glyph": self._cube}

self.FX = {
"color": "red",
"glyph": pv.Arrow(
start=(0, 0, -1),
direction=(0, 0, 1),
tip_length=0.5,
tip_radius=0.25,
scale=1.0,
),
}
self.UX = {"color": "red", "glyph": _cone(direction=(-1, 0, 0))}
self.UY = {"color": "green", "glyph": _cone(direction=(0, -1, 0))}
self.UZ = {"color": "blue", "glyph": _cone(direction=(0, 0, -1))}

def get_VOLT():
model_a = pv.Cylinder(
center=(0, 0, 0), direction=(1, 0, 0), radius=0.2, height=2
).triangulate()
self.FX = {"color": "red", "glyph": _arrow(direction=(-1, 0, 0))}
self.FY = {"color": "green", "glyph": _arrow(direction=(0, -1, 0))}
self.FZ = {"color": "blue", "glyph": _arrow(direction=(0, 0, -1))}

model_b = pv.Cylinder(
center=(0, 0, 0), direction=(0, 1, 0), radius=0.2, height=2
).triangulate()
self.VOLT = {"color": "yellow", "glyph": self.cross_cylinders_3d()}

model_c = pv.Cylinder(
center=(0, 0, 0), direction=(0, 0, 1), radius=0.2, height=2
).triangulate()
self.AMPS = {"color": "red", "glyph": self.cross_cylinders_3d()}
self.CHRG = {"color": "red", "glyph": self.cross_cylinders_3d()}

result = model_a.merge(model_b).triangulate()
result = result.merge(model_c)
@staticmethod
def cross_cylinders_3d():
model_a = pv.Cylinder(
center=(0, 0, 0), direction=(1, 0, 0), radius=0.2, height=1.5
).triangulate()

result.rotate_z(45.0, inplace=True)
result.rotate_vector(
vector=(1, -1, 0), angle=-45, point=(0, 0, 0), inplace=True
)
model_b = pv.Cylinder(
center=(0, 0, 0), direction=(0, 1, 0), radius=0.2, height=1.5
).triangulate()

return result
model_c = pv.Cylinder(
center=(0, 0, 0), direction=(0, 0, 1), radius=0.2, height=1.5
).triangulate()

self.FY = {"color": "green", "glyph": get_VOLT()}
result = model_a.merge(model_b).triangulate()
result = result.merge(model_c)

self.FZ = {
"color": "blue",
"glyph": pv.Cube(
center=(0, 0, 0), x_length=1.0, y_length=1.0, z_length=1.0
),
}
result.rotate_z(45.0, inplace=True)
result.rotate_vector(
vector=(1, -1, 0), angle=-45, point=(0, 0, 0), inplace=True
)

self.AMPS = {"color": "grey", "glyph": get_VOLT()}
self.CHRGS = {"color": "grey", "glyph": get_VOLT()}
return result
Binary file modified tests/.image_cache/bc_plot_options[False-False-False].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[False-False-True].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[False-True-False].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[False-True-True].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[True-False-False].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[True-False-True].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[True-True-False].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/.image_cache/bc_plot_options[True-True-True].png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/.image_cache/single_glyph[0-CHRG].png
Binary file modified tests/.image_cache/single_glyph[0-FX].png
Binary file modified tests/.image_cache/single_glyph[0-FY].png
Binary file modified tests/.image_cache/single_glyph[0-FZ].png
Binary file added tests/.image_cache/single_glyph[0-HEAT].png
Binary file added tests/.image_cache/single_glyph[0-TEMP].png
Binary file modified tests/.image_cache/single_glyph[0-UX].png
Binary file modified tests/.image_cache/single_glyph[0-UY].png
Binary file modified tests/.image_cache/single_glyph[0-UZ].png
Binary file added tests/.image_cache/single_glyph[0-VOLT].png
Binary file added tests/.image_cache/single_glyph[50-CHRG].png
Binary file modified tests/.image_cache/single_glyph[50-FX].png
Binary file modified tests/.image_cache/single_glyph[50-FY].png
Binary file modified tests/.image_cache/single_glyph[50-FZ].png
Binary file added tests/.image_cache/single_glyph[50-HEAT].png
Binary file added tests/.image_cache/single_glyph[50-TEMP].png
Binary file modified tests/.image_cache/single_glyph[50-UX].png
Binary file modified tests/.image_cache/single_glyph[50-UY].png
Binary file modified tests/.image_cache/single_glyph[50-UZ].png
Binary file added tests/.image_cache/single_glyph[50-VOLT].png
Binary file modified tests/.image_cache/single_glyph[500-FX].png
Binary file modified tests/.image_cache/single_glyph[500-FY].png
Binary file modified tests/.image_cache/single_glyph[500-FZ].png
Binary file added tests/.image_cache/single_glyph[500-HEAT].png
Binary file added tests/.image_cache/single_glyph[500-TEMP].png
Binary file modified tests/.image_cache/single_glyph[500-UX].png
Binary file modified tests/.image_cache/single_glyph[500-UY].png
Binary file modified tests/.image_cache/single_glyph[500-UZ].png
Binary file added tests/.image_cache/single_glyph[500-VOLT].png
Loading
Loading