示例#1
0
class TestHeaderSection(unittest.TestCase):
    def setUp(self):
        tags = Tags.from_text(TESTHEADER)
        dwg = DrawingProxy('AC1009')
        self.header = HeaderSection(tags)
        self.header.set_headervar_factory(dwg.dxffactory.headervar_factory)

    def test_get_acadver(self):
        result = self.header['$ACADVER']
        self.assertEqual('AC1009', result)

    def test_get_insbase(self):
        result = self.header['$INSBASE']
        self.assertEqual((0., 0., 0.), result)

    def test_getitem_keyerror(self):
        with self.assertRaises(KeyError):
            var = self.header['$TEST']

    def test_get(self):
        result = self.header.get('$TEST', 'TEST')
        self.assertEqual('TEST', result)

    def test_set_existing_var(self):
        self.header['$ACADVER'] = 'AC666'
        self.assertEqual('AC666', self.header['$ACADVER'])

    def test_set_existing_point(self):
        self.header['$INSBASE'] = (1, 2, 3)
        self.assertEqual((1, 2, 3), self.header['$INSBASE'])

    def test_set_unknown_var(self):
        with self.assertRaises(KeyError):
            self.header['$TEST'] = 'test'

    def test_create_var(self):
        self.header['$LIMMAX'] = (10, 20)
        self.assertEqual((10, 20), self.header['$LIMMAX'])

    def test_create_var_wrong_args_2d(self):
        self.header['$LIMMAX'] = (10, 20, 30)
        self.assertEqual((10, 20), self.header['$LIMMAX'])

    def test_create_var_wrong_args_3d(self):
        with self.assertRaises(IndexError):
            self.header['$PUCSORG'] = (10, 20)

    def test_contains(self):
        self.assertTrue('$ACADVER' in self.header)

    def test_not_contains(self):
        self.assertFalse('$MOZMAN' in self.header)

    def test_remove_headervar(self):
        del self.header['$ACADVER']
        self.assertTrue('$ACADVER' not in self.header)

    def test_str_point(self):
        insbase_str = str(self.header.hdrvars['$INSBASE'])
        self.assertEqual(INSBASE, insbase_str)
示例#2
0
class TestCustomProperties(unittest.TestCase):
    def setUp(self):
        tags = Tags.from_text(TESTCUSTOMPROPERTIES)
        dwg = DrawingProxy('AC1009')
        self.header = HeaderSection(tags)
        self.header.set_headervar_factory(dwg.dxffactory.headervar_factory)

    def test_custom_properties_exists(self):
        self.assertTrue(self.header.custom_vars.has_tag("Custom Property 1"))

    def test_order_of_occurrence(self):
        properties = self.header.custom_vars.properties
        self.assertEqual(("Custom Property 1", "Custom Value 1"),
                         properties[0])
        self.assertEqual(("Custom Property 2", "Custom Value 2"),
                         properties[1])

    def test_get_custom_property(self):
        self.assertEqual("Custom Value 1",
                         self.header.custom_vars.get("Custom Property 1"))

    def test_get_custom_property_2(self):
        self.assertEqual("Custom Value 2",
                         self.header.custom_vars.get("Custom Property 2"))

    def test_add_custom_property(self):
        self.header.custom_vars.append("Custom Property 3", "Custom Value 3")
        self.assertEqual(3, len(self.header.custom_vars))
        self.assertEqual("Custom Value 3",
                         self.header.custom_vars.get("Custom Property 3"))

    def test_remove_custom_property(self):
        self.header.custom_vars.remove("Custom Property 1")
        self.assertEqual(1, len(self.header.custom_vars))

    def test_remove_not_existing_property(self):
        with self.assertRaises(ValueError):
            self.header.custom_vars.remove("Does not Exist")

    def test_replace_custom_property(self):
        self.header.custom_vars.replace("Custom Property 1", "new value")
        self.assertEqual("new value",
                         self.header.custom_vars.get("Custom Property 1"))

    def test_replace_not_existing_property(self):
        with self.assertRaises(ValueError):
            self.header.custom_vars.replace("Does not Exist", "new value")
示例#3
0
文件: drawing.py 项目: vshu3000/ezdxf
 def _setup(self):
     self.header = HeaderSection.new()
     self.classes = ClassesSection(self)
     self.tables = TablesSection(self)
     self.blocks = BlocksSection(self)
     self.entities = EntitySection(self)
     self.objects = ObjectsSection(self)
     self.acdsdata = AcDsDataSection(self)  # AcDSData section is not supported for new drawings
     self.rootdict = self.objects.rootdict
     self.objects.setup_objects_management_tables(self.rootdict)  # create missing tables
     self.layouts = Layouts.setup(self)
     self._finalize_setup()
示例#4
0
    def _setup_doc(self) -> Drawing:
        doc = Drawing(dxfversion=self.specs.version)
        doc.encoding = self.specs.encoding
        doc.header = HeaderSection.new()

        # Setup basic header variables not stored in the header section of the DWG file.
        doc.header["$ACADVER"] = self.specs.version
        doc.header["$ACADMAINTVER"] = self.specs.maintenance_release_version
        doc.header["$DWGCODEPAGE"] = codepage.tocodepage(self.specs.encoding)

        doc.classes = ClassesSection(doc)
        # doc.tables = TablesSection(doc)
        # doc.blocks = BlocksSection(doc)
        # doc.entities = EntitySection(doc)
        # doc.objects = ObjectsSection(doc)
        # doc.acdsdata = AcDsDataSection(doc)
        return doc
示例#5
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()
    def _load(self, tagger: Iterable['DXFTag']):
        sections = load_dxf_structure(
            tagger)  # load complete DXF entity structure
        try:  # discard section THUMBNAILIMAGE
            del sections['THUMBNAILIMAGE']
        except KeyError:
            pass
        # -----------------------------------------------------------------------------------
        # 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)
        # -----------------------------------------------------------------------------------
        # missing $ACADVER defaults to DXF R12
        self._dxfversion = self.header.get('$ACADVER', DXF12)  # type: str
        self._loaded_dxfversion = self._dxfversion  # save dxf version of loaded file
        self.encoding = toencoding(self.header.get(
            '$DWGCODEPAGE', 'ANSI_1252'))  # type: str # read/write
        # get handle seed
        seed = self.header.get('$HANDSEED',
                               str(self.entitydb.handles))  # type: str
        # setup handles
        self.entitydb.handles.reset(seed)
        # store all necessary DXF entities in the drawing database
        fill_database(sections, self.dxffactory)
        # all handles used in the DXF file are known at this point
        # -----------------------------------------------------------------------------------
        # 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()
        # table records available
        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 valid for DXF R2013 and later
        self.acdsdata = AcDsDataSection(self, sections.get('ACDSDATA', None))

        for name, data in sections.items():
            if name not in MANAGED_SECTIONS:
                self.stored_sections.append(StoredSection(data))
        # -----------------------------------------------------------------------------------
        if self.dxfversion < DXF12:
            # upgrade to DXF R12
            logger.info('Upgrading drawing to DXF R12.')
            self.dxfversion = DXF12

        # DIMSTYLE: ezdxf uses names for blocks, linetypes and text style as internal data, handles are set at export
        # requires BLOCKS and TABLES section!
        self.tables.resolve_dimstyle_names()

        if self.dxfversion == DXF12:
            # TABLE requires in DXF12 no handle and has no owner tag, but DXF R2000+, requires a TABLE with handle
            # and each table entry has an owner tag, pointing to the TABLE entry
            self.tables.create_table_handles()

        if self.dxfversion in (DXF13, DXF14):
            # upgrade to DXF R2000
            self.dxfversion = DXF2000

        self.rootdict = self.objects.rootdict
        self.objects.setup_objects_management_tables(
            self.rootdict)  # create missing tables

        self.layouts = Layouts.load(self)
        self._finalize_setup()
示例#7
0
def header():
    tags = Tags.from_text(TESTHEADER)
    tags.pop()  # remove 'ENDSEC'
    dwg = DrawingProxy('AC1009')
    header = HeaderSection(tags)
    return header
示例#8
0
def header_custom():
    tags = Tags.from_text(TESTCUSTOMPROPERTIES)
    tags.pop()  # remove 'ENDSEC'
    dwg = DrawingProxy('AC1009')
    header = HeaderSection(tags)
    return header
def header():
    tags = Tags.from_text(TESTHEADER)
    tags.pop()  # remove 'ENDSEC'
    header = HeaderSection.load(tags)
    return header
示例#10
0
def header_custom():
    tags = Tags.from_text(TESTCUSTOMPROPERTIES)
    tags.pop()  # remove 'ENDSEC'
    header = HeaderSection.load(tags)
    return header
示例#11
0
def test_new_dxf12():
    header = HeaderSection.new(ezdxf.const.DXF12)
    assert header['$ACADVER'] == DXF12
示例#12
0
 def setUp(self):
     tags = Tags.from_text(TESTCUSTOMPROPERTIES)
     dwg = DrawingProxy('AC1009')
     self.header = HeaderSection(tags)
     self.header.set_headervar_factory(dwg.dxffactory.headervar_factory)