Ë
    £èhS#  ã                  óÞ   — 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     ú-lib/python3.12/site-packages/ezdxf/blkrefs.pyÚ__init__zBlockDefinitionIndex.__init__B   s&   € ØˆŒ	ä59³VˆÔä37³6ˆÔØ�‰�ó    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   € ô
 �D—I‘I×$Ñ$×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   su   € à×)Ñ)ˆØ×%Ñ%ˆ
Ø×ÑÔØ×ÑÔØ ×.Ô.ˆLØ×+Ó+Ø8D�˜\×-Ñ-×4Ñ4Ñ5Ø4@�
˜<×+Ñ+×0Ñ0Ò1ñ /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ð 	Ô ô
 )Ø�L‰L×ÑÓ! 4×#;Ñ#;ó
ˆŒð 	�‰×ÑÔ3°CÓ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   øè ø€ ÐA¡'˜Q¨U×-=Ñ-=¸aÕ-@”q¡'ùs   ƒ!š!)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ô “y€GÛˆáŒ˜vÓ&Ô'áÔ  Ó(Ô)ä�f˜gÔ&ÙÔ& v§{¡{Ó3Õ4Ü˜ Õ.àŸ™×/Ñ/°°Ó3�ÙÔ*¨4Ó0Õ1ñ 4ð ð €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±D¡[ T¨5¸DÀD»Lœ5±Dùs   ‚�)ÚxdataÚdatarG   ÚextendÚappdatar`   )re   rY   ra   s      r   r^   r^   »   s~   € Ø€GØ‡|�|ÐØ—L‘L×%Ñ%×,Ñ,Ö.ˆDØ�N‰NÑI±DÓIÕIð /à‡~�~Ð!Ø—N‘N×'Ñ'×.Ñ.Ö0ˆDØ�N‰NÔ.¨tÓ4Õ5ð 1à€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   è ø€ ÐC¡T‘k�d˜E¨T´]Ò-BŒE¡Tùs   ‚“rA   )ra   s    r   r`   r`   Æ   s   € ÙC¡TÓ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   Ê   sb   è ø€ Ø�Z‰Z€FÛEˆØ—:‘:˜h¨Ó-ˆØÑØ—J‘J—N‘N 8¨TÓ2ˆEØÑ Ø×(Ñ(×,Ñ,×3Ñ3Ó3ñ Fùs   ‚&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   Ô   sk   € ô& (¨Ó,€KÜ$'£EÐà—”ˆØ�~Š~ ×!4Ò!4ØØ×#Ñ# E§J¡JÓ/ˆØ�A‹:Ø×#Ñ# E§J¡JÕ/ð ð Ð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   