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
|
# Copyright (c) 2011-2022, Manfred Moitzi
# License: MIT License
import pytest
import ezdxf
from ezdxf.sections.tables import TablesSection
from ezdxf.lldxf.tagwriter import TagCollector
class TestGenericTableFeatures:
@pytest.fixture(scope="class")
def tables(self) -> TablesSection:
doc = ezdxf.new()
return doc.tables
def test_constructor(self, tables):
assert tables.layers is not None
assert tables.linetypes is not None
assert tables.appids is not None
assert tables.styles is not None
assert tables.dimstyles is not None
assert tables.views is not None
assert tables.viewports is not None
assert tables.ucs is not None
assert tables.block_records is not None
def test_getattr_upper_case(self, tables):
with pytest.raises(AttributeError):
_ = tables.LINETYPES
def test_error_getattr(self, tables):
with pytest.raises(AttributeError):
_ = tables.test
def test_get_entry_by_handle(self, tables):
layer = tables.layers.add("Test0")
assert tables.layers.get_entry_by_handle(layer.dxf.handle) is layer
def test_get_non_existing_entry_by_handle(self, tables):
assert tables.layers.get_entry_by_handle("FEFE") is None
def test_get_handle_of_entry(self, tables):
layer = tables.layers.add("Test1")
assert tables.layers.get_handle_of_entry("test1") == layer.dxf.handle
def test_get_handle_of_non_existing_entry(self, tables):
assert tables.layers.get_handle_of_entry("test2") == ""
def test_get_entry_by_handle_is_type_safe(self, tables):
arial = tables.styles.add("Arial0", font="arial.ttf")
assert tables.layers.get_entry_by_handle(arial.dxf.handle) is None
class TestLayerTableEntry:
@pytest.fixture(scope="class")
def tables(self) -> TablesSection:
doc = ezdxf.new()
return doc.tables
def test_add_layer(self, tables):
layer = tables.layers.add(
"NEW_LAYER",
color=2,
true_color=ezdxf.rgb2int((0x10, 0x20, 0x30)),
linetype="DASHED",
lineweight=18,
plot=True,
)
assert layer.dxf.name == "NEW_LAYER"
assert layer.dxf.color == 2
assert layer.dxf.true_color == 0x00102030
assert layer.dxf.linetype == "DASHED", "no check if line type exist!"
assert layer.dxf.lineweight == 18
assert layer.dxf.plot == 1
def test_check_invalid_aci_color(self, tables):
with pytest.raises(ValueError):
tables.layers.add("INVALID_ACI", color=300)
def test_check_invalid_line_weight(self, tables):
with pytest.raises(ValueError):
tables.layers.add("INVALID_LINE_WEIGHT", lineweight=300)
def test_add_new_line_type(self, tables):
ltype = tables.linetypes.add(
"SIMPLE_LINE_TYPE", [0.2, 0.1, -0.1], description="description"
)
assert ltype.dxf.name == "SIMPLE_LINE_TYPE"
assert ltype.dxf.description == "description"
# Correct pattern creation is tested in test suite 121.
class TestTextStyleTable:
@pytest.fixture(scope="class")
def tables(self) -> TablesSection:
doc = ezdxf.new()
return doc.tables
def test_add_new_ttf_font_text_style(self, tables):
style = tables.styles.add(
"NEW_STYLE", font="Arial.ttf", dxfattribs={"flags": 3}
)
assert style.dxf.name == "NEW_STYLE"
assert style.dxf.font == "Arial.ttf"
assert style.dxf.flags == 3
def test_add_new_shape_file(self, tables):
style = tables.styles.add_shx("shapes1.shx")
assert style.dxf.name == "", "shape files have no name"
assert style.dxf.font == "shapes1.shx"
assert style.dxf.flags == 1
assert style.is_shape_file is True
# can not add same shape file twice:
with pytest.raises(ezdxf.const.DXFTableEntryError):
tables.styles.add_shx("shapes1.shx")
def test_add_multiple_shape_files(self, tables):
style1 = tables.styles.add_shx("shapes4.shx")
style2 = tables.styles.add_shx("shapes5.shx")
assert tables.styles.find_shx(style1.dxf.font) is not None
assert tables.styles.find_shx(style2.dxf.font) is not None
def test_get_shape_file(self, tables):
style = tables.styles.get_shx("shapes2.shx")
assert style.dxf.name == "", "shape files have no name"
assert style.dxf.font == "shapes2.shx"
assert style.dxf.flags == 1
style2 = tables.styles.get_shx("shapes2.shx")
assert style is style2
def test_find_shape_file(self, tables):
tables.styles.add_shx("shapes3.shx")
style = tables.styles.find_shx("shapes3.shx")
assert style.dxf.font == "shapes3.shx"
def test_if_shape_file_entry_exist(self, tables):
assert tables.styles.find_shx("unknown.shx") is None
def test_discard_shape_files(self, tables: TablesSection):
style1 = tables.styles.add_shx("shapes6.shx")
style2 = tables.styles.add_shx("shapes7.shx")
tables.styles.discard_shx("shapes6.shx")
tables.styles.discard_shx("shapes7.shx")
assert tables.styles.find_shx(style1.dxf.font) is None
assert tables.styles.find_shx(style2.dxf.font) is None
def test_discard_not_existing_shape_file(self, tables):
assert tables.styles.find_shx("shapes8.shx") is None
tables.styles.discard_shx("shapes8.shx") # should not raise an exception
def test_export_multiple_shape_file_entries(self, tables):
doc = ezdxf.new()
doc.styles.add_shx("export1.shx")
doc.styles.add_shx("export2.shx")
tagwriter = TagCollector()
doc.styles.export_dxf(tagwriter)
font_names = [tag.value for tag in tagwriter.tags if tag.code == 3]
assert len(font_names) == 3
assert "export1.shx" in font_names
assert "export2.shx" in font_names
assert "txt" in font_names, "expected font of default text style STANDARD"
class TestLineTypeTable:
@pytest.fixture(scope="class")
def tables(self) -> TablesSection:
doc = ezdxf.new()
return doc.tables
def test_add_new_line_type(self, tables):
ltype = tables.linetypes.add(
"SIMPLE_LINE_TYPE", [0.2, 0.1, -0.1], description="description"
)
assert ltype.dxf.name == "SIMPLE_LINE_TYPE"
assert ltype.dxf.description == "description"
# Correct pattern creation is tested in test suite 121.
|