File: draw_text_frames.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 (51 lines) | stat: -rw-r--r-- 1,415 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
# Copyright (c) 2021-2023, Manfred Moitzi
# License: MIT License

import pathlib
import ezdxf
from ezdxf import disassemble, zoom
from ezdxf.fonts import fonts

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

fonts.load()

FILES = [
    "text_fonts.dxf",
    "text_oblique_rotate.dxf",
    "text_mirror_true_type_font.dxf",
    "text_stacked_shx_font.dxf",
]

# ------------------------------------------------------------------------------
# This example draws frames around existing text entities in DXF files locates
# at ../../examples_dxf/<filename>
#
# This shows the difference of text size calculation between Matplotlib and CAD
# applications.
# ------------------------------------------------------------------------------


def main():
    for filename in FILES:
        print(f"Processing: {filename}")
        doc = ezdxf.readfile(
            pathlib.Path(__file__).parent.parent / "examples_dxf" / filename
        )
        msp = doc.modelspace()

        # required to switch layer on/off
        doc.layers.add("TEXT_FRAME", color=6)
        for frame in disassemble.to_primitives(msp.query("TEXT")):
            msp.add_lwpolyline(
                frame.vertices(), close=True, dxfattribs={"layer": "TEXT_FRAME"}
            )

        zoom.extents(msp, factor=1.1)
        doc.saveas(CWD / filename)


if __name__ == "__main__":
    main()