# Path: core/ezdxf/sections/classes.py
# class ClassesSection(AbstractSection):
#     name = 'classes'
# 
#     def __init__(self, tags, drawing):
#         entity_space = EntitySpace(drawing.entitydb)
#         super(ClassesSection, self).__init__(entity_space, tags, drawing)
# 
#     def __iter__(self):  # no layout setting required/possible
#         for handle in self._entity_space:
#             yield self.dxffactory.wrap_handle(handle)
# 
# Path: core/ezdxf/lldxf/const.py
# class DXFStructureError(DXFError):
#     pass
# 
# class DXFInternalEzdxfError(DXFError):
#     pass
# 
# Path: core/ezdxf/modern/groups.py
# class DXFGroupTable(object):
#     def __init__(self, dxfgroups):
#         self.dxfgroups = dxfgroups  # AcDbDictionary
#         self.objects_section = dxfgroups.drawing.objects
#         self._next_unnamed_number = 0
# 
#     def __iter__(self):
#         wrap = self.dxfgroups.dxffactory.wrap_handle
#         for name, handle in self.dxfgroups.items():
#             yield name, wrap(handle)
# 
#     def __len__(self):
#         return len(self.dxfgroups)
# 
#     def __contains__(self, name):
#         return name in self.dxfgroups
# 
#     def groups(self):
#         for name, group in self:
#             yield group
# 
#     def next_name(self):
#         name_exists = True
#         while name_exists:
#             name = self._next_name()
#             name_exists = name in self.dxfgroups
#         return name
# 
#     def _next_name(self):
#         self._next_unnamed_number += 1
#         return "*A{}".format(self._next_unnamed_number)
# 
#     def add(self, name=None, description="", selectable=1):  # TODO remove deprecated interface
#         warnings.warn("DXFGroupTable.add() is deprecated use DXFGroupTable.new() instead.", DeprecationWarning)
#         self.new(name, description, selectable)
# 
#     def new(self, name=None, description="", selectable=1):
#         if name in self.dxfgroups:
#             raise ValueError("Group '{}' already exists. Group name has to be unique.".format(name))
#         unnamed = 0
#         if name is None:
#             name = self.next_name()
#             unnamed = 1
#         # The group name isn't stored in the group entity itself.
#         group = self.objects_section.create_new_dxf_entity("GROUP", dxfattribs={
#             'description': description,
#             'unnamed': unnamed,
#             'selectable': selectable,
#         })
#         self.dxfgroups[name] = group.dxf.handle
#         group.dxf.owner = self.dxfgroups.dxf.handle  # group table is owner of group
#         return group
# 
#     def get(self, name):
#         for group_name, group in self:
#             if name == group_name:
#                 return group
#         raise KeyError("KeyError: '{}'".format(name))
# 
#     def delete(self, group):
#         """Delete group. Does not delete any drawing entities referenced by this group.
#         """
#         if isstring(group):  # delete group by name
#             name = group
#             group_handle = self.dxfgroups[name]
#             del self.dxfgroups[name]
#         else:  # group should be a DXFEntity
#             group_handle = group.dxf.handle
#             for name, _group in self:
#                 if group_handle == _group.dxf.handle:
#                     del self.dxfgroups[name]
#                     return
#             raise ValueError("Group not in group table registered.")
#         self._destroy_dxf_group_entity(group_handle)
# 
#     def _destroy_dxf_group_entity(self, handle):
#         self.objects_section.remove_handle(handle)  # remove from entity space
#         self.objects_section.entitydb.delete_handle(handle)  # remove from drawing database
# 
#     def clear(self):
#         """Delete all groups. Does not delete any drawing entities referenced by this groups.
#         """
#         for name, group in self:  # destroy dxf group entities
#             self._destroy_dxf_group_entity(group.dxf.handle)
#         self.dxfgroups.clear()  # delete all group references
# 
#     def cleanup(self):
#         """Removes invalid handles in all groups and removes empty groups.
#         """
#         empty_groups = []
#         for name, group in self:
#             group.remove_invalid_handles()
#             if not len(group):  # remove empty group
#                 # do not delete groups while iterating over groups!
#                 empty_groups.append(name)
# 
#         # now delete emtpy groups
#         for name in empty_groups:
#             self.delete(name)


from .classes import ClassesSection
from ..lldxf.const import DXFStructureError, DXFInternalEzdxfError
from ..modern.groups import DXFGroupTable
# Purpose: entity section
# Created: 13.03.2011
# Copyright (C) 2011, Manfred Moitzi
# License: MIT License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"



class ObjectsSection(ClassesSection):
    name = 'objects'

    @property
    def roothandle(self):
        return self._entity_space[0]

    @property
    def rootdict(self):
        if len(self):
            return self.dxffactory.wrap_entity(self.entitydb[self.roothandle])
        else:
            return self.setup_rootdict()

    def setup_rootdict(self):
        """Create a root dictionary. Has to be the first object in the objects section.
        """
        if len(self):

# Path: core/ezdxf/sections/classes.py
# class ClassesSection(AbstractSection):
#     name = 'classes'
# 
#     def __init__(self, tags, drawing):
#         entity_space = EntitySpace(drawing.entitydb)
#         super(ClassesSection, self).__init__(entity_space, tags, drawing)
# 
#     def __iter__(self):  # no layout setting required/possible
#         for handle in self._entity_space:
#             yield self.dxffactory.wrap_handle(handle)
# 
# Path: core/ezdxf/lldxf/const.py
# class DXFStructureError(DXFError):
#     pass
# 
# class DXFInternalEzdxfError(DXFError):
#     pass
# 
# Path: core/ezdxf/modern/groups.py
# class DXFGroupTable(object):
#     def __init__(self, dxfgroups):
#         self.dxfgroups = dxfgroups  # AcDbDictionary
#         self.objects_section = dxfgroups.drawing.objects
#         self._next_unnamed_number = 0
# 
#     def __iter__(self):
#         wrap = self.dxfgroups.dxffactory.wrap_handle
#         for name, handle in self.dxfgroups.items():
#             yield name, wrap(handle)
# 
#     def __len__(self):
#         return len(self.dxfgroups)
# 
#     def __contains__(self, name):
#         return name in self.dxfgroups
# 
#     def groups(self):
#         for name, group in self:
#             yield group
# 
#     def next_name(self):
#         name_exists = True
#         while name_exists:
#             name = self._next_name()
#             name_exists = name in self.dxfgroups
#         return name
# 
#     def _next_name(self):
#         self._next_unnamed_number += 1
#         return "*A{}".format(self._next_unnamed_number)
# 
#     def add(self, name=None, description="", selectable=1):  # TODO remove deprecated interface
#         warnings.warn("DXFGroupTable.add() is deprecated use DXFGroupTable.new() instead.", DeprecationWarning)
#         self.new(name, description, selectable)
# 
#     def new(self, name=None, description="", selectable=1):
#         if name in self.dxfgroups:
#             raise ValueError("Group '{}' already exists. Group name has to be unique.".format(name))
#         unnamed = 0
#         if name is None:
#             name = self.next_name()
#             unnamed = 1
#         # The group name isn't stored in the group entity itself.
#         group = self.objects_section.create_new_dxf_entity("GROUP", dxfattribs={
#             'description': description,
#             'unnamed': unnamed,
#             'selectable': selectable,
#         })
#         self.dxfgroups[name] = group.dxf.handle
#         group.dxf.owner = self.dxfgroups.dxf.handle  # group table is owner of group
#         return group
# 
#     def get(self, name):
#         for group_name, group in self:
#             if name == group_name:
#                 return group
#         raise KeyError("KeyError: '{}'".format(name))
# 
#     def delete(self, group):
#         """Delete group. Does not delete any drawing entities referenced by this group.
#         """
#         if isstring(group):  # delete group by name
#             name = group
#             group_handle = self.dxfgroups[name]
#             del self.dxfgroups[name]
#         else:  # group should be a DXFEntity
#             group_handle = group.dxf.handle
#             for name, _group in self:
#                 if group_handle == _group.dxf.handle:
#                     del self.dxfgroups[name]
#                     return
#             raise ValueError("Group not in group table registered.")
#         self._destroy_dxf_group_entity(group_handle)
# 
#     def _destroy_dxf_group_entity(self, handle):
#         self.objects_section.remove_handle(handle)  # remove from entity space
#         self.objects_section.entitydb.delete_handle(handle)  # remove from drawing database
# 
#     def clear(self):
#         """Delete all groups. Does not delete any drawing entities referenced by this groups.
#         """
#         for name, group in self:  # destroy dxf group entities
#             self._destroy_dxf_group_entity(group.dxf.handle)
#         self.dxfgroups.clear()  # delete all group references
# 
#     def cleanup(self):
#         """Removes invalid handles in all groups and removes empty groups.
#         """
#         empty_groups = []
#         for name, group in self:
#             group.remove_invalid_handles()
#             if not len(group):  # remove empty group
#                 # do not delete groups while iterating over groups!
#                 empty_groups.append(name)
# 
#         # now delete emtpy groups
#         for name in empty_groups:
#             self.delete(name)


# Path: core/ezdxf/sections/objects.py
from .classes import ClassesSection
from ..lldxf.const import DXFStructureError, DXFInternalEzdxfError
from ..modern.groups import DXFGroupTable

# Purpose: entity section
# Created: 13.03.2011
# Copyright (C) 2011, Manfred Moitzi
# License: MIT License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"



class ObjectsSection(ClassesSection):
    name = 'objects'

    @property
    def roothandle(self):
        return self._entity_space[0]

    @property
    def rootdict(self):
        if len(self):
            return self.dxffactory.wrap_entity(self.entitydb[self.roothandle])
        else:
            return self.setup_rootdict()

    def setup_rootdict(self):
        """Create a root dictionary. Has to be the first object in the objects section.
        """
        if len(self):

            raise DXFStructureError("Can not create root dictionary in none empty objects section.")