Welcome to mirror list, hosted at ThFree Co, Russian Federation.

entitysection.py « dxfgrabber « io_import_dxf - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e27b657bb34363e86c89d54cbaeea81651a2911 (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
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
# SPDX-License-Identifier: MIT
# Copyright 2012 Manfred Moitzi

# Purpose: handle entity section
# Created: 21.07.2012, taken from my ezdxf project
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"

from itertools import islice

from .tags import TagGroups, DXFStructureError
from .tags import Tags
from .dxfentities 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)
        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):
        if len(tags) == 3:  # empty entities section
            return
        groups = TagGroups(islice(tags, 2, len(tags)-1))
        self._entities = build_entities(groups)


class ObjectsSection(EntitySection):
    name = 'objects'


def build_entities(tag_groups):
    def build_entity(group):
        try:
            entity = entity_factory(Tags(group))
        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 in ('POLYLINE', 'POLYFACE', 'POLYMESH'):
                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()