File: text_to_virtual_entities.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 (56 lines) | stat: -rw-r--r-- 1,719 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
# Copyright (c) 2021-2022, Manfred Moitzi
# License: MIT License
import pathlib
import ezdxf
from ezdxf.addons import text2path
from ezdxf import zoom, disassemble
from ezdxf.lldxf import const

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

# ------------------------------------------------------------------------------
# This example shows how to convert TEXT entities to outline paths.
#
# docs: https://ezdxf.mozman.at/docs/addons/text2path.html
# ------------------------------------------------------------------------------

EXAMPLES = pathlib.Path(__file__).parent.parent.parent / "examples_dxf"
FILE1 = "text_mirror_true_type_font.dxf"
FILE2 = "text_oblique_rotate.dxf"

FILE = FILE2


def main():
    doc = ezdxf.readfile(EXAMPLES / FILE)
    doc.layers.new("OUTLINE", dxfattribs={"color": 1})
    doc.layers.new("BBOX", dxfattribs={"color": 5})
    msp = doc.modelspace()
    text_entities = msp.query("TEXT")

    # Convert TEXT entities into SPLINE and POLYLINE entities:
    kind = text2path.Kind.SPLINES
    for text in text_entities:
        for e in text2path.virtual_entities(text, kind=kind):
            e.dxf.layer = "OUTLINE"
            e.dxf.color = const.BYLAYER
            msp.add_entity(e)

    # Add bounding boxes
    attrib = {"layer": "BBOX"}
    boxes = []

    # The "primitive" representation for TEXT entities is the bounding box:
    for prim in disassemble.to_primitives(text_entities):
        p = msp.add_lwpolyline(prim.vertices(), dxfattribs=attrib)
        boxes.append(p)

    # Zoom on bounding boxes (fast):
    zoom.objects(msp, boxes, factor=1.1)
    doc.saveas(CWD / FILE)


if __name__ == "__main__":
    main()