
    vj:                     Z   d dl 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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 d dlmZ d dlmZ d dl m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z'm(Z( d dl)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z?  G d d      Z@e
d   ZA	 d0d e7d!eBd"eAd#eCfd$ZD	 	 d1d e7d!eBd%eCd&eEd#eCf
d'ZFd2d e7d!eBd(eCd#eCfd)ZG	 d3d e7d*eHd+eHd#efd,ZId e7d!eBfd-ZJ	 	 d3d e7d!eBd*eHd+eHfd.ZK	 	 	 d4d e7d!eBd(eeC   d*eHd+eHf
d/ZLy)5    N)TemporaryDirectory)make_archive)Optional)Literal)vtkJSONSceneExportervtkVRMLExporter)vtkRenderWindow)XSControl_WorkSession)STEPCAFControl_Writer)STEPControl_StepModelType)IFSelect_ReturnStatus)	TDF_Label)TDataStd_Name)TDocStd_Document)XCAFApp_Application)XCAFDoc_DocumentToolXCAFDoc_ColorGen)&XmlXCAFDrivers_DocumentRetrievalDriver$XmlXCAFDrivers_DocumentStorageDriver)&BinXCAFDrivers_DocumentRetrievalDriver$BinXCAFDrivers_DocumentStorageDriver)TCollection_ExtendedStringTCollection_AsciiString)PCDM_StoreStatus)RWGltf_CafWriter)$TColStd_IndexedDataMapOfStringString)Message_ProgressRange)Interface_Static   )AssemblyProtocoltoCAFtoVTK
toFusedCAF)Location)ShapeCompoundc                       e Zd ZdZdZy)ExportModesdefaultfusedN)__name__
__module____qualname__DEFAULTFUSED     Q/DATA/.local/lib/python3.12/site-packages/cadquery/occ_impl/exporters/assembly.pyr(   r(   +   s    GEr1   r(   )r)   r*   assypathmodereturnc                 F   d}d|v r|d   sd}d|v r|d   nd}d|v r|d   nd}d|v r|d   nd}|d	k(  rt        | ||      \  }}	nt        | d
      \  }}	t               }
t        |
d      }|j	                  d
       |j                  d
       |j                  d
       t        j                  d|       t        j                  d|       t        j                  dd       |j                  |	t        j                         |j                  |      }|t        j                  k(  S )a  
    Export an assembly to a STEP file.

    kwargs is used to provide optional keyword arguments to configure the exporter.

    :param assy: assembly
    :param path: Path and filename for writing
    :param mode: STEP export mode. The options are "default", and "fused" (a single fused compound).
        It is possible that fused mode may exhibit low performance.
    :param fuzzy_tol: OCCT fuse operation tolerance setting used only for fused assembly export.
    :type fuzzy_tol: float
    :param glue: Enable gluing mode for improved performance during fused assembly export.
        This option should only be used for non-intersecting shapes or those that are only touching or partially overlapping.
        Note that when glue is enabled, the resulting fused shape may be invalid if shapes are intersecting in an incompatible way.
        Defaults to False.
    :type glue: bool
    :param write_pcurves: Enable or disable writing parametric curves to the STEP file. Default True.
        If False, writes STEP file without pcurves. This decreases the size of the resulting STEP file.
    :type write_pcurves: bool
    :param precision_mode: Controls the uncertainty value for STEP entities. Specify -1, 0, or 1. Default 0.
        See OCCT documentation.
    :type precision_mode: int
       write_pcurvesr   precision_mode	fuzzy_tolNglueFr*   Twrite.surfacecurve.modewrite.precision.modewrite.stepcaf.subshapes.name)r#   r!   r
   r   SetColorModeSetLayerModeSetNameModer   	SetIVal_sTransferr   STEPControl_AsIsWriter   IFSelect_RetDone)r3   r4   r5   kwargspcurvesr:   r;   r<   _docsessionwriterstatuss                r2   exportAssemblyrO   3   s(   > G& )@1AV1KV,-QRN'2f'<{#$I#v-6&>5D wD$	23tT"3#%G"7E2F


t8'B5~F=qA
OOC2CCD\\$F*;;;;r1   r9   r:   c                    
 d}|sd}t        j                         }t        t        d            }|j	                  |       t        j                  |j                               t        j                  |j                               t        j                  |j                               dt        dt        f fd	 ddt        dt        t           f
fd	
 
        j                          t               }t        |d
      }t!        j"                  dd       |j%                  d       |j'                  d       |j)                  d       t!        j"                  d|       t!        j"                  d|       |j+                  |t,        j.                         |j1                  |      }	|	t2        j4                  k(  S )a%  
    Export an assembly to a STEP file with faces tagged with names and colors. This is done as a
    separate method from the main STEP export because this is not compatible with the fused mode
    and also flattens the hierarchy of the STEP.

    Layers are used because some software does not understand the ADVANCED_FACE entity and needs
    names attached to layers instead.

    :param assy: assembly
    :param path: Path and filename for writing
    :param write_pcurves: Enable or disable writing parametric curves to the STEP file. Default True.
        If False, writes STEP file without pcurves. This decreases the size of the resulting STEP file.
    :param precision_mode: Controls the uncertainty value for STEP entities. Specify -1, 0, or 1. Default 0.
        See OCCT documentation.
    r8   r   XmlOcafchild
assy_labelc                    d}i j                   | j                   }i j                  | j                  }i j                  | j                  }| j                  r|t	        | j                  t
              r| j                  n3t        j                  d | j                  j                         D              | j                  | j                  | j                  f}|r|\  }}}}	j                  |j                  d      }
t        j                  |
t!        |             |	r!j#                  |
|	j                  t$               j'                  ||
|j                        }t        j                  |t!        |             t)        |      dkD  st)        |      dkD  st)        |      dkD  r|}|}|}|j+                         D ]  }||v s	||v s||v sj-                  |
|j                        }|j/                         r=||v r"t        j                  |t!        ||                ||v r$j#                  |||   j                  t$               ||v sj1                  t!        ||               }j3                  ||        yyy)a  
        Process a child part which is not a subassembly.
        :param child: Child part to process (we should already have filtered out subassemblies)
        :param assy_label: The label for the assembly to add this part to
        :return: None
        Nc              3   B   K   | ]  }t        |t              s|  y wN)
isinstancer%   ).0ss     r2   	<genexpr>z9exportStepMeta.<locals>._process_child.<locals>.<genexpr>   s      +/!:a3GA/s   Fr   )_subshape_names_subshape_colors_subshape_layersobjrW   r%   r&   makeCompoundvalsnameloccolorAddShapewrappedr   Set_sr   SetColorr   AddComponentlenFacesAddSubShapeIsNullAddLayerSetLayer)rR   rS   child_itemscombined_namescombined_colorscombined_layersshapera   rb   rc   
part_label
comp_labelnamescolorslayersface
face_labellayer_labelr3   
color_tool
layer_tool
shape_tools                     r2   _process_childz&exportStepMeta.<locals>._process_child   s]     KD00JE4I4IJMT22Me6L6LMMT22Me6L6LM 99 eii/ 		** +$yy~~/+  

			K &1#E4e $,,U]]EBJ
,Ft,LM##J?OP#00ZUJ
,Ft,LM N#a''!+'!+&(( "KKMDu}$&.%/%;%;J%U
  *002#u} - 3 3$.0J5QU;0W!"
  $v~ * 3 3$.t0D0DFV!"
  $v~.8.A.A$>vd|$L/" !+ 3 3J L3 * ,% r1   r3   parent_labelc                    | j                   r| j                   nt        t        j                               }j	                         }t        j                  |t        |             |r'j                  ||| j                  j                         | j                  D ]6  }t        t        |j                              dk(  r
 ||       . ||       8 y)z
        Recursively process the assembly and its children.
        :param assy: Assembly to process
        :param parent_label: The parent label for the assembly
        :return: None
        r   N)ra   struuiduuid1NewShaper   rf   r   rh   rb   re   childrenri   list)r3   r   assembly_namerS   rR   _process_assemblyr   r~   s        r2   r   z)exportStepMeta.<locals>._process_assembly   s     &*YY		C

4E  ((*
J(B=(QR ##L*dhh>N>NO ]]E4'(A-uj1 "%4 #r1   Fr?   Tr=   r>   rV   )r   GetApplication_sr   r   InitDocumentr   ShapeTool_sMainColorTool_sLayerTool_sr    r   r   UpdateAssembliesr
   r   r   rC   r@   rA   rB   rD   r   rE   rF   r   rG   )r3   r4   r9   r:   rI   apprK   rL   rM   rN   r   r   r|   r}   r~   s   `         @@@@@r2   exportStepMetar   n   s   , G 
.
.
0C
5i@
ACS &11#((*=J%11#((*=J%11#((*=JNM. NMI NM NMb EI55.6y.A5: d ! $%G"7E2F=qA


t8'B5~F
OOC2CCD\\$F*;;;;r1   binaryc                    t         j                  j                  |      \  }}t         j                  j                  |      \  }}|d   dk(  r|dd n|}t	        | |      \  }}t        j                         }	|r5t               }
t        d      }t        d      }t               }t               }
n4t        d      }t        d	      }t        t        d
            }t               }
|	j                  ||t        |      |
|       |j                  t        |             |j                  t        |             |	j!                  |t        |            }|	j#                  |       |t$        j&                  k(  S )zP
    Export an assembly to an XCAF xml or xbf file (internal OCCT formats).
    r   .r8   N)r   BinXCAFzBinary XCAF DocumentXmlXCAFzXml XCAF Documentz"Copyright: Open Cascade, 2001-2002)osr4   splitsplitextr!   r   r   r   r   r   r   r   r   DefineFormatSetRequestedFolderSetRequestedNameSaveAsCloser   
PCDM_SS_OK)r3   r4   r   folderfnamera   extrJ   rK   r   retformat_nameformat_descstorerN   s                  r2   	exportCAFr     sI   
 GGMM$'MFE  'ID#Vs]#ab'C4'FAs

.
.
0C 46-i8-.DE4646 .i8-.AB4&'KL
 56["9#">U 5f=>3D9:ZZ7=>FIIcN%0000r1   	toleranceangularTolerancec                     t        | ||      }t               }|j                  |       |j                          |j	                  ddd       |S )zP
    Convert an assembly to a vtkRenderWindow. Used by vtk based exporters.
    )r   r   r8   )r"   r	   AddRendererResetCameraSetBackground)r3   r   r   rendererrenderWindows        r2   _vtkRenderWindowr   A  sM     TYAQRH"$LX&1a#r1   c                     t        |       }t               5 }t               }|j                  |       |j	                  |       |j                          t        |d|       ddd       y# 1 sw Y   yxY w)zU
    Export an assembly to a zipped vtkjs. NB: .zip extensions is added to path.
    zipN)r   r   r   SetFileNameSetRenderWindowrF   r   )r3   r4   r   tmpdirexporters        r2   exportVTKJSr   R  s\    
 $D)L		')V$  .T5&) 
		s   A
A))A2c                     t               }|j                  |       |j                  t        | ||             |j	                          y)z6
    Export an assembly to a vrml file using vtk.
    N)r   r   r   r   rF   )r3   r4   r   r   r   s        r2   
exportVRMLr   b  s=      H-dI?OPQNNr1   c                 Z   |+d}|j                  d      }t        |      dkD  r
|d   dk(  rd}| j                  }| xj                  t        ddd	      z  c_        t	        | dd||      \  }}t        t        |      |      }	|	j                  |t               t                     }
|| _        |
S )
z,
    Export an assembly to a gltf file.
    Tr   r   gltfF)r   r   r   )r8   r   r   i)
r   ri   rb   r$   r!   r   r   Performr   r   )r3   r4   r   r   r   
path_partsorig_locrJ   rK   rM   results              r2   
exportGLTFr   r  s     ~ZZ_
 z?Q:b>V#;F xxHHHIs33H4tY0@AFAs5d;VDF^^135J5LF
 DHMr1   )r)   )Tr   )F)MbP?皙?)Nr   r   )Mos.pathr   r   tempfiler   shutilr   typingr   typing_extensionsr   vtkmodules.vtkIOExportr   r   vtkmodules.vtkRenderingCorer	   OCP.XSControlr
   OCP.STEPCAFControlr   OCP.STEPControlr   OCP.IFSelectr   OCP.TDFr   OCP.TDataStdr   OCP.TDocStdr   OCP.XCAFAppr   OCP.XCAFDocr   r   OCP.XmlXCAFDriversr   r   OCP.BinXCAFDriversr   r   OCP.TCollectionr   r   OCP.PCDMr   
OCP.RWGltfr   OCP.TColStdr   OCP.Messager   OCP.Interfacer   assemblyr    r!   r"   r#   geomr$   shapesr%   r&   r(   STEPExportModeLiteralsr   boolrO   intr   r   floatr   r   r   r   r0   r1   r2   <module>r      s     '   % H 7 / 4 5 .  & ( + > P % ' < - * A A  $ 
 !!34  $-8<
8<
8< !8<
 
8<| 	c<
c<
c< c< 	c<
 
c<L*1$ *1C *1 *1$ *1\ PS
',GL"*& *c *& !	

  	& "!$
$
$ TN$ 	$
 $r1   