abstract.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Purpose: entity section
  2. # Created: 13.03.2011
  3. # Copyright (c) 2011-2018, Manfred Moitzi
  4. # License: MIT License
  5. from typing import TYPE_CHECKING, Iterable, Iterator
  6. import abc
  7. from ezdxf.lldxf.tags import DXFStructureError, DXFValueError
  8. from ezdxf.lldxf.extendedtags import get_xtags_linker
  9. from ezdxf.query import EntityQuery
  10. if TYPE_CHECKING: # import forward declarations
  11. from ezdxf.eztypes import ExtendedTags, TagWriter, DXFEntity, EntitySpace, Drawing, DXFFactoryType, EntityDB
  12. class AbstractSection:
  13. name = 'abstract' # type: str
  14. def __init__(self, entity_space: 'EntitySpace', entities: Iterable['ExtendedTags'], drawing: 'Drawing'):
  15. self._entity_space = entity_space
  16. self.drawing = drawing
  17. if entities is not None:
  18. self._build(iter(entities))
  19. @property
  20. def dxffactory(self) -> 'DXFFactoryType':
  21. return self.drawing.dxffactory
  22. @property
  23. def entitydb(self) -> 'EntityDB':
  24. return self.drawing.entitydb
  25. def get_entity_space(self) -> 'EntitySpace':
  26. return self._entity_space
  27. def _build(self, entities: Iterator['ExtendedTags']) -> None:
  28. section_head = next(entities)
  29. if section_head[0] != (0, 'SECTION') or section_head[1] != (2, self.name.upper()):
  30. raise DXFStructureError("Critical structure error in {} section.".format(self.name.upper()))
  31. linked_tags = get_xtags_linker()
  32. for entity in entities:
  33. if not linked_tags(entity): # don't store linked entities (VERTEX, ATTRIB, SEQEND) in entity space
  34. self._entity_space.store_tags(entity)
  35. def write(self, tagwriter: 'TagWriter') -> None:
  36. tagwriter.write_str(" 0\nSECTION\n 2\n%s\n" % self.name.upper())
  37. self._entity_space.write(tagwriter)
  38. tagwriter.write_tag2(0, "ENDSEC")
  39. def create_new_dxf_entity(self, _type: str, dxfattribs: dict) -> 'DXFEntity':
  40. """
  41. Create new DXF entity add it to th entity database and add it to the entity space.
  42. """
  43. dxf_entity = self.dxffactory.create_db_entry(_type, dxfattribs)
  44. self._entity_space.add_handle(dxf_entity.dxf.handle)
  45. return dxf_entity
  46. def add_handle(self, handle: str) -> None:
  47. self._entity_space.add_handle(handle)
  48. def remove_handle(self, handle: str) -> None:
  49. try:
  50. self._entity_space.remove(handle)
  51. except ValueError:
  52. raise DXFValueError('Handle #{} not in entity space.'.format(handle))
  53. def delete_entity(self, entity: 'DXFEntity') -> None:
  54. self.remove_handle(entity.dxf.handle)
  55. self.entitydb.delete_entity(entity)
  56. # start of public interface
  57. @abc.abstractmethod
  58. def __iter__(self) -> Iterable['DXFEntity']:
  59. pass
  60. def __len__(self) -> int:
  61. return len(self._entity_space)
  62. def __contains__(self, handle: str) -> bool:
  63. return handle in self._entity_space
  64. def query(self, query: str = '*') -> EntityQuery:
  65. return EntityQuery(iter(self), query)
  66. def delete_all_entities(self) -> None:
  67. """ Delete all entities. """
  68. self._entity_space.delete_all_entities()
  69. # end of public interface