
    hc"                        S SK Jr  S SKJrJrJrJr  S SKJr  S SK	J
r
  S SKJr  \(       a  S SKJr  S SKJr  S SKJrJr  / S	Qr  " S
 S5      r " S S5      r      SS jrSS jrSS jrSS jrSS jrg)    )annotations)TYPE_CHECKINGIterableOptionalIterator)CounterPOINTER_CODES)referenced_blocks)Drawing)Tags)	DXFEntityBlockRecord)BlockDefinitionIndexBlockReferenceCounterfind_unreferenced_blocksc                  d    \ rS rSrSrSS jr\SS j5       rS rSS jr	SS jr
SS jrSS	 jrS
rg)r   ;   a  Index of all :class:`~ezdxf.entities.BlockRecord` entities representing
real BLOCK definitions, excluding all :class:`~ezdxf.entities.BlockRecord`
entities defining model space or paper space layouts. External references
(XREF) and XREF overlays are included.

c                l    Xl         [        5       U l        [        5       U l        U R	                  5         g N)_docdict_handle_index_name_indexrebuild)selfdocs     ?/var/www/html/env/lib/python3.13/site-packages/ezdxf/blkrefs.py__init__BlockDefinitionIndex.__init__B   s$    	59V376    c                T    [        U R                  R                  R                  5      $ )ziReturns an iterator of all :class:`~ezdxf.entities.BlockRecord`
entities representing BLOCK definitions.
)iterr   tablesblock_records)r   s    r   r%   "BlockDefinitionIndex.block_recordsJ   s    
 DII$$2233r!   c                    U R                   nU R                  nUR                  5         UR                  5         U R                   HF  nUR                  (       d  M  X1UR
                  R                  '   X2UR
                  R                  '   MH     g)zRebuild index from scratch.N)r   r   clearr%   is_block_layoutdxfhandlename)r   handle_index
name_indexblock_records       r   r   BlockDefinitionIndex.rebuildQ   st    ))%%
 ..L+++8D\--4454@<++001 /r!   c                    XR                   ;   $ )zdReturns ``True`` if a :class:`~ezdxf.entities.BlockRecord` for the
given block record handle exist.
)r   r   r+   s     r   
has_handleBlockDefinitionIndex.has_handle\   s     ++++r!   c                    XR                   ;   $ )z[Returns ``True`` if a :class:`~ezdxf.entities.BlockRecord` for the
given block name exist.
)r   r   r,   s     r   has_nameBlockDefinitionIndex.has_nameb   s     ''''r!   c                8    U R                   R                  U5      $ )z`Returns the :class:`~ezdxf.entities.BlockRecord` for the given block
record handle or ``None``.
)r   getr2   s     r   	by_handleBlockDefinitionIndex.by_handleh   s     !!%%f--r!   c                8    U R                   R                  U5      $ )zSReturns :class:`~ezdxf.entities.BlockRecord` for the given block name
or ``None``.
)r   r:   r6   s     r   by_nameBlockDefinitionIndex.by_namen   s     ##D))r!   )r   r   r   N)r   r   )returnzIterator[BlockRecord])r+   strr@   bool)r,   rA   r@   rB   )r+   rA   r@   Optional[BlockRecord])r,   rA   r@   rC   )__name__
__module____qualname____firstlineno____doc__r   propertyr%   r   r3   r7   r;   r>   __static_attributes__ r!   r   r   r   ;   s:     4 4	A,(.*r!   r   c                  :    \ rS rSrSrSS	S jjrS
S jrSS jrSrg)r   u   a  
Counts all block references in a DXF document.

Check if a block is referenced by any entity or any resource (DIMSYTLE,
MLEADERSTYLE) in a DXF document::

    import ezdxf
    from ezdxf.blkrefs import BlockReferenceCounter

    doc = ezdxf.readfile("your.dxf")
    counter = BlockReferenceCounter(doc)
    count = counter.by_name("XYZ")
    print(f"Block 'XYZ' if referenced {count} times.")

Nc                    Ub  UO
[        U5      U l        [        UR                  R	                  5       U R                  5      U l        U R
                  R                  [        U5      5        g r   )r   _block_record_indexcount_referencesentitydbvalues_counterupdateheader_section_handles)r   r   indexs      r   r   BlockReferenceCounter.__init__   s\     &E,@,E 	 
 )LL!4#;#;
 	3C89r!   c                     U R                   U   $ )z[Returns the block reference count for a given
:class:`~ezdxf.entities.BlockRecord` handle.
)rS   r2   s     r   r;   BlockReferenceCounter.by_handle   s     }}V$$r!   c                    SnU R                   R                  U5      nUb  UR                  R                  nU R                  U   $ )z9Returns the block reference count for a given block name. )rO   r>   r*   r+   rS   )r   
block_namer+   r/   s       r   r>   BlockReferenceCounter.by_name   sC    //77
C#!%%,,F}}V$$r!   )rO   rS   r   )r   r   rV   zOptional[BlockDefinitionIndex])r+   rA   r@   int)r\   rA   r@   r^   )	rD   rE   rF   rG   rH   r   r;   r>   rJ   rK   r!   r   r   r   u   s     
:%%r!   r   c                v  ^^ SSK JnJn  SUU4S jjn[        5       mU  H  nU" [	        U5      5        U" [        U5      5        [        XR5      (       a  U" [        UR                  5      5        MR  [        XS5      (       d  Md  UR                  R                  SS   H  nU" [        U5      5        M     M     T$ )Nr   )XRecordDXFTagStoragec                <   > TR                  U4S jU  5       5        g )Nc              3  X   >#    U  H  nTR                  U5      (       d  M  Uv   M!     g 7fr   )r3   ).0hrV   s     r   	<genexpr>3count_references.<locals>.update.<locals>.<genexpr>   s      A'QU-=-=a-@qq's   *	*)rT   )handlescounterrV   s    r   rT    count_references.<locals>.update   s    A'AAr!      )rh   Iterable[str])ezdxf.entitiesr`   ra   r   generic_handlesr   
isinstanceall_pointer_handlestagsxtags
subclasses)entitiesrV   r`   ra   rT   entityrq   ri   s    `     @r   rP   rP      s     6B B yGv&' ()f&&&v{{34..//3*401 4  Nr!   c                N   / nU R                   bC  U R                   R                  R                  5        H  nUR                  S U 5       5        M     U R                  bE  U R                  R                  R                  5        H  nUR                  [        U5      5        M     U$ )Nc              3  :   #    U  H  u  pUS :X  d  M  Uv   M     g7f)i  NrK   rd   codevalues      r   rf   "generic_handles.<locals>.<genexpr>   s     ID[TDDL55Ds   	)xdatadatarR   extendappdatarp   )ru   rh   rq   s      r   rn   rn      s~    G||LL%%,,.DNNIDII /~~!NN''..0DNN.t45 1Nr!   c                    S U  5       $ )Nc              3  B   #    U  H  u  pU[         ;   d  M  Uv   M     g 7fr   r	   rx   s      r   rf   &all_pointer_handles.<locals>.<genexpr>   s     CTkdT]-BEETs   	rK   )rq   s    r   rp   rp      s    CTCCr!   c              #     #    U R                   nS H]  nUR                  US 5      nUc  M  U R                  R                  US 5      nUc  M;  UR                  R                  R
                  v   M_     g 7f)N)z$DIMBLKz$DIMBLK1z$DIMBLK2z
$DIMLDRBLK)headerr:   blocksr/   r*   r+   )r   r   var_nameblk_nameblocks        r   rU   rU      sa     ZZFE::h-JJNN8T2E ((,,333 Fs   %A3A3'A3c                   [        U 5      n[        5       nU R                   He  nUR                  (       a  UR                  (       a  M'  UR                  UR                  5      nUS:X  d  MJ  UR                  UR                  5        Mg     U$ )a  Returns the names of all block definitions without references.

.. warning::

    The DXF reference does not document all uses of blocks. The INSERT entity is
    just one explicit use case, but there are also many indirect block references
    and the customizability of DXF allows you to store block names and handles in
    many places.

    There are some rules for storing names and handles and this module checks all of
    these known rules, but there is no guarantee that everyone follows these rules.

    Therefore, it is still possible to destroy a DXF document by deleting an
    absolutely necessary block definition.

.. versionadded:: 1.3.5

r   )r   setr   is_aliveis_any_layoutr>   r,   add)r   ref_counterunreferenced_blocksr   counts        r   r   r      sk    & (,K$'E~~!4!4##EJJ/A:##EJJ/  r!   N)rt   zIterable[DXFEntity]rV   r   r@   r   )ru   r   r@   rl   )rq   r   r@   rl   )r   z	'Drawing'r@   rl   )r   r   r@   zset[str])
__future__r   typingr   r   r   r   collectionsr   ezdxf.lldxf.typesr
   ezdxf.protocolsr   ezdxf.documentr   ezdxf.lldxf.tagsr   rm   r   r   __all__r   r   rP   rn   rp   rU   r   rK   r!   r   <module>r      sx    # > >  + -&%5
W'T7* 7*t)% )%X!*>4D4r!   