
    'j>                    V   d dl mZ d dlmZmZmZmZmZmZ d dl	m
Z
 d dlZd dlmZ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mZmZ dd
lmZ ddlmZ ddlm Z  ddl!m"Z"m#Z# er&d dlm$Z$ d dl%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ ddgZ0 ej1        d          Z2 ed edd           eddej3        e           eddej3        e          d          Z4 ee4          Z5dZ6e G d  de                      Z7	 d8d9d)Z8d:d0Z9d;d1Z:d<d4Z; G d5 de e7                   Z<d=d7Z=dS )>    )annotations)TYPE_CHECKINGIterableIteratorcastUnionOptional)contextmanagerN)	validatorconst)DXFAttrDXFAttributesDefSubclassRETURN_DEFAULTgroup_code_mapping)
AuditError   )
base_classSubclassProcessor	DXFEntity)	DXFObject)register_entity)ObjectCollection)default_copyCopyNotSupported)Auditor)Drawing)DXFNamespace
Dictionary)EntityDB)Layouts)AbstractTagWriterDXFGroupGroupCollectionezdxf	AcDbGroupi,   )defaultF   )r(   r   fixerG   descriptionunnamed
selectableiT  c                      e Zd ZdZdZ eee          Zd/ fdZ	e
fdZ	 d0d1 fdZd Zd2dZd3 fdZd4dZd5dZd6dZd Zd7dZd8dZd9 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d?d.Z  xZ!S )@r#   zGroups are not allowed in block definitions, and each entity can only
    reside in one group, so cloning of groups creates also new entities.

    GROUPreturnNonec                |    t                                                       t                      | _        g | _        d S N)super__init__set_handles_data)self	__class__s    R/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/entities/dxfgroups.pyr7   zDXFGroup.__init__N   s/    "%%%&(


    c                     t          d          )NzCopying of GROUP not supported.)r   )r;   copy_strategys     r=   copyzDXFGroup.copyS   s    @AAAr>   N	processorOptional[SubclassProcessor]r   c                    t                                          |          }|r3|                    |t          dd          }|                     |           |S )Nr   F)log)r6   load_dxf_attribsfast_load_dxfattribsacdb_group_group_codes
load_group)r;   rB   dxftagsr<   s       r=   rF   zDXFGroup.load_dxf_attribsV   sa     gg&&y11 	"11+QE 2  D OOD!!!
r>   c                `    |D ]*\  }}|t           k    r| j                            |           +d S r5   )GROUP_ITEM_CODEr9   add)r;   rK   codevalues       r=   rI   zDXFGroup.load_groupa   sC     	) 	)KD%&& !!%(((		) 	)r>   	tagwriterr"   boolc                    | j         J |                     | j                    t          | j                  st	          j        d          dS )NWAll entities have to be in the same layout and are not allowed to be in a block layout.T)docpurgeall_entities_on_same_layoutr:   r   DXFStructureError)r;   rQ   s     r=   preprocess_exportzDXFGroup.preprocess_exporth   sW    x###

48*4:66 	),   tr>   c                   t                                          |           |                    t          j        t
          j                   | j                            |g d           | 	                    |           dS )z(Export entity specific data as DXF tags.r,   N)
r6   export_entity
write_tag2r   SUBCLASS_MARKER
acdb_groupnamerJ   export_dxf_attribsexport_group)r;   rQ   r<   s     r=   r[   zDXFGroup.export_entityt   so    i(((U2JODDD##I/W/W/WXXX)$$$$$r>   c                d    | j         D ]'}|                    t          |j        j                   (d S r5   )r:   r\   rM   rJ   handle)r;   rQ   entitys      r=   ra   zDXFGroup.export_group{   s?    j 	E 	EF  &*2CDDDD	E 	Er>   Iterator[DXFEntity]c                $    d | j         D             S )zIterate over all DXF entities in :class:`DXFGroup` as instances of
        :class:`DXFGraphic` or inherited (LINE, CIRCLE, ...).

        c              3  (   K   | ]}|j         	|V  d S r5   )is_alive).0es     r=   	<genexpr>z$DXFGroup.__iter__.<locals>.<genexpr>   s)      44a4444444r>   r:   r;   s    r=   __iter__zDXFGroup.__iter__   s    
 544:4444r>   intc                *    t          | j                  S )z7Returns the count of DXF entities in :class:`DXFGroup`.)lenr:   rm   s    r=   __len__zDXFGroup.__len__   s    4:r>   c                    | j         |         S )z9Returns entities by standard Python indexing and slicing.rl   )r;   items     r=   __getitem__zDXFGroup.__getitem__   s    z$r>   rt   Union[str, DXFEntity]c                    t          |t                    r|n|j        j        }|t	          |                                           v S )zReturns ``True`` if item is in :class:`DXFGroup`. `item` has to be
        a handle string or an object of type :class:`DXFEntity` or inherited.

        )
isinstancestrrJ   rc   r8   handles)r;   rt   rc   s      r=   __contains__zDXFGroup.__contains__   s;    
 $D#..CDHOT\\^^,,,,r>   Iterable[str]c                    d | D             S )z=Iterable of handles of all DXF entities in :class:`DXFGroup`.c              3  .   K   | ]}|j         j        V  d S r5   rJ   rc   ri   rd   s     r=   rk   z#DXFGroup.handles.<locals>.<genexpr>   s'      55f
!555555r>    rm   s    r=   rz   zDXFGroup.handles   s    555555r>   rU   	'Drawing'c                     t                                          |           |j        j         fd} fd}t	           |                       _         `|S )Nc                     t                    } t          j        j        |           }t	          |          s4                                 t                              d|  d           d S |_        d S )NCleared z&, had entities from different layouts.)ry   filter_invalid_entitiesr:   rU   rW   clearloggerdebug)r_   entitiesr;   s     r=   set_group_entitiesz3DXFGroup.post_load_hook.<locals>.set_group_entities   so    t99D.tz48TJJH.x88 &

TTTTUUUUU%


r>   c               3  P   K   j         D ]}  |           }|r|j        r|V  d S r5   )r9   rh   )rc   rd   db_getr;   s     r=   r   z)DXFGroup.post_load_hook.<locals>.entities   sJ      - ! ! !fo ! LLL! !r>   )r6   post_load_hookentitydbgetlistr:   r9   )r;   rU   r   r   r   r<   s   `   @r=   r   zDXFGroup.post_load_hook   s    s###!	& 	& 	& 	& 	&	! 	! 	! 	! 	! 	! ((**%%
M!!r>   list[DXFEntity]c              #  Z   K   t          |           }|V  |                     |           dS )aD  Context manager which yields all the group entities as
        standard Python list::

            with group.edit_data() as data:
               # add new entities to a group
               data.append(modelspace.add_line((0, 0), (3, 0)))
               # remove last entity from a group
               data.pop()

        N)r   set_data)r;   datas     r=   	edit_datazDXFGroup.edit_data   s4       Dzz


dr>   r   Iterable[DXFEntity]c                *   | j         J t          |          }t          || j         t          |                     }t	          |          t	          |          k    rt          j        d          t          |          st          j        d          |S )Nzinvalid entities foundrT   )rU   r   r   ry   rq   r   rX   rW   r;   r   valid_entitiess      r=   _validate_entitieszDXFGroup._validate_entities   s    x###>>048SYYOO~#h--//)*BCCC*>:: 	),   r>   c                    |                      |          }|                                  |                     |           || _        dS )a!  Set `entities` as new group content, entities should be an iterable of
        :class:`DXFGraphic` (LINE, CIRCLE, ...).

        Raises:
            DXFValueError: not all entities are located on the same layout (modelspace
                or any paperspace layout but not block)

        N)r   r   _add_group_reactorr:   r   s      r=   r   zDXFGroup.set_data   sC     00::

///#


r>   c                    |                      |          }t          |                                           fd|D             }|                     |           | j                            |           dS )a!  Add `entities` to :class:`DXFGroup`, entities should be an iterable of
        :class:`DXFGraphic` (LINE, CIRCLE, ...).

        Raises:
            DXFValueError: not all entities are located on the same layout (modelspace
                or any paperspace layout but not block)

        c                0    g | ]}|j         j        v|S r   r   )ri   rj   rz   s     r=   
<listcomp>z#DXFGroup.extend.<locals>.<listcomp>   s'    SSSqu|77R7R!7R7R7Rr>   N)r   r8   rz   r   r:   extend)r;   r   r   rz   s      @r=   r   zDXFGroup.extend   sv     00::dllnn%%SSSS^SSS///
.)))))r>   c                H    |                      | j                   g | _        dS )z|Remove all entities from :class:`DXFGroup`, does not delete any
        drawing entities referenced by this group.

        N)_remove_group_reactorr:   rm   s    r=   r   zDXFGroup.clear   s%     	""4:...


r>   c                R    | j         j        }|D ]}|                    |           d S r5   )rJ   rc   append_reactor_handler;   r   group_handlerd   s       r=   r   zDXFGroup._add_group_reactor   s;    x 	7 	7F((6666	7 	7r>   c                R    | j         j        }|D ]}|                    |           d S r5   )rJ   rc   discard_reactor_handler   s       r=   r   zDXFGroup._remove_group_reactor   s;    x 	8 	8F)),7777	8 	8r>   auditorr   c           	        t          |           }|j        J |                     |j                   |t          |           z
  }|dk    r4|                    t          j        d| dt          |                       t          | j                  sF|                    t          j	        dt          |            d           | 
                                 | j        j        }|sdS | j        D ]O}|j        	||j        vr=|                    t          j        d| d	| d
           |                    |           PdS )zRemove invalid entities from :class:`DXFGroup`.

        Invalid entities are:

        - deleted entities
        - all entities which do not reside in model- or paper space
        - all entities if they do not reside in the same layout

        Nr   zRemoved z invalid entities from rO   messager   z2, not all entities are located in the same layout.zEntity z in group #z* does not have group as persistent reactor)rq   rU   rV   fixed_errorr   INVALID_GROUP_ENTITIESry   rW   r:   #GROUP_ENTITIES_IN_DIFFERENT_LAYOUTSr   rJ   rc   reactorsMISSING_PERSISTENT_REACTORr   )r;   r   entity_countremoved_entity_countr   rd   s         r=   auditzDXFGroup.audit   s    4yy{&&&

7;+c$ii7!##6[#7[[PSTXPYPY[[      +4:66 	C$3t99 $ $ $     
 JJLLLx 	Fj 	; 	;F&,fo*M*M###>3f 3 3 3 3 3 $   
 ,,\:::	; 	;r>   r   c                X    t          | j        |t          |                     | _        dS )zRemove invalid group entities.)r   rU   
group_nameN)r   r:   ry   )r;   rU   s     r=   rV   zDXFGroup.purge(  s+    ,ZSSYY
 
 



r>   )r2   r3   r5   )rB   rC   r2   r   )rQ   r"   r2   rR   )rQ   r"   r2   r3   )rQ   r"   )r2   re   )r2   ro   )rt   rv   r2   rR   )r2   r|   )rU   r   )r2   r   )r   r   r2   r   )r   r   r2   r3   )r   r   r2   r3   r   r   r2   r3   )rU   r   r2   r3   )"__name__
__module____qualname____doc__DXFTYPEr   r   r^   
DXFATTRIBSr7   r   rA   rF   rI   rY   r[   ra   rn   rr   ru   r{   rz   r   r
   r   r   r   r   r   r   r   r   rV   __classcell__r<   s   @r=   r#   r#   D   s;        
 Gz:66J) ) ) ) ) )
 ". B B B B 8<	 	 	 	 	 	 	) ) )
 
 
 
% % % % % %E E E E5 5 5 5        - - - -6 6 6 6" " " " " "2    ^   $ $ $ $* * * *   7 7 7 7
8 8 8 8
&; &; &; &;P
 
 
 
 
 
 
 
r>   r   r   rU   r   r   ry   r2   r   c                j   |J |j         }t          |j                  }g }| D ]}|j        r1t	          |j        j        ||          r|                    |           :|rR|j        r.t          	                    t          |           d| d           qt          	                    d|            |S )Nz in z has an invalid owner.zRemoved deleted entity in )r   valid_layout_handleslayoutsrh   _has_valid_ownerrJ   ownerappendr   r   ry   )r   rU   r   dbvalid_owner_handlesr   rd   s          r=   r   r   /  s    
 ???	B.s{;;&(N 	H 	H? 	H/Jb"5 
  
 	H !!&)))) 	H HFSSSSSTTTTF*FFGGGr>   r   r   r    r   set[str]rR   c                z    | dS |r| |vrdS |                     |           }||j        sdS |j        j        dk    S )NF0)r   rh   rJ   layout)r   r   r   r   s       r=   r   r   E  sU    }u u,???uVVE]]F~V_~u
 :##r>   c                Z    t          d | D                       }t          |          dk     S )zgCheck if all entities are on the same layout (model space or any paper
    layout but not block).

    c              3  .   K   | ]}|j         j        V  d S r5   )rJ   r   r   s     r=   rk   z.all_entities_on_same_layout.<locals>.<genexpr>\  s'      99f!999999r>      )r8   rq   )r   ownerss     r=   rW   rW   W  s0    
 9999999Fv;;?r>   r   r!   c                4    t          d | D                       S )z-Returns valid layout keys for group entities.c              3  2   K   | ]}|j         	|j        V  d S r5   )is_any_layout
layout_key)ri   r   s     r=   rk   z'valid_layout_handles.<locals>.<genexpr>c  s-      OOV&:NOv OOOOOOr>   )r8   )r   s    r=   r   r   a  s    OOwOOOOOOr>   c                  \     e Zd Zd fdZddZddZdd	Z	 	 	 dddZd  fdZd!dZ	 xZ
S )"r$   rU   r   c                ^    t                                          |dd           d| _        d S )N
ACAD_GROUPr1   )	dict_nameobject_typer   )r6   r7   _next_unnamed_number)r;   rU   r<   s     r=   r7   zGroupCollection.__init__g  s0    'JJJ$%!!!r>   r2   Iterator[DXFGroup]c              #  "   K   | D ]	\  }}|V  
dS )z Iterable of all existing groups.Nr   )r;   r_   groups      r=   groupszGroupCollection.groupsk  s.       	 	KD%KKKK	 	r>   ry   c                f    |                                  }|| v r|                                  }|| v |S r5   )
_next_name)r;   r_   s     r=   	next_namezGroupCollection.next_namep  s9      dll??$$D dllr>   c                6    | xj         dz  c_         d| j          S )Nr   z*A)r   rm   s    r=   r   zGroupCollection._next_namev  s'    !!Q&!!/D-///r>   Nr'   Tr_   Optional[str]r-   r/   rR   r#   c                   ||| v rt          j        d| d          ||                                 }d}nd}||t          t	          |                    d}t          t          |                     ||                    S )a`  Creates a new group. If `name` is ``None`` an unnamed group is
        created, which has an automatically generated name like "\*Annnn".
        Group names are case-insensitive.

        Args:
            name: group name as string
            description: group description as string
            selectable: group is selectable if ``True``

        NzGROUP 'z' already exists.r   r   r,   )r   DXFValueErrorr   ro   rR   r   r#   _new)r;   r_   r-   r/   r.   
dxfattribss         r=   newzGroupCollection.newz  s      %&G&G&G&GHHH<>>##DGGG 'd:..//
 


 Hdiij99:::r>   r   Union[DXFGroup, str]r3   c                ^   | j         j        }|J t          |t                    r|}nJ|                                dk    rt          ||          }n!t          |                                          || v r#t                                          |           dS t          j
        d          )zoDelete `group`, `group` can be an object of type :class:`DXFGroup`
        or a group name as string.

        Nr1   z$GROUP not in group table registered.)rU   r   rx   ry   dxftypeget_group_name	TypeErrorr6   deleter   r   )r;   r   r   r_   r<   s       r=   r   zGroupCollection.delete  s    
 8$###eS!! 	-DD]]__''!%22DDEMMOO,,,4<<GGNN4     %&LMMMr>   r   r   c                4   g }| D ]S\  }}t          t          |          }|                    |           t          |          s|                    |           T|D ]<}|                    t          j        d| d           |                     |           =dS )z9Removes empty groups and invalid handles from all groups.zRemoved empty group "z".r   N)	r   r#   r   rq   r   r   r   REMOVE_EMPTY_GROUPr   )r;   r   trashr_   r   s        r=   r   zGroupCollection.audit  s     	# 	#KD%5))EKK   u:: #T"""  	 	D28888      KK	 	r>   )rU   r   )r2   r   )r2   ry   )Nr'   T)r_   r   r-   ry   r/   rR   r2   r#   )r   r   r2   r3   r   )r   r   r   r7   r   r   r   r   r   r   r   r   s   @r=   r$   r$   f  s        & & & & & &   
   0 0 0 0 #	; ; ; ; ;@N N N N N N(       r>   r   c                    t          d|| j        j                           }|                                D ]\  }}|| u r|c S dS )zGet name of `group`.r   r'   )r   rJ   r   items)r   r   group_tabler_   rd   s        r=   r   r     sU    |R	%899K#))++  fU??KKK 2r>   )r'   )r   r   rU   r   r   ry   r2   r   )r   ry   r   r    r   r   r2   rR   )r   r   )r   r!   r2   r   )r   r#   r   r    r2   ry   )>
__future__r   typingr   r   r   r   r   r	   
contextlibr
   loggingezdxf.lldxfr   r   ezdxf.lldxf.attributesr   r   r   r   r   ezdxf.auditr   	dxfentityr   r   r   dxfobjr   factoryr   objectcollectionr   rA   r   r   r   ezdxf.documentr   ezdxf.entitiesr   r   ezdxf.entitydbr    ezdxf.layoutsr!   ezdxf.lldxf.tagwriterr"   __all__	getLoggerr   is_integer_boolr^   rH   rM   r#   r   r   rW   r   r$   r   r   r>   r=   <module>r     s   # " " " " "                & % % % % %  ( ( ( ( ( ( ( (              # " " " " " ? ? ? ? ? ? ? ? ? ?       $ $ $ $ $ $ . . . . . . 0 0 0 0 0 0 0 0 8######&&&&&&77777777''''''%%%%%%777777(
)		7	#	#[ wsB/// 7/ 	
 
 
 g/ 	
 
 
  
0 ,+J77  g
 g
 g
 g
 g
y g
 g
 g
Z     ,$ $ $ $$   P P P P
X X X X X&x0 X X Xv     r>   