File: test_243_replace_entity.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 (122 lines) | stat: -rw-r--r-- 3,603 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2020, Manfred Moitzi
# License: MIT License
import pytest
import ezdxf
from ezdxf.entities.dxfgfx import add_entity, replace_entity
from ezdxf.entities import Point


@pytest.fixture(scope="module")
def msp():
    return ezdxf.new().modelspace()


@pytest.fixture(scope="module")
def db(msp):
    return msp.entitydb


def test_add_entity(msp, db):
    point = msp.add_point((0, 0))
    new_point = Point.new(dxfattribs={"location": (3, 3)})
    add_entity(new_point, msp)
    assert point in msp
    assert point.dxf.handle in db
    assert new_point in msp
    assert new_point.dxf.handle in db
    assert point.dxf.handle != new_point.dxf.handle


def test_replace_entity(msp, db):
    point = msp.add_point((0, 0))
    handle = point.dxf.handle

    new_point = Point.new(dxfattribs={"location": (3, 3)})
    replace_entity(point, new_point, msp)
    assert point.is_alive is False
    assert new_point in msp
    assert new_point.dxf.handle in db
    assert new_point.dxf.handle == handle


def test_replace_entity_without_layout(msp, db):
    point = Point.new(dxfattribs={"location": (3, 3)})
    db.add(point)
    handle = point.dxf.handle

    assert point not in msp
    assert point.dxf.handle in db

    new_point = Point.new(dxfattribs={"location": (3, 3)})
    replace_entity(point, new_point, msp)
    assert point.is_alive is False
    assert new_point not in msp
    assert new_point.dxf.handle in db
    assert new_point.dxf.handle == handle


def test_convert_circle_to_ellipse(msp, db):
    circle = msp.add_circle(center=(3, 3), radius=2)
    ellipse = circle.to_ellipse(replace=False)
    assert circle.dxf.handle in db
    assert ellipse.dxftype() == "ELLIPSE"
    assert ellipse.dxf.handle in db
    assert circle in msp
    assert ellipse in msp


def test_replace_circle_by_ellipse(msp, db):
    circle = msp.add_circle(center=(3, 3), radius=2)
    circle_handle = circle.dxf.handle
    ellipse = circle.to_ellipse(replace=True)
    assert circle.is_alive is False
    assert ellipse.dxftype() == "ELLIPSE"
    assert ellipse.dxf.handle in db
    assert ellipse.dxf.handle == circle_handle
    assert ellipse in msp


def test_convert_circle_to_spline(msp, db):
    circle = msp.add_circle(center=(3, 3), radius=2)
    spline = circle.to_spline(replace=False)
    assert circle.dxf.handle in db
    assert spline.dxftype() == "SPLINE"
    assert spline.dxf.handle in db
    assert circle in msp
    assert spline in msp


def test_replace_circle_by_spline(msp, db):
    circle = msp.add_circle(center=(3, 3), radius=2)
    circle_handle = circle.dxf.handle
    spline = circle.to_spline(replace=True)
    assert circle.is_alive is False
    assert spline.dxftype() == "SPLINE"
    assert spline.dxf.handle in db
    assert spline.dxf.handle == circle_handle
    assert spline in msp


def test_convert_ellipse_to_spline(msp, db):
    ellipse = msp.add_ellipse(center=(3, 3), major_axis=(2, 0), ratio=0.5)
    spline = ellipse.to_spline(replace=False)
    assert ellipse.dxf.handle in db
    assert spline.dxftype() == "SPLINE"
    assert spline.dxf.handle in db
    assert ellipse in msp
    assert spline in msp


def test_replace_ellipse_by_spline(msp, db):
    ellipse = msp.add_ellipse(center=(3, 3), major_axis=(2, 0), ratio=0.5)
    ellipse_handle = ellipse.dxf.handle
    spline = ellipse.to_spline(replace=True)
    assert ellipse.is_alive is False
    assert spline.dxftype() == "SPLINE"
    assert spline.dxf.handle in db
    assert spline.dxf.handle == ellipse_handle
    assert spline in msp


if __name__ == "__main__":
    pytest.main([__file__])