Example #1
0
    def _build(self, sections: SectionDict) -> None:
        start_line_number = 1
        dummy_handle = 1
        entity_index: Dict[str, IndexEntry] = dict()
        dummy_handle_index: Dict[int, str] = dict()
        prev_entry: Optional[IndexEntry] = None
        for section in sections.values():
            for tags in section:
                assert isinstance(tags, Tags), "expected class Tags"
                assert len(tags) > 0, "empty tags should not be possible"
                try:
                    handle = tags.get_handle().upper()
                except ValueError:
                    handle = f"*{dummy_handle:X}"
                    # index dummy handle by id of the first tag:
                    dummy_handle_index[id(tags[0])] = handle
                    dummy_handle += 1

                next_entry = IndexEntry(tags, start_line_number)
                if prev_entry is not None:
                    next_entry.prev = prev_entry
                    prev_entry.next = next_entry
                entity_index[handle] = next_entry
                prev_entry = next_entry

                # calculate next start line number:
                # add 2 lines for each tag: group code, value
                start_line_number += len(tags) * 2
            start_line_number += 2  # for removed ENDSEC tag

        # subtract 1 and 2 for the last ENDSEC tag!
        self._max_line_number = start_line_number - 3
        self._index = entity_index
        self._dummy_handle_index = dummy_handle_index
Example #2
0
    def _load_section_dict(self, sections: loader.SectionDict) -> None:
        """ Internal API to load a DXF document from a section dict. """
        self.is_loading = True
        # Create header section:
        # All header tags are the first DXF structure entity
        header_entities = sections.get('HEADER', [None])[0]
        if header_entities is None:
            # Create default header, files without header are by default DXF R12
            self.header = HeaderSection.new(dxfversion=DXF12)
        else:
            self.header = HeaderSection.load(header_entities)

        self._dxfversion: str = self.header.get('$ACADVER', DXF12)

        # Store original DXF version of loaded file.
        self._loaded_dxfversion = self._dxfversion

        # Content encoding:
        self.encoding = toencoding(self.header.get('$DWGCODEPAGE',
                                                   'ANSI_1252'))

        # Set handle seed:
        seed: str = self.header.get('$HANDSEED', str(self.entitydb.handles))
        self.entitydb.handles.reset(_validate_handle_seed(seed))

        # Store all necessary DXF entities in the entity database:
        loader.load_and_bind_dxf_content(sections, self)

        # End of 1. loading stage, all entities of the DXF file are
        # stored in the entity database.

        # Create sections:
        self.classes = ClassesSection(self, sections.get('CLASSES', None))
        self.tables = TablesSection(self, sections.get('TABLES', None))

        # Create *Model_Space and *Paper_Space BLOCK_RECORDS
        # BlockSection setup takes care about the rest:
        self._create_required_block_records()

        # At this point all table entries are required:
        self.blocks = BlocksSection(self, sections.get('BLOCKS', None))
        self.entities = EntitySection(self, sections.get('ENTITIES', None))
        self.objects = ObjectsSection(self, sections.get('OBJECTS', None))

        # only DXF R2013+
        self.acdsdata = AcDsDataSection(self, sections.get('ACDSDATA', None))

        # Store unmanaged sections as raw tags:
        for name, data in sections.items():
            if name not in const.MANAGED_SECTIONS:
                self.stored_sections.append(StoredSection(data))

        # Objects section is not initialized!
        self._2nd_loading_stage()

        # DXF version upgrades:
        if self.dxfversion < DXF12:
            logger.info('DXF version upgrade to DXF R12.')
            self.dxfversion = DXF12

        if self.dxfversion == DXF12:
            self.tables.create_table_handles()

        if self.dxfversion in (DXF13, DXF14):
            logger.info('DXF version upgrade to DXF R2000.')
            self.dxfversion = DXF2000
            self.create_all_arrow_blocks()

        # Objects section setup:
        self.rootdict = self.objects.rootdict
        # Create missing management tables (DICTIONARY):
        self.objects.setup_objects_management_tables(self.rootdict)

        # Setup modelspace- and paperspace layouts:
        self.layouts = Layouts.load(self)

        # Additional work is common to the new and load process:
        self.is_loading = False
        self._finalize_setup()