
    vj6                        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 d dlmZ d d	lmZmZ d d
lmZ d dlmZmZmZmZ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'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- dede.fdZ/dede)dz  fdZ0dede(dz  fdZ1dedede(dz  fdZ2de'de.fd Z3de'de.fd!Z4de'de.fd"Z5d#ede'fd$Z6y)%    )cast)Path)TopoDS_Shape)TCollection_ExtendedString)Quantity_ColorRGBA)	TDF_LabelTDF_LabelSequence)IFSelect_RetDone)TDocStd_Document)TDataStd_NameTDataStd_TreeNode)STEPCAFControl_Reader)XCAFDoc_ColorSurfXCAFDoc_DocumentToolXCAFDoc_ColorToolXCAFDocXCAFDoc_ColorTypeXCAFDoc_Material)TDocStd_Application)XmlXCAFDrivers)BinXCAFDrivers)Interface_Static)PCDM_ReaderStatus   )AssemblyProtocolColorMaterial)Location)Shapelabelreturnc                     d}t               }| j                  t        j                               rK| j                  t        j                         |       t	        |j                         j                               }|S )z2
    Helper to get the name of a given label.
     )r   IsAttributeGetID_sFindAttributestrGetToExtString)r    rv	name_attrs      Q/DATA/.local/lib/python3.12/site-packages/cadquery/occ_impl/importers/assembly.py	_get_namer-      s^    
 
BI..01M113Y?,,./I    Nc                    d}t        j                         }| j                  |      rt               }| j	                  ||       |j                         j                         }t               }|j	                  t        j                         |       |j                         j                         }|j                         j                         }|j                         }|j                         j                         }	t        ||||	      }|S )z7
    Helper to get the material for a given label.
    N)namedescriptiondensitydensityUnit)r   MaterialRefGUID_sr$   r   r&   FatherLabelr   r%   GetName	ToCStringGetDescription
GetDensityGetDensValTyper   )
r    r*   material_ref_guidattrmaterial_labelmaterial_attrr0   r1   r2   density_units
             r,   _get_materialrA   .   s    
 
B113*+ "-t4,,.(*$$%5%=%=%?O$$&002#224>>@**,$335??A#$	
 Ir.   c                    t        j                  t        j                        }t        j                  t        j                        }t               }| j                  |      r| j                  ||       |j                         j                         }t               }t        j                  ||       |j                         }t        |j                         |j!                         |j#                         |j%                         d      }|S | j                  |      r| j                  ||       |j                         j                         }t               }t        j                  ||       |j                         }t        |j                         |j!                         |j#                         |j%                         d      }|S d}|S )z<
    Helper to get the instance color of a given label.
    FN)r   ColorRefGUID_sr   r   XCAFDoc_ColorGenr   r$   r&   r5   r6   r   r   
GetColor_sGetRGBr   RedGreenBlueAlpha)r    color_ref_guidcolor_ref_guid_genericr=   color_labelcolorrgbr*   s           r,   _get_ref_colorrP   N   sW   
 ++,=,O,OPN$334E4V4VWD(ND1kkm))+"$$$[%8lln3779ciik388:u{{}eL I 
		1	22D9kkm))+"$$$[%8lln3779ciik388:u{{}eL
 I Ir.   s
color_toolc                     t               }|j                  | t        |      rY|j                         }t	        |j                         |j                         |j                         |j                         d      }|S d}|S )z9
    Helper to get the shape color of a given shape.
    FN)	r   GetColorr   rF   r   rG   rH   rI   rJ   )rQ   rR   rN   rO   r*   s        r,   _get_shape_colorrU   s   si    
  E 1/7lln3779ciik388:u{{}eL I Ir.   assypathc                 ~   t               }|j                  d       |j                  d       |j                  d       |j	                  d       t        j                  dd       |j                  |      }|t        k7  rt        d|       t        t        d            }|j                  |       t        ||        y)z
    Import a step file into an assembly.

    :param assy: An Assembly object that will be packed with the contents of the STEP file.
    :param path: Path and filename to the STEP file to read.

    :return: None
    Tzread.stepcaf.subshapes.name   zError reading STEP file: XmXCAFN)r   SetColorModeSetNameModeSetLayerModeSetSHUOModer   	SetIVal_sReadFiler
   
ValueErrorr   r   Transfer
_importDoc)rV   rW   step_readerstatusdocs        r,   
importSteprg      s     ()KT"D!T"D!<a@ !!$'F!!4TF;<< 5h?
@C sDr.   c           	      r   t               }t        j                  |       t        |      j	                         j                         \  }}t        t        |j                  t        |      t        |                  }|j                         }|t        j                  k(  sJ d| d|        t        ||        y)z
    Import an xbf file into an assembly.

    :param assy: An Assembly object that will be packed with the contents of the STEP file.
    :param path: Path and filename to the xbf file to read.

    :return: None
    Opening of file 	 failed: N)r   r   DefineFormat_sr   absolute	splitpathr   r   Retriever   GetRetrieveStatusr   
PCDM_RS_OKrc   rV   rW   appdirnamefnamerf   re   s          r,   	importXbfru           
C!!#&$Z((*446NGU
&w/1KE1R	
C ""$F#...2	$y12. sDr.   c           	      r   t               }t        j                  |       t        |      j	                         j                         \  }}t        t        |j                  t        |      t        |                  }|j                         }|t        j                  k(  sJ d| d|        t        ||        y)z
    Import an xcaf xml file into an assembly.

    :param assy: An Assembly object that will be packed with the contents of the STEP file.
    :param path: Path and filename to the xml file to read.

    :return: None
    ri   rj   N)r   r   rk   r   rl   rm   r   r   rn   r   ro   r   rp   rc   rq   s          r,   	importXmlrx      rv   r.   rf   c                 j   dt         dt        ffdt        j                  | j	                               t        j
                  | j	                               t        j                  | j	                               t               }j                  |       |j                  d      }t        |      }j                  |      rt               }j                  ||       |}j                  |      rj                  |      rt               }|j!                  t        j"                         |       j$                  j'                  j(                         t+        |j-                         j/                               _        j$                  j(                  <   |r|_        j3                  |      }t5        |      }|j6                  j9                         s#|j;                         dk(  rt5               _        n|_        j?                         }	 ||	       j(                  |	v r.tA        t        |	j(                           }	|	j<                  _        |	jB                  D ]5  }
jE                  |
|
j(                  |
j0                  |
j<                         7 y tG        d      )Nlblparentc                    t               }j                  | |       t        |j                               D ]  }|j	                  |dz         }t        |      }j                  |      }|rt        |      n	t               }j                  |      s]t               }j                  ||       t        |      }	t        |      }
j                  |      r=t        |      }j                  |      } ||      }|j                  ||||	|
       ݉j!                  |      st        |      }j#                  |      }t%        j&                  |      }|	t)        |      }	|
t        |      }
|j+                  d      r||_        ||_        |	|_        |}n:j                  ||||	|
      }|j                  |       t'        t2        ||         }t               }j5                  ||       |D ]  }j#                  |      }t        |      }|r&|j7                  t%        j&                  |      |       t               }j9                  ||       |D ]3  } t        |       }|j7                  t%        j&                  |      |       5 t        |      }	t        |      }
|	s|j7                  t%        j&                  |      |	         |S )zP
        Recursive method to process the assembly in a top-down manner.
        rY   )r0   )locr0   rN   material_part)layer)rN   )r	   GetComponents_srangeLengthValuer-   GetLocation_sr   IsReference_sr   GetReferredShape_srP   rA   IsAssembly_s	__class__addIsSimpleShape_s
GetShape_sr   r   rU   endswithobjr}   rN   r   GetSubShapes_saddSubshape	GetLayers)rz   r{   comp_labelsi
comp_label	comp_namer}   cq_loc	ref_labelrN   r~   ref_namesub_assy_final_shapecq_shapecurrenttmpsubshape_labelschild_label	cur_shape
child_namelayers
layer_name_process_labelrV   rR   
layer_tool
shape_tools                           r,   r   z"_importDoc.<locals>._process_label   s    ()""34{))+,A$**1q51J!*-I **:6C&)Xc]xzF''
3%K	--j)D 'z2(4**95(3H#~~8~<H 'y(;A JJ "%#!)    //	:(4H #-"7"7	"BK$zz+6H } 0j I'#0#;
  ((1%-
%+
', #)"nn$ &!*"'%- -  

3 #''7	9J"K '8&9O--iI'6 3=2G2G2T	 &/{%;
%#// %

9 5J 0 
 "3!4",,[&A#)C)23J $//

90EZ/X $* !/{ ;#0#= #//

90EU/SA (7Y -\ r.   rY   )r   r   r   r   )r0   rN   r}   z&Step file does not contain an assembly)$r   r   r   ShapeTool_sMainColorTool_sLayerTool_sr	   GetFreeShapesr   rP   r   r   
IsTopLevelr   r   r&   r%   objectspopr0   r'   r(   r)   rN   r   r   wrapped
IsIdentitytoTupler}   r   r   childrenr   ra   )rf   rV   labelstop_level_labelcq_colorr   r+   r}   r   imported_assychildr   rR   r   r   s    `         @@@@r,   rc   rc      s/   wI w/? w wt &11#((*=J%11#((*=J%11#((*=J  FV$ ll1oOo.H 0k%%os; _-*2I2I3 "O	%%m&;&;&=yI 	#	3356	"&TYY!DJ &&7#~~((*v~~/? D
 0
  zDHDH (6 99% !1=3KLM
 %((DH #++EHHU5;;EIIHN , ABBr.   )7typingr   rW   r   
OCP.TopoDSr   OCP.TCollectionr   OCP.Quantityr   OCP.TDFr   r	   OCP.IFSelectr
   OCP.TDocStdr   OCP.TDataStdr   r   OCP.STEPCAFControlr   OCP.XCAFDocr   r   r   r   r   r   r   OCP.XmlXCAFDriversr   OCP.BinXCAFDriversr   OCP.Interfacer   OCP.PCDMr   assemblyr   r   r   geomr   shapesr   r'   r-   rA   rP   rU   rg   ru   rx   rc    r.   r,   <module>r      s     # 6 + 0 ) ( 9 4  , - - * & 8 8  Y 3  x$ @") " "J 2C PT "% S B$ C :$ C :C$ C,< Cr.   