File: using_true_color_and_transparency.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (88 lines) | stat: -rw-r--r-- 2,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (c) 2015-2022 Manfred Moitzi
# License: MIT License
import pathlib
import ezdxf

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")

# ------------------------------------------------------------------------------
# This example shows how to use true-color and transparency.
# ------------------------------------------------------------------------------


def lwpolyline_with_true_color():
    # for true-color and transparency is DXF version AC1018 (ACAD R2004)
    # or newer necessary
    doc = ezdxf.new("AC1018")
    msp = doc.modelspace()

    points = [(0, 0), (3, 0), (6, 3), (6, 6)]
    msp.add_lwpolyline(
        points,
        dxfattribs={
            "color": 2,
            "true_color": ezdxf.rgb2int(
                (38, 140, 89)
            ),  # true color has higher priority than the color attribute
        },
    )
    doc.saveas(CWD / "true_color_lwpolyline.dxf")


# Another way to set true-color values for DXF entities: Property DXFEntity.rgb
def lines_with_true_color():
    # for true-color and transparency is DXF version AC1018 (ACAD R2004)
    # or newer necessary
    doc = ezdxf.new("AC1018")
    msp = doc.modelspace()
    for y in range(10):
        line = msp.add_line((0, y * 10), (100, y * 10))
        line.rgb = (50, y * 20, 50)  # set true color as RGB tuple
        # IMPORTANT: as you see it is not in the line.dxf namespace!
    # getting RGB values by r, g, b = line.rgb also works
    doc.saveas(CWD / "true_color_lines.dxf")


def rect(x, y, width=10, height=10):
    return (x, y), (x + width, y), (x, y + height), (x + width, y + height)


def solids_with_true_color():
    # for true-color and transparency is DXF version AC1018 (ACAD R2004)
    # or newer necessary
    doc = ezdxf.new("AC1018")
    msp = doc.modelspace()
    for i in range(10):
        x = i * 7
        y = i * 7
        solid = msp.add_solid(rect(x, y))
        solid.rgb = (50, i * 20, 50)  # set true-color as RGB tuple
        # IMPORTANT: as you see it is not in the solid.dxf namespace!
    doc.saveas(CWD / "true_color_solids.dxf")


def solids_with_transparency():
    # for true-color and transparency is DXF version AC1018 (ACAD R2004)
    # or newer necessary
    doc = ezdxf.new("AC1018")
    msp = doc.modelspace()
    for i in range(10):
        x = i * 7
        y = i * 7
        solid = msp.add_solid(rect(x, y, 20, 20))
        solid.transparency = (
            i / 10.0
        )  # set transparency as float between 0.0 (opaque) and 1.0 (100% transparent)
        # transparency: 0.0, 0.1, 0.2, ...
        # IMPORTANT: as you see it is not in the sold.dxf namespace and better
        # never use the raw DXF data for transparency
    doc.saveas(CWD / "transparent_solids.dxf")


if __name__ == '__main__':
    lwpolyline_with_true_color()
    lines_with_true_color()
    solids_with_true_color()
    solids_with_transparency()