1# Copyright (c) 2019 Manfred Moitzi
2# License: MIT License
3# created 2019-03-05
4import pytest
5
6from ezdxf.entities.xline import XLine
7from ezdxf.lldxf.tagwriter import TagCollector, basic_tags_from_text
8from ezdxf.math import Matrix44
9
10XLINE = """0
11XLINE
125
130
14330
150
16100
17AcDbEntity
188
190
20100
21AcDbXline
2210
230.0
2420
250.0
2630
270.0
2811
291.0
3021
310.0
3231
330.0
34"""
35
36
37@pytest.fixture
38def entity():
39    return XLine.from_text(XLINE)
40
41
42def test_registered():
43    from ezdxf.entities.factory import ENTITY_CLASSES
44    assert 'XLINE' in ENTITY_CLASSES
45
46
47def test_default_init():
48    entity = XLine()
49    assert entity.dxftype() == 'XLINE'
50    assert entity.dxf.handle is None
51    assert entity.dxf.owner is None
52
53
54def test_default_new():
55    entity = XLine.new(handle='ABBA', owner='0', dxfattribs={
56        'color': 7,
57        'start': (1, 2, 3),
58        'unit_vector': (4, 5, 6),
59    })
60    assert entity.dxf.layer == '0'
61    assert entity.dxf.color == 7
62    assert entity.dxf.start == (1, 2, 3)
63    assert entity.dxf.unit_vector == (4, 5, 6)
64
65
66def test_load_from_text(entity):
67    assert entity.dxf.layer == '0'
68    assert entity.dxf.color == 256, 'default color is 256 (by layer)'
69    assert entity.dxf.start == (0, 0, 0)
70    assert entity.dxf.unit_vector == (1, 0, 0)
71
72
73def test_write_dxf():
74    entity = XLine.from_text(XLINE)
75    result = TagCollector.dxftags(entity)
76    expected = basic_tags_from_text(XLINE)
77    assert result == expected
78
79
80def test_xline_transform():
81    # same implementation for Ray()
82    xline = XLine.new(dxfattribs={'start': (2, 3, 4), 'unit_vector': (1, 0, 0)})
83    # 1. scaling - 2. rotation - 3. translation
84    m = Matrix44.chain(Matrix44.scale(2, 2, 3), Matrix44.translate(1, 1, 1))
85    xline.transform(m)
86
87    assert xline.dxf.start == (5, 7, 13)
88    assert xline.dxf.unit_vector == (1, 0, 0)
89
90
91def test_xline_fast_translation():
92    # same implementation for Ray()
93    xline = XLine.new(dxfattribs={'start': (2, 3, 4), 'unit_vector': (1, 0, 0)})
94    xline.translate(1, 2, 3)
95    assert xline.dxf.start == (3, 5, 7)
96    assert xline.dxf.unit_vector == (1, 0, 0)
97