
    OgT                       U 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
mZ d dlmZ d dlmZ d dlZd dlZd dlmZmZmZmZmZmZmZmZmZ erd dlmZ d d	lm Z m!Z!m"Z"m#Z#m$Z$m%Z% g d
Z& edd      Z' edd      Z(eeeeef   Z)de*d<   ee   Z+de*d<   ed   Z,de*d<    G d d      Z- G d de-      Z.	 	 	 	 	 	 	 	 	 	 ddZ/ ed      Z0 G d de-      Z1 G d de      Z2y)    )annotations)TYPE_CHECKINGIterableTupleUnioncastSequenceOptional)	TypeAlias)abstractmethod)
namedtupleN)	Vec2Vec3UVecBSplineConstructionRayOCSParallelRaysErrorbulge_to_arcConstructionArc)Drawing)
DXFGraphicSolidTraceFace3d
LWPolylinePolyline)TraceBuilderLinearTraceCurvedTraceLinearStation)vertexstart_width	end_widthCurveStation)vertex0vertex1r   FacePolygon)r   r   r   Quadrilateralc                  8    e Zd Zedd       ZddZ	 d	 	 	 ddZy)	AbstractTracec                     y N selfs    W/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/render/trace.pyfaceszAbstractTrace.faces1   s     	    c           	     
   dd}g }g }| j                         D ]-  \  }}}}|j                  ||f       |j                  ||f       / t         ||            }|j                  t        t         ||                         |S )Nc              3     K   t        |       sy t        |       }t        |      }| |D ]  }|j                  |      r| |} y wr.   )leniternextisclose)vertices	_verticesprevr"   s       r2   mergez$AbstractTrace.polygon.<locals>.merge:   sL     x=XI	?DJ# "||F+ L!D"s   =A	 	A	)r;   r)   returnzIterable[UVec])r3   extendlistreversed)	r1   r>   forward_contourbackward_contourup1down1down2up2contours	            r2   polygonzAbstractTrace.polygon9   s    
	" ')')&*jjl 	0"Cs""E5>2##S#J/	0 u_-.xU+;%< =>?r4   Nc              #  <  K   ddl m} |dvrt        d| d      t        |xs i       }| j	                         D ]]  }t        d      D ]  }||   |d| <    |dk7  r|d	   |d
<   |d   |d<    ||||      }|r|j                  j                  |       | _ yw)a  
        Yields faces as SOLID, TRACE or 3DFACE entities with DXF attributes
        given in `dxfattribs`.

        If a document is given, the doc attribute of the new entities will be
        set and the new entities will be automatically added to the entity
        database of that document.

        Args:
            dxftype: DXF type as string, "SOLID", "TRACE" or "3DFACE"
            dxfattribs: DXF attributes for SOLID, TRACE or 3DFACE entities
            doc: associated document

        r   )new>   SOLIDTRACE3DFACEzInvalid dxftype .   vtxrO      vtx2   vtx3N)ezdxf.entities.factoryrL   	TypeErrordictr3   rangeentitydbadd)r1   dxftype
dxfattribsdocrL   faceientitys           r2   virtual_entitieszAbstractTrace.virtual_entitiesP   s     " 	/66.wiq9::**+
JJL 	D1X 0(,Q
S9%0 ("%)!W
6"%)!W
6"*c2F  (L	s   BBr?   zIterable[Face])r?   r)   rN   NNr_   zOptional[Drawing]r?   zIterable[Quadrilateral])__name__
__module____qualname__r   r3   rJ   rc   r/   r4   r2   r,   r,   0   s5     0 JN!5F!	 !r4   r,   c                  X    e Zd ZdZd	dZd Zd Zed
d       Z	 d	 	 	 	 	 	 	 ddZ	ddZ
y)r   ztLinear 2D banded lines like polylines with start- and end width.

    Accepts 3D input, but z-axis is ignored.

    c                     g | _         d| _        y Ng-q=)	_stationsabs_tolr0   s    r2   __init__zLinearTrace.__init__{   s    .0r4   c                ,    t        | j                        S r.   r7   rm   r0   s    r2   __len__zLinearTrace.__len__       4>>""r4   c                     | j                   |   S r.   rm   r1   items     r2   __getitem__zLinearTrace.__getitem__       ~~d##r4   c                ,    t        | j                        S )z%`True` if at least one station exist.)boolrm   r0   s    r2   
is_startedzLinearTrace.is_started   s     DNN##r4   Nc           	         ||}t        |      }| j                  }t        |      r:|d   j                  j	                  || j
                        r|j                          |j                  t        |t        |      t        |                   y)aa  Add a trace station (like a vertex) at location `point`,
        `start_width` is the width of the next segment starting at this station,
        `end_width` is the end width of the next segment.

        Adding the last location again, replaces the actual last location e.g.
        adding lines (a, b), (b, c), creates only 3 stations (a, b, c), this is
        very important to connect to/from splines.

        Args:
            point: 2D location (vertex), z-axis of 3D vertices is ignored.
            start_width: start width of next segment
            end_width:  end width of next segment

        N)rn   )
r   rm   r{   r"   r:   rn   popappendr!   float)r1   pointr#   r$   stationss        r2   add_stationzLinearTrace.add_station   sp    " #IU>>>hrl1199%9VLLNeU;-?yAQRSr4   c           	   #     K   | j                    t               }|dk  ry	 	 	 	 d fd}	 	 	 	 	 	 	 	 d	d} d   j                  j                   d   j                        }g t	        |dz
        D ]:  } |   \  }}} |dz      j                  }	j                  t        ||	||             <  |d      \  }
}d}d}d   \  }}}}t	        t                    D ]  }|   \  }}}}|dk(  r;|r0 |t              dz
        \  }} |||
|      } ||||      }|}|}n|}|}|
}|}n |||
|      } ||||      }|t              dz
  k  r+ ||dz         \  }} ||||      } |||
|      }|
}|}|}
|}n|}|}||||f  yw)
a2  Yields all faces as 4-tuples of :class:`~ezdxf.math.Vec2` objects.

        First and last miter is 90 degrees if the path is not closed, otherwise
        the intersection of first and last segment is taken into account,
        a closed path has to have explicit the same last and first vertex.

        rU   Nc                J      fd}    \  }}}} |||       |||      fS )z0Create offset rays from segment offset vertices.c                    | j                  |      r8   j                  dz      j                  z
  j                  }t        | |      S t        | |      S )N   )r:   r"   angler   )v1v2r   segmentr   s      r2   rayz3LinearTrace.faces.<locals>.offset_rays.<locals>.ray   sX    ::b> !)008GaK3H3O3OOe  +2u55*2r22r4   r/   )r   r   left1left2right1right2segmentsr   s   `     r2   offset_raysz&LinearTrace.faces.<locals>.offset_rays   s6    
3 ,4G+<(E5&&ue$c&&&999r4   c                    t        | j                  j                  |j                              }|dk  st        t        j                  |z
        dk  r|S 	 | j                  |      S # t        $ r |cY S w xY w)z7Intersect two rays but take parallel rays into account.g*5{?)abs	directionangle_betweenmathpi	intersectr   )ray1ray2defaultr   s       r2   r   z$LinearTrace.faces.<locals>.intersect   sj    
 44T^^DEEx3tww#7(#B~~d++$ s   A' 'A54A5r   r~   r   )r   intr?   z'tuple[ConstructionRay, ConstructionRay])r   r   r   r   r   r   r?   r   )rm   r7   r"   r:   rZ   r   _normal_offset_points)!r1   countr   r   	is_closedstationstart_vertexr#   r$   
end_vertexoffset_ray1offset_ray2prev_offset_ray1prev_offset_ray2last_up1last_up2
last_down1
last_down2ra   rE   rH   rF   rG   last_offset_ray1last_offset_ray2vtx0vtx1next_offset_ray1next_offset_ray2rT   rV   r   r   s!                                  @@r2   r3   zLinearTrace.faces   s?     >>H19	:	:4	:$	!	)8	CG		 QK&&..x|/B/BC	 UQY' 	G3;G3D0L+y!'A+.55J OO%lJYW	 $/q> [ 6>b\2(J
s8}% &	)A%-a["CeUAv :ES]UVEV9W6$&6$%5{CHD$%5{EJD  $H!%J D D#. #.  !!1;D !1;F3x=1$$5@Q5G2 "2 !1;F !1;D#. #. .. "dD((M&	)s   FF
r?   None)r?   r{   r.   )r   r   r#   r   r$   zOptional[float]r?   r   rd   )rg   rh   ri   __doc__ro   rr   rx   propertyr|   r   r3   r/   r4   r2   r   r   t   sa    #$ $ $
 MQTT(-T:IT	T6o)r4   r   c                    || z
  j                         }|j                  d      }|j                  |dz        }|j                  |dz        }| |z   ||z   | |z
  ||z
  fS )NTrU   )	normalize
orthogonal)startendr#   r$   
dir_vectororthooffset_start
offset_ends           r2   r   r     st     +((*J!!$'E??;?3LQ/Jjj	 r4   )r   r   c                      e Zd ZdZd
dZd Zd Ze	 	 	 	 	 	 	 	 	 	 dd       Ze	 d	 	 	 	 	 	 	 	 	 dd       Z	ddZ
ddZy	)r    z2D banded curves like arcs or splines with start- and end width.

    Represents always only one curved entity and all miter of curve segments
    are perpendicular to curve tangents.

    Accepts 3D input, but z-axis is ignored.

    c                    g | _         y r.   ru   r0   s    r2   ro   zCurvedTrace.__init__3  s	    -/r4   c                ,    t        | j                        S r.   rq   r0   s    r2   rr   zCurvedTrace.__len__6  rs   r4   c                     | j                   |   S r.   ru   rv   s     r2   rx   zCurvedTrace.__getitem__9  ry   r4   c                F    |        }|dz   }t        j                  d|j                  |      }t        |j	                  |d      t        j                  |||            D ]>  \  \  }}	}
t        |	      j                  d      }|j                  t        |      ||
       @ |S )z
        Create curved trace from a B-spline.

        Args:
            spline: :class:`~ezdxf.math.BSpline` object
            start_width: start width
            end_width: end width
            segments: count of segments for approximation

        r   r   )nT)nplinspacemax_tzipderivativesr   r   _append)clsspliner#   r$   r   curve_tracer   tr   
derivativewidthnormals               r2   from_splinezCurvedTrace.from_spline<  s    $ e1KK6<</*-qA&KE(R+
 	<&UJ *%006FUVU;		<
 r4   c                   |j                   dk  rt        d|j                    d       |        }t        t        j                  |j
                  dz  |z        d      dz   }t        |j                        }t        |j                  |j                  |            t        j                  |||            D ]  \  }}	|j                  |||z
  |	        |S )a  
        Create curved trace from an arc.

        Args:
            arc: :class:`~ezdxf.math.ConstructionArc` object
            start_width: start width
            end_width: end width
            segments: count of segments for full circle (360 degree)
                approximation, partial arcs have proportional less segments,
                but at least 3

        Raises:
            ValueError: if arc.radius <= 0

        r   zInvalid radius: rP   g     v@rS   r   )radius
ValueErrormaxr   ceil
angle_spanr   centerr   r;   anglesr   r   r   )
r   arcr#   r$   r   r   r   r   r   r   s
             r2   from_arczCurvedTrace.from_arcX  s    . ::?/

|1=>>eDIIcnnu4x?@!DqHcjj!LLE*+KKY6
 	>LE5 uv~u=		>
 r4   c                    t         j                  |      rt         }n|j                  |dz        }| j                  j	                  t        ||z   ||z
               y)z
        Add a curve trace station (like a vertex) at location `point`.

        Args:
            point: 2D curve location (vertex), z-axis of 3D vertices is ignored.
            normal: curve normal
            width:  width of station

        rU   N)	_NULLVEC2r:   r   rm   r   r%   )r1   r   r   r   s       r2   r   zCurvedTrace._append{  sL     V$F%%eai0Fl56>56>JKr4   c              #     K   t        | j                        }|dk  ryd}d}| j                  D ]  \  }}||}|}||||f |}|} yw)zBYields all faces as 4-tuples of :class:`~ezdxf.math.Vec2` objects.rU   Nrq   )r1   r   r   r   rT   rV   s         r2   r3   zCurvedTrace.faces  sj     DNN#19.. 	JD$|dD((DD	s   AA
Nr   )
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   rd   )rg   rh   ri   r   ro   rr   rx   classmethodr   r   r   r3   r/   r4   r2   r    r    )  s    0#$   	
  
 6       	 
   
   DL r4   r    c                  x    e Zd ZdZddZd Zd ZddZddZddZ	ddZ
dd	Z	 d	 	 	 ddZd Zeddd       Zy
)r   a  Sequence of 2D banded lines like polylines with start- and end width or
    curves with start- and end width.


    .. note::

        Accepts 3D input, but z-axis is ignored. The :class:`TraceBuilder` is a
        2D only object and uses only the :ref:`OCS` coordinates!

    c                     g | _         d| _        y rl   )_tracesrn   r0   s    r2   ro   zTraceBuilder.__init__  s    ,.r4   c                ,    t        | j                        S r.   )r7   r   r0   s    r2   rr   zTraceBuilder.__len__  s    4<<  r4   c                     | j                   |   S r.   )r   rv   s     r2   rx   zTraceBuilder.__getitem__  s    ||D!!r4   c                :    | j                   j                  |       y)zAppend a new trace.N)r   r   r1   traces     r2   r   zTraceBuilder.append  s    E"r4   c              #  b   K   | j                   D ]  }|j                         E d{     y7 w)zaYields all faces as 4-tuples of :class:`~ezdxf.math.Vec2` objects
        in :ref:`OCS`.
        N)r   r3   r   s     r2   r3   zTraceBuilder.faces  s-      \\ 	%E{{}$$	%$s   #/-/c              #     K   | j                         D ]'  }t        |j                  fd|D                     ) yw)zaYields all faces as 4-tuples of :class:`~ezdxf.math.Vec3` objects
        in :ref:`WCS`.
        c              3  `   K   | ]%  }t        |j                  |j                         ' y wr.   r   xy.0v	elevations     r2   	<genexpr>z)TraceBuilder.faces_wcs.<locals>.<genexpr>  s"     )R$qssACC*C)R   +.N)r3   tuplepoints_to_wcs)r1   ocsr   r`   s     ` r2   	faces_wcszTraceBuilder.faces_wcs  s<      JJL 	TD)))RT)RRSS	Ts   <?c              #  R   K   | j                   D ]  }|j                           yw)z{Yields for each sub-trace a single polygon as sequence of
        :class:`~ezdxf.math.Vec2` objects in :ref:`OCS`.
        N)r   rJ   r   s     r2   polygonszTraceBuilder.polygons  s'      \\ 	"E--/!	"s   %'c              #     K   | j                   D ]5  }t        |j                  fd|j                         D                     7 yw)z{Yields for each sub-trace a single polygon as sequence of
        :class:`~ezdxf.math.Vec3` objects in :ref:`WCS`.
        c              3  `   K   | ]%  }t        |j                  |j                         ' y wr.   r   r   s     r2   r   z,TraceBuilder.polygons_wcs.<locals>.<genexpr>  s"     !U$qssACC";!Ur   N)r   r   r   rJ   )r1   r   r   r   s     ` r2   polygons_wcszTraceBuilder.polygons_wcs  sC      \\ 	E!!!UU]]_!UU 	s   AA	Nc              #  h   K   | j                   D ]  }|j                  |||      E d{     y7 w)ar  Yields faces as SOLID, TRACE or 3DFACE entities with DXF attributes
        given in `dxfattribs`.

        If a document is given, the doc attribute of the new entities will be
        set and the new entities will be automatically added to the entity
        database of that document.

        .. note::

            The :class:`TraceBuilder` is a 2D only object and uses only the
            :ref:`OCS` coordinates!

        Args:
            dxftype: DXF type as string, "SOLID", "TRACE" or "3DFACE"
            dxfattribs: DXF attributes for SOLID, TRACE or 3DFACE entities
            doc: associated document

        N)r   rc   )r1   r]   r^   r_   r   s        r2   rc   zTraceBuilder.virtual_entities  s8     * \\ 	HE--gz3GGG	HGs   &202c                *   | j                   }t        |      dk  ryt        |d   t              ret        |d   t              rQt	        t        |j                  d            }t	        t        |d         }|D ]  \  }}}|j                  |||        yyy)zEClose multi traces by merging first and last trace, if linear traces.rU   Nr   r~   )r   r7   
isinstancer   r   r   r   )r1   tracesfirstlastr   r#   r$   s          r2   closezTraceBuilder.close  s    v;?fQi-*VBZ2Ufjjm4EVBZ0D16 @-{I  Y?@ 3V-r4   c           	        |j                         }|dk(  rpt        d|      }|j                  j                  }g }|j                  D ]/  \  }}}}	}
t        ||      }|r|}|}	|j                  |||	|
f       1 |j                  }n5|dk(  r!t        d|      }|j                  st        d      |j                  }|j                  j                  }|j                  j                  }g }|j                  D ]  }t        |j                  j                        }|j                  j                  d      r|j                  j                   }n|}|j                  j                  d      r|j                  j"                  }	n|}	|j                  j$                  }
|j                  |||	|
f        nt        d|       |r1|d	   d	   j'                  |d
   d	         s|j                  |d	           |        }d}d}d}d}t)               }|D ]  \  }}}	}
|dk7  rt+        |||      \  }}}}|d	kD  rt-        ||t/        j0                  |      t/        j0                  |      d      }|j2                  j'                  |      r|}|}n|}|}|j                  t4        j7                  ||||             d}|
dk7  rC|j8                  r.|j;                  |||	       |j                  |       t)               }|
}|}|	}|}|j;                  |||	        |j8                  r|j                  |       |rt=        |      dkD  r|j?                          |S )a  
        Create a complete trace from a LWPOLYLINE or a 2D POLYLINE entity, the
        trace consist of multiple sub-traces if :term:`bulge` values are
        present. Uses only the :ref:`OCS` coordinates!

        Args:
            polyline: :class:`~ezdxf.entities.LWPolyline` or 2D
                :class:`~ezdxf.entities.Polyline`
            segments: count of segments for bulge approximation, given count is
                for a full circle, partial arcs have proportional less segments,
                but at least 3

        
LWPOLYLINEr   POLYLINEr   z2D POLYLINE requiredr#   r$   zInvalid DXF type r   r~   g        NT)is_counter_clockwiser   ) r]   r   dxfconst_widthlwpointsr   r   closedis_2d_polylinerX   r   default_start_widthdefault_end_widthr;   locationhasattrr#   r$   bulger:   r   r   r   r   degreesstart_pointr    r   r|   r   r7   r  )r   polyliner   r]   r  pointsr   r   r#   r$   r  r  r  r  r  r"   r   store_bulgestore_start_widthstore_end_widthstore_pointlinear_tracer   r   start_angle	end_angler   r   swews                                 r2   from_polylinezTraceBuilder.from_polyline  sA    ""$l"L(3H",,22KF7?7H7H I31k9e1: #.K +IxiGHI __F
"J1H** 677''F"*,,"B"B ( > >F"++ I

 3 34::%%m4"(**"8"8K"5K::%%k2 &

 4 4I 1I

((xiGHI /y9::&)A,..vbz!}=MM&)$#'"}4: !	D0E;	5c!9E:6Y A:)[1Y/-1C ..u5,.,.LL!5!5c2r8!LM!|** ,,UKKLL.#.=L#$/!"+#$$UKCC!	DD ""LL&c%j1n KKMr4   r   )r   r,   r?   r   rd   )r   r   r   r   r?   zIterable[Sequence[Vec3]])r?   zIterable[Polygon]re   rf   r   )r  r   r   r   r?   r   )rg   rh   ri   r   ro   rr   rx   r   r3   r   r   r   rc   r  r   r#  r/   r4   r2   r   r     sk    	!"#%T" JNH5FH	 H0	@ g gr4   r   )
r   r   r   r   r#   r   r$   r   r?   r(   )3
__future__r   typingr   r   r   r   r   r	   r
   typing_extensionsr   abcr   collectionsr   r   numpyr   
ezdxf.mathr   r   r   r   r   r   r   r   r   ezdxf.documentr   ezdxf.entitiesr   r   r   r   r   r   __all__r!   r%   r(   __annotations__r)   r*   r,   r   r   r   r    r   r/   r4   r2   <module>r/     s   #   (  "  
 
 
 &UU
8?,RS .*@AdD$./i /d^ # !;<y <A AH`)- `)F).;@	 L	q- qhC8 Cr4   