File: multileader.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 (372 lines) | stat: -rw-r--r-- 13,462 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Copyright (c) 2021-2023, Manfred Moitzi
# License: MIT License
from __future__ import annotations
from typing import TYPE_CHECKING
import pathlib
import logging

import ezdxf
from ezdxf import colors
from ezdxf.enums import TextEntityAlignment
from ezdxf.gfxattribs import GfxAttribs
from ezdxf.math import Vec2, UCS, NULLVEC, ConstructionBox
from ezdxf.render import forms, mleader

if TYPE_CHECKING:
    from ezdxf.document import Drawing
    from ezdxf.eztypes import BlockLayout

# ========================================
# Setup logging
# ========================================
logging.basicConfig(level="WARNING")

# ========================================
# Setup your preferred output directory
# ========================================
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path()

# ------------------------------------------------------------------------------
# This example shows how to create MULTILEADER entities.
#
# docs: https://ezdxf.mozman.at/docs/dxfentities/mleader.html
# tutorial: https://ezdxf.mozman.at/docs/tutorials/mleader.html
# ------------------------------------------------------------------------------

DXFVERSION = "R2013"


def simple_mtext_content_horizontal(name: str):
    doc = ezdxf.new(DXFVERSION, setup=True)
    msp = doc.modelspace()
    ml_builder = msp.add_multileader_mtext("Standard")
    ml_builder.set_content("Line1\nLine2", style="OpenSans")

    # Construction plane of the entity is defined by an render UCS.
    # The default render UCS is the WCS.
    # The leader lines vertices are expected in render UCS coordinates, which
    # means relative to the UCS origin!
    # This example shows the simplest way UCS==WCS!
    ml_builder.add_leader_line(mleader.ConnectionSide.right, [Vec2(40, 15)])
    ml_builder.add_leader_line(mleader.ConnectionSide.right, [Vec2(40, -15)])
    ml_builder.add_leader_line(mleader.ConnectionSide.left, [Vec2(-20, -15)])

    # The insert point (in UCS coordinates= is the alignment point for MTEXT
    # content and the insert location for BLOCK content:
    ml_builder.build(insert=Vec2(5, 0))

    doc.set_modelspace_vport(60, center=(10, 5))
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def all_mtext_content_horizontal(name: str):
    doc = ezdxf.new(DXFVERSION, setup=True)
    msp = doc.modelspace()
    attribs = GfxAttribs(color=colors.RED)
    for direction in range(9):
        for ax, alignment in enumerate(
            [
                mleader.TextAlignment.left,
                mleader.TextAlignment.center,
                mleader.TextAlignment.right,
            ]
        ):
            ml_builder = msp.add_multileader_mtext("Standard")
            dir_enum = mleader.HorizontalConnection(direction)
            ml_builder.set_connection_types(
                left=dir_enum,
                right=dir_enum,
            )

            ml_builder.set_content(
                f"{dir_enum.name}\n{dir_enum.name}\n{dir_enum.name}",
                alignment=alignment,
                style="OpenSans",
            )
            width = len(dir_enum.name) * 2.5
            x0: float = -30
            x1: float = 40 + width
            y0: float = -15
            y1: float = 20

            ml_builder.add_leader_line(
                mleader.ConnectionSide.right, [Vec2(x1, y1)]
            )
            ml_builder.add_leader_line(
                mleader.ConnectionSide.right, [Vec2(x1, y0)]
            )
            ml_builder.add_leader_line(
                mleader.ConnectionSide.left, [Vec2(x0, y0)]
            )
            ml_builder.add_leader_line(
                mleader.ConnectionSide.left, [Vec2(x0, y1)]
            )
            x: float = 5
            if alignment == mleader.TextAlignment.center:
                x += width / 2
            elif alignment == mleader.TextAlignment.right:
                x += width
            ucs = UCS(origin=(ax * 250, direction * 50))
            insert = Vec2(x, y1 / 2)
            ml_builder.build(insert=insert, ucs=ucs)
            e = msp.add_lwpolyline(
                [(x0, y0), (x1, y0), (x1, y1), (x0, y1)],
                close=True,
                dxfattribs=attribs,
            )
            e.transform(ucs.matrix)
            e = msp.add_circle(insert, radius=0.5, dxfattribs=attribs)
            e.transform(ucs.matrix)

    height = 600
    doc.set_modelspace_vport(height, center=(200, height / 2))
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def simple_mtext_content_vertical(name: str):
    doc = ezdxf.new(DXFVERSION, setup=True)
    msp = doc.modelspace()
    ml_builder = msp.add_multileader_mtext("Standard")
    ml_builder.set_content("Line1\nLine2", style="OpenSans")

    # Construction plane of the entity is defined by an render UCS.
    # The default render UCS is the WCS.
    # The leader lines vertices are expected in render UCS coordinates, which
    # means relative to the UCS origin!
    # This example shows the simplest way UCS==WCS!
    ml_builder.set_connection_types(
        top=mleader.VerticalConnection.center_overline,
        bottom=mleader.VerticalConnection.center_overline,
    )
    ml_builder.add_leader_line(mleader.ConnectionSide.top, [Vec2(15, 40)])
    ml_builder.add_leader_line(mleader.ConnectionSide.bottom, [Vec2(15, -40)])

    # The insert point (in UCS coordinates= is the alignment point for MTEXT
    # content and the insert location for BLOCK content:
    ml_builder.build(insert=Vec2(5, 0))
    msp.add_circle(
        ml_builder.multileader.context.base_point,
        radius=0.5,
        dxfattribs=GfxAttribs(color=colors.RED),
    )
    doc.set_modelspace_vport(60, center=(10, 5))
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def quick_mtext_horizontal(name: str):
    doc = ezdxf.new(DXFVERSION, setup=True)
    mleaderstyle = doc.mleader_styles.duplicate_entry("Standard", "EZDXF")
    mleaderstyle.set_mtext_style("OpenSans")
    msp = doc.modelspace()
    target_point = Vec2(40, 15)
    msp.add_circle(
        target_point, radius=0.5, dxfattribs=GfxAttribs(color=colors.RED)
    )

    for angle in [45, 135, 225, -45]:
        ml_builder = msp.add_multileader_mtext("EZDXF")
        ml_builder.quick_leader(
            "Line1\nLine2",
            target=target_point,
            segment1=Vec2.from_deg_angle(angle, 14),
        )

    doc.set_modelspace_vport(60, center=(10, 5))
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def quick_mtext_vertical(name: str):
    doc = ezdxf.new(DXFVERSION, setup=True)
    mleaderstyle = doc.mleader_styles.duplicate_entry("Standard", "EZDXF")
    mleaderstyle.set_mtext_style("OpenSans")
    msp = doc.modelspace()
    target_point = Vec2(40, 15)
    msp.add_circle(
        target_point, radius=0.5, dxfattribs=GfxAttribs(color=colors.RED)
    )

    for angle in [45, 135, 225, -45]:
        ml_builder = msp.add_multileader_mtext("EZDXF")
        ml_builder.quick_leader(
            "Line1\nLine2",
            target=target_point,
            segment1=Vec2.from_deg_angle(angle, 14),
            connection_type=mleader.VerticalConnection.center_overline,
        )

    doc.set_modelspace_vport(60, center=(10, 5))
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def create_block(
    doc: "Drawing", size: float, margin: float, base_point: Vec2 = NULLVEC
) -> "BlockLayout":
    attribs = GfxAttribs(color=colors.RED)
    block = doc.blocks.new("SQUARE", base_point=base_point)
    block.add_lwpolyline(forms.square(size), close=True, dxfattribs=attribs)

    attdef_attribs = dict(attribs)
    attdef_attribs["height"] = 1.0
    attdef_attribs["style"] = "OpenSans"
    tag = "ONE"
    attdef_attribs["prompt"] = tag
    bottom_left_attdef = block.add_attdef(
        tag, text=tag, dxfattribs=attdef_attribs
    )
    bottom_left_attdef.set_placement(
        (margin, margin), align=TextEntityAlignment.BOTTOM_LEFT
    )
    tag = "TWO"
    attdef_attribs["prompt"] = tag
    top_right_attdef = block.add_attdef(
        tag, text=tag, dxfattribs=attdef_attribs
    )
    top_right_attdef.set_placement(
        (size - margin, size - margin), align=TextEntityAlignment.TOP_RIGHT
    )
    return block


def block_content_horizontal(
    name: str,
    align: mleader.BlockAlignment,
    base_point=Vec2(0, 0),
    block_scale=1.0,
    block_rotation=0.0,
    overall_scale=1.0,
):
    x1, y1, x2, y2 = -40, -40, 60, 40
    construction_box = ConstructionBox.from_points((x1, y1), (x2, y2))

    doc = ezdxf.new(DXFVERSION, setup=True)
    block = create_block(doc, size=8.0, margin=0.25, base_point=base_point)
    msp = doc.modelspace()
    ml_builder = msp.add_multileader_block(style="Standard")
    ml_builder.set_content(name=block.name, alignment=align, scale=block_scale)
    ml_builder.set_overall_scaling(overall_scale)
    ml_builder.set_attribute("ONE", "Data1")
    ml_builder.set_attribute("TWO", "Data2")

    # Construction plane of the entity is defined by a render UCS.
    # The default render UCS is the WCS.
    # The leader lines vertices are expected in render UCS coordinates, which
    # means relative to the UCS origin!
    # This example shows the simplest way UCS==WCS!
    ml_builder.add_leader_line(mleader.ConnectionSide.right, [Vec2(x2, y1)])
    ml_builder.add_leader_line(mleader.ConnectionSide.right, [Vec2(x2, y2)])
    ml_builder.add_leader_line(mleader.ConnectionSide.left, [Vec2(x1, y1)])
    ml_builder.add_leader_line(mleader.ConnectionSide.left, [Vec2(x1, y2)])

    # The insert point (in UCS coordinates is the insert location for BLOCK
    # content:
    insert = Vec2(5, 2)
    ml_builder.build(insert=insert, rotation=block_rotation)

    # visual additions:
    msp.add_circle(insert, radius=0.25)  # show the insertion point
    msp.add_lwpolyline(construction_box.corners, close=True)  # draw target box
    doc.set_modelspace_vport(
        construction_box.width, center=construction_box.center
    )
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def block_content_vertical(
    name: str,
    align: mleader.BlockAlignment,
    base_point=Vec2(0, 0),
    block_scale=1.0,
    block_rotation=0.0,
    overall_scale=1.0,
):
    x1, y1, x2, y2 = -30, -30, 30, 30
    construction_box = ConstructionBox.from_points((x1, y1), (x2, y2))

    doc = ezdxf.new(DXFVERSION, setup=True)
    block = create_block(doc, size=8.0, margin=0.25, base_point=base_point)
    msp = doc.modelspace()
    ml_builder = msp.add_multileader_block(style="Standard")
    ml_builder.set_content(
        name=block.name, alignment=align, scale=block_scale
    )  # block scale
    ml_builder.set_overall_scaling(overall_scale)
    ml_builder.set_attribute("ONE", "Data1")
    ml_builder.set_attribute("TWO", "Data2")
    ml_builder.add_leader_line(mleader.ConnectionSide.top, [Vec2(x1, y2)])
    ml_builder.add_leader_line(mleader.ConnectionSide.top, [Vec2(x2, y2)])
    ml_builder.add_leader_line(mleader.ConnectionSide.bottom, [Vec2(x1, y1)])
    ml_builder.add_leader_line(mleader.ConnectionSide.bottom, [Vec2(x2, y1)])

    # The insert point (in UCS coordinates= is the insert location for BLOCK
    # content:
    insert = Vec2(5, 2)
    ml_builder.build(insert=insert, rotation=block_rotation)

    # visual additions:
    msp.add_circle(insert, radius=0.25)  # show the insertion point
    msp.add_lwpolyline(construction_box.corners, close=True)  # draw target box
    doc.set_modelspace_vport(
        construction_box.width, center=construction_box.center
    )
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


def make_template(name: str):
    doc = ezdxf.new(DXFVERSION, setup=True)
    create_block(doc, size=8.0, margin=0.25)
    doc.saveas(CWD / f"{name}_{DXFVERSION}.dxf")


if __name__ == "__main__":
    make_template("brics_block")
    quick_mtext_horizontal("mleader_quick_mtext_horizontal")
    quick_mtext_vertical("mleader_quick_mtext_vertical")
    simple_mtext_content_horizontal("mleader_simple_mtext_horizontal")
    simple_mtext_content_vertical("mleader_simple_mtext_vertical")
    all_mtext_content_horizontal("mleader_all_mtext_horizontal")

    # only block scaling - dogleg_length and arrow_size is not affected
    block_content_horizontal(
        "horizontal_block_x2_center_extents",
        mleader.BlockAlignment.center_extents,
        base_point=Vec2(1, 2),
        block_scale=2,
        block_rotation=30,
    )
    # only block scaling - dogleg_length and arrow_size is not affected
    block_content_horizontal(
        "horizontal_block_center_extents_all_x2",
        mleader.BlockAlignment.center_extents,
        base_point=Vec2(1, 2),
        overall_scale=2,
        block_rotation=30,
    )

    block_content_horizontal(
        "horizontal_block_x2_insertion_point",
        mleader.BlockAlignment.insertion_point,
        base_point=Vec2(1, 2),
        block_scale=2,
        block_rotation=30,
    )
    block_content_horizontal(
        "horizontal_block_insertion_point_all_x2",
        mleader.BlockAlignment.insertion_point,
        base_point=Vec2(1, 2),
        block_scale=2,
        block_rotation=30,
    )
    block_content_vertical(
        "vertical_block_center_extents",
        mleader.BlockAlignment.center_extents,
        block_rotation=30,
        base_point=Vec2(1, 2),
    )
    block_content_vertical(
        "vertical_block_insertion_point",
        mleader.BlockAlignment.insertion_point,
        block_rotation=30,
        base_point=Vec2(1, 2),
    )