
    ^j                         d Z ddlZddlZddl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mZmZmZmZmZmZmZ ddlmZ ded	edz  fd
Zded	efdZded	edz  fdZded	efdZded	efdZded	efdZ d Z!y)a  
build123d pickle support

name: persistence.py
by:   Jojain & bernhard-42
date: September 8th, 2023

desc:
    This python module enables build123d objects to be pickled.

license:

    Copyright 2023 Jojain & bernhard-42

    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)BinTools)gp_Quaterniongp_Trsfgp_Vec)TopLoc_Location)	TopoDS_CompoundTopoDS_CompSolidTopoDS_EdgeTopoDS_FaceTopoDS_ShapeTopoDS_ShellTopoDS_SolidTopoDS_VertexTopoDS_Wire)downcastshapereturnc                     | yt        j                         }t        j                  | |       |j	                         }|S )zj
    Serialize a OCP shape, this method can be used to provide a custom serialization algo for pickle
    N)ioBytesIOr   Write_sgetvalue)r   biobuffers      N/opt/ringagent/.cad-venv/lib/python3.12/site-packages/build123d/persistence.pyserialize_shaper   6   s7     }
**,CUC \\^FM    r   c                     | yt               }t        j                  |       }t        j                  ||       t        |      S )zV
    This does the opposite as serialize, it construct a TopoDS_Shape from bytes.
    N)r   r   r   r   Read_sr   )r   r   r   s      r   deserialize_shaper    C   s9     ~NE
**V
COOE3E?r   locationc           	         | y| j                         }|j                         }|j                         }t               }t	        dd      D ]6  }|j                  t        j                  d|j                  |                   8 t               }|j                  t        j                  d|j                                      |j                  t        j                  d|j                                      |j                  t        j                  d|j                                      |j                  t        j                  d|j                                      t               }|j                  |       |j                  |       |S )zq
    Serialize a OCP location, this method can be used to provide
    a custom serialization algo for pickle
    N      f)TransformationTranslationPartGetRotation	bytearrayrangeextendstructpackCoordXYZW)r!   	transformtranslationrotationtranslation_bytesirotation_bytesr   s           r   serialize_locationr9   P   s"   
 '')I++-K$$&H!1a[ I  S+2C2CA2F!GHI[N&++c8::<89&++c8::<89&++c8::<89&++c8::<89[F
MM#$
MM.!Mr   c                 r   | yt        t        j                  d|             }t        t	        |      d   t	        |      d   t	        |      d         }t        t	        |      d   t	        |      d   t	        |      d   t	        |      d         }t               }|j                  ||       t        |      S )zY
    This does the opposite as serialize, it construct a TopLoc_Location from bytes.
    Nr%   r   )	iterr,   iter_unpackr   nextr   r   SetTransformationr   )r   chunksr4   r5   r3   s        r   deserialize_locationr@   j   s     ~ &$$S&12FVQVQVQK VQVQVQVQ	H 	I+69%%r   c                 &    t         t        |       ffS )zNSpecial function used by pickle to serialize or deserialize OCP Shapes objects)r    r   )r   s    r   reduce_shaperB      s     6899r   c                 &    t         t        |       ffS )zPSpecial function used by pickle to serialize or deserialize OCP Location objects)r@   r9   )r!   s    r   reduce_locationrD      s     #5h#?"ABBr   c                  \   t        j                  t        t               t        j                  t        t               t        j                  t
        t               t        j                  t        t               t        j                  t        t               t        j                  t        t               t        j                  t        t               t        j                  t        t               t        j                  t        t               t        j                  t        t               y)zg
    Modify the copyreg so that pickle knows what to look for when it tries to pickle an OCP Shape
    N)copyregpickler   rB   r   r	   r   r   r   r   r
   r   r   rD    r   r   modify_copyregrI      s     NN<.NN?L1NN#\2NN<.NN<.NN;-NN;-NN;-NN=,/NN?O4r   )"__doc__rF   r   r,   OCP.BinToolsr   OCP.gpr   r   r   
OCP.TopLocr   
OCP.TopoDSr   r	   r
   r   r   r   r   r   r   build123d.topologyr   bytesr   r    r)   r9   r@   tuplerB   rD   rI   rH   r   r   <module>rR      s   >  	  ! 1 1 &
 
 
 (
< 
EDL 

e 
 
 Y5E 4& &? &<: : :
Co C% C
5r   