File: mpolygon.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 (181 lines) | stat: -rw-r--r-- 6,373 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (c) 2015-2022 Manfred Moitzi
# License: MIT License
import pathlib
import ezdxf
from ezdxf import zoom
from ezdxf.lldxf import const

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

# ------------------------------------------------------------------------------
# Multiple example for creating a MPOLYGON entity.
# The MPOLYGON is very similar to the HATCH entity.
#
# docs: https://ezdxf.mozman.at/docs/dxfentities/mpolygon.html
# ------------------------------------------------------------------------------


def create_simple_mpolygon_no_fill(dxfversion="R2000"):
    doc = ezdxf.new(dxfversion)
    msp = doc.modelspace()
    mpolygon = msp.add_mpolygon(color=2, fill_color=None)
    mpolygon.paths.add_polyline_path(
        [(0, 0), (0, 3), (3, 6), (6, 6), (6, 3), (3, 0)]
    )
    zoom.extents(msp)
    doc.saveas(CWD / f"simple_mpolygon_no_fill_{dxfversion}.dxf")


def create_simple_solid_aci_filled_mpolygon(dxfversion="R2000"):
    doc = ezdxf.new(dxfversion)
    msp = doc.modelspace()
    mpolygon = msp.add_mpolygon(color=1, fill_color=5)
    mpolygon.paths.add_polyline_path(
        [(0, 0), (0, 3), (3, 6), (6, 6), (6, 3), (3, 0)]
    )
    zoom.extents(msp)
    doc.saveas(CWD / f"simple_solid_aci_filled_mpolygon_{dxfversion}.dxf")


def create_simple_solid_rgb_filled_mpolygon(dxfversion="R2000"):
    doc = ezdxf.new(dxfversion)
    msp = doc.modelspace()
    mpolygon = msp.add_mpolygon(color=1, fill_color=5)
    mpolygon.paths.add_polyline_path(
        [(0, 0), (0, 3), (3, 6), (6, 6), (6, 3), (3, 0)]
    )
    mpolygon.set_solid_fill(rgb=(60, 180, 60))  # overrides fill_color!
    zoom.extents(msp)
    doc.saveas(CWD / f"simple_solid_rgb_filled_mpolygon_{dxfversion}.dxf")


def create_mpolygon_with_bulge(dxfversion="R2000"):
    doc = ezdxf.new(dxfversion)
    msp = doc.modelspace()
    mpolygon = msp.add_mpolygon(color=1, fill_color=5)
    mpolygon.paths.add_polyline_path(
        [(0, 0), (0, 3, 0.5), (3, 6), (6, 6), (6, 3), (3, 0)]
    )
    zoom.extents(msp)
    doc.saveas(CWD / f"simple_mpolygon_with_bulge_{dxfversion}.dxf")


def create_simple_pattern_filled_mpolygon(dxfversion="R2000"):
    doc = ezdxf.new(dxfversion)
    msp = doc.modelspace()
    mpolygon = msp.add_mpolygon()
    mpolygon.set_pattern_fill("ANSI33", color=7, scale=0.01)
    mpolygon.paths.add_polyline_path(
        [(0, 0), (0, 3), (3, 6), (6, 6), (6, 3), (3, 0)]
    )
    zoom.extents(msp)
    doc.saveas(CWD / f"simple_pattern_filled_mpolygon_{dxfversion}.dxf")


def create_pattern_filled_mpolygon_with_bgcolor():
    doc = ezdxf.new("R2010")
    msp = doc.modelspace()
    # This is not supported by TrueView/BricsCAD!
    # TrueView doesn't show this MPOLYGON at all!
    mpolygon = msp.add_mpolygon()
    mpolygon.set_pattern_fill("ANSI33", color=7, scale=0.01)
    mpolygon.paths.add_polyline_path(
        [(0, 0), (0, 3), (3, 6), (6, 6), (6, 3), (3, 0)]
    )
    mpolygon.bgcolor = (100, 200, 100)
    zoom.extents(msp)
    doc.saveas(
        CWD / f"simple_pattern_filled_mpolygon_with_bgcolor_{dxfversion}.dxf"
    )


def using_hatch_style():
    def place_square_1(hatch, x, y):
        def shift(point):
            return x + point[0], y + point[1]

        # outer loop - flags = 1 (external) default value
        hatch.paths.add_polyline_path(
            map(shift, [(0, 0), (8, 0), (8, 8), (0, 8)])
        )
        # first inner loop - flags = 16 (outermost)
        hatch.paths.add_polyline_path(
            map(shift, [(2, 2), (7, 2), (7, 7), (2, 7)]),
            flags=const.BOUNDARY_PATH_OUTERMOST,
        )
        # any further inner loops - flags = 0 (default)
        hatch.paths.add_polyline_path(
            map(shift, [(4, 4), (6, 4), (6, 6), (4, 6)]),
            flags=const.BOUNDARY_PATH_DEFAULT,
        )

    def place_square_2(hatch, x, y):
        def shift(point):
            return x + point[0], y + point[1]

        # outer loop - flags = 1 (external) default value
        hatch.paths.add_polyline_path(
            map(shift, [(0, 0), (8, 0), (8, 8), (0, 8)])
        )
        # partly 1. inner loop - flags = 16 (outermost)
        hatch.paths.add_polyline_path(
            map(shift, [(3, 1), (7, 1), (7, 5), (3, 5)]),
            flags=const.BOUNDARY_PATH_OUTERMOST,
        )
        # partly 1. inner loop - flags = 16 (outermost)
        hatch.paths.add_polyline_path(
            map(shift, [(1, 3), (5, 3), (5, 7), (1, 7)]),
            flags=const.BOUNDARY_PATH_OUTERMOST,
        )

    doc = ezdxf.new("R2010")
    msp = doc.modelspace()
    # The hatch style tag, group code 75, is not supported for the MPOLYGON
    # entity by Autodesk products!
    # This example remains as it is, maybe I find a solution for this issue in
    # the future.

    # first create MPOLYGON entities
    hatch_style_0 = msp.add_mpolygon(
        color=3, fill_color=1, dxfattribs={"hatch_style": 0}
    )
    hatch_style_1 = msp.add_mpolygon(
        color=3, fill_color=1, dxfattribs={"hatch_style": 1}
    )
    hatch_style_2 = msp.add_mpolygon(
        color=3, fill_color=1, dxfattribs={"hatch_style": 2}
    )
    # then insert path elements to define the MPOLYGON boundaries
    place_square_1(hatch_style_0, 0, 0)
    place_square_1(hatch_style_1, 10, 0)
    place_square_1(hatch_style_2, 20, 0)

    # first create DXF mpolygon entities
    hatch_style_0b = msp.add_mpolygon(
        color=4, fill_color=2, dxfattribs={"hatch_style": 0}
    )
    hatch_style_1b = msp.add_mpolygon(
        color=4, fill_color=2, dxfattribs={"hatch_style": 1}
    )
    hatch_style_2b = msp.add_mpolygon(
        color=4, fill_color=2, dxfattribs={"hatch_style": 2}
    )

    # then insert path elements to define the MPOLYGON boundaries
    place_square_2(hatch_style_0b, 0, 10)
    place_square_2(hatch_style_1b, 10, 10)
    place_square_2(hatch_style_2b, 20, 10)
    zoom.extents(msp)
    doc.saveas(CWD / "mpolygon_with_hatch_styles.dxf")  # save DXF drawing


for dxfversion in ["R2000", "R2004", "R2007"]:
    create_simple_mpolygon_no_fill(dxfversion)
    create_simple_solid_aci_filled_mpolygon(dxfversion)
    create_simple_solid_rgb_filled_mpolygon(dxfversion)
    create_mpolygon_with_bulge(dxfversion)
    create_simple_pattern_filled_mpolygon(dxfversion)
    create_pattern_filled_mpolygon_with_bgcolor()
    using_hatch_style()