|
| 1 | +# Copyright (C) 2024 - 2025 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +"""Provides the measure widget for the PyAnsys plotter.""" |
| 23 | +from pathlib import Path |
| 24 | +from typing import TYPE_CHECKING |
| 25 | + |
| 26 | +from vtk import vtkActor, vtkButtonWidget, vtkPNGReader |
| 27 | + |
| 28 | +from ansys.tools.visualization_interface.backends.pyvista.widgets.widget import PlotterWidget |
| 29 | + |
| 30 | +if TYPE_CHECKING: |
| 31 | + from ansys.tools.visualization_interface.backends.pyvista.pyvista import Plotter |
| 32 | + |
| 33 | + |
| 34 | +class PickRotCenterButton(PlotterWidget): |
| 35 | + """Provides the pick rotation center widget for the Visualization Interface Tool ``Plotter`` class. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + plotter_helper : PlotterHelper |
| 40 | + Plotter to add the pick rotation center widget to. |
| 41 | + dark_mode : bool, optional |
| 42 | + Whether to activate the dark mode or not. |
| 43 | +
|
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, plotter_helper: "Plotter", dark_mode: bool = False) -> None: |
| 47 | + """Initialize the ``PickRotCenterWidget`` class.""" |
| 48 | + # Call PlotterWidget ctor |
| 49 | + super().__init__(plotter_helper._pl.scene) |
| 50 | + self._dark_mode = dark_mode |
| 51 | + # Initialize variables |
| 52 | + self._actor: vtkActor = None |
| 53 | + self.plotter_helper = plotter_helper |
| 54 | + self._button: vtkButtonWidget = self.plotter_helper._pl.scene.add_checkbox_button_widget( |
| 55 | + self.callback, position=(45, 10), size=30, border_size=3 |
| 56 | + ) |
| 57 | + self.update() |
| 58 | + |
| 59 | + def callback(self, state: bool) -> None: |
| 60 | + """Remove or add the pick rotation center widget actor upon click. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + state : bool |
| 65 | + Whether the state of the button, which is inherited from PyVista, is active. |
| 66 | +
|
| 67 | + """ |
| 68 | + # This implementation uses direct calls to VTK due to limitations |
| 69 | + # in PyVista. If there are improvements in the compatibility between |
| 70 | + # the PyVista picker and the pick rotation center widget, this should be reviewed. |
| 71 | + |
| 72 | + # Lazy import to avoid circular import |
| 73 | + if not state: |
| 74 | + self._text_actor.SetVisibility(0) |
| 75 | + self.plotter_helper.scene.disable_picking() |
| 76 | + if self.plotter_helper._allow_picking: |
| 77 | + self.plotter_helper.enable_picking() |
| 78 | + elif self.plotter_helper._allow_hovering: |
| 79 | + self.plotter_helper.enable_hover() |
| 80 | + else: |
| 81 | + if self.plotter_helper._allow_picking: |
| 82 | + self.plotter_helper.disable_picking() |
| 83 | + elif self.plotter_helper._allow_hovering: |
| 84 | + self.plotter_helper.disable_hover() |
| 85 | + self.plotter_helper.enable_set_focus_center() |
| 86 | + self._text_actor = self.plotter_helper.scene.add_text( |
| 87 | + "Select a point to set the rotation center with right click", |
| 88 | + position="upper_edge", # options: 'upper_edge', 'lower_edge', 'left_edge', etc. |
| 89 | + font_size=14, |
| 90 | + color="grey", |
| 91 | + shadow=True |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | + def update(self) -> None: |
| 96 | + """Define the measurement widget button parameters.""" |
| 97 | + if self._dark_mode: |
| 98 | + is_inv = "_inv" |
| 99 | + else: |
| 100 | + is_inv = "" |
| 101 | + show_measure_vr = self._button.GetRepresentation() |
| 102 | + show_measure_icon_file = Path( |
| 103 | + Path(__file__).parent / "_images" / f"center_pick{is_inv}.png" |
| 104 | + ) |
| 105 | + show_measure_r = vtkPNGReader() |
| 106 | + show_measure_r.SetFileName(show_measure_icon_file) |
| 107 | + show_measure_r.Update() |
| 108 | + image = show_measure_r.GetOutput() |
| 109 | + show_measure_vr.SetButtonTexture(0, image) |
| 110 | + show_measure_vr.SetButtonTexture(1, image) |
0 commit comments