Download this example

Download this example as a Jupyter Notebook or as a Python script.


Tools: Highlighting a face selection in the plotter#

This example demonstrates how to build a FaceSelection from a real model and overlay the selected faces on top of the plotted geometry by using the highlight parameter of GeometryPlotter.plot().

The workflow mirrors a typical selection-builder pipeline: start from all faces, filter the selection by edge count, area, and location, and then highlight only the final subset in the plot.

Perform required imports#

Perform the required imports.

[1]:
from pathlib import Path
import requests

from ansys.geometry.core import launch_modeler
from ansys.geometry.core.misc.options import ImportOptions
from ansys.geometry.core.plotting import GeometryPlotter
from ansys.geometry.core.selection_builder.selection_builder import RangeType

Download the example file#

Download the bracket model from the PyAnsys Geometry repository.

[2]:
BASE_URL = (
    "https://raw.githubusercontent.com/ansys/pyansys-geometry/main/tests/integration/files/"
)


def download_file(filename):
    """Download an example file from the PyAnsys Geometry repository."""
    url = BASE_URL + filename
    local_path = Path.cwd() / filename
    response = requests.get(url)
    response.raise_for_status()
    local_path.write_bytes(response.content)
    print(f"Downloaded: {filename}")
    return local_path


file_path = download_file("Bracket_Static_Stress.dsco")
Downloaded: Bracket_Static_Stress.dsco

Initialize the modeler#

[3]:
modeler = launch_modeler()
print(modeler)
Ansys Geometry Modeler (0x7f1d2c7370e0)

Ansys Geometry Modeler Client (0x7f1d2c736f90)
  Target:     localhost:700
  Connection: Healthy
  Backend info:
    Version:            27.1.0
    Backend type:       CORE_LINUX
    Backend number:     20260814.5
    API server number:  3140
    CADIntegration:     27.1.0.12937947

Import the bracket model#

Import the bracket file and preserve entity names.

[4]:
options = ImportOptions()
options.import_names = True

design = modeler.open_file(file_path=file_path, import_options=options)

Build the face selection to highlight#

Create a face selection and progressively narrow it down by using the same filtering workflow as the local plotting script.

[5]:
sb = modeler.create_selection_builder()

all_faces = sb.faces.get_all_faces()
print("All faces:", len(all_faces.items))

faces_3_4_edges = all_faces.filter_faces_by_edge_count(3, 4)
print("Faces with 3-4 edges:", len(faces_3_4_edges.items))

faces_by_area = faces_3_4_edges.filter_faces_by_area(0.00000247858, 0.0002564094)
print("Faces with area range:", len(faces_by_area.items))

faces_by_x = faces_by_area & faces_by_area.get_faces_with_x_location(
    range_type=RangeType.RANGETYPE_CONTAIN, min=0, max=0.010
)
print("Faces with x location 0-0.010:", len(faces_by_x.items))

remove_face = faces_by_x.get_faces_with_y_location(
    range_type=RangeType.RANGETYPE_INTERSECT, min=0.11753, max=0.122
)
print("Faces to remove:", len(remove_face.items))

final_faces = faces_by_x - remove_face
print("Final faces:", len(final_faces.items))
All faces: 99
Faces with 3-4 edges: 83
Faces with area range: 77
Faces with x location 0-0.010: 61
Faces to remove: 5
Final faces: 60

Highlight the selected faces in the plotter#

Pass the final FaceSelection to GeometryPlotter.plot() by using the highlight parameter. The base design is plotted first, and then the selected faces are overlaid on top of it.

[6]:
plotter = GeometryPlotter()
plotter.plot(design, highlight=final_faces)
plotter.show()

Close the modeler#

Close the modeler to free up resources and release the connection.

[7]:
modeler.close()

Download this example

Download this example as a Jupyter Notebook or as a Python script.