File: transform_entities_by_block.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 (47 lines) | stat: -rw-r--r-- 1,539 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
# Copyright (c) 2019-2022 Manfred Moitzi
# License: MIT License
import pathlib

import ezdxf

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

# ------------------------------------------------------------------------------
# This example shows how to transform entities by putting them into an anonymous
# BLOCK and INSERT them transformed. This was the only way to transform entities
# by ezdxf before the general transformation support was added in version v0.14.
# ------------------------------------------------------------------------------


def main(size=5):
    doc = ezdxf.new()
    msp = doc.modelspace()
    msp.add_polyline2d([(0, 0), (size, 0), (size, size), (0, size)], close=True)
    msp.add_circle((size / 2, size / 2), radius=size / 2)

    doc.saveas(CWD / "transformation_example_before.dxf")

    # create a new anonymous block
    block = doc.blocks.new_anonymous_block(base_point=(0, 0))
    # move entities from modelspace to the new block
    # create a list, because entities are removed from modelspace while iterating
    for entity in list(msp):
        msp.move_to_layout(entity, block)

    # insert block wherever you want
    msp.add_blockref(
        block.name,
        insert=(10, 10),
        dxfattribs={
            "rotation": 23,  # with rotation
            "xscale": 1.5,  # and none uniform scaling
            "yscale": 3.7,
        },
    )
    doc.saveas(CWD / "transformation_example_after.dxf")


if __name__ == '__main__':
    main()