1# Copyright (c) 2018-2019 Manfred Moitzi
2# License: MIT License
3import pytest
4import ezdxf
5
6
7@pytest.fixture(scope='module')
8def dxf12():
9    dwg = ezdxf.new('R12', setup='all')
10    return dwg
11
12
13def test_dimstyle_standard_exist(dxf12):
14    assert 'EZDXF' in dxf12.dimstyles
15
16
17def test_dimstyle_override(dxf12):
18    msp = dxf12.modelspace()
19    dimstyle = msp.add_linear_dim(
20        base=(3, 2, 0),
21        p1=(0, 0, 0),
22        p2=(3, 0, 0),
23        dxfattribs={
24            'dimstyle': 'EZDXF',
25        }
26    )
27    dimline = dimstyle.dimension
28    assert dimline.dxf.dimstyle == 'EZDXF'
29
30    preset = {
31        'dimtxsty': 'TEST',  # virtual attribute - 'dimtxsty_handle' stores the text style handle
32        'dimexe': 0.777,
33        'dimblk': ezdxf.ARROWS.dot_blank
34    }
35    dimstyle.update(preset)
36    assert dimstyle['dimtxsty'] == 'TEST'
37    assert dimstyle['dimexe'] == 0.777
38
39    assert dimstyle['invalid'] is None
40
41    # ignores invalid attributes
42    dimstyle.update({'invalid': 7})
43    assert dimstyle['invalid'] == 7
44    dstyle_orig = dimstyle.get_dstyle_dict()
45    assert len(dstyle_orig) == 0
46
47    dimstyle.commit()
48    # check group code 5 handling for 'dimblk'
49    data = dimline.get_xdata_list('ACAD', 'DSTYLE')
50    for tag in data:
51        if tag.value == ezdxf.ARROWS.dot_blank:
52            assert tag.code == 1000, "Despite group code 5, 'dimblk' should be treated as string, not as handle"
53
54    dstyle = dimstyle.get_dstyle_dict()
55    assert dstyle['dimexe'] == 0.777
56    # unsupported DXF DimStyle attributes are not stored in dstyle
57    assert 'dimtxsty' not in dstyle, 'dimtxsty is not a DXF12 attribute'
58
59
60def test_dimstyle_override_arrows(dxf12):
61    msp = dxf12.modelspace()
62    preset = {
63        'dimblk': 'XYZ',
64        'dimblk1': 'ABC',
65        'dimblk2': 'DEF',
66        'dimldrblk': 'ZZZLDR',  # not supported by DXF12, but used by ezdxf for rendering
67
68    }
69
70    dimstyle = msp.add_linear_dim(
71        base=(3, 2, 0),
72        p1=(0, 0, 0),
73        p2=(3, 0, 0),
74        dimstyle='EZDXF',
75        override=preset,
76    )
77
78    assert dimstyle['dimblk'] == 'XYZ'
79    assert dimstyle['dimblk1'] == 'ABC'
80    assert dimstyle['dimblk2'] == 'DEF'
81    assert dimstyle['dimldrblk'] == 'ZZZLDR'
82
83    dstyle_orig = dimstyle.get_dstyle_dict()
84    assert len(dstyle_orig) == 0
85
86    dimstyle.commit()
87    dstyle = dimstyle.get_dstyle_dict()
88    assert dstyle['dimblk'] == 'XYZ'
89    assert dstyle['dimblk1'] == 'ABC'
90    assert dstyle['dimblk2'] == 'DEF'
91    assert 'dimldrblk' not in dstyle, "not supported by DXF12"
92
93    dimstyle.set_arrows(blk='BLK', blk1='B1', blk2='B2', ldrblk='LBLK')
94    assert dimstyle['dimblk'] == 'BLK'
95    assert dimstyle['dimblk1'] == 'B1'
96    assert dimstyle['dimblk2'] == 'B2'
97    assert dimstyle['dimldrblk'] == 'LBLK'  # not supported by DXF12, but used by ezdxf for rendering
98
99    dimstyle.commit()
100    dstyle = dimstyle.get_dstyle_dict()
101    assert dstyle['dimblk'] == 'BLK'
102    assert dstyle['dimblk1'] == 'B1'
103    assert dstyle['dimblk2'] == 'B2'
104    assert 'dimnldrblk' not in dstyle, "not supported by DXF12"
105
106
107def test_dimstyle_override_linetypes(dxf12):
108    msp = dxf12.modelspace()
109    preset = {
110        'dimltype': 'DOT',
111        'dimltex1': 'DOT2',
112        'dimltex2': 'DOTX2',
113    }
114    dimstyle = msp.add_linear_dim(
115        base=(3, 2, 0),
116        p1=(0, 0, 0),
117        p2=(3, 0, 0),
118        dimstyle='EZDXF',
119        override=preset,
120    )
121    assert dimstyle['dimltype'] == 'DOT'
122    assert dimstyle['dimltex1'] == 'DOT2'
123    assert dimstyle['dimltex2'] == 'DOTX2'
124
125    dimstyle.commit()
126    dstyle = dimstyle.get_dstyle_dict()
127    assert 'dimltype' not in dstyle, "not supported by DXF12"
128    assert 'dimltype_handle' not in dstyle, "not supported by DXF12"
129    assert 'dimltex1' not in dstyle, "not supported by DXF12"
130    assert 'dimltex1_handle' not in dstyle, "not supported by DXF12"
131    assert 'dimltex2' not in dstyle, "not supported by DXF12"
132    assert 'dimltex2_handle' not in dstyle, "not supported by DXF12"
133
134
135def test_horizontal_dimline(dxf12):
136    msp = dxf12.modelspace()
137    dimstyle = msp.add_linear_dim(
138        base=(3, 2, 0),
139        p1=(0, 0, 0),
140        p2=(3, 0, 0),
141    )
142    dimline = dimstyle.dimension
143    assert dimline.dxf.dimstyle == 'EZDXF'
144
145    dimstyle.render()
146    block_name = dimline.dxf.geometry
147    assert block_name.startswith('*D')
148
149    # shortcut for: block = dxf12.blocks.get(block_name)
150    block = dimline.get_geometry_block()
151    assert len(list(block.query('TEXT'))) == 1
152    assert len(list(block.query('INSERT'))) == 2
153    assert len(list(block.query('LINE'))) == 3  # dimension line + 2 extension lines
154    assert len(list(block.query('POINT'))) == 3  # def points
155