Skip to content

Commit 85e5df3

Browse files
feat: allow picking from easy access methods (#1499)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent c4c4d14 commit 85e5df3

File tree

5 files changed

+80
-31
lines changed

5 files changed

+80
-31
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,7 @@ docker/*-binaries.zip
164164
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
165165
#.idea/
166166

167+
# Ignore scripts folder
168+
scripts
169+
167170
# End of https://www.toptal.com/developers/gitignore/api/python

doc/changelog.d/1499.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow picking from easy access methods

doc/source/examples/01_getting_started/05_plotter_picker.mystnb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,6 @@ plotter = GeometryPlotter(use_trame=True)
168168
plotter.show(plot_list)
169169
```
170170

171-
## Render in different colors
172-
173-
You can render the objects in different colors automatically using PyVista's default
174-
color cycler. In order to do this, activate the ``multi_colors=True`` option when calling
175-
the ``plot()`` method.
176-
177-
In the following cell we will create a new design and plot a prism and a cylinder in different colors.
178-
179-
```{code-cell} ipython
180-
design = modeler.create_design("MultiColors")
181-
182-
# Create a sketch of a box
183-
sketch_box = Sketch().box(Point2D([0, 0], unit=UNITS.m), width=30 * UNITS.m, height=40 * UNITS.m)
184-
185-
# Create a sketch of a circle (overlapping the box slightly)
186-
sketch_circle = Sketch().circle(Point2D([20, 0], unit=UNITS.m), radius=3 * UNITS.m)
187-
188-
# Extrude both sketches to get a prism and a cylinder
189-
design.extrude_sketch("Prism", sketch_box, 50 * UNITS.m)
190-
design.extrude_sketch("Cylinder", sketch_circle, 50 * UNITS.m)
191-
192-
# Design plotting
193-
design.plot(multi_colors=True)
194-
```
195-
196171
## Clip objects
197172

198173
You can clip any object represented in the plotter by defining a ``Plane`` object that
@@ -231,6 +206,40 @@ picked_list = plotter.show(plot_list)
231206
print(picked_list)
232207
```
233208

209+
It is also possible to enable picking directly for a specific ``design`` or ``component``
210+
object alone. In the following cell, picking is enabled for the ``design`` object.
211+
212+
```{code-cell} ipython3
213+
picked_list = design.plot(allow_picking=True)
214+
print(picked_list)
215+
```
216+
217+
## Render in different colors
218+
219+
You can render the objects in different colors automatically using PyVista's default
220+
color cycler. In order to do this, activate the ``multi_colors=True`` option when calling
221+
the ``plot()`` method.
222+
223+
In the following cell we will create a new design and plot a prism and a cylinder in different colors.
224+
225+
```{code-cell} ipython
226+
design = modeler.create_design("MultiColors")
227+
228+
# Create a sketch of a box
229+
sketch_box = Sketch().box(Point2D([0, 0], unit=UNITS.m), width=30 * UNITS.m, height=40 * UNITS.m)
230+
231+
# Create a sketch of a circle (overlapping the box slightly)
232+
sketch_circle = Sketch().circle(Point2D([20, 0], unit=UNITS.m), radius=3 * UNITS.m)
233+
234+
# Extrude both sketches to get a prism and a cylinder
235+
design.extrude_sketch("Prism", sketch_box, 50 * UNITS.m)
236+
design.extrude_sketch("Cylinder", sketch_circle, 50 * UNITS.m)
237+
238+
# Design plotting
239+
design.plot(multi_colors=True)
240+
```
241+
242+
234243
## Close session
235244

236245
When you finish interacting with your modeling service, you should close the active

src/ansys/geometry/core/designer/component.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""Provides for managing components."""
2323

2424
from enum import Enum, unique
25-
from typing import TYPE_CHECKING, Optional, Union
25+
from typing import TYPE_CHECKING, Any, Optional, Union
2626
import uuid
2727

2828
from beartype import beartype as check_input_types
@@ -1394,8 +1394,9 @@ def plot(
13941394
screenshot: str | None = None,
13951395
use_trame: bool | None = None,
13961396
use_service_colors: bool | None = None,
1397+
allow_picking: bool | None = None,
13971398
**plotting_options: dict | None,
1398-
) -> None:
1399+
) -> None | list[Any]:
13991400
"""Plot the component.
14001401
14011402
Parameters
@@ -1419,9 +1420,17 @@ def plot(
14191420
Whether to use the colors assigned to the body in the service. The default
14201421
is ``None``, in which case the ``ansys.geometry.core.USE_SERVICE_COLORS``
14211422
global setting is used.
1423+
allow_picking : bool, default: None
1424+
Whether to enable picking. The default is ``None``, in which case the
1425+
picker is not enabled.
14221426
**plotting_options : dict, default: None
14231427
Keyword arguments for plotting. For allowable keyword arguments, see the
14241428
1429+
Returns
1430+
-------
1431+
None | list[Any]
1432+
If ``allow_picking=True``, a list of picked objects is returned. Otherwise, ``None``.
1433+
14251434
Examples
14261435
--------
14271436
Create 25 small cylinders in a grid-like pattern on the XY plane and
@@ -1465,6 +1474,16 @@ def plot(
14651474
if use_service_colors is not None
14661475
else pyansys_geometry.USE_SERVICE_COLORS
14671476
)
1477+
# If picking is enabled, we should not merge the component
1478+
if allow_picking:
1479+
# This blocks the user from selecting the component itself
1480+
# but honestly, who would want to select the component itself since
1481+
# you already have a reference to it? It is the object you are plotting!
1482+
self._grpc_client.log.info(
1483+
"Ignoring 'merge_component=True' (default behavior) as "
1484+
"'allow_picking=True' has been requested."
1485+
)
1486+
merge_component = False
14681487

14691488
# Add merge_component and merge_bodies to the plotting options
14701489
plotting_options["merge_component"] = merge_component
@@ -1479,9 +1498,13 @@ def plot(
14791498
"'multi_colors' or 'use_service_colors' are defined."
14801499
)
14811500

1482-
pl = GeometryPlotter(use_trame=use_trame, use_service_colors=use_service_colors)
1501+
pl = GeometryPlotter(
1502+
use_trame=use_trame,
1503+
use_service_colors=use_service_colors,
1504+
allow_picking=allow_picking,
1505+
)
14831506
pl.plot(self, **plotting_options)
1484-
pl.show(screenshot=screenshot, **plotting_options)
1507+
return pl.show(screenshot=screenshot, **plotting_options)
14851508

14861509
def __repr__(self) -> str:
14871510
"""Represent the ``Component`` as a string."""

src/ansys/geometry/core/plotting/plotter.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def show(
437437
plotting_object: Any = None,
438438
screenshot: str | None = None,
439439
**plotting_options,
440-
) -> None:
440+
) -> None | list[Any]:
441441
"""Show the plotter.
442442
443443
Parameters
@@ -452,4 +452,17 @@ def show(
452452
"""
453453
if plotting_object is not None:
454454
self.plot(plotting_object, **plotting_options)
455-
self._backend.show(screenshot=screenshot, **plotting_options)
455+
picked_objs = self._backend.show(screenshot=screenshot, **plotting_options)
456+
457+
# Return the picked objects if picking is enabled... but as the actual PyAnsys
458+
# Geometry objects (or PyVista objects if not)
459+
if picked_objs:
460+
lib_objects = []
461+
for element in picked_objs:
462+
if isinstance(element, MeshObjectPlot):
463+
lib_objects.append(element.custom_object)
464+
elif isinstance(element, EdgePlot):
465+
lib_objects.append(element.edge_object)
466+
else: # Either a PyAnsys Geometry object or a PyVista object
467+
lib_objects.append(element)
468+
return lib_objects

0 commit comments

Comments
 (0)