Exemple #1
0
def test_default_constructor():
    entity = DXFEntity()
    assert entity.dxftype() == 'DXFENTITY'
    assert entity.dxf.handle is None
    assert entity.dxf.owner is None
    assert entity == entity
    assert entity != DXFEntity()
Exemple #2
0
    def add(self, entity: DXFEntity) -> None:
        """ Add `entity` to database, assigns a new handle to the `entity`
        if :attr:`entity.dxf.handle` is ``None``. Adding the same entity
        multiple times is possible, but creates only a single entry.

        """
        if entity.dxftype() in DATABASE_EXCLUDE:
            if entity.dxf.handle is not None:
                # store entities with handles (TABLE, maybe others) to avoid
                # reassigning of its handle
                self[entity.dxf.handle] = entity
            return
        handle: str = entity.dxf.handle
        if handle is None:
            handle = self.next_handle()
            # update_handle() requires the objects section to update the owner
            # handle of the extension dictionary, but this is no problem at
            # file loading, all entities have handles, and DXF R12 (without handles)
            # have no extension dictionaries.
            entity.update_handle(handle)
        self[handle] = entity

        # add sub entities like ATTRIB, VERTEX and SEQEND to database
        if isinstance(entity, LinkedEntities):
            entity.add_sub_entities_to_entitydb(self)
Exemple #3
0
    def discard(self, entity: DXFEntity) -> None:
        """ Discard `entity` from database without destroying the `entity`. """
        if entity.is_alive:
            if hasattr(entity, 'process_sub_entities'):
                entity.process_sub_entities(lambda e: self.discard(e))

            handle = entity.dxf.handle
            try:
                del self._database[handle]
                entity.dxf.handle = None
            except KeyError:
                pass
Exemple #4
0
def test_can_not_reset_entity_handle():
    """Can not reset the DXF handle of an entity to a handle, which is already
    used by another entity.
    """
    db = EntityDB()
    entity1 = DXFEntity()
    entity2 = DXFEntity()
    db.add(entity1)
    db.add(entity2)
    handle = entity1.dxf.handle

    assert db.reset_handle(entity1, entity2.dxf.handle) is False
    assert entity1.dxf.handle == handle
def test_delete_entity():
    db = EntityDB()
    entity = DXFEntity()
    db.add(entity)
    assert len(db) == 1
    db.delete_entity(entity)
    assert len(db) == 0
Exemple #6
0
def test_reset_entity_handle():
    db = EntityDB()
    entity = DXFEntity()
    db.add(entity)
    assert db.reset_handle(entity, "FEFE") is True
    assert entity.dxf.handle == "FEFE"
    assert "FEFE" in db
Exemple #7
0
def test_delete_entity():
    db = EntityDB()
    entity = DXFEntity.from_text("0\nTEST\n5\nFFFF\n")
    db.add(entity)
    assert len(db) == 1
    db.delete_entity(entity)
    assert len(db) == 0
def test_discard_contained_entity():
    db = EntityDB()
    e = DXFEntity()
    db.add(e)
    assert len(db) == 1
    db.discard(e)
    assert len(db) == 0
    assert e.dxf.handle is None
def test_discard_entity_with_none_handle():
    db = EntityDB()
    e = DXFEntity()
    assert e.dxf.handle is None
    # call should not raise any Exception
    db.discard(e)
    # 2rd call should not raise any Exception
    db.discard(e)
def test_restore_integrity_remove_invalid_handle():
    db = EntityDB()
    e = DXFEntity.new(handle='ABBA')
    db.add(e)
    assert len(db) == 1

    # set invalid handle
    e.dxf.handle = 'XFFF'
    db.audit(auditor)
    assert len(db) == 0
def test_add_entity_multiple_times():
    db = EntityDB()
    e = DXFEntity()
    db.add(e)
    handle = e.dxf.handle
    assert len(db) == 1

    db.add(e)
    assert e.dxf.handle == handle, 'handle must not change'
    assert len(db) == 1, 'do not store same entity multiple times'
Exemple #12
0
def test_restore_integrity_remove_invalid_None():
    db = EntityDB()
    e = DXFEntity.from_text("0\nTEST\n5\nABBA\n")
    db.add(e)
    assert len(db) == 1

    # set invalid handle
    e.dxf.handle = None
    db.audit(auditor)
    assert len(db) == 0
def test_restore_integrity_purge():
    db = EntityDB()
    e = DXFEntity.new()
    db.add(e)
    assert len(db) == 1
    db.audit(auditor)
    assert len(db) == 1
    e.destroy()
    db.audit(auditor)
    assert len(db) == 0
Exemple #14
0
def test_restore_integrity_purge():
    db = EntityDB()
    e = DXFEntity.from_text("0\nTEST\n5\nABBA\n")
    db.add(e)
    assert len(db) == 1
    db.audit(auditor)
    assert len(db) == 1
    e.destroy()
    db.audit(auditor)
    assert len(db) == 0
def test_restore_integrity_remove_invalid_None():
    db = EntityDB()
    e = DXFEntity.new()
    db.add(e)
    assert len(db) == 1

    # set invalid handle
    e.dxf.handle = None
    db.audit(auditor)
    assert len(db) == 0
Exemple #16
0
def test_delete_dead_entity_entity():
    db = EntityDB()
    entity = DXFEntity.from_text("0\nTEST\n5\nFFFF\n")
    db.add(entity)
    assert len(db) == 1
    entity.destroy()
    # delete_entity() should not raise an error if entity is not alive!
    db.delete_entity(entity)
    # but entity.destroy() does not remove entity from EntityDB!
    assert 'FFFF' in db
    assert len(db) == 1
Exemple #17
0
    def add(self, entity: DXFEntity) -> None:
        """ Add `entity` to database, assigns a new handle to the `entity` if :attr:`entity.dxf.handle` is ``None``. """
        if entity.dxftype() in DATABASE_EXCLUDE:
            if entity.dxf.handle is not None:
                # store entities with handles (TABLE, maybe others) to avoid reassigning of its handle
                self[entity.dxf.handle] = entity
            return
        handle = entity.dxf.handle  # type: str
        if handle is None:
            handle = self.next_handle()
            # update_handle() requires the objects section to update the owner handle of the extension dictionary,
            # but this is no problem at file loading, all entities have handles, and DXF R12 (without handles) have no
            # extension dictionaries.
            entity.update_handle(handle)
        self[handle] = entity

        # add sub entities like ATTRIB, VERTEX and SEQEND to database
        # only INSERT and POLYLINE using this feature
        if hasattr(entity, 'add_sub_entities_to_entitydb'):
            entity.add_sub_entities_to_entitydb()
def test_delete_dead_entity_entity():
    db = EntityDB()
    entity = DXFEntity.new(handle='FEFE')
    db.add(entity)
    assert len(db) == 1
    entity.destroy()
    # delete_entity() should not raise an error if entity is not alive!
    db.delete_entity(entity)
    # but entity.destroy() does not remove entity from EntityDB!
    assert 'FEFE' in db
    assert len(db) == 1
def test_discard_entity_with_handle_not_in_database():
    db = EntityDB()
    e = DXFEntity()
    e.dxf.handle = 'ABBA'
    assert e.dxf.handle not in db
    # call should not raise any Exception
    db.discard(e)
    # 2rd call should not raise any Exception
    db.discard(e)
    assert e.dxf.handle is 'ABBA', \
        'set handle to None, only if entity was removed'
Exemple #20
0
    def add(self, entity: DXFEntity) -> None:
        """ Add `entity` to database, assigns a new handle to the `entity`
        if :attr:`entity.dxf.handle` is ``None``. Adding the same entity
        multiple times is possible and creates only a single database entry.

        """
        if entity.dxftype() in DATABASE_EXCLUDE:
            if entity.dxf.handle is not None:
                # Mark existing entity handle as used to avoid
                # reassigning the same handle again.
                self[entity.dxf.handle] = entity
            return
        handle: str = entity.dxf.handle
        if handle is None:
            handle = self.next_handle()
            entity.update_handle(handle)
        self[handle] = entity

        # Add sub entities ATTRIB, VERTEX and SEQEND to database.
        if isinstance(entity, LinkedEntities):
            entity.add_sub_entities_to_entitydb(self)
def test_trashcan_context_manager():
    db = EntityDB()
    entities = [DXFEntity() for _ in range(5)]
    for e in entities:
        db.add(e)
    handles = list(db.keys())
    with db.trashcan() as trashcan:
        trashcan.add(handles[0])
        trashcan.add(handles[1])

    assert len(db) == 3
    assert entities[0].is_alive is False
    assert entities[1].is_alive is False
def test_restore_integrity_recover():
    db = EntityDB()
    e = DXFEntity.new(handle='ABBA')
    db.add(e)
    assert len(db) == 1

    # modify handle
    e.dxf.handle = 'FEFE'
    assert 'ABBA' in db
    assert 'FEFE' not in db

    db.audit(auditor)
    assert len(db) == 1
    assert 'FEFE' in db
    assert 'ABBA' not in db
Exemple #23
0
def test_restore_integrity_recover():
    db = EntityDB()
    e = DXFEntity.from_text("0\nTEST\n5\nABBA\n")
    db.add(e)
    assert len(db) == 1

    # modify handle
    e.dxf.handle = 'FEFE'
    assert 'ABBA' in db
    assert 'FEFE' not in db

    db.audit(auditor)
    assert len(db) == 1
    assert 'FEFE' in db
    assert 'ABBA' not in db
Exemple #24
0
def test_restore_integrity_recover():
    db = EntityDB()
    e = DXFEntity.new(handle="ABBA")
    db.add(e)
    assert len(db) == 1

    # modify handle
    e.dxf.handle = "FEFE"
    assert "ABBA" in db
    assert "FEFE" not in db

    db.audit(auditor)
    assert len(db) == 1
    assert "FEFE" in db
    assert "ABBA" not in db
Exemple #25
0
    def duplicate_entity(self, entity: DXFEntity) -> DXFEntity:
        """ Duplicates `entity` and its sub entities (VERTEX, ATTRIB, SEQEND)
        and store them with new handles in the entity database.
        Graphical entities have to be added to a layout by
        :meth:`~ezdxf.layouts.BaseLayout.add_entity`.

        To import DXF entities from another drawing use the
        :class:`~ezdxf.addons.importer.Importer` add-on.

        A new owner handle will be set by adding the duplicated entity to a
        layout.

        """
        new_entity: DXFEntity = entity.copy()
        new_entity.dxf.handle = self.next_handle()
        factory.bind(new_entity, entity.doc)
        return new_entity
Exemple #26
0
    def duplicate_entity(self, entity: DXFEntity) -> DXFEntity:
        """
        Duplicates `entity` and its sub entities (VERTEX, ATTRIB, SEQEND) and store them with new handles in the
        drawing database. This is the recommend method to duplicate DXF entities in a drawing. Graphical entities
        have to be added to a layout by :meth:`~ezdxf.layouts.BaseLayout.add_entity`, for other DXF entities:
        DON'T DUPLICATE THEM.

        To import DXF entities into another drawing use the :class:`~ezdxf.addons.importer.Importer` add-on.

        An existing owner tag is not changed because this is not the domain of the :class:`EntityDB` class, will be set
        by adding the duplicated entity to a layout.

        This is not a deep copy in the meaning of Python, because handles and links are changed.

        """
        new_entity = entity.copy()  # type: DXFEntity
        new_entity.dxf.handle = self.next_handle()
        self.add(new_entity)
        return new_entity
Exemple #27
0
    def duplicate_entity(self, entity: DXFEntity) -> DXFEntity:
        """Duplicates `entity` and its sub entities (VERTEX, ATTRIB, SEQEND)
        and store them with new handles in the entity database.
        Graphical entities have to be added to a layout by
        :meth:`~ezdxf.layouts.BaseLayout.add_entity`. DXF objects will
        automatically added to the OBJECTS section.

        To import DXF entities from another drawing use the
        :class:`~ezdxf.addons.importer.Importer` add-on.

        A new owner handle will be set by adding the duplicated entity to a
        layout.

        """
        doc = entity.doc
        assert doc is not None, "valid DXF document required"
        new_handle = self.next_handle()
        new_entity: DXFEntity = entity.copy()
        new_entity.dxf.handle = new_handle
        factory.bind(new_entity, doc)
        if isinstance(new_entity, DXFObject):
            # add DXF objects automatically to the OBJECTS section
            doc.objects.add_object(new_entity)
        return new_entity
Exemple #28
0
def entity():
    return DXFEntity.from_text(ENTITY)
def test_set_value(db):
    new_entity = DXFEntity.new(handle='FEFE')
    db['FEFE'] = new_entity
    assert new_entity is db['FEFE']
# Copyright (c) 2011-2020, Manfred Moitzi
# License: MIT License
import pytest
from ezdxf.entitydb import EntityDB
from ezdxf.entities.dxfentity import DXFEntity
from ezdxf.audit import Auditor

ENTITY = DXFEntity.new(handle='FFFF')
auditor = Auditor(None)


@pytest.fixture
def db():
    db = EntityDB()
    db['FEFE'] = ENTITY
    return db


def test_get_value(db):
    assert ENTITY is db['FEFE']


def test_set_value(db):
    new_entity = DXFEntity.new(handle='FEFE')
    db['FEFE'] = new_entity
    assert new_entity is db['FEFE']


def test_del_value(db):
    del db['FEFE']
    with pytest.raises(KeyError):