Source code for declaracad.occ.display

"""
Copyright (c) 2020, CodeLV.

Distributed under the terms of the GPL v3 License.

The full license is in the file LICENSE, distributed with this software.

Created on Dec 27, 2020

@author: jrm
"""

from typing import Any

from atom.api import Bool, Coerced, Float, ForwardTyped, Str, Tuple, Typed, observe
from enaml.colors import Color, ColorMember
from enaml.core.declarative import d_
from enaml.widgets.control import ProxyControl
from enaml.widgets.toolkit_object import ToolkitObject

from .geom import Direction, Point, coerce_direction, coerce_point


class ProxyDisplayItem(ProxyControl):
    #: A reference to the Shape declaration.
    declaration = ForwardTyped(lambda: DisplayItem)

    def set_position(self, position: Point):
        raise NotImplementedError

    def set_color(self, color: Color):
        raise NotImplementedError

    def set_transparency(self, transparency: float):
        raise NotImplementedError

    def set_direction(self, direction: Direction):
        raise NotImplementedError


class ProxyDisplayLine(ProxyDisplayItem):
    #: A reference to the Shape declaration.
    declaration = ForwardTyped(lambda: DisplayLine)


class ProxyDisplayPlane(ProxyDisplayItem):
    #: A reference to the Shape declaration.
    declaration = ForwardTyped(lambda: DisplayPlane)


class ProxyDisplayArrow(ProxyDisplayItem):
    #: A reference to the Shape declaration.
    declaration = ForwardTyped(lambda: DisplayArrow)

    def set_cone_size(self, size: tuple[float, float]):
        raise NotImplementedError

    def set_tube_size(self, size: tuple[float, float]):
        raise NotImplementedError


class ProxyDisplayText(ProxyDisplayItem):
    #: A reference to the Shape declaration.
    declaration = ForwardTyped(lambda: DisplayText)

    def set_text(self, text: str):
        raise NotImplementedError

    def set_size(self, size: float):
        raise NotImplementedError

    def set_font(self, font: str):
        raise NotImplementedError


class DisplayItem(ToolkitObject):
    """Basic display item. This represents an item in the display
    that has no effect on the model.

    """

    #: Reference to the implementation control
    proxy = Typed(ProxyDisplayItem)

    #: Whether the item should be displayed
    display = d_(Bool(True))

    #: Description
    description = d_(Str())

    #: A string representing the color of the shape.
    color = d_(ColorMember()).tag(view=True, group="Display")

    #: The transparency of the item
    transparency = d_(Float(strict=False))

    def _default_color(self) -> Color:
        return Color(0, 0, 0)

    #: A tuple or list of the (x, y, z) direction of this shape. This is
    #: coerced into a Point. The direction is relative to the dimensions axis.
    position = d_(Coerced(Point, coercer=coerce_point))

    def _default_position(self) -> Point:
        return Point(0, 0, 0)

    #: A tuple or list of the (x, y, z) direction of this shape. This is
    #: coerced into a Point. The direction is relative to the dimensions axis.
    direction = d_(Coerced(Direction, coercer=coerce_direction))

    def _default_direction(self) -> Direction:
        return Direction(0, 0, 1)

    @observe("position", "color", "direction")
    def _update_proxy(self, change: dict[str, Any]):
        super()._update_proxy(change)

    def show(self):
        """Generates the display item

        Returns
        -------
        item: Graphic3d item
            The item generated by this declaration.

        """
        if not self.is_initialized:
            self.initialize()
        if not self.proxy_is_active:
            self.activate_proxy()
        return self.proxy.item


[docs] class DisplayLine(DisplayItem): #: Reference to the implementation control proxy = Typed(ProxyDisplayLine)
[docs] class DisplayPlane(DisplayItem): #: Reference to the implementation control proxy = Typed(ProxyDisplayPlane)
[docs] class DisplayArrow(DisplayItem): """Add an arrow to the 3d display.""" #: Reference to the implementation control proxy = Typed(ProxyDisplayArrow) #: A tuple of (radius, length) cone_size = d_(Tuple()) #: A tuple of (radius, length) tube_size = d_(Tuple()) def _default_cone_size(self) -> tuple[float, float]: return (1, 2) def _default_tube_size(self) -> tuple[float, float]: return (0.5, 8) @observe("cone_size", "tube_size") def _update_proxy(self, change: dict[str, Any]): super()._update_proxy(change)
[docs] class DisplayText(DisplayItem): """Add text to the 3d display.""" #: Reference to the implementation control proxy = Typed(ProxyDisplayText) #: Text to display text = d_(Str()) #: Font size size = d_(Float(12)) #: Font family font = d_(Str()) @observe("text", "size", "font") def _update_proxy(self, change: dict[str, Any]): super()._update_proxy(change)