|
22 | 22 | """Module for managing a face.""" |
23 | 23 |
|
24 | 24 | from enum import Enum, unique |
| 25 | +from functools import cached_property |
25 | 26 | from typing import TYPE_CHECKING |
26 | 27 |
|
| 28 | +from beartype import beartype as check_input_types |
| 29 | +import matplotlib.colors as mcolors |
27 | 30 | from pint import Quantity |
28 | 31 |
|
29 | 32 | from ansys.api.dbu.v0.dbumodels_pb2 import EntityIdentifier |
|
34 | 37 | CreateIsoParamCurvesRequest, |
35 | 38 | EvaluateRequest, |
36 | 39 | GetNormalRequest, |
| 40 | + SetColorRequest, |
37 | 41 | ) |
38 | 42 | from ansys.api.geometry.v0.faces_pb2_grpc import FacesStub |
39 | 43 | from ansys.api.geometry.v0.models_pb2 import Edge as GRPCEdge |
40 | 44 | from ansys.geometry.core.connection.client import GrpcClient |
41 | 45 | from ansys.geometry.core.connection.conversions import grpc_curve_to_curve, grpc_surface_to_surface |
42 | 46 | from ansys.geometry.core.designer.edge import Edge |
43 | 47 | from ansys.geometry.core.errors import GeometryRuntimeError, protect_grpc |
| 48 | +from ansys.geometry.core.math.bbox import BoundingBox2D |
44 | 49 | from ansys.geometry.core.math.point import Point3D |
45 | 50 | from ansys.geometry.core.math.vector import UnitVector3D |
| 51 | +from ansys.geometry.core.misc.auxiliary import convert_color_to_hex |
46 | 52 | from ansys.geometry.core.misc.checks import ( |
47 | 53 | deprecated_method, |
48 | 54 | ensure_design_is_active, |
|
56 | 62 | ReversedTrimmedSurface, |
57 | 63 | TrimmedSurface, |
58 | 64 | ) |
| 65 | +from ansys.tools.visualization_interface.utils.color import Color |
59 | 66 |
|
60 | 67 | if TYPE_CHECKING: # pragma: no cover |
61 | 68 | from ansys.geometry.core.designer.body import Body |
@@ -181,6 +188,7 @@ def __init__( |
181 | 188 | self._commands_stub = CommandsStub(grpc_client.channel) |
182 | 189 | self._is_reversed = is_reversed |
183 | 190 | self._shape = None |
| 191 | + self._color = None |
184 | 192 |
|
185 | 193 | @property |
186 | 194 | def id(self) -> str: |
@@ -287,6 +295,57 @@ def loops(self) -> list[FaceLoop]: |
287 | 295 |
|
288 | 296 | return loops |
289 | 297 |
|
| 298 | + @property |
| 299 | + @protect_grpc |
| 300 | + @min_backend_version(25, 2, 0) |
| 301 | + def color(self) -> str: |
| 302 | + """Get the current color of the face.""" |
| 303 | + if self._color is None and self.body.is_alive: |
| 304 | + # Assigning default value first |
| 305 | + self._color = Color.DEFAULT.value |
| 306 | + |
| 307 | + # If color is not cached, retrieve from the server |
| 308 | + response = self._faces_stub.GetColor(EntityIdentifier(id=self.id)) |
| 309 | + |
| 310 | + # Return if valid color returned |
| 311 | + if response.color: |
| 312 | + self._color = mcolors.to_hex(response.color) |
| 313 | + else: |
| 314 | + self._color = Color.DEFAULT.value |
| 315 | + |
| 316 | + return self._color |
| 317 | + |
| 318 | + @color.setter |
| 319 | + def color(self, color: str | tuple[float, float, float]) -> None: |
| 320 | + self.set_color(color) |
| 321 | + |
| 322 | + @cached_property |
| 323 | + @protect_grpc |
| 324 | + @min_backend_version(25, 2, 0) |
| 325 | + def bounding_box(self) -> BoundingBox2D: |
| 326 | + """Get the bounding box for the face.""" |
| 327 | + self._grpc_client.log.debug(f"Getting bounding box for {self.id}.") |
| 328 | + |
| 329 | + result = self._faces_stub.GetBoundingBox(request=self._grpc_id) |
| 330 | + |
| 331 | + return BoundingBox2D(result.min.x, result.max.x, result.min.y, result.max.y) |
| 332 | + |
| 333 | + @protect_grpc |
| 334 | + @check_input_types |
| 335 | + @min_backend_version(25, 2, 0) |
| 336 | + def set_color(self, color: str | tuple[float, float, float]) -> None: |
| 337 | + """Set the color of the face.""" |
| 338 | + self._grpc_client.log.debug(f"Setting face color of {self.id} to {color}.") |
| 339 | + color = convert_color_to_hex(color) |
| 340 | + |
| 341 | + self._faces_stub.SetColor( |
| 342 | + SetColorRequest( |
| 343 | + face_id=self.id, |
| 344 | + color=color, |
| 345 | + ) |
| 346 | + ) |
| 347 | + self._color = color |
| 348 | + |
290 | 349 | @protect_grpc |
291 | 350 | @ensure_design_is_active |
292 | 351 | def normal(self, u: float = 0.5, v: float = 0.5) -> UnitVector3D: |
|
0 commit comments