5454 ProjectCurvesRequest ,
5555)
5656from ansys .api .geometry .v0 .commands_pb2_grpc import CommandsStub
57+ from ansys .geometry .core .connection .backend import BackendType
5758from ansys .geometry .core .connection .client import GrpcClient
5859from ansys .geometry .core .connection .conversions import (
5960 frame_to_grpc_frame ,
8182from ansys .geometry .core .misc .measurements import DEFAULT_UNITS , Angle , Distance
8283from ansys .geometry .core .sketch .sketch import Sketch
8384from ansys .geometry .core .typing import Real
85+ from ansys .tools .visualization_interface .utils .color import Color
8486
8587if TYPE_CHECKING : # pragma: no cover
8688 from pyvista import MultiBlock , PolyData
@@ -546,6 +548,7 @@ def plot(
546548 merge : bool = False ,
547549 screenshot : str | None = None ,
548550 use_trame : bool | None = None ,
551+ use_service_colors : bool | None = None ,
549552 ** plotting_options : dict | None ,
550553 ) -> None :
551554 """Plot the body.
@@ -560,8 +563,12 @@ def plot(
560563 Path for saving a screenshot of the image that is being represented.
561564 use_trame : bool, default: None
562565 Whether to enable the use of `trame <https://kitware.github.io/trame/index.html>`_.
563- The default is ``None``, in which case the ``USE_TRAME`` global setting
564- is used.
566+ The default is ``None``, in which case the
567+ ``ansys.tools.visualization_interface.USE_TRAME`` global setting is used.
568+ use_service_colors : bool, default: None
569+ Whether to use the colors assigned to the body in the service. The default
570+ is ``None``, in which case the ``ansys.geometry.core.USE_SERVICE_COLORS``
571+ global setting is used.
565572 **plotting_options : dict, default: None
566573 Keyword arguments for plotting. For allowable keyword arguments, see the
567574 :meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
@@ -757,24 +764,27 @@ def fill_style(self, value: FillStyle): # noqa: D102
757764 @property
758765 def color (self ) -> str : # noqa: D102
759766 """Get the current color of the body."""
760- if self ._color is None :
761- if self ._grpc_client .backend_version < (25 , 1 , 0 ): # pragma: no cover
767+ if self ._color is None and self .is_alive :
768+ # Assigning default value first
769+ self ._color = Color .DEFAULT .value
770+
771+ # TODO: Remove this check when the Linux service backend supports color setting
772+ # https://github.com/ansys/pyansys-geometry/issues/1383
773+ if self ._grpc_client .backend_type == BackendType .LINUX_SERVICE :
774+ self ._grpc_client .log .warning (
775+ "Colors are not supported in the Linux backend. Default value assigned..."
776+ )
777+ elif self ._grpc_client .backend_version < (25 , 1 , 0 ): # pragma: no cover
762778 # Server does not support color retrieval before version 25.1.0
763779 self ._grpc_client .log .warning (
764- "Server does not support color retrieval. Assigning default ."
780+ "Server does not support color retrieval. Default value assigned.. ."
765781 )
766- self ._color = "#000000" # Default color
767782 else :
768783 # Fetch color from the server if it's not cached
769784 color_response = self ._bodies_stub .GetColor (EntityIdentifier (id = self ._id ))
770-
771785 if color_response .color :
772786 self ._color = mcolors .to_hex (color_response .color )
773- else : # pragma: no cover
774- self ._grpc_client .log .warning (
775- f"Color could not be retrieved for body { self ._id } . Assigning default."
776- )
777- self ._color = "#000000" # Default color
787+
778788 return self ._color
779789
780790 @color .setter
@@ -980,6 +990,15 @@ def set_color(self, color: str | tuple[float, float, float]) -> None:
980990 """Set the color of the body."""
981991 self ._grpc_client .log .debug (f"Setting body color of { self .id } to { color } ." )
982992
993+ # TODO: Remove this check when the Linux service backend supports color setting
994+ # https://github.com/ansys/pyansys-geometry/issues/1383
995+ if self ._grpc_client .backend_type == BackendType .LINUX_SERVICE :
996+ self ._grpc_client .log .warning (
997+ "Setting color is not supported in the Linux service backend."
998+ )
999+ self ._grpc_client .log .warning ("Ignoring request..." )
1000+ return
1001+
9831002 try :
9841003 if isinstance (color , tuple ):
9851004 # Ensure that all elements are within 0-1 or 0-255 range
@@ -1125,6 +1144,7 @@ def plot( # noqa: D102
11251144 merge : bool = False ,
11261145 screenshot : str | None = None ,
11271146 use_trame : bool | None = None ,
1147+ use_service_colors : bool | None = None ,
11281148 ** plotting_options : dict | None ,
11291149 ) -> None :
11301150 raise NotImplementedError (
@@ -1509,17 +1529,27 @@ def plot( # noqa: D102
15091529 merge : bool = False ,
15101530 screenshot : str | None = None ,
15111531 use_trame : bool | None = None ,
1532+ use_service_colors : bool | None = None ,
15121533 ** plotting_options : dict | None ,
15131534 ) -> None :
15141535 # lazy import here to improve initial module load time
1536+ import ansys .geometry .core as pyansys_geometry
15151537 from ansys .geometry .core .plotting import GeometryPlotter
15161538 from ansys .tools .visualization_interface .types .mesh_object_plot import (
15171539 MeshObjectPlot ,
15181540 )
15191541
1520- meshobject = MeshObjectPlot (self , self .tessellate (merge = merge ))
1521- pl = GeometryPlotter (use_trame = use_trame )
1522- pl .plot (meshobject , ** plotting_options )
1542+ use_service_colors = (
1543+ use_service_colors
1544+ if use_service_colors is not None
1545+ else pyansys_geometry .USE_SERVICE_COLORS
1546+ )
1547+
1548+ mesh_object = (
1549+ self if use_service_colors else MeshObjectPlot (self , self .tessellate (merge = merge ))
1550+ )
1551+ pl = GeometryPlotter (use_trame = use_trame , use_service_colors = use_service_colors )
1552+ pl .plot (mesh_object , ** plotting_options )
15231553 pl .show (screenshot = screenshot , ** plotting_options )
15241554
15251555 def intersect (self , other : Union ["Body" , Iterable ["Body" ]], keep_other : bool = False ) -> None : # noqa: D102
@@ -1596,6 +1626,7 @@ def __repr__(self) -> str:
15961626 if self .is_surface :
15971627 lines .append (f" Surface thickness : { self .surface_thickness } " )
15981628 lines .append (f" Surface offset : { self .surface_offset } " )
1629+ lines .append (f" Color : { self .color } " )
15991630
16001631 nl = "\n "
16011632 return f"{ nl } { nl .join (lines )} { nl } "
0 commit comments