
    'j@                    R   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j+        Z+dgZ,h dZ-h dZ. G d de"          Z/dS )    )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<   dN fdZefdOd	Z	 dPdQ fdZ	dRdZ
dRdZdRdZdS fdZdRdZedTd            ZedTd            ZedTd            ZedUd            Zej        dVd!            Zej        dNd"            Z ed#d#d#           ed$d$d$          d%d%d#d%d&fdWd1Z	 	 	 	 	 	 	 dXdYd;Z	 dZd[d?Zd\d@Zd]dAZd^dDZd_d`dGZdadIZej         dbdcdJ            Z!dd fdMZ" xZ#S )er   z1Base class for the HATCH and the MPOLYGON entity.z dict[int, Union[str, list[str]]]LOAD_GROUP_CODESreturnNonec                    t                                                       t                      | _        d | _        d | _        g | _        d S N)super__init__r   pathsr   gradientseeds)self	__class__s    P/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/entities/polygon.pyr?   zDXFPolygon.__init__<   s=    "__
*.,002


    entityr	   c                $   t          |t                    sJ t          j        | j                  |_        t          j        | j                  |_        t          j        | j                  |_        t          j        | j                  |_        dS )z%Copy paths, pattern, gradient, seeds.N)
isinstancer   copydeepcopyr@   r   rA   rB   )rC   rG   copy_strategys      rE   	copy_datazDXFPolygon.copy_dataC   sh    &*-----}TZ00t|44-66}TZ00rF   N	processorOptional[SubclassProcessor]r   c                v   t                                          |          }|rt          |j        d         dd                    }|                     |          }|                     |          }|                     |          }|                     |          }|                    || j	        |d           |S )N   r   T)subclassrecover)
r>   load_dxf_attribsr   
subclasses
load_pathsload_gradientload_pattern
load_seedsfast_load_dxfattribsr9   )rC   rN   dxftagsrD   s       rE   rT   zDXFPolygon.load_dxf_attribsK   s     gg&&y11 		,Q/344D??4((D%%d++D$$T**D??4((D **T*T4 +    
rF   r\   r   c                n   	 |                     d          }n<# t          j        $ r* t          j        |                                  d          w xY w|                    t          |dz             }t          |          rt          j	        |          | _
        |t          |          z   dz   }|||= |S )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@   )rC   r\   start_index	path_tags	end_indexs        rE   rV   zDXFPolygon.load_pathsa   s    	..,,KK" 	 	 	)<<>> 6 6 6  	 11*KRSO1TT	y>> 	<&0;;DJ#i..014	Y&'s	    9Ac                   	 |                     d          }n# t          j        $ r |cY S w xY w|                    t          |dz             }t          j        |          | _        |||t          |          z   dz   = |S )NN   r   r_   )	ra   r   rb   re   PATTERN_DEFINITION_LINE_CODESr   rh   r   rg   )rC   r\   indexpattern_tagss       rE   rX   zDXFPolygon.load_patterns   s    	NN2&&EE" 	 	 	KKK	 44) 5 
 
 (66 \!2!22Q667    ,,c                    	 |                     d          }n# t          j        $ r |cY S w xY wt          j        ||d                    | _        ||d = |S )Ni  )ra   r   rb   r   rh   rA   )rC   r\   ro   s      rE   rW   zDXFPolygon.load_gradient   sm    	NN3''EE" 	 	 	KKK	
 !*4<88Lrq   clonemappingxref.ResourceMapperc                   t          |t                    sJ |j        J t                                          |           |j        j        |j        D ](}fd|j        D             }fd|D             |_        )dS )z3Translate resources from self to the copied entity.Nc                :    g | ]}                     |          S  )
get_handle).0hrt   s     rE   
<listcomp>z,DXFPolygon.map_resources.<locals>.<listcomp>   s'    SSSw))!,,SSSrF   c                    g | ]}|v |	S rx   rx   )rz   r{   dbs     rE   r|   z,DXFPolygon.map_resources.<locals>.<listcomp>   s    +J+J+J!!r''A'''rF   )rI   r   docr>   map_resourcesentitydbr@   source_boundary_objects)rC   rs   rt   pathhandlesr~   rD   s     `  @rE   r   zDXFPolygon.map_resources   s    %,,,,,y$$$eW---YK 	K 	KDSSSSd6RSSSG+J+J+J+Jw+J+J+JD((	K 	KrF   c                    |S r=   rx   )rC   r\   s     rE   rY   zDXFPolygon.load_seeds   s    rF   boolc                4    t          | j        j                  S )z0``True`` if entity has a solid fill. (read only)r   r[   
solid_fillrC   s    rE   has_solid_fillzDXFPolygon.has_solid_fill   s     DH'(((rF   c                6    t          | j        j                   S )z2``True`` if entity has a pattern fill. (read only)r   r   s    rE   has_pattern_fillzDXFPolygon.has_pattern_fill   s     +,,,,rF   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   rA   r   s    rE   has_gradient_datazDXFPolygon.has_gradient_data   s    
 DM"""rF   Optional[RGB]c                   	 |                      d          }n# t          j        $ r Y dS w xY w|                    dd          }	 t	          j        t          |                    S # 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   rb   get_first_valuer   int2rgbint
ValueErrorRGB)rC   xdata_bgcolorcolors      rE   bgcolorzDXFPolygon.bgcolor   s    	 NN+ABBMM" 	 	 	44	--dA66	 >#e**--- 	  	  	 q!Q<<	 s    ++ A& &BBrgbr   c                    t          j        |          dz  }|                     d           |                     dd|fg           d S )Ni   r   r   )r   rgb2intdiscard_xdata	set_xdata)rC   r   color_values      rE   r   zDXFPolygon.bgcolor   sV     N3"CC 	 	1222-{0C/DEEEEErF   c                0    |                      d           d S )Nr   )r   r   s    rE   r   zDXFPolygon.bgcolor   s    122222rF   r              LINEARcolor1color2rotationfloatcentered	one_colorr   tintnamestrc                   | 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                      }||_        ||_        ||_        ||_        ||_        ||_        ||_        || _        dS )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)r   
dxfversionr   DXF2004DXFVersionErrorGRADIENT_TYPESrb   r   r[   r   pattern_nameHATCH_TYPE_PREDEFINEDpattern_typer   r   r   r   r   r   r   r   rA   )	rC   r   r   r   r   r   r   r   rA   s	            rE   set_gradientzDXFPolygon.set_gradient   s    L 8DH$7%-$G$G'(MNNN 	MD 444%&KT&K&KLLL ' % ;::  &$$ rF            ?r   r   anglescaledoublestyler   c	                <   d| _         d| j        _        || j        _        || j        _        t          |          | j        _        t          |          | j        _        t          |          | j        _	        || j        _
        || j        _        |_d}	| j        r | j        j                            d|	          }	|	rt          j        nt          j        }
|
                    ||
d                   }|                     || j        j        | j        j                   dS )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factorr   )rA   r[   r   r   r   r   pattern_scalepattern_angler   pattern_doublehatch_styler   r   headergetr   ISO_PATTERNIMPERIAL_PATTERNset_pattern_definition)rC   r   r   r   r   r   r   r   
definitionmeasurementpredefined_patterns              rE   set_pattern_fillzDXFPolygon.set_pattern_fill  s   @  $!&u!&u"%f++$ ,Kx O"ho11.+NN'2P##8P  ,//6H6RSSJ##8)(( 	$ 	
 	
 	
 	
 	
rF   linesr   r   c                    |dk    s|rt          j        |||          }t          d |D                       | _         dS )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   c           	     b    g | ],}t          |d          |d         |d         |d                   -S )r   r   rQ      )r   )rz   lines     rE   r|   z5DXFPolygon.set_pattern_definition.<locals>.<listcomp>]  s6    OOO[a$q'47DG<<OOOrF   N)r   scale_patternr   )rC   r   r   r   s       rE   r   z!DXFPolygon.set_pattern_definitionF  sN    ( Q;;%;)%eLLLEOOOOO
 
rF   c                    | j         sdS | j        }| j                            d|j        z  |z             ||_        dS )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

        Nr   )r   )r   r[   r   r   r   )rC   r   r[   s      rE   set_pattern_scalezDXFPolygon.set_pattern_scale`  sO     $ 	Fh#(9"9E"ABBB!rF   c                    | j         sdS | j        }| j                            ||j        z
             |dz  |_        dS )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   r[   r   r   r   )rC   r   r[   s      rE   set_pattern_anglezDXFPolygon.set_pattern_angles  sN     $ 	Fh):!:;;;!EMrF   mr   c                \   | j         }t          |j        |          }t          |j                  j        }| j                            ||           |                    t          dd|                    	                    dd          |_        |j
        |_        | j        rt|                    | j         j        ddf          }|                    | j         j                  }| j                            ||           || j         _        || j         _        |                     |           | S )z6Transform entity by transformation matrix `m` inplace.)	elevationr   r   )xy)r[   r   	extrusionr   r   zr@   	transformtransform_vertexreplacenew_extrusionr   transform_lengthr   transform_deg_angler   r   post_transform)rC   r   r[   ocsr   r   r   s          rE   r   zDXFPolygon.transform  s   h3=!,,'')	
SI666,,T!Q	-B-BCCKKS L 
 
 )< 	+ ))48+A1a*HIIF++DH,BCCELvu---%+DH"%*DH"ArF      Iterator[Sequence[Vec3]]c              #  L  K   ddl m t          | j        j                  | j                            d          rt          | j        j                  z  fd| j        D             }                    |||          D ]}t          fd|D                       V   dS )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   )r   offsetc                :    g | ]}                     |          S rx   )from_hatch_boundary_path)rz   pr   s     rE   r|   z*DXFPolygon.triangulate.<locals>.<listcomp>  s'    OOOq$77::OOOrF   c              3  "   K   | ]	}|z   V  
d S r=   rx   )rz   vr   s     rE   	<genexpr>z)DXFPolygon.triangulate.<locals>.<genexpr>  s'      88!	A888888rF   N)
ezdxfr   r   r[   r   hasattrr   r@   triangulatetuple)rC   max_sagittamin_segmentsboundary_pathsverticesr   r   s        @@rE   r   zDXFPolygon.triangulate  s       	+,,	8H%% 	/dho...IOOOODJOOO((lSS 	9 	9H8888x888888888	9 	9rF   Iterator[tuple[Vec3, Vec3]]c              #     K   ddl m} | j        r/	 |                    |           E d{V  dS # |j        $ r Y dS w xY wdS )zUYields the pattern lines in WCS coordinates.

        .. versionadded:: 1.1

        r   )hatchingN)ezdxf.renderr   r   hatch_entityHatchingError)rC   r   s     rE   render_pattern_lineszDXFPolygon.render_pattern_lines  s       	*)))))  	#0066666666666)   	 	s   . 
<<c                    d S r=   rx   )rC   r   r   r   s       rE   set_solid_fillzDXFPolygon.set_solid_fill  s    rF   auditorr
   c                    t                                          |           | j        sd S | j                                        sI|                    t          j        dt          |            d           |	                    |            d S d S )NzDeleted entity z# containing invalid boundary paths.)codemessage)
r>   auditis_aliver@   is_validfixed_errorr   INVALID_HATCH_BOUNDARY_PATHr   trash)rC   r  rD   s     rE   r	  zDXFPolygon.audit  s    g} 	Fz""$$ 	 ;X#d))XXX      MM$	  	 rF   )r:   r;   )rG   r	   r:   r;   r=   )rN   rO   r:   r   )r\   r   r:   r   )rs   r	   rt   ru   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   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   )r:   r   )r:   r   )r   r   N)r   r   r   r   r   r   )r  r
   r:   r;   )$__name__
__module____qualname____doc__r9   __annotations__r?   r   rM   rT   rV   rX   rW   r   rY   propertyr   r   r   r   setterdeleterr   r   r   r   r   r   r   r   r  abcabstractmethodr  r	  __classcell__)rD   s   @rE   r   r   7   s&        ;;9;;;;;3 3 3 3 3 3 5A 1 1 1 1 1 8<      ,   $   "   	K 	K 	K 	K 	K 	K    ) ) ) X) - - - X- # # # X#       X , ^F F F ^F _3 3 3 _3
 c!Qllc#sC((8! 8! 8! 8! 8!z 6
 6
 6
 6
 6
r BC
 
 
 
 
4" " " "&* * * *(   .9 9 9 9 9,    	    	  	  	  	  	  	  	  	  	  	 rF   )0
__future__r   typingr   r   r   r   r   typing_extensionsr	   r  rJ   ezdxf.auditr
   r   ezdxf.lldxfr   ezdxf.lldxf.tagsr   r   r   ezdxf.toolsr   
ezdxf.mathr   r   ezdxf.math.transformtoolsr   r   r   dxfnsr   r   dxfgfxr   rA   r   r   r   	dxfentityr   r   r   r   __all__rf   rn   r   rx   rF   rE   <module>r'     s    # " " " " " E E E E E E E E E E E E E E " " " " " " 



  + + + + + + + +       ! ! ! ! ! !             % % % % % % % % 2 2 2 2 2 2 ) ) ) ) ) ) 2 2 2 2 2 2 2 2             ) ) ) ) ) ) ) )                   j.  
* != < < X  X  X  X  X  X  X  X  X  X rF   