
    vj(                         d dl ZddlmZmZmZ ddlmZ d dlm	Z	m
Z
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
ZdZdZdZ G d d      Zd Zd Zd ZddZddefdZy)    N   )ShapeCompound	TOLERANCE)BoundBox)gp_Ax2gp_Pntgp_Dir)BRepLib)HLRBRep_AlgoHLRBRep_HLRToShape)HLRAlgo_Projector)GCPnts_QuasiUniformDeflectiongMbP?a  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   width="%(width)s"
   height="%(height)s"

>
    <g transform="scale(%(unitScale)s, -%(unitScale)s)   translate(%(xTranslate)s,%(yTranslate)s)" stroke-width="%(strokeWidth)s"  fill="none">
       <!-- hidden lines -->
       <g  stroke="rgb(%(hiddenColor)s)" fill="none" stroke-dasharray="%(strokeWidth)s,%(strokeWidth)s" >
%(hiddenContent)s
       </g>

       <!-- solid lines -->
       <g  stroke="rgb(%(strokeColor)s)" fill="none">
%(visibleContent)s
       </g>
    </g>
    %(axesIndicator)s
</svg>
a  <g transform="translate(20,%(textboxY)s)" stroke="rgb(0,0,255)">
        <line x1="30" y1="-30" x2="75" y2="-33" stroke-width="3" stroke="#000000" />
         <text x="80" y="-30" style="stroke:#000000">X </text>

        <line x1="30" y1="-30" x2="30" y2="-75" stroke-width="3" stroke="#000000" />
         <text x="25" y="-85" style="stroke:#000000">Y </text>

        <line x1="30" y1="-30" x2="58" y2="-15" stroke-width="3" stroke="#000000" />
         <text x="65" y="-5" style="stroke:#000000">Z </text>
        <!--
            <line x1="0" y1="0" x2="%(unitScale)s" y2="0" stroke-width="3" />
            <text x="0" y="20" style="stroke:#000000">1  %(uom)s </text>
        -->
    </g>z			<path d="%s" />
c                       e Zd ZdZdZy)UNITSmminN)__name__
__module____qualname__MMIN     L/DATA/.local/lib/python3.12/site-packages/cadquery/occ_impl/exporters/svg.pyr   r   9   s    	B	Br   r   c                 Z   t        j                  | j                        }|j                  |j                  |j
                  g}t        |      dkD  rt        j                  S t        |      dk  rt        j                  S t        |      dk  rt        j                  S t        j                  S )z/
    Guess the unit of measure of a shape.
    
   g?)r   _fromTopoDSwrappedxlenylenzlenmaxr   r   minr   sum)shapebbdimLists      r   guessUnitOfMeasurer)   >   s|     
		emm	,Bww)G
7|bxx 7|cxx 7|bxx88Or   c                 L   t        j                          }| j                         }|j                         }|j                         }t	        |t
        ||      j                         rfdt        j                               D        }t        |      }|j                  dj                  |j                         |j                                      |D ]?  }|j                  dj                  |j                         |j                                      A |j                         S )z/
    Creates an SVG edge from a OCCT edge.
    c              3   F   K   | ]  }j                  |d z           yw)   N)Value).0ipointss     r   	<genexpr>zmakeSVGedge.<locals>.<genexpr>b   s!     J1IAFLLQ'1Is   !zM{},{} zL{},{} )StringIO_geomAdaptorFirstParameterLastParameterr   DISCRETIZATION_TOLERANCEIsDonerangeNbPointsnextwriteformatXYgetvalue)ecscurvestartendpoint_itpr0   s          @r   makeSVGedgerG   T   s    
 
			BNNE  "E



C*52JESVWF}}Jv7H1IJN
!!!##%/0AHHY%%acceQSSU34  ;;=r   c                     g }g }| D ]1  }|j                         D ]  }|j                  t        |              3 |D ]1  }|j                         D ]  }|j                  t        |              3 ||fS )zI
    Collects the visible and hidden edges from the CadQuery object.
    )EdgesappendrG   )visibleShapeshiddenShapeshiddenPathsvisiblePathssr@   s         r   getPathsrP   m   su    
 KLAA/   A{1~.   &&r   c                 6
   ddddddddd	dd
d}|r|j                  |       t        |       }|d   }|d
k7  rt        |d         }|d   }|d   d
k7  rt        |d         }t        |d         }t        |d         }t        |d         }t	        |d         }	t        |d         }
t        |d         }t        |d         }t	        |d         }|j                  d      rt        |d         nd
}t               }|j                  | j                         t        t               t        |       }|t        ||      }nt        |      }|j                  |       |j                          |j                          t!        |      }g }|j#                         }|j%                         s|j'                  |       |j)                         }|j%                         s|j'                  |       |j+                         }|j%                         s|j'                  |       g }|j-                         }|j%                         s|j'                  |       |j/                         }|j%                         s|j'                  |       |D ]  }t1        j2                  |t4                |D ]  }t1        j2                  |t4                t7        t9        t:        |            }t7        t9        t:        |            }t=        ||      \  }}t?        j@                  ||z         jC                         }|d
k(  s|d
k(  rl|d
k(  r)|d|z  z
  |jD                  |jF                  z  z  d|z  z   }n(|d|z  z
  |jF                  |jD                  z  z  d|z  z   }|d|z  z
  |jD                  z  }n.d}tI        ||jD                  z  |z  ||jF                  z  |z        }d|jJ                  z
  ||z  z   d|jL                  z
  ||z  z
  }!} |
dk(  rd|z  }
d}"|r|D ]  }#|"tN        |#z  z  }" d}$|D ]  }#|$tN        |#z  z  }$ |	r1|dk(  r,tP        tS        |      tS        |dz
        tS        |      dz  }%nd}%tT        tS        |      tS        |
      djW                  |D &cg c]  }&tS        |&       c}&      djW                  |D &cg c]  }&tS        |&       c}&      |"|$tS        |       tS        |!      tS        |      tS        |      tS        |dz
        tS        |      |%dz  }'|'S c c}&w c c}&w ) a  
    Export a shape to SVG text.

    :param shape: A CadQuery shape object to convert to an SVG string.
    :type Shape: Vertex, Edge, Wire, Face, Shell, Solid, or Compound.
    :param opts: An options dictionary that influences the SVG that is output.
    :type opts: Dictionary, keys are as follows:
        width: Width of the resulting image (None to fit based on height).
        height: Height of the resulting image (None to fit based on width).
        marginLeft: Inset margin from the left side of the document.
        marginTop: Inset margin from the top side of the document.
        projectionDir: Direction the camera will view the shape from.
        showAxes: Whether or not to show the axes indicator, which will only be
                  visible when the projectionDir is also at the default.
        strokeWidth: Width of the line that visible edges are drawn with.
        strokeColor: Color of the line that visible edges are drawn with.
        hiddenColor: Color of the line that hidden edges are drawn with.
        showHidden: Whether or not to show hidden lines.
        focus: If specified, creates a perspective SVG with the projector
               at the distance specified.
    i            )g      g?   Tg      )r   r   r   )   rV   rV   N)widthheight
marginLeft	marginTopprojectionDirshowAxesstrokeWidthstrokeColorhiddenColor
showHiddenfocusrW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   g       @g      ?r   g      ?    )	unitScaletextboxYuom,)rd   r]   r^   r_   hiddenContentvisibleContent
xTranslate
yTranslaterW   rX   re   rf   axesIndicator),updater)   floattupleboolgetr   Addr   r   r	   r
   r   	ProjectorUpdateHider   	VCompoundIsNullrJ   Rg1LineVCompoundOutLineVCompound	HCompoundOutLineHCompoundr   BuildCurves3d_sr   listmapr   rP   r   makeCompoundBoundingBoxr    r!   r$   xminymaxPATHTEMPLATEAXES_TEMPLATEstrSVG_TEMPLATEjoin)(r&   optsdrf   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   hlrcoordinate_system	projector
hlr_shapesvisiblevisible_sharp_edgesvisible_smooth_edgesvisible_contour_edgeshiddenhidden_sharp_edgeshidden_contour_edgeselrM   rN   r'   rd   bb_scalerj   rk   rh   rF   ri   rl   xsvgs(                                           r   getSVGr      s   2 ( &	A 	 U
#C gJE}aj!x[F{dq{#q'Jan%I!O,-MAjM"H-()K-()K-()Kao&J!"wE!G*TE
.CGGEMMvx)?@%&7?	%&78	MM)JJLHHJ#C(JG$..0%%'*+%668&&(+,&779 ''),-F#--/$$&()%668&&(*+ I. I.  3ug&'G#eV$%F"*7F";[, 
		v/	0	<	<	>B }$D=sY/"''!j !E cJ..277RWW3DEiWF S:--8	(2FRWW4Dx4OP	 
RWW
Y..	
RWW	I-- Z dIo M A\A--M  N,**  M_4%i.c&2+6FsSVxX
 
Y{+88[$A[SV[$AB88[$A[SV[$AB*,j/j/Z&kFRK(s8*	
C$ J %B$As   	T0TfileNamec                     t        | j                         |      }t        |d      }|j                  |       |j	                          y)z
    Accept a cadquery shape, and export it to the provided file
    TODO: should use file-like objects, not a fileName, and/or be able to return a string instead
    export a view of a part to svg
    wN)r   valopenr;   close)r&   r   r   r   fs        r   	exportSVGr   7  s6     d
#CXsAGGCLGGIr   )N) ior2   shapesr   r   r   geomr   OCP.gpr   r	   r
   OCP.BRepLibr   OCP.HLRBRepr   r   OCP.HLRAlgor   
OCP.GCPntsr   r6   r   r   r   r   r)   rG   rP   r   r   r   r   r   r   <module>r      sl     / /  * )  8 ) 4 0 ) 
,2'&tn
s 
r   