
    OglU                    b   d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZ  G d	 d
      Z G d dej                        ZddZ	 	 	 	 ddZ G d d      Zd Z G d d      Z	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZddZ dd dZ!dd!dZ"	 d"	 	 	 	 	 d#dZ#y)$    )annotations)IterableIteratorCallableHashableSequenceUnionOptionalN)abc)	DXFEntity)groupby)Vec3Vec2)EntityQueryParserc                  (    e Zd ZddZddZd Zd Zy)_AttributeDescriptorc                    || _         y N)name)selfr   s     P/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/query.py__init__z_AttributeDescriptor.__init__   s	    	    Nc                8    |j                  | j                        S r   )__getitem__r   )r   objobjtypes      r   __get__z_AttributeDescriptor.__get__   s    tyy))r   c                <    |j                  | j                  |       y r   )__setitem__r   )r   r   values      r   __set__z_AttributeDescriptor.__set__   s    		5)r   c                :    |j                  | j                         y r   )__delitem__r   )r   r   s     r   
__delete__z_AttributeDescriptor.__delete__"   s    		"r   )r   strr   )__name__
__module____qualname__r   r   r"   r%    r   r   r   r      s    **#r   r   c                     e Zd ZdZ ed      Z ed      Z ed      Z ed      Z ed      Z	 ed      Z
 ed      Z ed	      Z	 d-	 	 	 d.dZd/dZd0dZd Zd Zd Zd1dZ	 	 	 	 d2dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d3d1dZ!d4d Z"d4d!Z#e$d"        Z%e$d#        Z&	 d5	 	 	 	 	 d6d$Z'd5d7d%Z(d5d7d&Z)	 	 d8	 	 	 	 	 d9d'Z*d:d(Z+d;d)Z,d;d*Z-d;d+Z.d;d,Z/y
)<EntityQuerya
  EntityQuery is a result container, which is filled with dxf entities
    matching the query string. It is possible to add entities to the container
    (extend), remove entities from the container and to filter the container.

    Query String
    ============

    QueryString := EntityQuery ("[" AttribQuery "]")*

    The query string is the combination of two queries, first the required
    entity query and second the optional attribute query, enclosed in square
    brackets.

    Entity Query
    ------------

    The entity query is a whitespace separated list of DXF entity names or the
    special name ``*``. Where ``*`` means all DXF entities, exclude some entity
    types by appending their names with a preceding ``!`` (e.g. all entities
    except LINE = ``* !LINE``). All DXF names have to be uppercase.

    Attribute Query
    ---------------

    The attribute query is used to select DXF entities by its DXF attributes.
    The attribute query is an addition to the entity query and matches only if
    the entity already match the entity query.
    The attribute query is a boolean expression, supported operators are:

      - not: !term is true, if term is false
      - and: term & term is true, if both terms are true
      - or: term | term is true, if one term is true
      - and arbitrary nested round brackets

    Attribute selection is a term: "name comparator value", where name is a DXF
    entity attribute in lowercase, value is a integer, float or double quoted
    string, valid comparators are:

      - "==" equal "value"
      - "!=" not equal "value"
      - "<" lower than "value"
      - "<=" lower or equal than "value"
      - ">" greater than "value"
      - ">=" greater or equal than "value"
      - "?" match regular expression "value"
      - "!?" does not match regular expression "value"

    Query Result
    ------------

    The EntityQuery() class based on the abstract Sequence() class, contains all
    DXF entities of the source collection, which matches one name of the entity
    query AND the whole attribute query. If a DXF entity does not have or
    support a required attribute, the corresponding attribute search term is
    false.

    Examples:

        - 'LINE[text ? ".*"]' is always empty, because the LINE entity has no
          text attribute.
        - 'LINE CIRCLE[layer=="construction"]' => all LINE and CIRCLE entities
          on layer "construction"
        - '*[!(layer=="construction" & color<7)]' => all entities except those
          on layer == "construction" and color < 7

    layercolorlinetype
lineweightltscale	invisible
true_colortransparencyNc                    d| _         d| _        |  |g | _        y|dk(  rt        |      | _        yt	        |      }|D cg c]  } ||      s| c}| _        yc c}w )z
        Setup container with entities matching the initial query.

        Args:
            entities: sequence of wrapped DXF entities (at least GraphicEntity class)
            query: query string, see class documentation

         TN*)selected_dxf_attributeignore_caseentitieslistentity_matcher)r   r:   querymatchentitys        r   r   zEntityQuery.__init__s   s[     ,.#DMc\ NDM"5)E2:LeFmVLDMLs    AAc                ,    t        | j                        S )zReturns count of DXF entities.lenr:   r   s    r   __len__zEntityQuery.__len__   s    4==!!r   c                ,    t        | j                        S )z&Returns iterable of DXFEntity objects.)iterr:   rC   s    r   __iter__zEntityQuery.__iter__   s    DMM""r   c                z    t        |t              r| j                  |      S | j                  j	                  |      S )zReturns DXFEntity at index `item`, supports negative indices and
        slicing. Returns all entities which support a specific DXF attribute,
        if `item` is a DXF attribute name as string.
        )
isinstancer&   &_get_entities_with_supported_attributer:   r   )r   items     r   r   zEntityQuery.__getitem__   s4    
 dC >>tDD}}((..r   c                ^    t        |t              st        d      | j                  ||       y)zOSet the DXF attribute `key` for all supported DXF entities to `value`.
        +key has to be a string (DXF attribute name)N)rI   r&   	TypeError_set_dxf_attribute_for_all)r   keyr!   s      r   r    zEntityQuery.__setitem__   s*     #s#IJJ''U3r   c                \    t        |t              st        d      | j                  |       y)z@Discard the DXF attribute `key` from all supported DXF entities.rM   N)rI   r&   rN   _discard_dxf_attribute_for_all)r   rP   s     r   r$   zEntityQuery.__delitem__   s&    #s#IJJ++C0r   c                h    | j                   D cg c]  }|j                  s| c}| _         | S c c}w )zRemove destroyed entities.)r:   is_alive)r   es     r   purgezEntityQuery.purge   s)    $(MM@qQZZ@ As   //c                ^    | j                  fd| j                  D              }|_        |S )Nc              3  Z   K   | ]"  }|j                   j                        s| $ y wr   )dxfis_supported).0rU   	attributes     r   	<genexpr>zEEntityQuery._get_entities_with_supported_attribute.<locals>.<genexpr>   s'      
(:(:9(EA
s    ++)	__class__r:   r8   )r   r\   r=   s    ` r   rJ   z2EntityQuery._get_entities_with_supported_attribute   s3      
}}
 
 (1$r   c                ~    | j                   D ]  }	 |j                  j                  ||       ! y # t        $ r Y .w xY wr   )r:   rY   setAttributeError)r   rP   r!   rU   s       r   rO   z&EntityQuery._set_dxf_attribute_for_all   s@     	A		#u%	 " s   0	<<c                \    | j                   D ]  }|j                  j                  |        y r   )r:   rY   discard)r   rP   rU   s      r   rR   z*EntityQuery._discard_dxf_attribute_for_all   s%     	AEEMM#	r   c                p    | j                   st        d      | j                  |t        j                        S )zEqual selector (self == other).
        Returns all entities where the selected DXF attribute is equal to
        `other`.
        no DXF attribute selected)r8   rN   _select_by_operatoroperatoreqr   others     r   __eq__zEntityQuery.__eq__   s0    
 **788''x{{;;r   c                p    | j                   st        d      | j                  |t        j                        S )zNot equal selector (self != other). Returns all entities where the
        selected DXF attribute is not equal to `other`.
        re   )r8   rN   rf   rg   neri   s     r   __ne__zEntityQuery.__ne__   s0     **788''x{{;;r   c                t    | j                   st        d      | j                  |t        j                  d      S )zLess than selector (self < other). Returns all entities where the
        selected DXF attribute is less than `other`.

        Raises:
             TypeError: for vector based attributes like `center` or `insert`
        re   Fvectors)r8   rN   rf   rg   ltri   s     r   __lt__zEntityQuery.__lt__   5     **788''x{{E'JJr   c                t    | j                   st        d      | j                  |t        j                  d      S )zGreater than selector (self > other). Returns all entities where the
        selected DXF attribute is greater than `other`.

        Raises:
             TypeError: for vector based attributes like `center` or `insert`
        re   Frp   )r8   rN   rf   rg   gtri   s     r   __gt__zEntityQuery.__gt__   rt   r   c                t    | j                   st        d      | j                  |t        j                  d      S )zLess equal selector (self <= other). Returns all entities where the
        selected DXF attribute is less or equal `other`.

        Raises:
             TypeError: for vector based attributes like `center` or `insert`
        re   Frp   )r8   rN   rf   rg   leri   s     r   __le__zEntityQuery.__le__   rt   r   c                t    | j                   st        d      | j                  |t        j                  d      S )zGreater equal selector (self >= other). Returns all entities where
        the selected DXF attribute is greater or equal `other`.

        Raises:
             TypeError: for vector based attributes like `center` or `insert`
        re   Frp   )r8   rN   rf   rg   geri   s     r   __ge__zEntityQuery.__ge__   rt   r   c                P    t        |t              r| j                  |      S t        S )z"Union operator, see :meth:`union`.)rI   r,   unionNotImplementedri   s     r   __or__zEntityQuery.__or__  s!    e[)::e$$r   c                P    t        |t              r| j                  |      S t        S )z0Intersection operator, see :meth:`intersection`.)rI   r,   intersectionr   ri   s     r   __and__zEntityQuery.__and__  s#    e[)$$U++r   c                P    t        |t              r| j                  |      S t        S )z,Difference operator, see :meth:`difference`.)rI   r,   
differencer   ri   s     r   __sub__zEntityQuery.__sub__  s!    e[)??5))r   c                P    t        |t              r| j                  |      S t        S )z@Symmetric difference operator, see :meth:`symmetric_difference`.)rI   r,   symmetric_differencer   ri   s     r   __xor__zEntityQuery.__xor__  s#    e[),,U33r   c                >   | j                   }| j                  r t        |t              r|j	                         }| j                         }||_         |j                  }|r| j                  D ]  }	 |j                  j                  |      }|s:t        |t        t        f      r$t        dt        |j                         d|       | j                  r t        |t              r|j	                         } |||      s|j                  |        |S # t        $ r Y w xY w)Nzunsupported operation 'z' for DXF attribute )r8   r9   rI   r&   lowerr^   r:   rY   get_defaultra   r   r   rN   r'   append)	r   r!   oprq   r\   r=   r:   r?   entity_values	            r   rf   zEntityQuery._select_by_operator  s	   //	
5# 6KKME '0$>>-- ,#)::#9#9)#DL :lT4L#I#1#bkk2B1C D%%.K1  ##
<(E#/#5#5#7LlE*OOF+,  & s   .D	DDc                2      fd} j                  ||      S )zReturns all entities where the selected DXF attribute matches the
        regular expression `pattern`.

        Raises:
             TypeError: for non-string based attributes

        c                z    t        | t              r|j                  |       d uS t        dj                         )Nz2cannot apply regular expression to DXF attribute: )rI   r&   r>   rN   r8   )r!   regexr   s     r   r>   z EntityQuery.match.<locals>.match;  sB    %%{{5)55D../1 r   )_regex_match)r   patternr>   s   `  r   r>   zEntityQuery.match2  s    	   %00r   c                    | j                   }d| _         |rt        j                  nd}|j                  d      s|dz  }| j	                  t        j
                  ||      |      }|| _         |S )NFr   $flags)r9   re
IGNORECASEendswithrf   compile)r   r   funcr9   re_flagsresults         r   r   zEntityQuery._regex_matchE  sj    && $/2==Q $sNG))JJwh/
 'r   c                L    t        | j                        r| j                  d   S y)zFirst entity or ``None``.r   NrA   rC   s    r   firstzEntityQuery.firstS  s"     t}}==##r   c                L    t        | j                        r| j                  d   S y)zLast entity or ``None``.NrA   rC   s    r   lastzEntityQuery.last[  s"     t}}==$$r   c                f    | j                  | j                  ||            j                  | _        | S )zeExtent the :class:`EntityQuery` container by entities matching an
        additional query.

        )r   r^   r:   )r   r:   r=   s      r   extendzEntityQuery.extendc  s*     

4>>(E#BCLLr   c                z    | j                  | j                  | j                  |            j                  | _        | S )ziRemove all entities from :class:`EntityQuery` container matching this
        additional query.

        )r   r^   r:   r   r=   s     r   removezEntityQuery.removeo  s5    
 NN4==%0

( 	 r   c                :    | j                  | j                  |      S )zReturns a new :class:`EntityQuery` container with all entities
        matching this additional query.

        Raises:
            pyparsing.ParseException: query string parsing error

        )r^   r:   r   s     r   r=   zEntityQuery.queryy  s     ~~dmmU33r   c                0    t        | j                  ||      S )a  Returns a dict of entity lists, where entities are grouped by a DXF
        attribute or a key function.

        Args:
            dxfattrib: grouping DXF attribute as string like ``'layer'``
            key: key function, which accepts a DXFEntity as argument, returns
                grouping key of this entity or ``None`` for ignore this object.
                Reason for ignoring: a queried DXF attribute is not supported by
                this entity

        )r   r:   )r   	dxfattribrP   s      r   r   zEntityQuery.groupby  s      t}}i55r   c                L    | j                  t        || j                              S )a  Returns a new :class:`EntityQuery` with all entities from this
        container for which the callable `func` returns ``True``.

        Build your own operator to filter by attributes which are not DXF
        attributes or to build complex queries::

            result = msp.query().filter(
                lambda e: hasattr(e, "rgb") and e.rbg == (0, 0, 0)
            )
        )r^   filterr:   )r   r   s     r   r   zEntityQuery.filter  s     ~~fT4==9::r   c                v    | j                  t        | j                        t        |j                        z        S )zReturns a new :class:`EntityQuery` with entities from `self` and
        `other`. All entities are unique - no duplicates.
        r^   r`   r:   ri   s     r   r   zEntityQuery.union  *     ~~c$--03u~~3FFGGr   c                v    | j                  t        | j                        t        |j                        z        S )z_Returns a new :class:`EntityQuery` with entities common to `self`
        and `other`.
        r   ri   s     r   r   zEntityQuery.intersection  r   r   c                v    | j                  t        | j                        t        |j                        z
        S )zjReturns a new :class:`EntityQuery` with all entities from `self` that
        are not in `other`.
        r   ri   s     r   r   zEntityQuery.difference  r   r   c                v    | j                  t        | j                        t        |j                        z        S )zkReturns a new :class:`EntityQuery` with entities in either `self` or
        `other` but not both.
        r   ri   s     r   r   z EntityQuery.symmetric_difference  r   r   Nr7   )r:   Optional[Iterable[DXFEntity]]r=   r&   )returnint)r   Iterator[DXFEntity])r   r,   )r\   r&   r   r,   )T)r   r&   r   r,   r7   )r:   Iterable[DXFEntity]r=   r&   r   r,   )r=   r&   r   r,   )r6   N)r   r&   rP   z)Optional[Callable[[DXFEntity], Hashable]]r   zdict[Hashable, list[DXFEntity]])r   Callable[[DXFEntity], bool]r   r,   )rj   r,   r   r,   )0r'   r(   r)   __doc__r   r-   r.   r/   r0   r1   r2   r3   r4   r   rD   rG   r   r    r$   rV   rJ   rO   rR   rk   rn   rs   rw   rz   r}   r   r   r   r   rf   r>   r   propertyr   r   r   r   r=   r   r   r   r   r   r   r*   r   r   r,   r,   &   s   AF !)E )E#J/H%l3J"9-G$[1I%l3J'7L LOM5MEHM2"#/41
	<<	K	K	K	K21&     
%
 
 
	
4 9=66 76 
)	6$;HHHHr   r,   c                    t        j                  | d      }t        |j                        t	        |j
                  |j                        dfd}|S )NT)parseAllc                (     |       xr  |       S r   r*   )r?   attrib_matcherentity_matcher_s    r   matcherzentity_matcher.<locals>.matcher  s    v&A>&+AAr   r?   r   r   bool)r   parseStringbuild_entity_name_matcherr,   build_entity_attributes_matcherAttribQueryAttribQueryOptions)r=   
query_argsr   r   r   s      @@r   r<   r<     sM    "..utDJ/
0F0FGO4
 = =NB Nr   c                J    dfd}t        dj                  |             |S )Nc                0     | j                               S r   )dxftype)rU   _matchs    r   r>   z(build_entity_name_matcher.<locals>.match  s    aiik""r    )r=   )rU   r   r   r   )name_matcherjoin)namesr>   r   s     @r   r   r     s!    # 0FLr   c            	          e Zd Zej                  ej
                  ej                  ej                  ej                  ej                  d d dZ
 ee
j                               ZddZddZy)	Relationc                (    |j                  |       d uS r   r>   rU   r   s     r   <lambda>zRelation.<lambda>  s    ekk!nD8 r   c                (    |j                  |       d u S r   r   r   s     r   r   zRelation.<lambda>  s    u{{1~5 r   )z==z!=<z<=>z>=?z!?c                
   |\  }}}|| _         t        j                  |   | _        |rt        nd | _        |rt        j                  nd}d|v r t        j                  |dz   |      | _	        y | j                  |      | _	        y )Nc                    | S r   r*   xs    r   r   z#Relation.__init__.<locals>.<lambda>  s    1 r   r   r   r   r   )

dxf_attribr   CMP_OPERATORScompareto_lowerconvert_caser   r   r   r!   )r   relationr9   r   r   r!   r   s          r   r   zRelation.__init__  ss    "b%--b1(3H$/2==Q"98DJ **51DJr   c                    	 | j                  |j                  j                  | j                              }| j	                  || j
                        S # t        $ r Y yt        $ r Y yw xY w)NF)r   rY   r   r   r   r!   ra   
ValueError)r   r?   r!   s      r   evaluatezRelation.evaluate  s\    	%%fjj&<&<T__&MNE<<tzz22 	 		s   AA 	A(A('A(N)r   r   r9   r   r   )r'   r(   r)   rg   rh   rm   rr   ry   rv   r|   r   	frozensetkeysVALID_CMP_OPERATORSr   r   r*   r   r   r   r     sY    kkkk[[kk[[kk85	M $M$6$6$892r   r   c                >    t        | d      r| j                         S | S )Nr   )hasattrr   )r!   s    r   r   r     s    #E735;;=>>r   c                  T    e Zd Zej                  ej
                  dZddZd ZddZ	y)BoolExpression)&|c                    || _         y r   tokens)r   r   s     r   r   zBoolExpression.__init__  s	    r   c                ,    t        | j                        S r   )rF   r   rC   s    r   rG   zBoolExpression.__iter__  s    DKK  r   c                   t        | j                  t              r| j                  j                  |      S g }g }| j                  D ]@  }t	        |d      r!|j                  |j                  |             0|j                  |       B |j                          |D ]_  }|dk(  r|j                          }n5t        j                  |   |j                         |j                               }|j                  |       a |j                         S )Nr   !)
rI   r   r   r   r   r   reversepopr   	OPERATORS)r   r?   values	operatorstokenr   r!   s          r   r   zBoolExpression.evaluate  s    KK
 ;;''//	[[ 	(Euj)ennV45  '		(
 	 	!BSy"JJL(&004VZZ\6::<PMM% 	! zz|r   N)r   r   r   )
r'   r(   r)   rg   and_or_r   r   rG   r   r*   r   r   r   r     s&    ]]\\I
!r   r   c           	         dd}t        | t              r| S t        |       }  ||       rt        | |      S t	        | D cg c]  }t        ||       c}      S c c}w )Nc                L    t        |       dk(  xr | d   t        j                  v S )N      )rB   r   r   r   s    r   is_relationz$_compile_tokens.<locals>.is_relation!  s%    6{aMF1I1M1M$MMr   )r   r   r   r   )rI   r&   tupler   r   _compile_tokens)r   r9   r  r  s       r   r
  r
    s\    N &#6]F6,,>DEU_UK0E
 	
Es   Ac                d    t        |       sd S d|k(  }t        t        | |            dfd}|S )Nc                     y)NTr*   r   s    r   r   z1build_entity_attributes_matcher.<locals>.<lambda>4  s    r   ic                &    j                  |       S r   )r   )r?   exprs    r   match_bool_exprz8build_entity_attributes_matcher.<locals>.match_bool_expr8  s    }}V$$r   r   )rB   r   r
  )r   optionsr9   r  r  s       @r   r   r   0  s9     v;.K/&+>?D% r   c              #  b   K   t               }| D ]  }||vs|j                  |       |  yw)zCYield all unique entities, order of all entities will be preserved.N)r`   add)r:   doner?   s      r   unique_entitiesr  >  s5     5D HHVLs   //c                0    t        |      fd| D        S )a  Filters `names` by `query` string. The `query` string of entity names
    divided by spaces. The special name "*" matches any given name, a
    preceding "!" means exclude this name. Excluding names is only useful if
    the match any name is also given (e.g. "LINE !CIRCLE" is equal to just
    "LINE", where "* !CIRCLE" matches everything except CIRCLE").

    Args:
        names: iterable of names to test
        query: query string of entity names separated by spaces

    Returns: yield matching names

    c              3  4   K   | ]  } |      s|  y wr   r*   )r[   r   r>   s     r   r]   zname_query.<locals>.<genexpr>V  s     2TeDkD2s   )r   )r   r=   r>   s     @r   
name_queryr  G  s     E2U22r   c                "   dfd}t        | j                         j                               }dt               t               |D ]A  }|dk(  rd|j                  d      rj	                  |dd         1j	                  |       C |S )Nc                    r| vS | v S r   r*   )rU   excludeincludetake_alls    r   r>   zname_matcher.<locals>.matchZ  s    G##<r   Fr7   Tr   r  )rU   r&   r   r   )r`   uppersplit
startswithr  )r=   r>   match_stringsr   r  r  r  s       @@@r   r   r   Y  s      ++-.MHeGeG 3;H__S!KKQR!KK Lr   c                    t        | |      S )zStart a new query based on sequence `entities`. The `entities` argument
    has to be an iterable of :class:`~ezdxf.entities.DXFEntity` or inherited
    objects and returns an :class:`EntityQuery` object.

    )r,   )r:   r=   s     r   newr#  o  s     x''r   )r=   r&   r   r   )r   zSequence[str]r   r   )r   zUnion[str, Sequence]r9   r   r   z$Union[str, Relation, BoolExpression])r   r   r  r&   r   r   )r:   r   r   r   r   )r   zIterable[str]r=   r&   r   zIterator[str])r=   r&   r   zCallable[[str], bool]r   )r:   r   r=   r&   r   r,   )$
__future__r   typingr   r   r   r   r   r	   r
   r   rg   collectionsr   ezdxf.entities.dxfentityr   ezdxf.groupbyr   
ezdxf.mathr   r   ezdxf.queryparserr   r   r,   r<   r   r   r   r   r
  r   r  r  r   r#  r*   r   r   <module>r+     s    #   
   . ! ! /# #RH#,, RHj
 " "J?   F
 
/3
)
$" 3$. BE(+(;>((r   