Remove deprecated 2D_/3D_ prefix
[blender-addons.git] / io_import_dxf / dxfgrabber / tablessection.py
blob9c1599cb474d97afe243da8bfaff1940f121979d
1 # SPDX-License-Identifier: MIT
2 # Copyright 2012 Manfred Moitzi
4 # Purpose: handle tables section
5 # Created: 21.07.2012, taken from my ezdxf project
7 from __future__ import unicode_literals
8 __author__ = "mozman <mozman@gmx.at>"
10 from .defaultchunk import iterchunks, DefaultChunk
11 from .layers import LayerTable
12 from .styles import StyleTable
13 from .linetypes import LinetypeTable
15 TABLENAMES = {
16 'layer': 'layers',
17 'ltype': 'linetypes',
18 'appid': 'appids',
19 'dimstyle': 'dimstyles',
20 'style': 'styles',
21 'ucs': 'ucs',
22 'view': 'views',
23 'vport': 'viewports',
24 'block_record': 'block_records',
28 def tablename(dxfname):
29 """ Translate DXF-table-name to attribute-name. ('LAYER' -> 'layers') """
30 name = dxfname.lower()
31 return TABLENAMES.get(name, name+'s')
34 class DefaultDrawing(object):
35 dxfversion = 'AC1009'
36 encoding = 'cp1252'
39 class TablesSection(object):
40 name = 'tables'
42 def __init__(self):
43 self._tables = dict()
44 self._create_default_tables()
46 def _create_default_tables(self):
47 for cls in TABLESMAP.values():
48 table = cls()
49 self._tables[table.name] = table
51 @staticmethod
52 def from_tags(tags, drawing):
53 tables_section = TablesSection()
54 tables_section._setup_tables(tags)
55 return tables_section
57 def _setup_tables(self, tags):
58 def name(table):
59 return table[1].value
61 def skiptags(tags, count):
62 for i in range(count):
63 next(tags)
64 return tags
66 itertags = skiptags(iter(tags), 2) # (0, 'SECTION'), (2, 'TABLES')
67 for table in iterchunks(itertags, stoptag='ENDSEC', endofchunk='ENDTAB'):
68 table_class = table_factory(name(table))
69 if table_class is not None:
70 new_table = table_class.from_tags(table)
71 self._tables[new_table.name] = new_table
73 def __getattr__(self, key):
74 try:
75 return self._tables[key]
76 except KeyError:
77 raise AttributeError(key)
79 # support for further tables types are possible
80 TABLESMAP = {
81 'LAYER': LayerTable,
82 'STYLE': StyleTable,
83 'LTYPE': LinetypeTable,
87 def table_factory(name):
88 return TABLESMAP.get(name, None)