Exemplo n.º 1
0
    def export_dxf(self, tagwriter: "TagWriter") -> None:
        # Tag order matters!
        write_tag = tagwriter.write_tag2
        write_tag(450, self.kind)  # gradient or solid
        write_tag(451, 0)  # reserved for the future

        # rotation angle in radians:
        write_tag(460, math.radians(self.rotation))
        write_tag(461, self.centered)
        write_tag(452, self.one_color)
        write_tag(462, self.tint)
        write_tag(453, self.number_of_colors)
        if self.number_of_colors > 0:
            write_tag(463, 0)  # first value, see DXF standard
            if self.aci1 is not None:
                # code 63 "color as ACI" could be left off
                write_tag(63, self.aci1)
            write_tag(421, rgb2int(self.color1))  # first color
        if self.number_of_colors > 1:
            write_tag(463, 1)  # second value, see DXF standard
            if self.aci2 is not None:
                # code 63 "color as ACI" could be left off
                write_tag(63, self.aci2)
            write_tag(421, rgb2int(self.color2))  # second color
        write_tag(470, self.name)
Exemplo n.º 2
0
def test_set_bg_true_color(layout):
    mtext = layout.add_mtext("TEST").set_bg_color((10, 20, 30), scale=2)
    assert mtext.dxf.bg_fill == 1
    assert mtext.dxf.bg_fill_true_color == rgb2int((10, 20, 30))
    assert mtext.dxf.box_fill_scale == 2
    assert (mtext.dxf.hasattr("bg_fill_color") is True
            ), "bg_fill_color attribute must exists, else AutoCAD complains"
Exemplo n.º 3
0
 def attribute_true_color(self, data: bytes):
     self.reset_colors()
     code, value = colors.decode_raw_color(struct.unpack('<L', data)[0])
     if code == colors.COLOR_TYPE_RGB:
         self.true_color = colors.rgb2int(value)
     else:  # ACI colors, BYLAYER, BYBLOCK
         self.color = value
Exemplo n.º 4
0
    def bgcolor(self, rgb: RGB) -> None:
        color_value = (
            colors.rgb2int(rgb) | -0b111110000000000000000000000000
        )  # it's magic

        self.discard_xdata("HATCHBACKGROUNDCOLOR")
        self.set_xdata("HATCHBACKGROUNDCOLOR", [(1071, color_value)])
Exemplo n.º 5
0
 def items(self, default_values=False) -> List[Tuple[str, Any]]:
     """Returns the DXF attributes as list of name, value pairs, returns
     also the default values if argument `default_values` is ``True``.
     The true_color and transparency attributes do not have default values,
     the absence of these attributes is the default value.
     """
     data: List[Tuple[str, Any]] = []
     if default_values or self._layer != DEFAULT_LAYER:
         data.append(("layer", self._layer))
     if default_values or self._aci_color != DEFAULT_ACI_COLOR:
         data.append(("color", self._aci_color))
     if self._true_color is not None:
         # absence is the default value
         data.append(("true_color", colors.rgb2int(self._true_color)))
     if default_values or self._linetype != DEFAULT_LINETYPE:
         data.append(("linetype", self._linetype))
     if default_values or self._lineweight != DEFAULT_LINEWEIGHT:
         data.append(("lineweight", self.lineweight))
     if self._transparency is not None:
         # absence is the default value
         if self._transparency == TRANSPARENCY_BYBLOCK:
             data.append(("transparency", colors.TRANSPARENCY_BYBLOCK))
         else:
             data.append(
                 (
                     "transparency",
                     colors.float2transparency(self._transparency),
                 )
             )
     if default_values or self._ltscale != DEFAULT_LTSCALE:
         data.append(("ltscale", self._ltscale))
     return data
Exemplo n.º 6
0
    def set_bg_color(
        self,
        color: Union[int, str, Tuple[int, int, int], None],
        scale: float = 1.5,
        text_frame=False,
    ):
        """Set background color as :ref:`ACI` value or as name string or as RGB
        tuple ``(r, g, b)``.

        Use special color name ``canvas``, to set background color to canvas
        background color.

        Use `color` = ``None`` to remove the background filling.

        Setting only a text border is supported (`color`=``None``), but in this
        case the scaling is always 1.5.

        Args:
            color: color as :ref:`ACI`, string, RGB tuple or ``None``
            scale: determines how much border there is around the text, the
                value is based on the text height, and should be in the range
                of [1, 5], where 1 fits exact the MText entity.
            text_frame: draw a text frame in text color if ``True``

        """
        if 1 <= scale <= 5:
            self.dxf.box_fill_scale = scale
        else:
            raise ValueError("argument scale has to be in range from 1 to 5.")

        text_frame = const.MTEXT_TEXT_FRAME if text_frame else 0
        if color is None:
            self.dxf.discard("bg_fill")
            self.dxf.discard("box_fill_scale")
            self.dxf.discard("bg_fill_color")
            self.dxf.discard("bg_fill_true_color")
            self.dxf.discard("bg_fill_color_name")
            if text_frame:
                # special case, text frame only with scaling factor = 1.5
                self.dxf.bg_fill = 16
        elif color == "canvas":  # special case for use background color
            self.dxf.bg_fill = const.MTEXT_BG_CANVAS_COLOR | text_frame
            self.dxf.bg_fill_color = 0  # required but ignored
        else:
            self.dxf.bg_fill = const.MTEXT_BG_COLOR | text_frame
            if isinstance(color, int):
                self.dxf.bg_fill_color = color
            elif isinstance(color, str):
                self.dxf.bg_fill_color = 0  # required but ignored
                self.dxf.bg_fill_color_name = color
            elif isinstance(color, tuple):
                self.dxf.bg_fill_color = 0  # required but ignored
                self.dxf.bg_fill_true_color = rgb2int(color)
        return self  # fluent interface
Exemplo n.º 7
0
    def set_bg_color(self,
                     color: Union[int, str, Tuple[int, int, int], None],
                     scale: float = 1.5):
        """ Set background color as :ref:`ACI` value or as name string or as RGB
        tuple ``(r, g, b)``.

        Use special color name ``canvas``, to set background color to canvas
        background color.

        Args:
            color: color as :ref:`ACI`, string or RGB tuple
            scale: determines how much border there is around the text, the
                value is based on the text height, and should be in the range
                of [1, 5], where 1 fits exact the MText entity.

        """
        if 1 <= scale <= 5:
            self.dxf.box_fill_scale = scale
        else:
            raise ValueError('argument scale has to be in range from 1 to 5.')
        if color is None:
            self.dxf.discard('bg_fill')
            self.dxf.discard('box_fill_scale')
            self.dxf.discard('bg_fill_color')
            self.dxf.discard('bg_fill_true_color')
            self.dxf.discard('bg_fill_color_name')
        elif color == 'canvas':  # special case for use background color
            self.dxf.bg_fill = const.MTEXT_BG_CANVAS_COLOR
            self.dxf.bg_fill_color = 0  # required but ignored
        else:
            self.dxf.bg_fill = const.MTEXT_BG_COLOR
            if isinstance(color, int):
                self.dxf.bg_fill_color = color
            elif isinstance(color, str):
                self.dxf.bg_fill_color = 0  # required but ignored
                self.dxf.bg_fill_color_name = color
            elif isinstance(color, tuple):
                self.dxf.bg_fill_color = 0  # required but ignored
                self.dxf.bg_fill_true_color = rgb2int(color)
        return self  # fluent interface
Exemplo n.º 8
0
def test_from_rgb():
    assert 0xA0B0C0 == rgb2int((0xA0, 0xB0, 0xC0))
Exemplo n.º 9
0
 def rgb(self, rgb: Tuple[int, int, int]) -> None:
     """ Set RGB true color as (r, g, b)-tuple e.g. (12, 34, 56). """
     self.dxf.set('true_color', clr.rgb2int(rgb))
Exemplo n.º 10
0
 def test_true_color_descriptor_exists(self, entities: EntityQuery):
     assert (len(entities.true_color == colors.rgb2int((
         0, 0, 0))) == 0), "has no default value"
Exemplo n.º 11
0
 def rgb(self, rgb: clr.RGB) -> None:
     """Set RGB true color as (r, g , b) tuple e.g. (12, 34, 56)."""
     self.dxf.set("true_color", clr.rgb2int(rgb))
Exemplo n.º 12
0
 def attribute_true_color(self, data: bytes):
     # todo check byte order!
     self.true_color = rgb2int((data[1], data[2], data[3]))