entities.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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
  6. from itertools import chain
  7. from ezdxf.entityspace import EntitySpace
  8. from .abstract import AbstractSection
  9. if TYPE_CHECKING: # import forward declarations
  10. from ezdxf.eztypes import ExtendedTags, Drawing, DXFEntity, TagWriter
  11. class EntitySection(AbstractSection):
  12. """
  13. The new EntitySection() collects only at startup the entities in the ENTITIES section. By creating the Layouts()
  14. object all entities form the EntitySection() moved into the Layout() objects, and the entity space of the
  15. EntitySection() will be deleted by calling EntitySection.clear().
  16. """
  17. name = 'ENTITIES'
  18. def __init__(self, entities: Iterable['ExtendedTags'], drawing: 'Drawing'):
  19. super().__init__(EntitySpace(drawing.entitydb), entities, drawing)
  20. def __iter__(self) -> 'DXFEntity':
  21. layouts = self.drawing.layouts
  22. for entity in chain(layouts.modelspace(), layouts.active_layout()):
  23. yield entity
  24. def __len__(self) -> int:
  25. layouts = self.drawing.layouts
  26. return len(layouts.modelspace()) + len(layouts.active_layout())
  27. # none public interface
  28. def clear(self) -> None:
  29. # remove entities for entities section -> stored in layouts
  30. del self._entity_space
  31. def _setup_entities(self) -> Iterable['DXFEntity']:
  32. # required for the drawing setup process
  33. wrap = self.dxffactory.wrap_handle
  34. for handle in self._entity_space:
  35. yield wrap(handle)
  36. def model_space_entities(self) -> 'EntitySpace':
  37. # required for the drawing setup process
  38. es = EntitySpace(self.entitydb)
  39. es.extend(self._filter_entities(paper_space=0))
  40. return es
  41. def active_layout_entities(self) -> 'EntitySpace':
  42. # required for the drawing setup process
  43. es = EntitySpace(self.entitydb)
  44. es.extend(self._filter_entities(paper_space=1))
  45. return es
  46. def _filter_entities(self, paper_space: int = 0) -> Iterable[str]:
  47. # required for the drawing setup process
  48. return (entity.dxf.handle for entity in self._setup_entities() if
  49. entity.get_dxf_attrib('paperspace', 0) == paper_space)
  50. def delete_all_entities(self) -> None:
  51. layouts = self.drawing.layouts
  52. layouts.modelspace().delete_all_entities()
  53. layouts.active_layout().delete_all_entities()
  54. def write(self, tagwriter: 'TagWriter') -> None:
  55. tagwriter.write_str(" 0\nSECTION\n 2\n%s\n" % self.name.upper())
  56. # Just write *Model_Space and the active *Paper_Space into the ENTITIES section.
  57. self.drawing.layouts.write_entities_section(tagwriter)
  58. tagwriter.write_tag2(0, "ENDSEC")