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

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

# Purpose: handle drawing data of DXF files
# Created: 21.07.12

__author__ = "mozman <mozman@gmx.at>"

from .tags import stream_tagger
from .sections import Sections

DEFAULT_OPTIONS = {
    "grab_blocks": True,  # import block definitions True=yes, False=No
    "assure_3d_coords": False,  # guarantees (x, y, z) tuples for ALL coordinates
    "resolve_text_styles": True,  # Text, Attrib, Attdef and MText attributes will be set by the associated text style if necessary
}


class Drawing(object):
    def __init__(self, stream, options=None):
        if options is None:
            options = DEFAULT_OPTIONS
        self.grab_blocks = options.get('grab_blocks', True)
        self.assure_3d_coords = options.get('assure_3d_coords', False)
        self.resolve_text_styles = options.get('resolve_text_styles', True)

        tagreader = stream_tagger(stream, self.assure_3d_coords)
        self.dxfversion = 'AC1009'
        self.encoding = 'cp1252'
        self.filename = None
        sections = Sections(tagreader, self)
        self.header = sections.header
        self.layers = sections.tables.layers
        self.styles = sections.tables.styles
        self.linetypes = sections.tables.linetypes
        self.blocks = sections.blocks
        self.entities = sections.entities
        self.objects = sections.objects if ('objects' in sections) else []
        if 'acdsdata' in sections:
            self.acdsdata = sections.acdsdata
            # sab data introduced with DXF version AC1027 (R2013)
            if self.dxfversion >= 'AC1027':
                self.collect_sab_data()

        if self.resolve_text_styles:
            resolve_text_styles(self.entities, self.styles)
            for block in self.blocks:
                resolve_text_styles(block, self.styles)

    def modelspace(self):
        return (entity for entity in self.entities if not entity.paperspace)

    def paperspace(self):
        return (entity for entity in self.entities if entity.paperspace)

    def collect_sab_data(self):
        for entity in self.entities:
            if hasattr(entity, 'set_sab_data'):
                sab_data = self.acdsdata.sab_data[entity.handle]
                entity.set_sab_data(sab_data)


def resolve_text_styles(entities, text_styles):
    for entity in entities:
        if hasattr(entity, 'resolve_text_style'):
            entity.resolve_text_style(text_styles)