blob: a64a364e88edccf48a4112d02390adf51c0cc8f0 (
plain)
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
|
# Purpose: blocks section
# Created: 09.08.2012, taken from my package ezdxf
# Copyright (C) 2011, Manfred Moitzi
# License: MIT-License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"
from itertools import islice
from .tags import TagGroups
from .entitysection import build_entities
class BlocksSection(object):
name = 'blocks'
def __init__(self):
self._blocks = dict()
@staticmethod
def from_tags(tags, drawing):
blocks_section = BlocksSection()
if drawing.grab_blocks:
blocks_section._build(tags)
return blocks_section
def _build(self, tags):
if len(tags) == 3: # empty block section
return
groups = list()
for group in TagGroups(islice(tags, 2, len(tags)-1)):
groups.append(group)
if group[0].value == 'ENDBLK':
entities = build_entities(groups)
block = entities[0]
block.set_entities(entities[1:-1])
self._add(block)
groups = list()
def _add(self, block):
self._blocks[block.name] = block
# start of public interface
def __len__(self):
return len(self._blocks)
def __iter__(self):
return iter(self._blocks.values())
def __contains__(self, name):
return name in self._blocks
def __getitem__(self, name):
return self._blocks[name]
def get(self, name, default=None):
return self._blocks.get(name, default)
|