Ë
    Jÿi:1  ã                   ó˜  — d Z ddlZddlmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZm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 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% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z< ddl=m>Z> ddl?m@Z@mAZA ddlBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZL e-eCe.eDe/eEe1eHe2eIe3eJe4eKiZMdeeNz  eOz  deFfd„ZPdeeNz  eOz  deCfd„ZQdeeNz  eOz  deEfd„ZRdeeNz  eOz  deSeNeNf   fd „ZTd!d"d#dd$œd%eNe	z  ez  d&eUd'eUd(ed)   eNz  d*eUdz  deGeKeEz     fd+„ZVy),aì  
build123d imports

name: importers.py
by:   Gumyr
date: March 1st, 2023

desc:
    This python module contains importers from multiple file formats.

license:

    Copyright 2022 Gumyr

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

é    N)ÚPathLikeÚfsdecode)Údegrees)ÚPath)ÚLiteralÚOptionalÚTextIOÚUnion)ÚBRep_Builder)Ú	BRepGProp)Ú	BRepTools)ÚGProp_GProps)ÚQuantity_ColorRGBA)ÚRWStl)ÚSTEPCAFControl_Reader)ÚTCollection_AsciiStringÚTCollection_ExtendedString)ÚTDataStd_Name)Ú	TDF_LabelÚTDF_LabelSequence)ÚTDocStd_Document)ÚTopAbs_FACE)ÚTopExp_Explorer)ÚTopoDS_CompoundÚTopoDS_EdgeÚTopoDS_FaceÚTopoDS_ShapeÚTopoDS_ShellÚTopoDS_SolidÚTopoDS_VertexÚTopoDS_Wire)ÚXCAFDoc_ColorCurvÚXCAFDoc_ColorGenÚXCAFDoc_ColorSurfÚXCAFDoc_DocumentTool)ÚColorAndLabelÚimport_svg_document)Ú	svg2paths)ÚColorÚLocation)
ÚCompoundÚEdgeÚFaceÚShapeÚ	ShapeListÚShellÚSolidÚVertexÚWireÚdowncastÚ	file_nameÚreturnc                 óÔ   — t        «       }t        «       }t        | «      }t        j                  |||«       |j                  «       rt        d|› �«      ‚t        j                  |«      S )zÆImport shape from a BREP file

    Args:
        file_name (Union[PathLike, str, bytes]): brep file

    Raises:
        ValueError: file not found

    Returns:
        Shape: build123d object
    zCould not import )	r   r   r   r   ÚRead_sÚIsNullÚ
ValueErrorr+   Úcast)r5   ÚshapeÚbuilderÚfile_name_strs       úd/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/build123d/importers.pyÚimport_brepr@   b   sY   € ô ‹N€EÜ‹n€Gä˜YÓ'€MÜ×Ñ�U˜M¨7Ô3à‡|�|„~ÜÐ,¨]¨OÐ<Ó=Ð=ä�=‰=˜ÓÐó    Úfilenamec                 óê  ‡‡‡‡‡	— dt         dt        fd„Šdt        dt        fˆfd„Šddt         dz  dt        t
           fˆˆˆˆ	fd„Št        j                  j                  | «      st        | «      ‚t        d	«      }t        |«      }t        j                  |j                  «       «      Š	t        j                  |j                  «       «      Št!        «       }|j#                  d
«       |j%                  d
«       |j'                  d
«       |j)                  t+        | «      «       |j-                  |«       t/        «       } ‰«       |_        t3        |j0                  «      dk(  r|j0                  d   }|S )a  import_step

    Extract shapes from a STEP file and return them as a Compound object.

    Args:
        file_name (Union[PathLike, str, bytes]): file path of STEP file to import

    Raises:
        ValueError: can't open file

    Returns:
        Compound: contents of STEP file
    Úlabelr6   c                 ó*  — d}t        «       }| j                  t        j                  «       |«      r't        |j	                  «       «      j                  «       }dj                  d„ |D «       «      }|j                  t        j                  dd«      «      S )zExtract name and formatÚ c              3   óX   K  — | ]"  }t        j                  |«      d    dk7  sŒ|–— Œ$ y­w)r   ÚCN)ÚunicodedataÚcategory)Ú.0Úchs     r?   Ú	<genexpr>z0import_step.<locals>.get_name.<locals>.<genexpr>�   s)   è ø€ ÐU©$ B´+×2FÑ2FÀrÓ2JÈ1Ñ2MÐQTÓ2TœR©$ùs   ‚ *£*z .()Ú____)
r   ÚFindAttributeÚGetID_sr   ÚGetÚ	ToCStringÚjoinÚ	translateÚstrÚ	maketrans)rD   ÚnameÚstd_nameÚ
clean_names       r?   Úget_namezimport_step.<locals>.get_name‰   sp   € àˆÜ “?ˆØ×Ñœ}×4Ñ4Ó6¸ÔAÜ*¨8¯<©<«>Ó:×DÑDÓFˆDà—W‘WÑU©$ÓUÓUˆ
Ø×#Ñ#¤C§M¡M°&¸&Ó$AÓBÐBrA   r<   c                 ó¢  •— dt         dt        fˆ	fd„} || «      }i }t        | t        «      }|j	                  «       rp|j                  «       }t        «       }t        j                  ||«       |j                  «       } ||«      }|�|||<   |j                  «        |j	                  «       rŒp|rt        |j                  «       «      d   d   }|S )z9Get the color - take that of the largest Face if multipleÚobjr6   c                 ó¨   •— t        «       }‰j                  | t        |«      s.‰j                  | t        |«      s‰j                  | t        |«      r|S y ©N)r   ÚGetColorr"   r#   r$   )r\   ÚcolÚ
color_tools     €r?   Úget_colz/import_step.<locals>.get_color.<locals>.get_col–   sO   ø€ Ü$Ó&ˆCà×#Ñ# CÔ):¸CÔ@Ø×&Ñ& sÔ,<¸cÔBØ×&Ñ& sÔ,=¸sÔCà�
ð DrA   éÿÿÿÿé   )r   r   r   r   ÚMoreÚCurrentr   r   ÚSurfaceProperties_sÚMassÚNextÚsortedÚitems)
r<   rb   Úshape_colorÚcolorsÚface_explorerÚcurrent_faceÚ
propertiesÚareaÚcolorra   s
            €r?   Ú	get_colorzimport_step.<locals>.get_color“   sÈ   ø€ ð	œð 	Ô*<õ 	ñ ˜e“nˆàˆÜ'¨¬{Ó;ˆØ× Ñ Ô"Ø(×0Ñ0Ó2ˆLÜ%›ˆJÜ×)Ñ)¨,¸
ÔCØ—?‘?Ó$ˆDÙ˜LÓ)ˆEØÐ Ø$��t‘Ø×ÑÔ ð × Ñ Õ"ñ Ü  §¡£Ó0°Ñ4°QÑ7ˆKàÐrA   NÚparent_tdf_labelc                 óž  •— t        «       }| €‰j                  |«       n‰j                  | |«       g }t        |j	                  «       «      D ]ý  }|j                  |dz   «      }‰j                  |«      rt        «       }‰j                  ||«       n|}t        ‰j                  |«      «      }‰j                  |«      rt        «       } ‰|«      |_        nt        t        |«         |«      }t!         ‰	|«      «      |_         ‰
|«      |_        |j'                  t)        ‰j+                  |«      «      «       |j-                  |«       Œÿ |S )z+Recursively extract object into an assemblyrd   )r   ÚGetFreeShapesÚGetComponents_sÚrangeÚLengthÚValueÚIsReference_sr   ÚGetReferredShape_sr4   Ú
GetShape_sÚIsAssembly_sr+   ÚchildrenÚ
topods_lutÚtyper)   rr   rD   Úmover*   ÚGetLocation_sÚappend)rt   Úsub_tdf_labelsÚ
sub_shapesÚiÚsub_tdf_labelÚref_tdf_labelÚsub_topo_shapeÚ	sub_shapeÚbuild_assemblyrs   rZ   Ú
shape_tools           €€€€r?   rŒ   z#import_step.<locals>.build_assembly³   s%  ø€ ä*Ó,ˆØÐ#Ø×$Ñ$ ^Õ4à×&Ñ&Ð'7¸ÔHà"$ˆ
Ü�~×,Ñ,Ó.Ö/ˆAØ*×0Ñ0°°Q±Ó7ˆMØ×'Ñ'¨Ô6Ü )£�Ø×-Ñ-¨m¸]ÕKà -�ä% j×&;Ñ&;¸MÓ&JÓKˆNØ×&Ñ& }Ô5Ü$›J�	Ù%3°MÓ%B�	Õ"ä&¤t¨NÓ';Ñ<¸^ÓL�	ä#¡I¨nÓ$=Ó>ˆIŒOÙ& }Ó5ˆIŒOØ�N‰Nœ8 J×$<Ñ$<¸]Ó$KÓLÔMà×Ñ˜iÕ(ð' 0ð( ÐrA   ÚXCAFTrd   r   r^   )r   rU   r   r   Úlistr.   ÚosÚpathÚexistsÚFileNotFoundErrorr   r   r%   ÚShapeTool_sÚMainÚColorTool_sr   ÚSetNameModeÚSetColorModeÚSetLayerModeÚReadFiler   ÚTransferr+   r   Úlen)
rB   ÚfmtÚdocÚreaderÚrootrŒ   ra   rs   rZ   r�   s
        @@@@@r?   Úimport_stepr¡   z   s0  ü€ ðCœ	ð C¤có Cðœð Ô*<õ ñ@¬°TÑ)9ð ÄTÌ%Á[÷ ð ô> �7‰7�>‰>˜(Ô#Ü Ó)Ð)ä
$ VÓ
,€CÜ
˜3Ó
€CÜ%×1Ñ1°#·(±(³*Ó=€JÜ%×1Ñ1°#·(±(³*Ó=€JÜ"Ó$€FØ
×Ñ�tÔØ
×Ñ˜ÔØ
×Ñ˜ÔØ
‡O�O”H˜XÓ&Ô'Ø
‡O�O�CÔä‹:€DÙ"Ó$€D„Mä
ˆ4�=‰=Ó˜QÒØ�}‰}˜QÑˆà€KrA   c                 ó´   — t        j                  t        | «      «      }t        «       }t	        «       j                  ||«       t        j                  |«      }|S )aÕ  import_stl

    Extract shape from an STL file and return it as a Face reference object.

    Note that importing with this method and creating a reference is very fast while
    creating an editable model (with Mesher) may take minutes depending on the size
    of the STL file.

    Args:
        file_name (Union[PathLike, str, bytes]): file path of STL file to import

    Raises:
        ValueError: Could not import file

    Returns:
        Face: STL model
    )r   Ú
ReadFile_sr   r   r   ÚMakeFacer-   r;   )r5   rŸ   ÚfaceÚstl_objs       r?   Ú
import_stlr§   é   sD   € ô& ×Ñœh yÓ1Ó2€FÜ‹=€DÜƒN×Ñ˜D &Ô)Ü�i‰i˜‹o€GØ€NrA   c                 óê  — g d¢g d¢g d¢g d¢dœ}t        | «      } t        | «      }|d   |d   }}t        j                  j	                  | «      j                  d«      d   }|j                  «       r|nd	}d
d|› d�g}|D �]Õ  }|D �]Ì  }t        |«      j                  }	|	dk(  �r|j                  d   g}
|
j                  |j                  d   j                  «       |
j                  |j                  d   j                  «       t        |j                  d   |j                  d   |j                  d   z   g«      \  }}|
j                  |«       |
j                  |«       |
j                  t        |j                  d   «      «       |j                  d   dk  r|
j                  d«       n4|
j                  d«       n"||	   dd D �cg c]  }|j                  |   ‘Œ }
}dj                  |
D �cg c]:  }t!        |t"        «      rd|j                  › d|j                  › d�n
t%        |«      ‘Œ< c}«      }|j                  d||	   d   › d|› d�«       �ŒÏ �ŒØ dj                  |«      |fS c c}w c c}w )a	  translate_to_buildline_code

    Translate the contents of the given svg file into executable build123d/BuildLine code.

    Args:
        file_name (Union[PathLike, str, bytes]): svg file name

    Returns:
        tuple[str, str]: code, builder instance name
    )ÚLineÚstartÚend)ÚBezierrª   Úcontrol1Úcontrol2r«   )r¬   rª   Úcontrolr«   )ÚEllipticalCenterArcrª   r«   ÚradiusÚrotationÚ	large_arcÚsweep)r©   ÚCubicBezierÚQuadraticBezierÚArcr   rd   Ú.r=   zfrom build123d import *zwith BuildLine() as Ú:r·   Úcenterr±   ÚthetaÚdeltaÚphig        zAngularDirection.CLOCKWISEz"AngularDirection.COUNTER_CLOCKWISENÚ,Ú(z, Ú)z    Ú
)r   r(   r�   r‘   ÚbasenameÚsplitÚisidentifierr�   Ú__name__Ú__dict__r„   ÚrealÚimagrj   r   rS   Ú
isinstanceÚcomplexrU   )r5   Ú
translatorÚ
paths_infoÚpathsÚ_path_attributesÚbuilder_nameÚbuildline_coder‘   ÚcurveÚ
class_nameÚvaluesrª   r«   ÚparmÚvÚ
values_strs                   r?   Úimport_svg_as_buildline_coder×     sk  € ò )ÚIÚ@ò	
ñ	€Jô ˜Ó#€IÜ˜9Ó%€JØ(¨™m¨Z¸©]Ð€EÜ—7‘7×#Ñ# IÓ.×4Ñ4°SÓ9¸!Ñ<€LØ#/×#<Ñ#<Ô#>‘<ÀI€Là!Ø
˜|˜n¨AÐ.ð€Nô ˆÜˆEÜ˜e›×-Ñ-ˆJØ˜UÓ"ØŸ.™.¨Ñ2Ð3�Ø—‘˜eŸn™n¨XÑ6×;Ñ;Ô<Ø—‘˜eŸn™n¨XÑ6×;Ñ;Ô<Ü#àŸ™ wÑ/ØŸ™ wÑ/°%·.±.ÀÑ2IÑIðó‘
��sð —‘˜eÔ$Ø—‘˜cÔ"Ø—‘œg e§n¡n°UÑ&;Ó<Ô=Ø—>‘> 'Ñ*¨SÒ0Ø—M‘MÐ">Õ?à—M‘MÐ"FÕGð <FÀjÑ;QÐRSÐRTÑ;UÓVÑ;U°4˜%Ÿ.™.¨Ó.Ð;U�ÐVØŸ™ñ $óá#˜ô 0:¸!¼WÔ/E�a˜Ÿ™�x˜r !§&¡& ¨Ñ+Ì3ÈqË6ÑQØ#ñóˆJð ×!Ñ! D¨°JÑ)?ÀÑ)BÐ(CÀ1ÀZÀLÐPQÐ"RÖSòC ð ðH �I‰I�nÓ% |Ð4Ð4ùò Wùòs   ÇI+Ç.?I0TFÚid)Úflip_yÚignore_visibilityÚlabel_byÚis_inkscape_labelÚsvg_filerÙ   rÚ   rÛ   )rØ   Úclasszinkscape:labelrÜ   c                óN  — |�)d}|rd|z   }|d|›d�z  }t        j                  |d¬«       g }t        j                  dd|«      }t	        | ||t        j                  |«      ¬	«      D ]²  \  }}t        |t        «      rt        |«      }	n=t        |t        «      rt        |«      }	n!t        d
t        |«      j                  › �«      ‚|	j                  r&t!        |j#                  |	j                  «      Ž |	_        |j&                  |	_        |j)                  |	«       Œ´ t+        |«      S )aR  import_svg

    Args:
        svg_file (Union[str, Path, TextIO]): svg file
        flip_y (bool, optional): flip objects to compensate for svg orientation. Defaults to True.
        ignore_visibility (bool, optional): Defaults to False.
        label_by (str, optional): XML attribute to use for imported shapes' `label` property.
            Defaults to "id".
            Use `inkscape:label` to read labels set from Inkscape's "Layers and Objects" panel.

    Raises:
        ValueError: unexpected shape type

    Returns:
        ShapeList[Union[Wire, Face]]: objects contained in svg
    z+`is_inkscape_label` parameter is deprecatedz	inkscape:z, use `label_by=z	` insteadé   )Ú
stacklevelz^inkscape:(.+)z/{http://www.inkscape.org/namespaces/inkscape}\1)rÙ   rÚ   Úmetadatazunexpected shape type: )ÚwarningsÚwarnÚreÚsubr'   r&   ÚLabel_byrÉ   r!   r3   r   r-   r:   r�   rÅ   Úwrappedr)   Ú	color_forrr   rD   r„   r/   )
rÝ   rÙ   rÚ   rÛ   rÜ   ÚmsgÚshapesÚface_or_wireÚcolor_and_labelr<   s
             r?   Ú
import_svgrî   P  s  € ð0 Ð$Ø;ˆÙØ" XÑ-ˆHØÐ% h \°Ð;Ñ;ˆCÜ�‰�c aÕ(à€FÜ�v‰vØÐMÈxó€Hô *=ØØØ+Ü×'Ñ'¨Ó1÷	*Ñ%ˆ�oô �l¤KÔ0Ü˜Ó&‰EÜ˜¤kÔ2Ü˜Ó&‰EäÐ6´t¸LÓ7I×7RÑ7RÐ6SÐTÓUÐUà�=Š=Ü ×!:Ñ!:¸5¿=¹=Ó!IÐJˆEŒKØ%×+Ñ+ˆŒØ�‰�eÕð!*ô$ �VÓÐrA   )WÚ__doc__r�   r   r   rå   rI   Úmathr   Úpathlibr   Útypingr   r   r	   r
   rã   ÚOCP.BRepr   ÚOCP.BRepGPropr   ÚOCP.BRepToolsr   Ú	OCP.GPropr   ÚOCP.Quantityr   Ú	OCP.RWStlr   ÚOCP.STEPCAFControlr   ÚOCP.TCollectionr   r   ÚOCP.TDataStdr   ÚOCP.TDFr   r   ÚOCP.TDocStdr   Ú
OCP.TopAbsr   Ú
OCP.TopExpr   Ú
OCP.TopoDSr   r   r   r   r   r   r    r!   ÚOCP.XCAFDocr"   r#   r$   r%   Úocpsvgr&   r'   Úsvgpathtoolsr(   Úbuild123d.geometryr)   r*   Úbuild123d.topologyr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r€   rU   Úbytesr@   r¡   r§   Útupler×   Úboolrî   © rA   r?   Ú<module>r
     s�  ðñó> 
ß !Û 	Û Ý Ý ß 3Ó 3Û å !Ý #Ý #Ý "Ý +Ý Ý 4ß OÝ &ß 0Ý (Ý "Ý &÷	÷ 	ó 	÷ó ÷ 6Ý "ç .÷÷ ÷ ð �XØ�Ø�Ø�%Ø�%Ø�6Ø�ð€
ð ˜8 c™>¨EÑ1ð  °eó  ð0l˜( S™.¨5Ñ0ð l°Xó lð^˜( S™.¨5Ñ0ð °Tó ð4J5Ø˜#‰~ Ñ%ðJ5à
ˆ3�ˆ8�_óJ5ð` Ø#Ø?CØ%)ò5Ø�D‰j˜6Ñ!ð5ð ð5ð ð	5ð
 Ð5Ñ6¸Ñ<ð5ð ˜d‘{ð5ð ˆt�d‰{Ñô5rA   