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

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

# Purpose: acdsdata section manager
# Created: 05.05.2014

from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"

from itertools import islice
from .tags import TagGroups, DXFStructureError, Tags, binary_encoded_data_to_bytes


class AcDsDataSection(object):
    name = 'acdsdata'

    def __init__(self):
        # Standard_ACIS_Binary (SAB) data store, key = handle of DXF Entity in the ENTITIES section: BODY, 3DSOLID
        # SURFACE, PLANESURFACE, REGION
        self.sab_data = {}

    @classmethod
    def from_tags(cls, tags, drawing):
        data_section = cls()
        data_section._build(tags)
        return data_section

    def _build(self, tags):
        if len(tags) == 3:  # empty entities section
            return

        for group in TagGroups(islice(tags, 2, len(tags)-1)):
            data_record = AcDsDataRecord(Tags(group))
            if data_record.dxftype == 'ACDSRECORD':
                asm_data = data_record.get_section('ASM_Data', None)
                if asm_data is not None:
                    self.add_asm_data(data_record)

    def add_asm_data(self, acdsrecord):
        """ Store SAB data as binary string in the sab_data dict, with handle to owner Entity as key.
        """
        try:
            asm_data = acdsrecord.get_section('ASM_Data')
            entity_id = acdsrecord.get_section('AcDbDs::ID')
        except ValueError:
            return
        else:
            handle = entity_id[2].value
            binary_data_text = (tag.value for tag in asm_data if tag.code == 310)
            binary_data = binary_encoded_data_to_bytes(binary_data_text)
            self.sab_data[handle] = binary_data


class Section(Tags):
    @property
    def name(self):
        return self[0].value

    @property
    def type(self):
        return self[1].value

    @property
    def data(self):
        return self[2:]


class AcDsDataRecord(object):
    def __init__(self, tags):
        self.dxftype = tags[0].value
        start_index = 2
        while tags[start_index].code != 2:
            start_index += 1
        self.sections = [Section(tags) for tags in TagGroups(islice(tags, start_index, None), split_code=2)]

    def has_section(self, name):
        return self.get_section(name, default=None) is not None

    def get_section(self, name, default=KeyError):
        for section in self.sections:
            if section.name == name:
                return section
        if default is KeyError:
            raise KeyError(name)
        else:
            return default

    def __getitem__(self, name):
        return self.get_section(name)