File: test_231_underlay_2.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 (205 lines) | stat: -rw-r--r-- 5,038 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright (c) 2016-2019, Manfred Moitzi
# License: MIT License
import pytest

import ezdxf
from ezdxf.entities.underlay import PdfDefinition, PdfUnderlay


@pytest.fixture(scope="module")
def doc():
    return ezdxf.new("R2000")


@pytest.fixture
def pdf_def(doc):
    return PdfDefinition.from_text(PDF_DEFINITION, doc)


def test_pdf_def_properties(pdf_def):
    assert "PDFDEFINITION" == pdf_def.dxftype()
    assert "PDFUNDERLAY" == pdf_def.entity_name


def test_pdf_def_dxf_attribs(pdf_def):
    assert "underlay.pdf" == pdf_def.dxf.filename
    assert "underlay_key" == pdf_def.dxf.name


@pytest.fixture
def pdf(doc):
    return PdfUnderlay.from_text(PDF_UNDERLAY, doc)


def test_pdf_properties(pdf):
    assert "PDFUNDERLAY" == pdf.dxftype()


def test_pdf_dxf_attribs(pdf):
    assert pdf.dxf.insert == (0.0, 0.0, 0.0)
    assert pdf.dxf.scale_x == 2.5
    assert pdf.dxf.scale_y == 2.5
    assert pdf.dxf.scale_z == 2.5
    assert pdf.scaling == (2.5, 2.5, 2.5)
    assert pdf.dxf.flags == 2
    assert pdf.clipping == 0
    assert pdf.on == 1
    assert pdf.monochrome == 0
    assert pdf.adjust_for_background == 0
    assert pdf.dxf.contrast == 100
    assert pdf.dxf.fade == 0
    assert pdf.dxf.underlay_def_handle == "DEAD1"


def test_get_boundary_path(pdf):
    assert pdf.boundary_path == []


def test_reset_boundary_path(pdf):
    pdf.reset_boundary_path()
    assert pdf.boundary_path == []
    assert pdf.clipping is False


def test_set_boundary_path(pdf):
    pdf.set_boundary_path([(0, 0), (640, 180), (320, 360)])  # 3 vertices triangle
    assert pdf.clipping == 1
    assert pdf.boundary_path == [(0, 0), (640, 180), (320, 360)]


def test_set_scale(pdf):
    pdf.scaling = (1.2, 1.3, 1.4)
    assert pdf.scaling == (1.2, 1.3, 1.4)

    pdf.scaling = 1.7
    assert pdf.scaling == (1.7, 1.7, 1.7)


@pytest.fixture
def new_doc():
    # setting up a drawing is expensive - use as few test methods as possible
    return ezdxf.new("R2000")


def test_new_pdf_underlay_def(new_doc):
    rootdict = new_doc.rootdict
    assert "ACAD_PDFDEFINITIONS" not in rootdict
    underlay_def = new_doc.add_underlay_def("underlay.pdf", fmt="pdf", name="u1")

    # check internals pdf_def_owner -> ACAD_PDFDEFINITIONS
    pdf_dict = rootdict["ACAD_PDFDEFINITIONS"]
    assert underlay_def.dxf.owner == pdf_dict.dxf.handle

    assert "underlay.pdf" == underlay_def.dxf.filename
    assert "u1" == underlay_def.dxf.name


def test_new_pdf(new_doc):
    msp = new_doc.modelspace()
    underlay_def = new_doc.add_underlay_def("underlay.pdf")
    underlay = msp.add_underlay(underlay_def, insert=(0, 0, 0), scale=2)
    assert underlay.dxf.insert == (0, 0, 0)
    assert underlay.dxf.scale_x == 2
    assert underlay.dxf.scale_y == 2
    assert underlay.dxf.scale_z == 2
    assert underlay_def.dxf.handle == underlay.dxf.underlay_def_handle
    assert underlay.clipping is False
    assert underlay.on is True
    assert underlay.monochrome is False
    assert underlay.adjust_for_background is True
    assert underlay.dxf.flags == 10

    underlay_def2 = underlay.get_underlay_def()
    assert underlay_def.dxf.handle == underlay_def2.dxf.handle


class TestCopyAndTransformUnderlay:
    @pytest.fixture(scope="class")
    def doc(self):
        return ezdxf.new()

    @pytest.fixture(scope="class")
    def underlay_def(self, doc):
        return doc.add_underlay_def("underlay.pdf")

    @pytest.fixture(scope="class")
    def msp(self, doc):
        return doc.modelspace()

    def test_copied_underlay_has_same_underlay_definition(self, msp, underlay_def):
        underlay = msp.add_underlay(underlay_def, insert=(0, 0, 0), scale=2)
        clone = underlay.copy()
        msp.add_entity(clone)
        assert (
            underlay.get_underlay_def() is clone.get_underlay_def()
        ), "expected the same underlay definition"

    def test_underlay_definition_has_reactor_handles_to_copies(self, msp, underlay_def):
        underlay = msp.add_underlay(underlay_def, insert=(0, 0, 0), scale=2)
        clone = underlay.copy()
        msp.add_entity(clone)

        assert (
            underlay.dxf.handle in underlay_def.reactors
        ), "expected reactor handle of original underlay"
        assert (
            clone.dxf.handle in underlay_def.reactors
        ), "expected reactor handle of cloned underlay"

    def test_transform_underlay(self, msp, underlay_def):
        """The UNDERLAY entity uses the same low-level transform function as INSERT,
        no extensive transformation testing is required.
        """
        underlay = msp.add_underlay(underlay_def, insert=(0, 0, 0), scale=2)
        underlay.translate(1, 2, 3)

        assert underlay.dxf.insert == (1, 2, 3)


PDF_DEFINITION = """  0
PDFDEFINITION
  5
DEAD1
330
DEAD2
100
AcDbUnderlayDefinition
  1
underlay.pdf
  2
underlay_key
"""

PDF_UNDERLAY = """  0
PDFUNDERLAY
  5
DEAD3
330
DEAD4
100
AcDbEntity
  8
0
100
AcDbUnderlayReference
340
DEAD1
 10
0.0
 20
0.0
 30
0.0
 41
2.5
 42
2.5
 43
2.5
280
  2
281
   100
282
     0
"""