
    'j02                       d dl mZ d dl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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 d d
lmZ d dlmZ d dlmZmZm Z 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j/        d          Z0dZ1ej2        ej2        ej2        fZ3dZ4d:dZ5 G d de          Z6d;dZ7d<d#Z8ddd$d%dddd&d=d9Z9dS )>    )annotations)IterableOptionalUnionN)PathLike)LineCollection)	AxesImage)Line2D)	PathPatch)Path)Affine2D)to_matplotlib_path)BackendBkPath2d
BkPoints2d	ImageData)BackendPropertiesLayoutProperties)
FilterFunc)Color)Vec2Matrix44)Layout   )Configurationezdxfgr;p@g?axplt.Axesc                &   | j                             d           | j                            d           | j                                        D ]}|                    d           |                     d           |                     dd           d S )NFequaldatalim)xaxisset_visibleyaxisspinesvalues	autoscale
set_aspect)r   ss     Y/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/addons/drawing/matplotlib.py
setup_axesr+   *   s    HHY  	eLLMM'9%%%%%    c                       e Zd ZdZddd. fdZd/ fdZd0dZd1dZd2dZd3dZ	d4dZ
d5dZd6d"Zd7d%Zd8d(Zd9d+Zd, Z fd-Z xZS ):MatplotlibBackenda  Backend which uses the :mod:`Matplotlib` package for image export.

    Args:
        ax: drawing canvas as :class:`matplotlib.pyplot.Axes` object
        adjust_figure: automatically adjust the size of the parent
            :class:`matplotlib.pyplot.Figure` to display all content
    T)adjust_figurer   r   r/   boolc                   t                                                       t          |           || _        || _        d| _        d S )Nr   )super__init__r+   r   _adjust_figure
_current_z)selfr   r/   	__class__s      r*   r3   zMatplotlibBackend.__init__>   s@     	2+r,   configr   returnNonec                    |j         9| j                                        }|r|                    d|j        z            }t                                          |           d S )Ng      R@)min_lineweight)r<   r   
get_figurewith_changesdpir2   	configure)r6   r8   figurer7   s      r*   r@   zMatplotlibBackend.configureJ   sc     (W''))F O,,D6:<M,NN&!!!!!r,   intc                4    | j         }| xj         dz  c_         |S )Nr   )r5   )r6   zs     r*   _get_zzMatplotlibBackend._get_zS   s    O1r,   colorr   c                :    | j                             |           d S N)r   set_facecolor)r6   rF   s     r*   set_backgroundz MatplotlibBackend.set_backgroundX   s    e$$$$$r,   posr   
propertiesr   c                    |j         }| j                            |j        g|j        gt
          ||                                            dS )z Draw a real dimensionless point.r)   czorderN)rF   r   scatterxySCATTER_POINT_SIZErE   )r6   rK   rL   rF   s       r*   
draw_pointzMatplotlibBackend.draw_point[   sP     UGUG ;;== 	 	
 	
 	
 	
 	
r,   floatc                v    | j         j        J t          |j        | j         j        z  | j         j                  S )z>Set lineweight_scaling=0 to use a constant minimal lineweight.)r8   r<   max
lineweightlineweight_scaling)r6   rL   s     r*   get_lineweightz MatplotlibBackend.get_lineweightf   s=    {)555!DK$BBK&
 
 	
r,   startendc           
     <   |                     |          r|                     ||           dS | j                            t	          |j        |j        f|j        |j        f|                     |          |j        | 	                                                     dS )zeDraws a single solid line, line type rendering is done by the
        frontend since v0.18.1
        )	linewidthrF   rP   N)
iscloserU   r   add_liner
   rR   rS   r[   rF   rE   )r6   r\   r]   rL   s       r*   	draw_linezMatplotlibBackend.draw_linen   s     == 	OOE:.....GWce$Wce$"11*==$*;;==      r,   linesIterable[tuple[Vec2, Vec2]]c           	        |j         }|                     |          }g }g }g }|                                 }|D ]~\  }	}
|	                    |
          r5|                    |	j                   |                    |	j                   O|                    |	j        |	j        f|
j        |
j        ff           | j                            ||t          ||           | j        
                    t          ||||d                     dS )zDFast method to draw a bunch of solid lines with the same properties.rN   butt)
linewidthsrF   rP   capstyleN)rF   r[   rE   r`   appendrR   rS   r   rQ   rT   add_collectionr   )r6   rc   rL   rF   rY   _linespoint_xpoint_yrD   r)   es              r*   draw_solid_linesz"MatplotlibBackend.draw_solid_lines   s     ((44
KKMM 	8 	8DAqyy|| 8qs###qs####QSzAC:67777,>%PQRRR%  	
 	
 	
 	
 	
r,   pathr   c                \   t          |g          }	 t          ||                     |          d|j        |                                           }| j                            |           dS # t          $ r5}t          	                    dt          |                      Y d}~dS d}~ww xY w)zbDraw a solid line path, line type rendering is done by the
        frontend since v0.18.1
        F)r_   fillrF   rP   zignored matplotlib error: N)r   r   r[   rF   rE   r   	add_patch
ValueErrorloggerinfostr)r6   rp   rL   mpl_pathpatchrn   s         r*   	draw_pathzMatplotlibBackend.draw_path   s    
 &tf--	%--j99 &{{}}  E Ge$$$$$  	? 	? 	?KK=SVV==>>>>>>>>>	?s   >A, ,
B+6*B&&B+pathsIterable[BkPath2d]c                8   d}	 t          t          |d          |j        |d|                                           }| j                            |           d S # t          $ r5}t                              dt          |                      Y d }~d S d }~ww xY w)Nr   T)detect_holes)rF   r_   rr   rP   z1ignored matplotlib error in draw_filled_paths(): )
r   r   rF   rE   r   rs   rt   ru   rv   rw   )r6   r{   rL   r_   ry   rn   s         r*   draw_filled_pathsz#MatplotlibBackend.draw_filled_paths   s     		%"5t<<< &#{{}}  E Ge$$$$$  	V 	V 	VKKTCPQFFTTUUUUUUUUU	Vs   :A 
B$*BBpointsr   c                     | j         j        t          d |                                D              |j        d|                                 d d S )Nc              3  2   K   | ]}|j         |j        fV  d S rH   )rR   rS   ).0ps     r*   	<genexpr>z8MatplotlibBackend.draw_filled_polygon.<locals>.<genexpr>   s*      99!AC:999999r,   r   )rF   r_   rP   )r   rr   zipverticesrF   rE   )r6   r   rL   s      r*   draw_filled_polygonz%MatplotlibBackend.draw_filled_polygon   s^    99v'8'8999:";;==		
 	
 	
 	
 	
 	
r,   
image_datar   c                   |j         j        \  }}}|dk    sJ t          | j        d          }|                    t          j        |j         d                     |                    |                                            |j	        \  }}}	}
}}}}}}}}}}}}t          t          j        |||g|||gg dg                    }|                    || j        j        z              | j                            |           d S )N   antialiased)interpolationr   )axis)r   r   r   )matrix)imageshaper	   r   set_datanpflip
set_zorderrE   	transformr   arrayset_transform	transData	add_image)r6   r   rL   heightwidthdepthhandlem11m12m13m14m21m22m23m24m31m32m33m34m41m42m43m44matplotlib_transforms                           r*   
draw_imagezMatplotlibBackend.draw_image   s5     */5uzzzz 47-@@@
 0q999:::$++--(((&  #	
'8#sO#sOII  
  
  
 	1DG4EEFFF&!!!!!r,   c                8    | j                                          d S rH   )r   clear)r6   s    r*   r   zMatplotlibBackend.clear   s    r,   c                   t                                                       | j                            d           | j        r| j                                        \  }}| j                                        \  }}||z
  ||z
  }}t          j        |d          sMt          j
        ||z            \  }}| j                                                            ||d           d S d S d S )NTr   )forward)r2   finalizer   r'   r4   get_xlimget_ylimmathr`   plt	figaspectr=   set_size_inches)
r6   minxmaxxminymaxy
data_widthdata_heightr   r   r7   s
            r*   r   zMatplotlibBackend.finalize   s    $ 	R))++JD$))++JD$&*Tk4$;J<
A.. R #kJ.F G Gv""$$44UFD4QQQQQ	R 	RR Rr,   )r   r   r/   r0   )r8   r   r9   r:   )r9   rB   )rF   r   )rK   r   rL   r   )rL   r   r9   rV   )r\   r   r]   r   rL   r   )rc   rd   rL   r   )rp   r   rL   r   )r{   r|   rL   r   )r   r   rL   r   )r   r   rL   r   r9   r:   )__name__
__module____qualname____doc__r3   r@   rE   rJ   rU   r[   rb   ro   rz   r   r   r   r   r   __classcell__)r7   s   @r*   r.   r.   5   sn         #	
 
 
 
 
 
 
 
" " " " " "   
% % % %	
 	
 	
 	

 
 
 
   $
 
 
 
<% % % %&% % % %$
 
 
 
+" +" +" +"Z  	R 	R 	R 	R 	R 	R 	R 	R 	Rr,   r.   r9   rV   c                    |                                  \  }}|                                 \  }}||z
  ||z
  }}t          |          dk    r||z  S dS )Ng&.>g      ?)r   r   abs)r   r   r   r   r   r   r   s          r*   _get_aspect_ratior     sX    JD$JD$"Tk4$;J
;$K''3r,   ratior   r   tuple[float, float]c                n    |dk    r|dk    rt          d          |dk    r|| z  }n|dk    r|| z  }||fS )Ng        zinvalid (width, height) values)rt   )r   r   r   s      r*   _get_width_heightr     sR    ||#9:::||	3&=r,   i,  agg)bgfgr?   backendr8   filter_funcsize_incheslayoutr   filenameUnion[str, PathLike]r   Optional[Color]r   r?   rB   r   rw   r8   Optional[Configuration]r   Optional[FilterFunc]r   Optional[tuple[float, float]]r:   c               8   ddl m}	 ddlm}
 ddl}|                                }|                    |           |t                      }	 t          j	        |          }|
                    d          } |	| j                  }t          j        |           }||                    ||           t          |          } |
|||                              | d||	           |Ft#          |          }t%          ||d         |d                   \  }}|                    ||d           |                    |||                                d
           t          j        |           |                    |           dS # |                    |           w xY w)at  Quick and simplified render export by matplotlib.

    Args:
        layout: modelspace or paperspace layout to export
        filename: export filename, file extension determines the format e.g.
            "image.png" to save in PNG format.
        bg: override default background color in hex format #RRGGBB or #RRGGBBAA,
            e.g. use bg="#FFFFFF00" to get a transparent background and a black
            foreground color (ACI=7), because a white background #FFFFFF gets a
            black foreground color or vice versa bg="#00000000" for a transparent
            (black) background and a white foreground color.
        fg: override default foreground color in hex format #RRGGBB or #RRGGBBAA,
            requires also `bg` argument. There is no explicit foreground color
            in DXF defined (also not a background color), but the ACI color 7
            has already a variable color value, black on a light background and
            white on a dark background, this argument overrides this (ACI=7)
            default color value.
        dpi: image resolution (dots per inches).
        size_inches: paper size in inch as `(width, height)` tuple, which also
            defines the size in pixels = (`width` * `dpi`) x (`height` * `dpi`).
            If `width` or `height` is 0.0 the value is calculated by the aspect
            ratio of the drawing.
        backend: the matplotlib rendering backend to use (agg, cairo, svg etc)
            (see documentation for `matplotlib.use() <https://matplotlib.org/3.1.1/api/matplotlib_configuration_api.html?highlight=matplotlib%20use#matplotlib.use>`_
            for a complete list of backends)
        config: drawing parameters
        filter_func: filter function which takes a DXFGraphic object as input
            and returns ``True`` if the entity should be drawn or ``False`` if
            the entity should be ignored

    r   )RenderContext)Frontendr   N)r?   )r   r   r   r   T)r   r   layout_properties)r?   	facecolortransparent)rL   r   frontendr   
matplotlibget_backenduser   r   rA   add_axesdocr   from_layout
set_colorsr.   draw_layoutr   r   r   savefigget_facecolorclose)r   r   r   r   r?   r   r8   r   r   r   r   r   old_backendfigr   ctxr   outr   whs                        r*   qsaver     s   V *)))))"""""" ((**KNN7~$*---||L11mFJ'',8@@>((R000##c6""..#/	 	/ 	
 	
 	
 "%b))E$UKNKNKKDAq1d+++H#1A1A1C1CQUVVV	#{#####
{####s   D F F)r   r   )r   r   r9   rV   )r   rV   r   rV   r   rV   r9   r   )r   r   r   r   r   r   r   r   r?   rB   r   rw   r8   r   r   r   r   r   r9   r:   ):
__future__r   typingr   r   r   r   loggingosr   matplotlib.pyplotpyplotr   numpyr   matplotlib.collectionsr   matplotlib.imager	   matplotlib.linesr
   matplotlib.patchesr   matplotlib.pathr   matplotlib.transformsr   ezdxf.npshapesr   ezdxf.addons.drawing.backendr   r   r   r   ezdxf.addons.drawing.propertiesr   r   ezdxf.addons.drawing.type_hintsr   r   
ezdxf.mathr   r   ezdxf.layoutsr   r8   r   	getLoggerru   POINTSCURVE4CURVE4x3rT   r+   r.   r   r   r    r,   r*   <module>r     s   # " " " " " , , , , , , , , , ,                   1 1 1 1 1 1 & & & & & & # # # # # # ( ( ( ( ( (             * * * * * * - - - - - - Q Q Q Q Q Q Q Q Q Q Q Q O O O O O O O O 6 6 6 6 6 6 1 1 1 1 1 1 % % % % % % % %             ! ! ! ! ! !		7	#	# 
Kdk2 & & & &OR OR OR OR OR OR OR ORd       &*(,15O$ O$ O$ O$ O$ O$ O$ O$r,   