
    'j$                    J   d dl mZ d dlmZmZmZmZ d dlmZ d dl	Z	d dl
Z
d dlZd dl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 d dlmZmZ d dlmZ  G d	 d
e	j                  ZdZej        Zej         Z!ej"        Z#d%dZ$d&dZ%d'd(dZ&d)dZ'	 d'd*dZ( G d d          Z)d+d$Z*dS ),    )annotations)UnionIterableSequenceOptional)PathN)uuid4)Matrix44UVecVec3Vec2)MeshBuilderMeshTransformer)meshexc                      e Zd ZdZdZdZdS )	Operationr         N)__name__
__module____qualname__union
differenceintersection     O/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/addons/openscad.pyr   r      s        EJLLLr   r   openscadreturnstrc                     t          j                    dv rt          S t          j                            dd                              d          S )NLinuxDarwinzopenscad-addonwin_exec_path")platformsystemCMDezdxfoptionsgetstripr   r   r   get_openscad_pathr.      sB    ///
}  !1?CCII#NNNr   boolc                     t          j                    dv rt          j        t                    duS t          t                                                                S )a  Returns ``True`` if OpenSCAD is installed.

    Searches on Windows the path stored in the options as "win_exec_path" in section
    "[openscad-addon]" which is "C:\Program Files\OpenSCAD\openscad.exe" by default.

    Searches the "openscad" command on Linux and macOS.

    r"   N)r'   r(   shutilwhichr)   r   r.   existsr   r   r   is_installedr4   &   sN     ///|C  ,,!##$$++---r   script	exec_pathOptional[str]r   c           	     8   |t                      }t          t          j                              }t	          t                                }|d| dz  }|d| dz  }|                    |            t          j        |ddt	          |          t	          |          g           |	                    d           t                      }|                                r&t          j        |                                          }|	                    d           |S )	a"  Executes the given `script` by OpenSCAD and returns the result mesh as
    :class:`~ezdxf.render.MeshTransformer`.

    Args:
        script: the OpenSCAD script as string
        exec_path: path to the executable as string or ``None`` to use the
            default installation path

    Nezdxf_z.offz.scadz--quietz-oT)
missing_ok)r.   r   tempfile
gettempdirr    r	   
write_text
subprocesscallunlinkr   r3   r   	off_loads	read_text)r5   r6   workdiruuidoff_path	scad_pathnew_meshs          r   runrH   4   s    %''	8&(())Guww<<D,$,,,,H.4....I   OMM	NN	
   %%%  H :#H$6$6$8$899 OOtO$$$Or   mr
   c                    dd l d
fdd                    fd|                                 D                       }d| d	S )Nr   valuesr   r   c              3  l   K   | D ]-}t          |j                  rt          |          V  )|V  .d S N)
isinstancefloat64float)rK   valuenps     r   cleanupzstr_matrix44.<locals>.cleanupb   sT       	 	E%,, Ell""""		 	r   , c           	     Z    g | ]'}t          t           |                              (S r   )r    list).0crS   s     r   
<listcomp>z str_matrix44.<locals>.<listcomp>i   s1    >>>Q3tGGAJJ''((>>>r   [])rK   r   r   r   )numpyjoincolumns)rI   srS   rR   s     @@r   str_matrix44r`   ^   sk          			>>>>!))++>>>??Aq888Or   pathIterable[UVec]holes"Optional[Sequence[Iterable[UVec]]]c                   fd}g  ||           g}|#|D ] }|                      ||                     !dg}|                               |                     d           |U|                     d           |D ](}|                     dt          |           d           )|                     d           |                     d           d	                    |          S )
a}  Returns a ``polygon()`` command as string. This is a 2D command, all
    z-axis values of the input vertices are ignored and all paths and holes
    are closed automatically.

    OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_the_2D_Subsystem#polygon

    Args:
        path: exterior path
        holes: a sequences of one or more holes as vertices

    c                X   t                    }g }t          j        |           }|d                             |d                   s|                    |d                    |D ]D}|                    |                               d|j        dd|j        dd           |dz  }E|S )Nr   z  [grT   ],r   )lenr   rV   iscloseappendxy)verticesindexindicesvlistvpointss        r   add_verticesz!str_polygon.<locals>.add_vertices}   s    F	(##Qxb	** 	#LLq""" 	 	ANN5!!!MM22222222333QJEEr   Nzpolygon(points = [ri   z	paths = [z  ,zconvexity = 10);
)rl   extendr    r]   )ra   rc   ru   pathsholelinesrq   rt   s          @r   str_polygonr|   m   s         F\$ E 	- 	-DLLd++,,,,!"E	LL	LL[!!! 	/ 	/GLL-c'll---....T	LL#$$$99Ur   c                  v    e Zd Zd)dZd*dZd+d	Z	 d,d-dZd.dZd/dZd0dZ	d1dZ
d2d Z	 d,d3d&Zd/d'Zd4d(Zd
S )5Scriptr   Nonec                    g | _         d S rM   )dataselfs    r   __init__zScript.__init__   s    !			r   r   r    c                :    | j                             |           dS )zAdd a string.N)r   rl   )r   r   s     r   addz
Script.add   s    	r   meshr   c                T    |                      t          j        |                     dS )zAdd `mesh` as ``polyhedron()`` command.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Primitive_Solids#polyhedron

        N)r   r   
scad_dumps)r   r   s     r   add_polyhedronzScript.add_polyhedron   s'     	"4(()))))r   Nra   rb   rc   rd   c                L    |                      t          ||                     dS )a  Add a ``polygon()`` command. This is a 2D command, all
        z-axis values of the input vertices are ignored and all paths and holes
        are closed automatically.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_the_2D_Subsystem#polygon

        Args:
            path: exterior path
            holes: a sequence of one or more holes as vertices, or ``None`` for no holes

        N)r   r|   )r   ra   rc   s      r   add_polygonzScript.add_polygon   s&      	T5))*****r   rI   r
   c                R    |                      dt          |           d           dS )zAdd a transformation matrix of type :class:`~ezdxf.math.Matrix44` as
        ``multmatrix()`` operation.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#multmatrix

        zmultmatrix(m = )N)r   r`   )r   rI   s     r   add_multmatrixzScript.add_multmatrix   s.     	5<??55566666r   rs   r   c           	         t          |          }|                     d|j        dd|j        dd|j        dd           dS )zAdd a ``translate()`` operation.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#translate

        Args:
            v: translation vector

        ztranslate(v = [rh   rT   ])Nr   r   rm   rn   z)r   rs   vecs      r   add_translatezScript.add_translate   sR     1ggD35DDDceDDDDDDDEEEEEr   axrP   ayazc           	     J    |                      d|dd|dd|dd           dS )a9  Add a ``rotation()`` operation.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#rotate

        Args:
            ax: rotation about the x-axis in degrees
            ay: rotation about the y-axis in degrees
            az: rotation about the z-axis in degrees

        zrotate(a = [rh   rT   r   Nr   )r   r   r   r   s       r   
add_rotatezScript.add_rotate   sA     	8888888888899999r   ac                    t          |          }|                     d|dd|j        dd|j        dd|j        dd	           dS )a+  Add a ``rotation()`` operation about the given axis `v`.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#rotate

        Args:
            a: rotation angle about axis `v` in degrees
            v: rotation axis as :class:`ezdxf.math.UVec` object

        zrotate(a = rh   z, v = [rT   r   Nr   )r   r   rs   r   s       r   add_rotate_about_axiszScript.add_rotate_about_axis   s]     1ggLqLLL35LLLceLLLLLLLMMMMMr   sxsyszc           	     J    |                      d|dd|dd|dd           dS )a   Add a ``scale()`` operation.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#scale

        Args:
            sx: scaling factor for the x-axis
            sy: scaling factor for the y-axis
            sz: scaling factor for the z-axis

        zscale(v = [rh   rT   r   Nr   )r   r   r   r   s       r   	add_scalezScript.add_scale   sA     	7r777r777r777788888r   nxnynzauto.Optional[Union[bool, tuple[bool, bool, bool]]]c                L   d|dd|dd|dd}||                      |dz              dS t          |t                    r"t          |                                          }n%d                    d |D                       }d| d}|                      |d	| dz              dS )
a  Add a ``resize()`` operation.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#resize

        Args:
            nx: new size in x-axis
            ny: new size in y-axis
            nz: new size in z-axis
            auto: If the `auto` argument is set to ``True``, the operation
                auto-scales any 0-dimensions to match. Set the `auto` argument
                as a  3-tuple of bool values to auto-scale individual axis.

        zresize(newsize = [rh   rT   r[   Nr   c                P    g | ]#}t          |                                          $S r   )r    lower)rW   r   s     r   rY   z%Script.add_resize.<locals>.<listcomp>  s&    <<<!s1vv||~~<<<r   rZ   z	, auto = )r   rN   r/   r    r   r]   )r   r   r   r   r   mainflagss          r   
add_resizezScript.add_resize   s    ( <B;;;B;;;B;;;;<HHTCZ   Fd## 	!IIOO%%EEII<<t<<<==E LLLE,E,,,,-----r   c           	         t          |                                          }|                     d|j        dd|j        dd|j        dd           dS )a  Add a ``mirror()`` operation.

        OpenSCAD docs: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#mirror

        Args:
            v: the normal vector of a plane intersecting the origin through
                which to mirror the object

        zmirror(v = [rh   rT   r   N)r   	normalizer   rm   rn   r   )r   rs   ns      r   
add_mirrorzScript.add_mirror  s^     GG;;;;;;;!#;;;;<<<<<r   c                6    d                     | j                  S )z"Returns the OpenSCAD build script.rw   )r]   r   r   s    r   
get_stringzScript.get_string%  s    yy###r   )r   r   )r   r    r   r   )r   r   r   r   rM   )ra   rb   rc   rd   r   r   )rI   r
   r   r   )rs   r   r   r   )r   rP   r   rP   r   rP   r   r   )r   rP   rs   r   r   r   )r   rP   r   rP   r   rP   r   r   )
r   rP   r   rP   r   rP   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~      s"       " " " "   * * * * 59+ + + + +$7 7 7 7
F 
F 
F 
F: : : :N N N N9 9 9 9$ @D. . . . .>= = = =$ $ $ $ $ $r   r~   opmesh1r   mesh2c                <   t          | t                    s
J d            t                      }|                    | j         d           |                    |           |                    |           |                    d           |                                S )zReturns an `OpenSCAD`_ script to apply the given boolean operation to the
    given meshes.

    The supported operations are:

        - UNION
        - DIFFERENCE
        - INTERSECTION

    zenum of type Operation expectedz() {})rN   r   r~   r   namer   r   )r   r   r   r5   s       r   boolean_operationr   *  s     b)$$GG&GGG$XXF
JJ"'   !!!
%   
%   
JJsOOOr   r   )r   r/   rM   )r5   r    r6   r7   r   r   )rI   r
   r   r    )ra   rb   rc   rd   r   r    )r   r   r   r   r   r   r   r    )+
__future__r   typingr   r   r   r   pathlibr   enumr'   r1   r>   rD   r	   r;   r*   
ezdxf.mathr
   r   r   r   ezdxf.renderr   r   ezdxf.addonsr   Enumr   r)   r   UNIONr   
DIFFERENCEr   INTERSECTIONr.   r4   rH   r`   r|   r~   r   r   r   r   <module>r      s   # " " " " " 6 6 6 6 6 6 6 6 6 6 6 6                      1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 5 5 5 5 5          	    !
%O O O O. . . .' ' ' ' 'T   " 15+ + + + +\L$ L$ L$ L$ L$ L$ L$ L$^     r   