def add_entity(entity: DXFGraphic, layout: "BaseLayout") -> None: """Add `entity` entity to the entity database and to the given `layout`.""" assert entity.dxf.handle is None assert layout is not None if layout.doc: factory.bind(entity, layout.doc) layout.add_entity(entity)
def test_bind_entity_with_existing_handle_to_doc(doc): e = factory.new('POINT') e.dxf.handle = 'ABBA' factory.bind(e, doc) assert e.doc is doc assert e.dxf.handle == 'ABBA', 'should have the original handle' assert e.dxf.handle in doc.entitydb, 'should be stored in the entity database'
def test_direct_interface(doc, entity): factory.bind(entity, doc) xdict = entity.new_extension_dict() placeholder = xdict.add_placeholder("TEST") assert "TEST" in xdict placeholder2 = xdict["TEST"] assert placeholder is placeholder2 xdict["TEST2"] = placeholder2
def test_direct_interface(doc, entity): factory.bind(entity, doc) xdict = entity.new_extension_dict() placeholder = xdict.add_placeholder('TEST', doc) assert 'TEST' in xdict placeholder2 = xdict['TEST'] assert placeholder is placeholder2 xdict['TEST2'] = placeholder2
def test_bind_entity_to_doc(doc): e = factory.new('POINT') factory.bind(e, doc) assert e.doc is doc assert e.dxf.handle is not None, 'should have a handle' assert e.dxf.handle in doc.entitydb, 'should be stored in the entity database' assert e.dxf.owner is None, 'should not be linked to a layout or owner' assert e.is_virtual is False, 'is not a virtual entity'
def test_bind_entity_with_existing_handle_to_doc(doc): e = factory.new("POINT") e.dxf.handle = "ABBA" factory.bind(e, doc) assert e.doc is doc assert e.dxf.handle == "ABBA", "should have the original handle" assert (e.dxf.handle in doc.entitydb), "should be stored in the entity database"
def attrib_to_text(attrib: "Attrib") -> "Text": dxfattribs = attrib.dxfattribs(drop=IGNORE_FROM_ATTRIB) # ATTRIB has same owner as INSERT but does not reside in any EntitySpace() # and must not deleted from any layout. # New TEXT entity has same handle as the replaced ATTRIB entity and replaces # the ATTRIB entity in the database. text = factory.new("TEXT", dxfattribs=dxfattribs) if attrib.doc: factory.bind(text, attrib.doc) return cast("Text", text)
def test_fully_manual_dictionary_copy(self, source: Dictionary): doc = source.doc # manual copy procedure: copy = source.copy() factory.bind(copy, doc) doc.objects.add_object(copy) # this is all done automatically if you use: # doc.entitydb.duplicate_entity(source) assert copy in doc.objects assert factory.is_bound(copy, doc)
def load_and_bind_dxf_content(sections: Dict, doc: 'Drawing') -> None: # HEADER has no database entries. for name in ['TABLES', 'CLASSES', 'ENTITIES', 'BLOCKS', 'OBJECTS']: if name in sections: section = sections[name] for index, entity in enumerate(load_dxf_entities(section)): # Replace Tags() by DXFEntity() objects section[index] = entity # Bind entities to the DXF document: factory.bind(entity, doc)
def test_new_extension_dict(doc, entity): factory.bind(entity, doc) assert entity.has_extension_dict is False xdict = entity.new_extension_dict() assert xdict.dictionary.dxftype() == "DICTIONARY" assert len(xdict.dictionary) == 0 placeholder = xdict.add_placeholder("TEST") assert len(xdict.dictionary) == 1 assert placeholder.dxf.owner == xdict.dictionary.dxf.handle assert "TEST" in xdict.dictionary
def test_copy_entity(doc, entity): factory.bind(entity, doc) try: xdict = entity.get_extension_dict() except AttributeError: xdict = entity.new_extension_dict() xdict.add_placeholder('Test', doc) new_entity = entity.copy() # copying of extension dict is not supported assert new_entity.has_extension_dict is False
def post_bind_hook(self) -> None: """ Called by binding a new or copied dictionary to the document, bind hard owned sub-entities to the same document and add them to the objects section. """ if not self.dxf.hard_owned: return # copied or new dictionary: doc = self.doc owner_handle = self.dxf.handle for _, entity in self.items(): entity.dxf.owner = owner_handle factory.bind(entity, doc) # For a correct DXF export add entities to the objects section: doc.objects.add_object(entity)
def replace_entity(source: 'DXFGraphic', target: 'DXFGraphic', layout: 'BaseLayout') -> None: """ Add `target` entity to the entity database and to the given `layout` and replace the `source` entity by the `target` entity. """ assert target.dxf.handle is None assert layout is not None target.dxf.handle = source.dxf.handle if source in layout: layout.delete_entity(source) if layout.doc: factory.bind(target, layout.doc) layout.add_entity(target) else: source.destroy()
def add_entity(self, entity: 'DXFGraphic') -> None: """ Add an existing :class:`DXFGraphic` entity to a layout, but be sure to unlink (:meth:`~BaseLayout.unlink_entity`) entity from the previous owner layout. Adding entities from a different DXF drawing is not supported. """ # bind virtual entities to the DXF document: if entity.dxf.handle is None and self.doc: factory.bind(entity, self.doc) handle = entity.dxf.handle if handle is None or handle not in self.doc.entitydb: raise DXFStructureError( 'Adding entities from a different DXF drawing is not supported.' ) self.block_record.add_entity(entity)
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
def add_entity(self, entity: "DXFGraphic") -> None: """Add an existing :class:`DXFGraphic` entity to a layout, but be sure to unlink (:meth:`~BaseLayout.unlink_entity`) entity from the previous owner layout. Adding entities from a different DXF drawing is not supported. """ # bind virtual entities to the DXF document: doc = self.doc if entity.dxf.handle is None and doc: factory.bind(entity, doc) handle = entity.dxf.handle if handle is None or handle not in self.doc.entitydb: raise DXFStructureError( "Adding entities from a different DXF drawing is not supported." ) if not is_graphic_entity(entity): raise DXFTypeError(f"invalid entity {str(entity)}") self.block_record.add_entity(entity)
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`, 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: DXFEntity = entity.copy() new_entity.dxf.handle = self.next_handle() factory.bind(new_entity, entity.doc) return new_entity
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
def test_is_bound_true(doc): e = factory.new('POINT') factory.bind(e, doc) assert factory.is_bound(e, doc) is True assert e.is_bound is True
def test_bind_dead_entity_to_doc(doc): e = factory.new('POINT') e.destroy() with pytest.raises(AssertionError): factory.bind(e, doc)
def test_if_destroyed_entity_is_bound(doc): e = factory.new('POINT') factory.bind(e, doc) e.destroy() assert factory.is_bound(e, doc) is False assert e.is_bound is False
def bind(entities: Iterable[DXFEntity], doc: Drawing): """Bind all entities to the DXF document.""" for entity in entities: factory.bind(entity, doc)
def bounded_blocks(dxf12): entities = list(load_entities(TESTBLOCKS, 'BLOCKS')) for entity in entities: factory.bind(entity, dxf12) return BlocksSection(dxf12, entities)