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
|
# Purpose: handle entity section
# Created: 21.07.2012, taken from my ezdxf project
# Copyright (C) 2012, Manfred Moitzi
# License: MIT License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"
from itertools import islice
from .tags import TagGroups, DXFStructureError
from .tags import ClassifiedTags
from .entities import entity_factory
class EntitySection(object):
name = 'entities'
def __init__(self):
self._entities = list()
@classmethod
def from_tags(cls, tags, drawing):
entity_section = cls()
entity_section._build(tags, drawing.dxfversion)
return entity_section
def get_entities(self):
return self._entities
# start of public interface
def __len__(self):
return len(self._entities)
def __iter__(self):
return iter(self._entities)
def __getitem__(self, index):
return self._entities[index]
# end of public interface
def _build(self, tags, dxfversion):
if len(tags) == 3: # empty entities section
return
groups = TagGroups(islice(tags, 2, len(tags)-1))
self._entities = build_entities(groups, dxfversion)
class ObjectsSection(EntitySection):
name = 'objects'
def build_entities(tag_groups, dxfversion):
def build_entity(group):
try:
entity = entity_factory(ClassifiedTags(group), dxfversion)
except KeyError:
entity = None # ignore unsupported entities
return entity
entities = list()
collector = None
for group in tag_groups:
entity = build_entity(group)
if entity is not None:
if collector:
if entity.dxftype == 'SEQEND':
collector.stop()
entities.append(collector.entity)
collector = None
else:
collector.append(entity)
elif entity.dxftype == 'POLYLINE':
collector = _Collector(entity)
elif entity.dxftype == 'INSERT' and entity.attribsfollow:
collector = _Collector(entity)
else:
entities.append(entity)
return entities
class _Collector:
def __init__(self, entity):
self.entity = entity
self._data = list()
def append(self, entity):
self._data.append(entity)
def stop(self):
self.entity.append_data(self._data)
if hasattr(self.entity, 'cast'):
self.entity = self.entity.cast()
|