
    Ogc"                        d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ erd dlmZ d dlmZ d dlmZmZ g d	Z	  G d
 d      Z G d d      Z	 	 	 	 	 	 ddZddZddZddZddZy)    )annotations)TYPE_CHECKINGIterableOptionalIterator)CounterPOINTER_CODES)referenced_blocks)Drawing)Tags)	DXFEntityBlockRecord)BlockDefinitionIndexBlockReferenceCounterfind_unreferenced_blocksc                  P    e Zd ZdZd
dZedd       Zd ZddZddZ	ddZ
ddZy	)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                n    || _         t               | _        t               | _        | j	                          y N)_docdict_handle_index_name_indexrebuild)selfdocs     R/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/blkrefs.py__init__zBlockDefinitionIndex.__init__B   s&    	59V376    c                T    t        | j                  j                  j                        S )zyReturns an iterator of all :class:`~ezdxf.entities.BlockRecord`
        entities representing BLOCK definitions.
        )iterr   tablesblock_records)r   s    r   r#   z"BlockDefinitionIndex.block_recordsJ   s    
 DII$$2233r   c                   | j                   }| j                  }|j                          |j                          | j                  D ]A  }|j                  s|||j
                  j                  <   |||j
                  j                  <   C y)zRebuild index from scratch.N)r   r   clearr#   is_block_layoutdxfhandlename)r   handle_index
name_indexblock_records       r   r   zBlockDefinitionIndex.rebuildQ   s|    ))%%
 .. 	AL++8D\--4454@
<++001	Ar   c                    || j                   v S )ztReturns ``True`` if a :class:`~ezdxf.entities.BlockRecord` for the
        given block record handle exist.
        )r   r   r(   s     r   
has_handlezBlockDefinitionIndex.has_handle\   s     ++++r   c                    || j                   v S )zkReturns ``True`` if a :class:`~ezdxf.entities.BlockRecord` for the
        given block name exist.
        )r   r   r)   s     r   has_namezBlockDefinitionIndex.has_nameb   s     t''''r   c                8    | j                   j                  |      S )zpReturns the :class:`~ezdxf.entities.BlockRecord` for the given block
        record handle or ``None``.
        )r   getr.   s     r   	by_handlezBlockDefinitionIndex.by_handleh   s     !!%%f--r   c                8    | j                   j                  |      S )zcReturns :class:`~ezdxf.entities.BlockRecord` for the given block name
        or ``None``.
        )r   r4   r1   s     r   by_namezBlockDefinitionIndex.by_namen   s     ##D))r   N)r   r   )returnzIterator[BlockRecord])r(   strr8   bool)r)   r9   r8   r:   )r(   r9   r8   Optional[BlockRecord])r)   r9   r8   r;   )__name__
__module____qualname____doc__r   propertyr#   r   r/   r2   r5   r7    r   r   r   r   ;   s:     4 4	A,(.*r   r   c                  *    e Zd ZdZdddZddZd	dZy)
r   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                    ||n
t        |      | _        t        |j                  j	                         | j                        | _        | j
                  j                  t        |             y r   )r   _block_record_indexcount_referencesentitydbvalues_counterupdateheader_section_handles)r   r   indexs      r   r   zBlockReferenceCounter.__init__   s\     &E,@,E 	 
 )LL!4#;#;
 	3C89r   c                     | j                   |   S )zkReturns the block reference count for a given
        :class:`~ezdxf.entities.BlockRecord` handle.
        )rH   r.   s     r   r5   zBlockReferenceCounter.by_handle   s     }}V$$r   c                    d}| j                   j                  |      }||j                  j                  }| j                  |   S )z9Returns the block reference count for a given block name. )rD   r7   r'   r(   rH   )r   
block_namer(   r,   s       r   r7   zBlockReferenceCounter.by_name   sC    //77
C#!%%,,F}}V$$r   r   )r   r   rK   zOptional[BlockDefinitionIndex])r(   r9   r8   int)rO   r9   r8   rP   )r<   r=   r>   r?   r   r5   r7   rA   r   r   r   r   u   s     
:%%r   r   c                V   ddl m}m} dfd}t               | D ]  } |t	        |              |t        |             t        ||      r |t        |j                               Mt        ||      sZ|j                  j                  dd  D ]  } |t        |               S )Nr   )XRecordDXFTagStoragec                :    j                  fd| D               y )Nc              3  F   K   | ]  }j                  |      s|  y wr   )r/   ).0hrK   s     r   	<genexpr>z3count_references.<locals>.update.<locals>.<genexpr>   s     AQU-=-=a-@qAs   !!)rI   )handlescounterrK   s    r   rI   z count_references.<locals>.update   s    A'AAr      )rY   Iterable[str])ezdxf.entitiesrR   rS   r   generic_handlesr   
isinstanceall_pointer_handlestagsxtags
subclasses)entitiesrK   rR   rS   rI   entityra   rZ   s    `     @r   rE   rE      s     6B yG 2v&' ()fg&&v{{34.//3 2*40122 Nr   c                B   g }| j                   A| j                   j                  j                         D ]  }|j                  d |D                | j                  C| j                  j                  j                         D ]  }|j                  t        |              |S )Nc              3  2   K   | ]  \  }}|d k(  s|  yw)i  NrA   rV   codevalues      r   rX   z"generic_handles.<locals>.<genexpr>   s     I[T5DDL5Is   )xdatadatarG   extendappdatar`   )re   rY   ra   s      r   r^   r^      s    G||LL%%,,. 	JDNNIDII	J~~!NN''..0 	6DNN.t45	6Nr   c                    d | D        S )Nc              3  8   K   | ]  \  }}|t         v s|  y wr   r	   rh   s      r   rX   z&all_pointer_handles.<locals>.<genexpr>   s     CkdET]-BECs   rA   )ra   s    r   r`   r`      s    CTCCr   c              #     K   | j                   }dD ]X  }|j                  |d       }|| j                  j                  |d       }|7|j                  j                  j
                   Z y w)N)z$DIMBLKz$DIMBLK1z$DIMBLK2z
$DIMLDRBLK)headerr4   blocksr,   r'   r(   )r   rr   var_nameblk_nameblocks        r   rJ   rJ      sf     ZZFE 4::h-JJNN8T2E ((,,3334s   &A-A-%A-c                    t        |       }t               }| j                  D ]W  }|j                  r|j                  r|j                  |j                        }|dk(  s=|j                  |j                         Y |S )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   setrs   is_aliveis_any_layoutr7   r)   add)r   ref_counterunreferenced_blocksrv   counts        r   r   r      sp    & (,K$'E 0~~!4!4##EJJ/A:##EJJ/0 r   N)rd   zIterable[DXFEntity]rK   r   r8   r   )re   r   r8   r\   )ra   r   r8   r\   )r   z	'Drawing'r8   r\   )r   r   r8   zset[str])
__future__r   typingr   r   r   r   collectionsr   ezdxf.lldxf.typesr
   ezdxf.protocolsr   ezdxf.documentr   ezdxf.lldxf.tagsr   r]   r   r   __all__r   r   rE   r^   r`   rJ   r   rA   r   r   <module>r      sx    # > >  + -&%5
W'T7* 7*t)% )%X!*>4D4r   