File: test_806_acadctb.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 (458 lines) | stat: -rw-r--r-- 15,553 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# Copyright (c) 2010-2024, Manfred Moitzi
# License: MIT License
import pytest

import os
import math
from ezdxf.addons.acadctb import *


class TestPlotStyle:
    def test_init(self):
        style = PlotStyle(
            0,
            dict(
                description="memo",
                color_policy=3,
                physical_pen_number=7,
                virtual_pen_number=8,
                screen=99,
                linepattern_size=0.7,
                linetype=17,
                adaptive_linetype=False,
                lineweight=19,
                end_style=1,
                join_style=2,
                fill_style=3,
            ),
        )
        assert style.description == "memo"
        assert style.name == "Color_1"
        assert style.localized_name == "Color_1"
        assert style.dithering is True
        assert style.grayscale is True
        assert style.physical_pen_number == 7
        assert style.virtual_pen_number == 8
        assert style.screen == 99
        assert style.linepattern_size == 0.7
        assert style.adaptive_linetype is False
        assert style.lineweight == 19
        assert style.end_style == 1
        assert style.join_style == 2
        assert style.fill_style == 3

    def test_get_set_color(self):
        style = PlotStyle(0)
        style.color = (123, 23, 77)
        assert style.color == (123, 23, 77)

    def test_object_color(self):
        style = PlotStyle(0)
        style.color = (123, 77, 1)
        style.set_object_color()
        assert style.has_object_color()

    def test_dithering(self):
        style = PlotStyle(0)
        style.dithering = True
        assert style.dithering
        style.dithering = False
        assert style.dithering is False

    def test_grayscale(self):
        style = PlotStyle(0)
        style.grayscale = True
        assert style.grayscale
        style.grayscale = False
        assert style.grayscale is False

    def test_dxf_color_index(self):
        style = PlotStyle(0)
        assert style.aci == 1

    def test_set_lineweight(self):
        styles = ColorDependentPlotStyles()
        style = styles[5]
        style.set_lineweight(0.5)
        assert style.lineweight == 13

    def test_write(self):
        expected = (
            " 0{\n"
            '  name="Color_1\n'
            '  localized_name="Color_1\n'
            '  description="\n'
            "  color=-1\n"
            "  color_policy=1\n"
            "  physical_pen_number=0\n"
            "  virtual_pen_number=0\n"
            "  screen=100\n"
            "  linepattern_size=0.5\n"
            "  linetype=31\n"
            "  adaptive_linetype=TRUE\n"
            "  lineweight=0\n"
            "  fill_style=73\n"
            "  end_style=4\n"
            "  join_style=5\n"
            " }\n"
        )
        style = PlotStyle(0)
        fp = StringIO()
        style.write(fp)
        result = fp.getvalue()
        fp.close()
        assert result == expected


class TestColorDependentPlotStyles:
    @pytest.fixture
    def styles(self):
        return ColorDependentPlotStyles(description="TestCase")

    def test_set_style(self, styles):
        style = styles.new_style(3, dict(description="TestCase"))
        assert style.named_color is False
        assert style.color_type is None
        assert style.description == "TestCase"

    def test_get_style(self, styles):
        styles.new_style(3, dict(description="TestCase"))
        assert styles[3].description == "TestCase"

    def test_get_color(self, styles):
        style = styles.new_style(4, dict(description="TestCase"))
        style.color = (123, 17, 99)
        assert style.named_color is False
        assert style.color_type is COLOR_RGB
        assert styles[4].color == (123, 17, 99)

    def test_get_lineweight(self, styles):
        style = styles.new_style(5, dict(description="TestCase"))
        style.set_lineweight(0.70)
        assert math.isclose(styles.get_lineweight(5), 0.70, abs_tol=1e-6)

    def test_get_lineweight_none(self, styles):
        style = styles.new_style(5, dict(description="TestCase"))
        style.set_lineweight(0.0)
        assert styles.get_lineweight(5) is None

    def test_get_lineweight_index(self, styles):
        assert styles.get_lineweight_index(0.50) == 13

    def test_get_table_lineweight(self, styles):
        assert styles.get_table_lineweight(13) == 0.50

    def test_set_table_lineweight(self, styles):
        styles.set_table_lineweight(7, 1.70)
        assert math.isclose(styles.get_table_lineweight(7), 1.70, abs_tol=1e-6)

    def test_set_table_lineweight_index_error(self, styles):
        index_out_of_range = len(styles.lineweights) + 7
        index = styles.set_table_lineweight(index_out_of_range, 7.77)
        assert math.isclose(styles.get_table_lineweight(index), 7.77, abs_tol=1e-6)

    def test_write_header(self):
        expected = (
            'description="\n'
            "aci_table_available=TRUE\n"
            "scale_factor=1.0\n"
            "apply_factor=FALSE\n"
            "custom_lineweight_display_units=0\n"
        )
        styles = ColorDependentPlotStyles()
        fp = StringIO()
        styles._write_header(fp)
        result = fp.getvalue()
        fp.close()
        assert result == expected

    def test_write_aci_table(self):
        expected = (
            "aci_table{\n"
            + "\n".join((' %s="Color_%d' % (index, index + 1) for index in range(255)))
            + "\n}\n"
        )
        styles = ColorDependentPlotStyles()
        fp = StringIO()
        styles._write_aci_table(fp)
        result = fp.getvalue()
        fp.close()
        assert str(result) == str(expected)

    def test_write_lineweights(self):
        expected = (
            "custom_lineweight_table{\n"
            " 0=0.00\n 1=0.05\n 2=0.09\n 3=0.10\n 4=0.13\n"
            " 5=0.15\n 6=0.18\n 7=0.20\n 8=0.25\n 9=0.30\n"
            " 10=0.35\n 11=0.40\n 12=0.45\n 13=0.50\n 14=0.53\n"
            " 15=0.60\n 16=0.65\n 17=0.70\n 18=0.80\n 19=0.90\n"
            " 20=1.00\n 21=1.06\n 22=1.20\n 23=1.40\n 24=1.58\n"
            " 25=2.00\n 26=2.11\n}\n"
        )
        styles = ColorDependentPlotStyles()
        fp = StringIO()
        styles._write_lineweights(fp)
        result = fp.getvalue()
        fp.close()
        assert str(result) == str(expected)


class TestNamedPlotStyles:
    @pytest.fixture
    def stb(self):
        return NamedPlotStyles(description="TestCase")

    def test_set_style(self, stb):
        style = stb.new_style("ezdxf", dict(description="TestCase"))
        assert style.named_color is True
        assert style.color_type is None
        assert style.description == "TestCase"

    def test_get_style(self, stb):
        stb.new_style("mozman", dict(description="ezdxf comment"))
        assert stb["mozman"].description == "ezdxf comment"

    def test_get_color(self, stb):
        style = stb.new_style("mozman2")
        style.color = (123, 17, 99)
        assert style.named_color is True
        # named colors has always color type: COLOR_ACI, why?
        assert style.color_type == COLOR_ACI
        assert stb["mozman2"].color == (123, 17, 99)

    def test_get_lineweight(self, stb):
        style = stb.new_style("mozman5")
        style.set_lineweight(0.70)
        assert math.isclose(stb.get_lineweight("mozman5"), 0.70, abs_tol=1e-6)

    def test_get_lineweight_none(self, stb):
        style = stb.new_style("mozman4")
        style.set_lineweight(0.0)
        assert stb.get_lineweight("mozman4") is None

    def test_get_lineweight_index(self, stb):
        assert stb.get_lineweight_index(0.50) == 13

    def test_get_table_lineweight(self, stb):
        assert stb.get_table_lineweight(13) == 0.50

    def test_set_table_lineweight(self, stb):
        stb.set_table_lineweight(7, 1.70)
        assert math.isclose(stb.get_table_lineweight(7), 1.70, abs_tol=1e-6)

    def test_set_table_lineweight_index_error(self, stb):
        index_out_of_range = len(stb.lineweights) + 7
        index = stb.set_table_lineweight(index_out_of_range, 7.77)
        assert math.isclose(stb.get_table_lineweight(index), 7.77, abs_tol=1e-6)

    def test_write_header(self):
        expected = (
            'description="\n'
            "aci_table_available=FALSE\n"
            "scale_factor=1.0\n"
            "apply_factor=FALSE\n"
            "custom_lineweight_display_units=0\n"
        )
        stb = NamedPlotStyles()
        fp = StringIO()
        stb._write_header(fp)
        result = fp.getvalue()
        fp.close()
        assert result == expected

    def test_write_lineweights(self):
        expected = (
            "custom_lineweight_table{\n"
            " 0=0.00\n 1=0.05\n 2=0.09\n 3=0.10\n 4=0.13\n"
            " 5=0.15\n 6=0.18\n 7=0.20\n 8=0.25\n 9=0.30\n"
            " 10=0.35\n 11=0.40\n 12=0.45\n 13=0.50\n 14=0.53\n"
            " 15=0.60\n 16=0.65\n 17=0.70\n 18=0.80\n 19=0.90\n"
            " 20=1.00\n 21=1.06\n 22=1.20\n 23=1.40\n 24=1.58\n"
            " 25=2.00\n 26=2.11\n}\n"
        )
        stb = NamedPlotStyles()
        fp = StringIO()
        stb._write_lineweights(fp)
        result = fp.getvalue()
        fp.close()
        assert str(result) == str(expected)


class TestCTBImport:
    @pytest.fixture(scope="class")
    def ctb(self):
        path, name = os.path.split(__file__)
        ctbfile = os.path.join(path, "ctbtest.ctb")
        return load(ctbfile)

    def test_ctb_attribs(self, ctb):
        styles = ctb
        assert styles.description == "Comment"
        assert styles.apply_factor is True
        assert styles.scale_factor == 2

    def test_lineweight_table(self, ctb):
        lineweights = ctb.lineweights
        for default_lw, ctb_lw in zip(DEFAULT_LINE_WEIGHTS, lineweights):
            assert math.isclose(default_lw, ctb_lw, abs_tol=1e-6)

    def test_style_1(self, ctb):
        """all attribs are user defined."""
        style = ctb[1]
        assert isinstance(style, PlotStyle)
        assert style.aci == 1
        assert style.color_type == COLOR_RGB
        assert style.color == (235, 135, 20)
        assert style.dithering is True
        assert style.grayscale is True
        assert style.has_object_color() is False
        assert style.physical_pen_number is 11
        assert style.virtual_pen_number == 5
        assert style.screen == 95
        assert style.linetype == 1
        assert style.end_style == END_STYLE_SQUARE
        assert style.join_style == JOIN_STYLE_ROUND
        assert style.fill_style == FILL_STYLE_SOLID

    def test_style_3(self, ctb):
        """all attribs are default or auto"""
        style = ctb[3]
        assert isinstance(style, PlotStyle)
        assert style.aci == 3
        assert style.color_type is None
        assert style.color is None
        assert style.dithering is True
        assert style.grayscale is False
        assert style.has_object_color() is True
        assert style.physical_pen_number == AUTOMATIC
        assert style.virtual_pen_number == AUTOMATIC
        assert style.screen == 100
        assert style.linetype == OBJECT_LINETYPE
        assert style.end_style == END_STYLE_OBJECT
        assert style.join_style == JOIN_STYLE_OBJECT
        assert style.fill_style == FILL_STYLE_OBJECT


class TestCTBExport:
    def test_create_ctb(self, tmpdir):
        filename = str(tmpdir.join("new.ctb"))
        styles = ColorDependentPlotStyles("TestCTB")
        styles.save(filename)
        assert os.path.exists(filename)


class TestSTBImport:
    @pytest.fixture(scope="class")
    def stb(self):
        path, name = os.path.split(__file__)
        stb_file = os.path.join(path, "stbtest.stb")
        return load(stb_file)

    def test_stb_attribs(self, stb):
        assert stb.description == "STB file for ezdxf testing"
        assert stb.apply_factor is True
        assert stb.scale_factor == 2

    def test_normal(self, stb):
        """All attributes of 'Normal' are fixed default values."""
        style = stb["Normal"]
        assert isinstance(style, PlotStyle)
        assert style.index == 0
        assert style.color is None
        assert style.dithering is True
        assert style.grayscale is False
        assert style.has_object_color() is True
        assert style.color_type is None
        assert style.physical_pen_number == AUTOMATIC
        assert style.virtual_pen_number == AUTOMATIC
        assert style.screen == 100
        assert style.linetype == OBJECT_LINETYPE
        assert style.lineweight == OBJECT_LINEWEIGHT
        assert style.end_style == END_STYLE_OBJECT
        assert style.join_style == JOIN_STYLE_OBJECT
        assert style.fill_style == FILL_STYLE_OBJECT

    def test_style_1(self, stb):
        """All attribs are user defined."""
        style = stb["Style_1"]
        assert isinstance(style, PlotStyle)
        assert style.name == "Style_1"
        assert style.localized_name == "Style 1"
        assert style.color_type == COLOR_ACI  # ???
        assert style.color == (235, 135, 20)
        assert style.named_color is False  # why?
        assert style.dithering is True
        assert style.grayscale is True
        assert style.has_object_color() is False
        assert style.physical_pen_number == 11
        assert style.virtual_pen_number == 5
        assert style.screen == 95
        assert style.linetype == 1
        assert style.end_style == END_STYLE_SQUARE
        assert style.join_style == JOIN_STYLE_ROUND
        assert style.fill_style == FILL_STYLE_SOLID

    def test_mozman(self, stb):
        """All attribs are user defined."""
        style = stb["mozman"]
        assert isinstance(style, PlotStyle)
        assert style.name == "mozman"
        assert style.localized_name == "mozman"
        assert style.color_type == COLOR_ACI
        assert style.color == (255, 0, 255)
        assert style.named_color is True
        assert style.dithering is True
        assert style.grayscale is False
        assert style.has_object_color() is False
        assert style.physical_pen_number == AUTOMATIC
        assert style.virtual_pen_number == AUTOMATIC
        assert style.screen == 90
        assert style.linetype == 4
        assert style.end_style == END_STYLE_SQUARE
        assert style.join_style == JOIN_STYLE_MITER
        assert style.fill_style == FILL_STYLE_CHECKERBOARD


class TestSTBExport:
    def test_create_ctb(self, tmpdir):
        filename = str(tmpdir.join("new.stb"))
        styles = NamedPlotStyles("TestSTB")
        styles.save(filename)
        assert os.path.exists(filename)


class TestFunctions:
    def test_color_name(self):
        assert color_name(0) == "Color_1"

    def test_get_bool(self):
        assert get_bool(True)
        assert get_bool("true")
        assert get_bool(False) is False
        assert get_bool("false") is False
        pytest.raises(ValueError, get_bool, "falsch")


def test_parser_ignores_unknown_appendix():
    parser = PlotStyleFileParser(
        r"""custom_lineweight_table{
 0=0.0076200000000 (+7.Z+"8V?S_LC )
 1=0.0254
 6=0.17780000000   (  44F=@";"QC\\SM0)
 7=0.2
 10=0.35560000000 (44F=@";"UC_SN )
 11=0.4
}
"""
    )
    table = parser.get("custom_lineweight_table", {})
    assert table["0"] == "0.0076200000000"
    assert table["1"] == "0.0254"
    assert table["6"] == "0.17780000000"
    assert table["7"] == "0.2"
    assert table["10"] == "0.35560000000"
    assert table["11"] == "0.4"


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