
    vj0                     t   d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
m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 d
dlmZ ddlmZmZmZmZmZ ddl m!Z! ed   Z"eed   ee#ef   f   Z$e G d de
             Z% G d d      Z&	 	 dejN                  ddee%ee	e   f   de#dee"   de(de)ddfdZ*y)zDXF export utilities.    )
AnyDictListLiteralOptionalTupleUnionIterableProtocolruntime_checkableN)unitszoom)factory)GeomConvert)gp_Dir)GC_MakeArcOfEllipse)Self   )RAD2DEG   )FaceEdgeShapeCompoundcompound)Plane)splinearc)ARCCIRCLEELLIPSELINESPLINEc                   4    e Zd Zedefd       Zdee   fdZy)WorkplaneLikereturnc                      y N selfs    L/DATA/.local/lib/python3.12/site-packages/cadquery/occ_impl/exporters/dxf.pyplanezWorkplaneLike.plane%   s        c                      y r(   r)   r*   s    r,   __iter__zWorkplaneLike.__iter__)   s    r.   N)	__name__
__module____qualname__propertyr   r-   r
   r   r0   r)   r.   r,   r%   r%   #   s+    u  (5/ r.   r%   c                   P   e Zd ZdZdZddej                  fddddded	ee	e
e   f   d
edeeeef   df   dee   defdZddddedededefdZd"deeef   dedefdZedede
e   fd       Zededefd       Zededefd       Zededefd       Zeded edefd!       Zy)#DxfDocumenta  Create DXF document from CadQuery objects.

    A wrapper for `ezdxf <https://ezdxf.readthedocs.io/>`_ providing methods for
    converting :class:`cadquery.Workplane` objects to DXF entities.

    The ezdxf document is available as the property ``document``, allowing most
    features of ezdxf to be utilised directly.

    .. rubric:: Example usage

    .. code-block:: python
       :caption: Single layer DXF document

       rectangle = cq.Workplane().rect(10, 20)

       dxf = DxfDocument()
       dxf.add_shape(rectangle)
       dxf.document.saveas("rectangle.dxf")

    .. code-block:: python
       :caption: Multilayer DXF document

       rectangle = cq.Workplane().rect(10, 20)
       circle = cq.Workplane().circle(3)

       dxf = DxfDocument()
       dxf = (
           dxf.add_layer("layer_1", color=2)
           .add_layer("layer_2", color=3)
           .add_shape(rectangle, "layer_1")
           .add_shape(circle, "layer_2")
       )
       dxf.document.saveas("rectangle-with-hole.dxf")
    g&.>AC1027FNMbP?)metadataapprox	tolerance
dxfversionsetup	doc_unitsr9   r:   r;   c                `   |i }| j                   | j                  | j                  d| _        || _        || _        t        j                  |||      | _        | j                  j                         | _
        | j                  j                         }|j                         D ]
  \  }}	|	||<    y)aW  Initialize DXF document.

        :param dxfversion: :attr:`DXF version specifier <ezdxf-stable:ezdxf.document.Drawing.dxfversion>`
            as string, default is "AC1027" respectively "R2013"
        :param setup: setup default styles, ``False`` for no setup, ``True`` to set up
            everything or a list of topics as strings, e.g. ``["linetypes", "styles"]``
            refer to :func:`ezdxf-stable:ezdxf.new`.
        :param doc_units: ezdxf document/modelspace :doc:`units <ezdxf-stable:concepts/units>`
        :param metadata: document :ref:`metadata <ezdxf-stable:ezdxf_metadata>` a dictionary of name value pairs
        :param approx: Approximation strategy for converting :class:`cadquery.Workplane` objects to DXF entities:

            ``None``
                no approximation applied
            ``"spline"``
                all splines approximated as cubic splines
            ``"arc"``
                all curves approximated as arcs and straight segments

        :param tolerance: Approximation tolerance for converting :class:`cadquery.Workplane` objects to DXF entities.
        N)r"   r    r!   )r<   r=   r   )	_dxf_line_dxf_circle_dxf_ellipse_DISPATCH_MAPr:   r;   ezdxfnewdocument
modelspacemspezdxf_metadataitems)
r+   r<   r=   r>   r9   r:   r;   doc_metadatakeyvalues
             r,   __init__zDxfDocument.__init__S   s    < H NN&&((
 "		ZuIV==++-}}335"..*JC %L +r.      
CONTINUOUScolorlinetypenamerR   rS   r&   c                V    | j                   j                  j                  |||       | S )a  Create a layer definition

        Refer to :ref:`ezdxf layers <ezdxf-stable:layer_concept>` and
        :doc:`ezdxf layer tutorial <ezdxf-stable:tutorials/layers>`.

        :param name: layer definition name
        :param color: color index. Standard colors include:
            1 red, 2 yellow, 3 green, 4 cyan, 5 blue, 6 magenta, 7 white/black
        :param linetype: ezdxf :doc:`line type <ezdxf-stable:concepts/linetypes>`
        rQ   )rF   layersadd)r+   rT   rR   rS   s       r,   	add_layerzDxfDocument.add_layer   s(     	  UX Fr.   shapelayerc                    t        |t              r<|j                  }t        |j	                          j                  |j                        }n|}i }|r||d<   | j                  dk(  rB| j                  |      D cg c]'  }|j                         dk(  r|j                         n|) }}n| j                  dk(  rdg }|j                         D ]N  }|j                  | j                  t        j                  |      j                  | j                                      P n| j                  |      }|D ]  }	| j"                  j%                  |	j                         d      }
|
rB |
|	      \  }}t'        j(                  |i ||      }| j*                  j-                  |       q| j/                  |	      \  }}t1        j2                  j4                  di |}| j*                  j7                  |      j9                  |        | S c c}w )zAdd CadQuery shape to a DXF layer.

        :param s: CadQuery Workplane or Shape
        :param layer: layer definition name
        rZ   r   BSPLINEr   N)
dxfattribsr)   )
isinstancer%   r-   r   r0   transformShapefGr:   _ordered_edgesgeomType	toSplinesWiresextendr   makeFromWirestoArcsr;   rC   getr   rE   rH   
add_entity_dxf_splinerD   mathBSpline
add_splineapply_construction_tool)r+   rY   rZ   r-   shape_general_attributeseedgeseledge	converterentity_typeentity_attributesentity_s                  r,   	add_shapezDxfDocument.add_shape   s    e]+KKEu~~/0??IFF*/w';;(" ,,V44A "#!:A4  
 [[E!E lln''(:(:2(>(E(Ednn(UV % ''/ED**..t}}EI1:4.. ,W/@,WDV,W ##F+'+'7'7e'D$$++@.?@##1 $ ))&1   Cs   :,Hsc                     g }| j                         D ]  }|j                  |        t        | t              r|j                  d | D               |S )Nc              3   B   K   | ]  }t        |t              s|  y wr(   )r^   r   ).0rq   s     r,   	<genexpr>z-DxfDocument._ordered_edges.<locals>.<genexpr>   s     :AjD&9as   )rd   re   r^   r   )r{   rvws      r,   ra   zDxfDocument._ordered_edges   sI      AIIaL  a"II:::	r.   rt   c                     d| j                         j                         | j                         j                         dfS )zConvert a Line to DXF entity attributes.

        :param edge: CadQuery Edge to be converted to a DXF line

        :return: dictionary of DXF entity attributes for creating a line
        r"   )startend)
startPointtoTupleendPoint)rt   s    r,   r@   zDxfDocument._dxf_line   s8     oo'//1$--/:Q:Q:SU
 	
r.   c                 P   | j                         }|j                         }|j                         }|j                         }|j	                         j                         }|j                         j                         }t        ddd      }|j                  ||      }|j                         j                         dkD  r5t        |j                         |z
  z  }	t        |j                         |z
  z  }
n<t         |j                         |z
  z  dz   }	t         |j                         |z
  z  dz   }
| j                         r4d|j                         |j!                         |j                         f|dfS d|j                         |j!                         |j                         f||	|
dfS )zConvert a Circle to DXF entity attributes.

        :param edge: CadQuery Edge to be converted to a DXF circle

        :return: dictionary of DXF entity attributes for creating either a circle or arc
        r         r    )centerradiusr   )r   r   start_angle	end_angle)_geomAdaptorCircleRadiusLocationYAxis	DirectionAxisr   AngleWithRefXYZZr   FirstParameterLastParameterIsClosedXY)rt   geomcircr   locationdirection_ydirection_zdyphia1a2s              r,   rA   zDxfDocument._dxf_circle   ss      "{{}==?jjl,,.iik++-Aq!_&&r;7?? 1$D//1C78BD..0367BT//1C783>BT002S89C?B==?'zz|XZZ\8::<H$  'zz|XZZ\8::<H$#%!#	 r.   c                 &   | j                         }|j                         }|j                         }|j                         }|j	                         }|j                         j                         }||j                         z  }|j                         j                         }|j                         dkD  r!|j                         }	|j                         }
nXt        ||j                         |j                         d      j                         }|j                         }	|j                         }
d|j                         |j                         |j                         f|j                         |j                         |j                         f||z  |	|
dfS )zConvert an Ellipse to DXF entity attributes.

        :param edge: CadQuery Edge to be converted to a DXF ellipse

        :return: dictionary of DXF entity attributes for creating an ellipse
        r   Fr!   )r   
major_axisratiostart_param	end_param)r   EllipseMinorRadiusMajorRadiusr   XAxisr   r   r   r   r   r   r   Valuer   r   )rt   r   ellipser1r2cxdirxaxzdirr   r   gcs               r,   rB   zDxfDocument._dxf_ellipse  sR      ",,.  "  "}}((*488:o||~'')668a<--/K**,I$##%""$	
 eg  ++-K((*I 335!##%/"uuw9b*&	
 		
r.   r-   c                 Z   |j                         }t        j                  |j                         j                               }t        j                  ||j                         |j                         | j                        }|j                  |j                                |j                         dz   }t        |j                               }|j                         D cg c]1  }|j                         |j                         |j!                         f3 }	}|j#                         r:t%        d|j'                         dz         D 
cg c]  }
|j)                  |
       c}
nd}|j+                         r)|j-                         |j/                         z
  }|	|	d| z  }	d|	|||dfS c c}w c c}
w )zConvert a Spline to ezdxf.math.BSpline parameters.

        :param edge: CadQuery Edge to be converted to a DXF spline
        :param plane: CadQuery Plane

        :return: dictionary of ezdxf.math.BSpline parameters
        r   Nr#   )control_pointsorderknotsweights)r   r   CurveToBSplineCurve_sCurveSplitBSplineCurve_sr   r   CURVE_TOLERANCE	TransformTrsfDegreelistKnotSequencePolesr   r   r   
IsRationalrangeNbPolesWeight
IsPeriodicNbKnotsLastUKnotIndex)clsrt   r-   adaptorcurver   r   r   ppolesir   pads                r,   rj   zDxfDocument._dxf_splineA  sz    ##%11'--/2G2G2IJ00""$!!#	
 	(!#V((*+06?1!##%&?   " (-Q0@10D'EF'E!V]]1'EF 	 .."V%:%:%<<CU4C[ E "'"	
 	
 @Fs   6F#F() ) r1   r2   r3   __doc__r   r   MMstrr	   boolr   intr   r   ApproxOptionsfloatrN   r   rX   r%   r   rz   staticmethodr   ra   DxfEntityAttributesr@   rA   rB   classmethodr   rj   r)   r.   r,   r6   r6   -   s   !F O #(-	/& 15*./&/& T49_%/& 	/& S#X,-/& '/& /&d *+L#&69	"3u]E%9: 33 3PT 3j % DJ   

 

!4 

 

 ,$ ,#6 , ,\ )
4 )
$7 )
 )
V *
t *
E *
6I *
 *
r.   r6   )r>   r   fnamer:   r;   r>   r&   c                   t        |||      }t        | t        t        f      r|j	                  |        n| D ]  }|j	                  |        t        j                  |j                         |j                  j                  |       y)a)  
    Export Workplane content to DXF. Works with 2D sections.

    :param w: Workplane to be exported.
    :param fname: Output filename.
    :param approx: Approximation strategy. None means no approximation is applied.
        "spline" results in all splines being approximated as cubic splines. "arc" results
        in all curves being approximated as arcs and straight segments.
    :param tolerance: Approximation tolerance.
    :param doc_units: ezdxf document/modelspace :doc:`units <ezdxf-stable:concepts/units>` (in. = ``1``, mm = ``4``).
    )r:   r;   r>   N)
r6   r^   r%   r   rz   r   extentsrH   rF   saveas)r   r   r:   r;   r>   dxfr{   s          r,   	exportDXFr   o  sg    ( VyI
NC!mU+,aAMM!  	LLLLr.   )Nr8   )+r   typingr   r   r   r   r   r   r	   r
   r   r   rD   r   r   ezdxf.entitiesr   OCP.GeomConvertr   OCP.gpr   OCP.GCr   typing_extensionsr   r   shapesr   r   r   r   r   r   r   r   r   r   r%   r6   r   r   r   r   r)   r.   r,   <module>r      s         " '  & "  : :  (9:DcNJ 
 H  
 
J
 '+	 XX]E8E?23 ]# 	  
r.   