
    Og@                    T   d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
Z
d dl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	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m Z  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) erd dlm*Z* ejV                  Z+dgZ,h dZ-h dZ. G d de"      Z/y)    )annotations)SequenceOptionalUnionTYPE_CHECKINGIterator)SelfN)Auditor
AuditError)const)Tags)colors)pattern)Vec3Matrix44)OCSTransform   )BoundaryPaths)SubclassProcessorDXFNamespace)
DXFGraphic)Gradient)PatternPatternLine)	DXFEntity)default_copy)xref
DXFPolygon>   J  
            (   *   2   3   H   I   J   \   ]   ^   _   `   a   >   +   ,   -   .   1   5   O   c                      e Zd ZU dZi Zded<   d  fdZefd!dZ	 d"	 	 	 d# fdZ	d$dZ
d$dZd$d	Zd% fd
Zd$dZed&d       Zed&d       Zed&d       Zed'd       Zej(                  d(d       Zej*                  d d       Z eddd       eddd      dddddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)dZ	 	 	 	 	 	 	 d*	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+dZ	 d,	 	 	 	 	 	 	 d-dZd.dZd/dZd0dZd1d2dZd3dZej@                  d4d5d       Z!d6 fdZ" xZ#S )7r   z1Base class for the HATCH and the MPOLYGON entity.z dict[int, Union[str, list[str]]]LOAD_GROUP_CODESc                j    t         |           t               | _        d | _        d | _        g | _        y N)super__init__r   pathsr   gradientseeds)self	__class__s    [/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/entities/polygon.pyr=   zDXFPolygon.__init__<   s,    "_
*.,002
    c                H   t        |t              sJ t        j                  | j                        |_        t        j                  | j
                        |_        t        j                  | j                        |_        t        j                  | j                        |_        y)z%Copy paths, pattern, gradient, seeds.N)
isinstancer   copydeepcopyr>   r   r?   r@   )rA   entitycopy_strategys      rC   	copy_datazDXFPolygon.copy_dataC   sb    &*---}}TZZ0t||4--6}}TZZ0rD   c                &   t         |   |      }|r~t        |j                  d   dd        }| j	                  |      }| j                  |      }| j                  |      }| j                  |      }|j                  || j                  |d       |S )N   r   T)subclassrecover)
r<   load_dxf_attribsr   
subclasses
load_pathsload_gradientload_pattern
load_seedsfast_load_dxfattribsr9   )rA   	processordxftagsrB   s       rC   rP   zDXFPolygon.load_dxf_attribsK   s     g&y1	,,Q/34D??4(D%%d+D$$T*D??4(D **T**T4 +  
rD   c                N   	 |j                  d      }|j                  t        |dz         }t        |      rt        j                  |      | _
        |t        |      z   dz   }|||= |S # t        j                  $ r' t        j                  | j	                          d      w xY w)N[   zH: Missing required DXF tag 'Number of boundary paths (loops)' (code=91).r   start)	tag_indexr   DXFValueErrorDXFStructureErrordxftypecollect_consecutive_tags
PATH_CODESlenr   	load_tagsr>   )rA   rY   start_index	path_tags	end_indexs        rC   rR   zDXFPolygon.load_pathsa   s    	..,K 11*KRSO1T	y>&00;DJ#i.014	Y&' "" 	))<<>" #5 6 	s   A* *:B$c                    	 |j                  d      }|j                  t        |dz         }t        j                  |      | _        |||t        |      z   dz   = |S # t        j                  $ r |cY S w xY w)NN   r   r\   )	r^   r   r_   rb   PATTERN_DEFINITION_LINE_CODESr   re   r   rd   )rA   rY   indexpattern_tagss       rC   rT   zDXFPolygon.load_patterns   s    	NN2&E
 44) 5 
 ((6 \!22Q667 "" 	K	s   A A54A5c                    	 |j                  d      }t        j                  ||d        | _        ||d = |S # t        j                  $ r |cY S w xY w)Ni  )r^   r   r_   r   re   r?   )rA   rY   rl   s      rC   rS   zDXFPolygon.load_gradient   sY    	NN3'E !**4<8L "" 	K	s   7 AAc                L   t        |t              sJ |j                  J t        |   ||       |j                  j
                  }|j                  D ]D  }|j                  D cg c]  }|j                  |       }}|D cg c]	  }||v s| c}|_        F yc c}w c c}w )z3Translate resources from self to the copied entity.N)	rF   r   docr<   map_resourcesentitydbr>   source_boundary_objects
get_handle)rA   clonemappingdbpathhhandlesrB   s          rC   rq   zDXFPolygon.map_resources   s    %,,,yy$$$eW-YYKK 	KD6:6R6RSw))!,SGS7>+J!!r'A+JD(	KS+Js   %B	B!B!c                    |S r;    )rA   rY   s     rC   rU   zDXFPolygon.load_seeds   s    rD   c                @    t        | j                  j                        S )z0``True`` if entity has a solid fill. (read only)boolrX   
solid_fillrA   s    rC   has_solid_fillzDXFPolygon.has_solid_fill   s     DHH''((rD   c                B    t        | j                  j                         S )z2``True`` if entity has a pattern fill. (read only)r~   r   s    rC   has_pattern_fillzDXFPolygon.has_pattern_fill   s     ++,,,rD   c                ,    t        | j                        S )zv``True`` if entity has a gradient fill. A hatch with gradient fill
        has also a solid fill. (read only)
        )r   r?   r   s    rC   has_gradient_datazDXFPolygon.has_gradient_data   s    
 DMM""rD   c                    	 | j                  d      }|j                  dd      }	 t	        j
                  t        |            S # t        j                  $ r Y yw xY w# t        $ r t        ddd      cY S w xY w)at  
        Set pattern fill background color as (r, g, b)-tuple, rgb values
        in the range [0, 255] (read/write/del)

        usage::

            r, g, b = entity.bgcolor  # get pattern fill background color
            entity.bgcolor = (10, 20, 30)  # set pattern fill background color
            del entity.bgcolor  # delete pattern fill background color

        HATCHBACKGROUNDCOLORN/  r   )		get_xdatar   r_   get_first_valuer   int2rgbint
ValueErrorRGB)rA   xdata_bgcolorcolors      rC   bgcolorzDXFPolygon.bgcolor   sz    	 NN+ABM --dA6	 >>#e*--	 "" 		
  	 q!Q<	 s"   A A AAA65A6c                    t        j                  |      dz  }| j                  d       | j                  dd|fg       y )Ni   r   r   )r   rgb2intdiscard_xdata	set_xdata)rA   rgbcolor_values      rC   r   zDXFPolygon.bgcolor   sC     NN3"CC 	 	12-{0C/DErD   c                &    | j                  d       y )Nr   )r   r   s    rC   r   zDXFPolygon.bgcolor   s    12rD   r              LINEARc                    | j                   <| j                   j                  t        j                  k  rt        j                  d      |r*|t        j
                  vrt        j                  d|       d| _        d| j                  _	        d| j                  _
        t        j                  | j                  _        t               }||_        ||_        ||_        ||_        ||_        ||_        ||_        || _        y)a'  Sets the gradient fill mode and removes all pattern fill related data, requires
        DXF R2004 or newer.  A gradient filled hatch is also a solid filled hatch.

        Valid gradient type names are:

            - "LINEAR"
            - "CYLINDER"
            - "INVCYLINDER"
            - "SPHERICAL"
            - "INVSPHERICAL"
            - "HEMISPHERICAL"
            - "INVHEMISPHERICAL"
            - "CURVED"
            - "INVCURVED"

        Args:
            color1: (r, g, b)-tuple for first color, rgb values as int in
                the range [0, 255]
            color2: (r, g, b)-tuple for second color, rgb values as int in
                the range [0, 255]
            rotation: rotation angle in degrees
            centered: determines whether the gradient is centered or not
            one_color: 1 for gradient from `color1` to tinted `color1`
            tint: determines the tinted target `color1` for a one color
                gradient. (valid range 0.0 to 1.0)
            name: name of gradient type, default "LINEAR"

        Nz#Gradient support requires DXF R2004zInvalid gradient type name: r   SOLID)rp   
dxfversionr   DXF2004DXFVersionErrorGRADIENT_TYPESr_   r   rX   r   pattern_nameHATCH_TYPE_PREDEFINEDpattern_typer   color1color2	one_colorrotationcenteredtintnamer?   )	rA   r   r   r   r   r   r   r   r?   s	            rC   set_gradientzDXFPolygon.set_gradient   s    L 88DHH$7$7%--$G''(MNND 4 44%%(DTF&KLL ' % ; ;:  &$$ rD   c	                   d| _         d| j                  _        || j                  _        || j                  _        t        |      | j                  _        t        |      | j                  _        t        |      | j                  _	        || j                  _
        || j                  _        |kd}	| j                  r&| j                  j                  j                  d|	      }	|	rt        j                   nt        j"                  }
|
j                  ||
d         }| j%                  || j                  j                  | j                  j                         y)a  Sets the pattern fill mode and removes all gradient related data.

        The pattern definition should be designed for a scale factor 1 and a rotation
        angle of 0 degrees.  The predefined hatch pattern like "ANSI33" are scaled
        according to the HEADER variable $MEASUREMENT for ISO measurement (m, cm, ... ),
        or imperial units (in, ft, ...), this replicates the behavior of BricsCAD.

        Args:
            name: pattern name as string
            color: pattern color as :ref:`ACI`
            angle: pattern rotation angle in degrees
            scale: pattern scale factor
            double: double size flag
            style: hatch style (0 = normal; 1 = outer; 2 = ignore)
            pattern_type: pattern type (0 = user-defined;
                1 = predefined; 2 = custom)
            definition: list of definition lines and a definition line is a
                4-tuple [angle, base_point, offset, dash_length_items],
                see :meth:`set_pattern_definition`

        Nr   r   z$MEASUREMENTANSI31factorangle)r?   rX   r   r   r   floatpattern_scalepattern_angler   pattern_doublehatch_styler   rp   headergetr   ISO_PATTERNIMPERIAL_PATTERNset_pattern_definition)rA   r   r   r   scaledoublestyler   
definitionmeasurementpredefined_patterns              rC   set_pattern_fillzDXFPolygon.set_pattern_fill  s   @  $!&u!&u"%f+$ ,Kxx"hhoo11.+N'2##8P8P  ,//6H6RSJ##88))(((( 	$ 	
rD   c                    |dk7  s|rt        j                  |||      }t        |D cg c]  }t        |d   |d   |d   |d          c}      | _         yc c}w )a  Setup pattern definition by a list of definition lines and the
        definition line is a 4-tuple (angle, base_point, offset, dash_length_items).
        The pattern definition should be designed for a pattern scale factor of 1 and
        a pattern rotation angle of 0.

            - angle: line angle in degrees
            - base-point: (x, y) tuple
            - offset: (dx, dy) tuple
            - dash_length_items: list of dash items (item > 0 is a line,
              item < 0 is a gap and item == 0.0 is a point)

        Args:
            lines: list of definition lines
            factor: pattern scale factor
            angle: rotation angle in degrees

        r   r   r   rM      N)r   scale_patternr   r   )rA   linesr   r   lines        rC   r   z!DXFPolygon.set_pattern_definitionF  sY    ( Q;%))%eLEINO[a$q'47DG<O
Os   !Ac                    | j                   sy| j                  }| j                  j                  d|j                  z  |z         ||_        y)a	  Sets the pattern scale factor and scales the pattern definition.

        The method always starts from the original base scale, the
        :code:`set_pattern_scale(1)` call resets the pattern scale to the original
        appearance as defined by the pattern designer, but only if the pattern attribute
        :attr:`dxf.pattern_scale` represents the actual scale, it cannot
        restore the original pattern scale from the pattern definition itself.

        Args:
            scale: pattern scale factor

        N      ?)r   )r   rX   r   r   r   )rA   r   rX   s      rC   set_pattern_scalezDXFPolygon.set_pattern_scale`  sF     $$hh#(9(9"9E"AB!rD   c                    | j                   sy| j                  }| j                  j                  ||j                  z
         |dz  |_        y)a>  Sets the pattern rotation angle and rotates the pattern definition.

        The method always starts from the original base rotation of 0, the
        :code:`set_pattern_angle(0)` call resets the pattern rotation angle to the
        original appearance as defined by the pattern designer, but only if the
        pattern attribute :attr:`dxf.pattern_angle` represents the actual pattern
        rotation, it cannot restore the original rotation angle from the
        pattern definition itself.

        Args:
            angle: pattern rotation angle in degrees

        N)r   g     v@)r   rX   r   r   r   )rA   r   rX   s      rC   set_pattern_anglezDXFPolygon.set_pattern_angles  sE     $$hh):):!:;!EMrD   c                   | j                   }t        |j                  |      }t        |j                        j
                  }| j                  j                  ||       |j                  t        dd|            j                  dd      |_        |j                  |_        | j                  r|j                  | j                   j                  ddf      }|j                  | j                   j                        }| j                  j!                  ||       || j                   _        || j                   _        | j#                  |       | S )z6Transform entity by transformation matrix `m` inplace.)	elevationr   r   )xy)rX   r   	extrusionr   r   zr>   	transformtransform_vertexreplacenew_extrusionr   transform_lengthr   transform_deg_angler   r   post_transform)rA   mrX   ocsr   r   r   s          rC   r   zDXFPolygon.transform  s	   hh3==!,'))	

SI6,,T!Q	-BCKKS L 
 ))<< ))488+A+A1a*HIF++DHH,B,BCELLvu-%+DHH"%*DHH"ArD   c              #    K   ddl m} t        | j                  j                        | j                  j                  d      r"t        | j                  j                        z  | j                  D cg c]  }|j                  |       }}|j                  |||      D ]  }t        fd|D                yc c}w w)u  Triangulate the HATCH/MPOLYGON in OCS coordinates, Elevation and offset is
        applied to all vertices.

        Args:
            max_sagitta: maximum distance from the center of the curve to the
                center of the line segment between two approximation points to determine
                if a segment should be subdivided.
            min_segments: minimum segment count per Bézier curve

        .. versionadded:: 1.1

        r   )rx   offsetc              3  (   K   | ]	  }|z     y wr;   r|   ).0vr   s     rC   	<genexpr>z)DXFPolygon.triangulate.<locals>.<genexpr>  s     8!	A8s   N)ezdxfrx   r   rX   r   hasattrr   r>   from_hatch_boundary_pathtriangulatetuple)rA   max_sagittamin_segmentsrx   pboundary_pathsverticesr   s          @rC   r   zDXFPolygon.triangulate  s      	++,	88H%dhhoo..IDHJJOq$77:OO((lS 	9H8x888	9 Ps   A1C4B=6Cc              #     K   ddl m} | j                  r	 |j                  |       E d{    yy7 # |j                  $ r Y yw xY ww)zUYields the pattern lines in WCS coordinates.

        .. versionadded:: 1.1

        r   )hatchingN)ezdxf.renderr   r   hatch_entityHatchingError)rA   r   s     rC   render_pattern_lineszDXFPolygon.render_pattern_lines  sL      	*  #00666 !6)) s1   A3 13 A3 AAAAc                     y r;   r|   )rA   r   r   r   s       rC   set_solid_fillzDXFPolygon.set_solid_fill  s    rD   c                    t         |   |       | j                  sy | j                  j	                         s@|j                  t        j                  dt        |        d       |j                  |        y y )NzDeleted entity z# containing invalid boundary paths.)codemessage)
r<   auditis_aliver>   is_validfixed_errorr   INVALID_HATCH_BOUNDARY_PATHstrtrash)rA   auditorrB   s     rC   r   zDXFPolygon.audit  si    g}}zz""$;;)#d)4WX    MM$ %rD   )returnNone)rI   r	   r   r  r;   )rW   zOptional[SubclassProcessor]r   r   )rY   r   r   r   )ru   r	   rv   zxref.ResourceMapperr   r  )r   r   )r   Optional[RGB])r   r   r   r  )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  )   r   r   r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  )r   r   )r   r   r   r   r   r   r   r  )r   r   r   r  )r   r   r   r  )r   r   r   r   )   )r   zIterator[Sequence[Vec3]])r   zIterator[tuple[Vec3, Vec3]])r  r   N)r   r   r   r   r   r  )r   r
   r   r  )$__name__
__module____qualname____doc__r9   __annotations__r=   r   rK   rP   rR   rT   rS   rq   rU   propertyr   r   r   r   setterdeleterr   r   r   r   r   r   r   r   r   abcabstractmethodr   r   __classcell__)rB   s   @rC   r   r   7   s(   ;9;6;3 5A 1 8<4	,$"	K ) ) - - # #    , ^^F F __3 3
 !Ql#sC(8!8! 8! 	8!
 8! 8! 8! 8! 
8!z 6
6
 6
 	6

 6
 6
 6
 6
 
6
r BC

',
9>
	
4"&*(.9, 	 	  	 rD   )0
__future__r   typingr   r   r   r   r   typing_extensionsr	   r  rG   ezdxf.auditr
   r   ezdxf.lldxfr   ezdxf.lldxf.tagsr   r   r   ezdxf.toolsr   
ezdxf.mathr   r   ezdxf.math.transformtoolsr   r   r   dxfnsr   r   dxfgfxr   r?   r   r   r   	dxfentityr   r   r   r   __all__rc   rk   r   r|   rD   rC   <module>r     sv    # E E " 
  +  !   % 2 ) 2   )   jj.
* != X  X rD   