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