File: render_r12_ellipse.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 (83 lines) | stat: -rw-r--r-- 2,325 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
# Copyright (c) 2018-2022, Manfred Moitzi
# License: MIT License
import pathlib
from math import radians
import ezdxf
from ezdxf.render.forms import ellipse
from ezdxf.math import basic_transformation

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

# ------------------------------------------------------------------------------
# This example shows how to get ellipses in DXF R12 files which does not support
# the ELLIPSE entity.
#
# docs:
# ellipse: https://ezdxf.mozman.at/docs/render/forms.html#ezdxf.render.forms.ellipse
# basic_transformation: https://ezdxf.mozman.at/docs/math/core.html#ezdxf.math.basic_transformation
# ------------------------------------------------------------------------------


def render(msp, points):
    msp.add_polyline2d(list(points))


def tmatrix(x, y, angle):
    return basic_transformation((x, y), z_rotation=radians(angle))


def main():

    doc = ezdxf.new("R12", setup=True)
    msp = doc.modelspace()

    for axis in [0.5, 0.75, 1.0, 1.5, 2.0, 3.0]:
        render(msp, ellipse(200, rx=5.0, ry=axis))

    attribs = {
        "color": 1,
        "linetype": "DASHDOT",
    }

    msp.add_line((-7, 0), (+7, 0), dxfattribs=attribs)
    msp.add_line((0, -5), (0, +5), dxfattribs=attribs)

    for rotation in [0, 30, 45, 60, 90]:
        m = tmatrix(20, 0, rotation)
        render(msp, m.transform_vertices(ellipse(100, rx=5.0, ry=2.0)))

    for startangle in [0, 30, 45, 60, 90]:
        m = tmatrix(40, 0, startangle)
        render(
            msp,
            m.transform_vertices(
                ellipse(
                    90,
                    rx=5.0,
                    ry=2.0,
                    start_param=radians(startangle),
                    end_param=radians(startangle + 90),
                )
            ),
        )
        render(
            msp,
            m.transform_vertices(
                ellipse(
                    90,
                    rx=5.0,
                    ry=2.0,
                    start_param=radians(startangle + 180),
                    end_param=radians(startangle + 270),
                )
            ),
        )
    filename = CWD / "ellipse_r12.dxf"
    doc.saveas(filename)
    print(f"drawing {filename} created.")


if __name__ == "__main__":
    main()