
    Og~@                    
   d dl mZ d dlmZmZmZmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ erd d
lmZmZmZ d dl m!Z! h dZ" G d de      Z# G d de#      Z$ G d de#      Z%y)    )annotations)TYPE_CHECKINGIteratorUnionIterableOptional)factoryis_graphic_entitySortEntsTable)InsertUnits)DXFKeyErrorDXFValueErrorDXFStructureErrorLATEST_DXF_VERSIONDXFTypeError)EntityQuery)groupby)EntityDBEntitySpace)CreatorInterface)
DXFGraphicBlockRecordExtensionDict)KeyFunc>   ARCLINEMESHTEXTHATCHMTEXTPOINTSHAPESOLIDTRACE3DFACEATTDEFATTRIBCIRCLESPLINEELLIPSEPOLYLINE
LWPOLYLINEc                  b    e Zd Z e       Zed
d       ZddZddZd Z	dddZ
dddZd Zd	 Zy)_AbstractLayoutc                .    | j                   j                  S )z/Returns drawing entity database. (internal API))docentitydbselfs    W/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/layouts/base.pyr1   z_AbstractLayout.entitydb1   s     xx       c                ,    t        | j                        S )z.Returns count of entities owned by the layout.)lenentity_spacer2   s    r4   __len__z_AbstractLayout.__len__6   s    4$$%%r5   c                ,    t        | j                        S )z8Returns iterable of all drawing entities in this layout.)iterr8   r2   s    r4   __iter__z_AbstractLayout.__iter__:   s    D%%&&r5   c                     | j                   |   S )a  Get entity at `index`.

        The underlying data structure for storing entities is organized like a
        standard Python list, therefore `index` can be any valid list indexing
        or slicing term, like a single index ``layout[-1]`` to get the last
        entity, or an index slice ``layout[:10]`` to get the first 10 or less
        entities as ``list[DXFGraphic]``.

        )r8   )r3   indexs     r4   __getitem__z_AbstractLayout.__getitem__>   s       ''r5   c                ,    t        t        |       |      S )z=Get all DXF entities matching the :ref:`entity query string`.)r   r;   )r3   querys     r4   rA   z_AbstractLayout.queryJ   s    4:u--r5   Nc                .    t        t        |       ||      S )a  
        Returns a ``dict`` of entity lists, where entities are grouped by a
        `dxfattrib` or a `key` function.

        Args:
            dxfattrib: grouping by DXF attribute like ``'layer'``
            key: key function, which accepts a :class:`DXFGraphic` entity as
                argument and returns the grouping key of an entity or ``None``
                to ignore the entity. Reason for ignoring: a queried DXF
                attribute is not supported by entity.

        )r   r;   )r3   	dxfattribkeys      r4   r   z_AbstractLayout.groupbyN   s     tDz9c22r5   c                     y N r2   s    r4   destroyz_AbstractLayout.destroy]   s    r5   c                8    | j                   j                          y)z;Remove all destroyed entities from the layout entity space.N)r8   purger2   s    r4   rJ   z_AbstractLayout.purge`   s    !r5   )returnr   )rK   int)rK   zIterator[DXFGraphic])*)rA   strrK   r   ) N)rC   rN   rD   zOptional[KeyFunc]rK   dict)__name__
__module____qualname__r   r8   propertyr1   r9   r<   r?   rA   r   rH   rJ   rG   r5   r4   r.   r.   .   s>    =L! !&'
(.3"r5   r.   c                  J    e Zd Zd fdZed        Zedd       Zed        Zedd       Zedd       Z	edd       Z
edd       Zedd	       Zedd
       Zej                  dd       ZddZddZdddZddZddZd dZd!dZd dZdd"dZd#dZd$dZd%d&dZ xZS )'
BaseLayoutc                t    |j                   }|J t        | 	  |       |j                  | _        || _        y rF   )r0   super__init__r8   block_record)r3   rZ   r0   	__class__s      r4   rY   zBaseLayout.__init__f   s;    (55)5r5   c                B    | j                   j                  j                  S )z+Returns block record handle. (internal API)rZ   dxfhandler2   s    r4   block_record_handlezBaseLayout.block_record_handlen   s       $$+++r5   c                B    | j                   j                  j                  S )zReturns the layout key as hex string.

        The layout key is the handle of the associated BLOCK_RECORD entry in the
        BLOCK_RECORDS table.

        (internal API)
        r]   r2   s    r4   
layout_keyzBaseLayout.layout_keys   s       $$+++r5   c                .    | j                   j                  S )z``False`` if layout is deleted.)rZ   is_aliver2   s    r4   rd   zBaseLayout.is_alive~   s       )))r5   c                \    | j                   j                  r| j                   j                  S y)z``True`` if is active layout.F)rZ   rd   is_active_paperspacer2   s    r4   rf   zBaseLayout.is_active_paperspace   s(     %%$$999r5   c                .    | j                   j                  S )z-``True`` if is any kind of paperspace layout.)rZ   is_any_paperspacer2   s    r4   rh   zBaseLayout.is_any_paperspace   s       222r5   c                .    | j                   j                  S )z!``True`` if is modelspace layout.)rZ   is_modelspacer2   s    r4   rj   zBaseLayout.is_modelspace          ...r5   c                .    | j                   j                  S )z;``True`` if is any kind of modelspace or paperspace layout.)rZ   is_any_layoutr2   s    r4   rm   zBaseLayout.is_any_layout   rk   r5   c                .    | j                   j                  S )zn``True`` if not any kind of modelspace or paperspace layout, just a
        regular block definition.
        )rZ   is_block_layoutr2   s    r4   ro   zBaseLayout.is_block_layout   s    
   000r5   c                B    | j                   j                  j                  S )z_Get/Set layout/block drawing units as enum, see also :ref:`set
        drawing units`.
        rZ   r^   unitsr2   s    r4   rr   zBaseLayout.units   s       $$***r5   c                :    || j                   j                  _        y)z'Set layout/block drawing units as enum.Nrq   )r3   values     r4   rr   zBaseLayout.units   s     ',#r5   c                r    | j                   }|j                  r|j                         S |j                         S )z]Returns the associated extension dictionary, creates a new one if
        necessary.
        )rZ   has_extension_dictget_extension_dictnew_extension_dict)r3   rZ   s     r4   rw   zBaseLayout.get_extension_dict   s6     ((**22442244r5   c                h   | j                   }|j                  j                  |rt        j                  ||       |j                  j                  }||| j                   j
                  vrt        d      t        |      st        dt        |             | j                  j                  |       y)a  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.

        .. warning:: 
        
            This is a low-level tool - use it with caution and make sure you understand 
            what you are doing! If used improperly, the DXF document may be damaged.

        Nz>Adding entities from a different DXF drawing is not supported.zinvalid entity )r0   r^   r_   r	   bindr1   r   r
   r   rN   rZ   
add_entity)r3   entityr0   r_   s       r4   r{   zBaseLayout.add_entity   s     hh::$LL%"">V488+<+<<#P  !(V>??$$V,r5   c                F   |j                   }|j                         }|t        vrt        d|       || j                   u rt	        d      |(|r|j                         }nt        j                  |       |j                  | j                          | j                  |       y)a  Add a foreign DXF entity to a layout, this foreign entity could be
        from another DXF document or an entity without an assigned DXF document.
        The intention of this method is to add **simple** entities from another
        DXF document or from a DXF iterator, for more complex operations use the
        :mod:`~ezdxf.addons.importer` add-on. Especially objects with BLOCK
        section (INSERT, DIMENSION, MLEADER) or OBJECTS section dependencies
        (IMAGE, UNDERLAY) can not be supported by this simple method.

        Not all DXF types are supported and every dependency or resource
        reference from another DXF document will be removed except attribute
        layer will be preserved but only with default attributes like
        color ``7`` and linetype ``CONTINUOUS`` because the layer attribute
        doesn't need a layer table entry.

        If the entity is part of another DXF document, it will be unlinked from
        this document and its entity database if argument `copy` is ``False``,
        else the entity will be copied. Unassigned entities like from DXF
        iterators will just be added.

        Supported DXF types:

            - POINT
            - LINE
            - CIRCLE
            - ARC
            - ELLIPSE
            - LWPOLYLINE
            - SPLINE
            - POLYLINE
            - 3DFACE
            - SOLID
            - TRACE
            - SHAPE
            - MESH
            - ATTRIB
            - ATTDEF
            - TEXT
            - MTEXT
            - HATCH

        Args:
            entity: DXF entity to copy or move
            copy: if ``True`` copy entity from other document else unlink from
                other document

        Raises:
            CopyNotSupported: copying of `entity` i not supported
        zunsupported DXF type: zentity from same DXF documentN)
r0   dxftypeSUPPORTED_FOREIGN_ENTITY_TYPESr   r   copyr	   unbindremove_dependenciesr{   )r3   r|   r   foreign_docr~   s        r4   add_foreign_entityzBaseLayout.add_foreign_entity   s    b jj.."88!7yABB$((" ?@@" v&""488,r5   c                :    | j                   j                  |       y)zUnlink `entity` from layout but does not delete entity from the
        entity database, this removes `entity` just from the layout entity space.
        N)rZ   unlink_entityr3   r|   s     r4   r   zBaseLayout.unlink_entity       	''/r5   c                :    | j                   j                  |       y)znDelete `entity` from layout entity space and the entity database,
        this destroys the `entity`.
        N)rZ   delete_entityr   s     r4   r   zBaseLayout.delete_entity  r   r5   c                F    t        |       D ]  }| j                  |        y)zzDelete all entities from this layout and from entity database,
        this destroys all entities in this layout.
        N)listr   r   s     r4   delete_all_entitieszBaseLayout.delete_all_entities"  s%     4j 	'Fv&	'r5   c                    |j                   |j                   k7  rt        d      	 | j                  |       |j                  |       y# t        $ r t        d      w xY w)zMove entity to another layout.

        Args:
            entity: DXF entity to move
            layout: any layout (modelspace, paperspace, block) from
                **same** drawing

        z7Moving between different DXF drawings is not supported.zLayout does not contain entity.N)r0   r   r   r{   
ValueErrorr   )r3   r|   layouts      r4   move_to_layoutzBaseLayout.move_to_layout+  sf     ::##I 	&v& f%  	C ABB	Cs   A	 	Ac                    | j                   j                  j                  | j                  j                  j
                         y)z+Delete all linked resources. (internal API)N)r0   block_recordsremoverZ   r^   namer2   s    r4   rH   zBaseLayout.destroy?  s.     	%%d&7&7&;&;&@&@Ar5   c                    | j                         }	 |d   }|S # t        $ rT |rF| j                  j                  j	                  d|j
                  | j                  d      }||d<   Y |S t        d      w xY w)a  Get/Create the SORTENTSTABLE object associated to the layout.

        Args:
            create: new table if table do not exist and `create` is ``True``

        Raises:
            DXFValueError: if table not exist and `create` is ``False``

        (internal API)
        ACAD_SORTENTSSORTENTSTABLE)ownerr`   
dxfattribsz8Extension dictionary entry ACAD_SORTENTS does not exist.)rw   r   r0   objects
new_entityr_   rb   r   )r3   createxdictsortents_tables       r4   get_sortents_tablezBaseLayout.get_sortents_tableE  s     '')	"?3N   	!%!1!1!<!<#!&/3  "= " *8o&
  $N 	s    AA6*A6c                    | j                         }t        |t              r|j                         }|j	                  |       y)a  If the header variable $SORTENTS `Regen` flag (bit-code value 16)
        is set, AutoCAD regenerates entities in ascending handles order.

        To change redraw order associate a different sort-handle to entities,
        this redefines the order in which the entities are regenerated.
        The `handles` argument can be a dict of entity_handle and sort_handle
        as (k, v) pairs, or an iterable of (entity_handle, sort_handle) tuples.

        The sort-handle doesn't have to be unique, some or all entities can
        share the same sort-handle and a sort-handle can be an existing handle.

        The "0" handle can be used, but this sort-handle will be drawn as
        latest (on top of all other entities) and not as first as expected.

        Args:
            handles: iterable or dict of handle associations; an iterable
                of 2-tuples (entity_handle, sort_handle) or a dict (k, v)
                association as (entity_handle, sort_handle)

        N)r   
isinstancerP   itemsset_handles)r3   handlessortentss      r4   set_redraw_orderzBaseLayout.set_redraw_orderc  s5    * **,gt$mmoGW%r5   c                    | j                   j                  r| j                         }n
t               S 	 |d   }t        |      S # t        $ r t               cY S w xY w)zReturns iterable for all existing table entries as (entity_handle,
        sort_handle) pairs, see also :meth:`~BaseLayout.set_redraw_order`.

        r   )rZ   rv   rw   tupler   r;   )r3   r   r   s      r4   get_redraw_orderzBaseLayout.get_redraw_order}  s\    
 //++-E7N	"?3N N##  	7N	s   A AAc                    ddl m} | j                         }|r|j                  | j                  |      S |j                  | j                  |      S )zYields all entities from layout in ascending redraw order or
        descending redraw order if `reverse` is ``True``.
        r   )reorder)ezdxfr   r   
descendingr8   	ascending)r3   reverser   redraw_orders       r4   entities_in_redraw_orderz#BaseLayout.entities_in_redraw_order  sJ     	",,.%%d&7&7FF  !2!2LAAr5   )rZ   r   rK   rN   )rK   bool)rK   r   )rt   r   rK   None)rK   r   r|   r   rK   r   )TrK   r   )r|   r   r   rV   rK   r   )r   r   rK   r   )r   z&Union[dict, Iterable[tuple[str, str]]]rK   r   )rK   zIterable[tuple[str, str]])F)rK   zIterable[DXFGraphic])rQ   rR   rS   rY   rT   r`   rb   rd   rf   rh   rj   rm   ro   rr   setterrw   r{   r   r   r   r   r   rH   r   r   r   r   __classcell__r[   s   @r4   rV   rV   e   s   6 , , , , * *   3 3 / / / / 1 1 + + \\, ,5-4A F00'&(B<&4$ 	B 	Br5   rV   c                  l     e Zd ZdZ fdZedd       ZddZddZddZ	ddZ
ddZdd	Zdd
Z xZS )VirtualLayoutah  Helper class to disassemble complex entities into basic DXF
    entities by rendering into a virtual layout.

    All entities do not have an assigned DXF document and therefore
    are not stored in any entity database and can not be added to another
    layout by :meth:`add_entity`.

    Deleting entities from this layout does not destroy the entity!

    c                B    t         |   d        t               | _        y rF   )rX   rY   r   r8   )r3   r[   s    r4   rY   zVirtualLayout.__init__  s    'Mr5   c                    t         S rF   )r   r2   s    r4   
dxfversionzVirtualLayout.dxfversion  s    !!r5   c                :    | j                   j                  |       y rF   )r8   addr   s     r4   r{   zVirtualLayout.add_entity  s    f%r5   c                j    t        j                  ||      }| j                  j                  |       |S )Nr   )r	   newr8   r   )r3   type_r   r|   s       r4   r   zVirtualLayout.new_entity  s+    Uz:f%r5   c                :    | j                   j                  |       y rF   r8   r   r   s     r4   r   zVirtualLayout.unlink_entity        (r5   c                :    | j                   j                  |       y rF   r   r   s     r4   r   zVirtualLayout.delete_entity  r   r5   c                8    | j                   j                          y rF   )r8   clearr2   s    r4   r   z!VirtualLayout.delete_all_entities  s    !r5   c                    |j                   }|j                  }| j                  D ]<  }	 |j                         }||_         |j                  |       |j                  |       > y# t        $ r Y Kw xY w)z,Copy all entities to a real document layout.N)r0   r1   r8   r   r   r   r{   )r3   r   r0   r1   r|   clones         r4   copy_all_to_layoutz VirtualLayout.copy_all_to_layout  sp    jj<<'' 	%F EILLe$	%   s   A%%	A10A1c                    |j                   }|j                  }| j                  D ]+  }||_         |j                  |       |j	                  |       - | j                          y)z,Move all entities to a real document layout.N)r0   r1   r8   r   r{   r   )r3   r   r0   r1   r|   s        r4   move_all_to_layoutz VirtualLayout.move_all_to_layout  sX    jj<<'' 	&FFJLL f%	& 	  "r5   r   r   )r   rN   r   rP   rK   r   r   )r   rV   rK   r   )rQ   rR   rS   __doc__rY   rT   r   r{   r   r   r   r   r   r   r   r   s   @r4   r   r     sD    	* " "&
))"%#r5   r   N)&
__future__r   typingr   r   r   r   r   ezdxf.entitiesr	   r
   r   ezdxf.enumsr   ezdxf.lldxf.constr   r   r   r   r   ezdxf.queryr   ezdxf.groupbyr   ezdxf.entitydbr   r   ezdxf.graphicsfactoryr   r   r   r   ezdxf.eztypesr   r   r.   rV   r   rG   r5   r4   <module>r      sp    # E E D D #  $ ! 0 2EE%" ,4"& 4"nqB qBh	:#O :#r5   