
    'j>                       d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlZd dlmZ d dlmZmZmZmZ  ej        d          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zi dddddddddddd d!d"d#d$d%d&d'd(d)d*d+dd,dd-dd.dd/dd0d d"d$d&d1Z h d2Z!d3Z"d4Z#d5Z$dfd8Z%dfd9Z&dgd;Z'dhd=Z(	 did>d?djdEZ)	 did>d>dFdkdJZ*	 dld%dLd>dMdmdPZ+dndRZ,	 	 	 dodpdXZ-dqdZZ.ed[             Z/drd_Z0dsdcZ1dtdeZ2dS )u    )annotations)OptionalN)contextmanager)Path)Drawing)is_dxf_filedxf_infois_binary_dxf_filedwg_versionezdxfc                      e Zd ZdS )
ODAFCErrorN__name__
__module____qualname__     L/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/addons/odafc.pyr   r              Dr   r   c                      e Zd ZdS )UnknownODAFCErrorNr   r   r   r   r   r       r   r   r   c                      e Zd ZdS )ODAFCNotInstalledErrorNr   r   r   r   r   r   $   r   r   r   c                      e Zd ZdS )UnsupportedFileFormatNr   r   r   r   r   r   (   r   r   r   c                      e Zd ZdS )UnsupportedPlatformNr   r   r   r   r   r   ,   r   r   r   c                      e Zd ZdS )UnsupportedVersionNr   r   r   r   r    r    0   r   r   r    R12ACAD12R13ACAD13R14ACAD14R2000ACAD2000R2004ACAD2004R2007ACAD2007R2010ACAD2010R2013ACAD2013R2018ACAD2018AC1004ACAD9AC1006ACAD10AC1009AC1012AC1014AC1015AC1018AC1021)AC1024AC1027AC1032>   r4   r6   r"   r$   r&   r(   r*   r,   r.   r0   r2   WindowsLinuxDarwinreturnstrc                 h    t           j                            dd                              d          S )Nodafc-addonwin_exec_path"r   optionsgetstripr   r   r   get_win_exec_pathrM   ^   s(    =]O<<BB3GGGr   c                 h    t           j                            dd                              d          S )NrF   unix_exec_pathrH   rI   r   r   r   get_unix_exec_pathrP   b   s)    =],<==CCCHHHr   boolc                 ,   t          j                    t          t          fv rIt	                      } | r#t          |                                           rdS t          j        d          duS t          j
                            t                                S )z6Returns ``True`` if the ODAFileConverter is installed.TODAFileConverterN)platformsystemLINUXDARWINrP   r   is_fileshutilwhichospathexistsrM   )rO   s    r   is_installedr^   f   s}    UFO+++-- 	d>22::<< 	4|.//t;;7>>+--...r   versionc                    t                               |                                 |                                           S N)VERSION_MAPrK   upper)r_   s    r   map_versionrd   q   s&    ??7==??GMMOO<<<r   F)auditfilenamestr | os.PathLikeOptional[str]re   r   c          	        t          |                                           }|                                st          d| d          t	          |t
                    rt          |          }nt          |           }t          j	        d          5 }t          |j        t          |j                  |d||          }t          |           t          |          |                    d          j        z  }|                                rVt!          j        t          |                    }t          |                    d                    |_        |cddd           S 	 ddd           n# 1 swxY w Y   t'          d	          )
a  Uses an installed `ODA File Converter`_ to convert a DWG/DXB/DXF file
    into a temporary DXF file and load this file by `ezdxf`.

    Args:
        filename: file to load by ODA File Converter
        version: load file as specific DXF version, by default the same version
            as the source file or if not detectable the latest by `ezdxf`
            supported version.
        audit: audit source file before loading

    Raises:
        FileNotFoundError: source file not found
        odafc.UnknownODAFCError: conversion failed for unknown reasons
        odafc.UnsupportedVersion: invalid DWG version specified
        odafc.UnsupportedFileFormat: unsupported file extension
        odafc.ODAFCNotInstalledError: ODA File Converter not installed

    zNo such file: ''odafc_prefixDXFoutput_formatr_   re   .dxfNz%Failed to convert file: Unknown Error)r   absoluterX   FileNotFoundError
isinstancerD   rd   _detect_versiontempfileTemporaryDirectory_odafc_argumentsnameparent_execute_odafcwith_suffixr]   r   readfilerf   r   )rf   r_   re   infiletmp_dirargsout_filedocs           r   r}   r}   u   s   * (^^$$&&F>> = ;& ; ; ;<<<'3 ,g&&!(++		$H	5	5	5 K
 
 
 	t==6#5#5f#=#=#BB?? 	.X//Cv11&99::CL       	               C
D
DDs   CE//E36E3)re   replacer   r   Nonec          	        || j         }t          |         }t          |                                          }t          |j                  }|                                r)|r|                                 nt          d|           |                                rt          j	        d          5 }t          |          |
                    d          j        z  }	| j        }
|                     |	           |
| _        t          |	j        |t          |          d||          }t!          |           ddd           dS # 1 swxY w Y   dS t#          dt          |           d	          )
a  Uses an installed `ODA File Converter`_ to export the DXF document `doc`
    as a DWG file.

    A temporary DXF file will be created and converted to DWG by the
    ODA File Converter. If `version` is not specified the DXF version of the
    source document is used.

    Args:
        doc: `ezdxf` DXF document as :class:`~ezdxf.drawing.Drawing` object
        filename: output DWG filename, the extension will be set to ".dwg"
        version: DWG version to export, by default the same version as
            the source document.
        audit: audit source file by ODA File Converter at exporting
        replace: replace existing DWG file if ``True``

    Raises:
        FileExistsError: target file already exists, and argument `replace` is
            ``False``
        FileNotFoundError: parent directory of target file does not exist
        odafc.UnknownODAFCError: exporting DWG failed for unknown reasons
        odafc.ODAFCNotInstalledError: ODA File Converter not installed

    NzFile already exists: rk   rl   rq   DWGro   zNo such file or directory: 'rj   )
dxfversionrb   r   rr   rz   r]   unlinkFileExistsErrorrv   rw   r|   ry   rf   saveasrx   rD   r{   rs   )r   rf   r_   re   r   export_versiondwg_file
out_folderr   dxf_fileold_filename	argumentss               r   
export_dwgr      s   > . )NH~~&&((Hho&&J F 	FOO!"D("D"DEEE S(999 	&WG}}x';';F'C'C'HHH <LJJx   'CL(J#&  I 9%%%!	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&$   Qs: Q Q QRRRs   2BEE
E T)r_   re   r   sourcedestc          	     H   t          |          }|t          vrt          d| d          t          |                                                                           }|                                st          d|  d          |rt          |          }nd|j        	                                }|dk    r|
                    d          }n/|dk    r|
                    d          }nt          d| d          |                                r|st          d| d          |j        }|                                r|                                st          d| d          |j        }|                                d	d
         }	|	dvrt          d| d          t!          j        d          5 }
t%          |j        t)          |j                  t)          |
          |	||          }t+          |           t-          t          |
                                                    }|rH	 t1          j        |d         |           n># t4          $ r t1          j        |d         |           Y nw xY wt9          d|	 d           d
d
d
           d
S # 1 swxY w Y   d
S )a  Convert `source` file to `dest` file.

    The file extension defines the target format
    e.g. :code:`convert("test.dxf", "Test.dwg")` converts the source file to a
    DWG file.
    If `dest` is an empty string the conversion depends on the source file
    format and is DXF to DWG or DWG to DXF.
    To convert DXF to DXF an explicit destination filename is required:
    :code:`convert("r12.dxf", "r2013.dxf", version="R2013")`

    Args:
        source: source file
        dest: destination file, an empty string uses the source filename with
            the extension of the target format e.g. "test.dxf" -> "test.dwg"
        version: output DXF/DWG version e.g. "ACAD2018", "R2018", "AC1032"
        audit: audit files
        replace: replace existing destination file

    Raises:
        FileNotFoundError: source file or destination folder does not exist
        FileExistsError: destination file already exists and argument `replace`
            is ``False``
        odafc.UnsupportedVersion: invalid DXF version specified
        odafc.UnsupportedFileFormat: unsupported file extension
        odafc.UnknownODAFCError: conversion failed for unknown reasons
        odafc.ODAFCNotInstalledError: ODA File Converter not installed

    zInvalid version: 'rj   zSource file not found: '.dwgrq   Unsupported file format: 'zTarget file already exists: 'z$Destination folder does not exist: '   N)rn   r   rk   rl   )	in_folderr   rp   r_   re   r   zUnknown error: no z file was created)rd   VALID_VERSIONSr    r   
expanduserrr   r]   rs   suffixlowerr|   r   r   rz   is_dirrc   rv   rw   rx   ry   rD   r{   listiterdirrY   moveIOErrorcopyr   )r   r   r_   re   r   src_path	dest_pathext
parent_dirfmtr   r   results                r   convertr      s3   H '""Gn$$ !@g!@!@!@AAAF||&&((1133H?? F D6 D D DEEE 	MJJ		o##%%&== ,,V44IIF]] ,,V44II'(KS(K(K(KLLL L' LJiJJJKKK!J Vj&7&7&9&9 V Tz T T TUUU

C
))++abb/C
.  #$G$G$G$GHHH		$H	5	5	5 K$M(/**7||
 
 
	 	y!!!d7mm++--.. 	K2F1Iy1111 2 2 2F1Iy111112 I3IIIJJJ#K K K K K K K K K K K K K K K K K Ks7   6A9J0IJ%I41J3I44JJJr\   c                   d}t           j                            |           d                                         }|dk    rit	          |           rnt          |           rIt          | d          5 }t          |          }t          |j	                 }ddd           n# 1 swxY w Y   n:|dk    r!t          |           }|t          d          nt          d| d	          t          |          S )
zReturns the DXF/DWG version of file `path` as ODAFC compatible version
    string.

    Raises:
        odafc.UnsupportedVersion: unknown or unsupported DWG version
        odafc.UnsupportedFileFormat; unsupported file extension

    r2   r   rq   rtNr   z#Unknown or unsupported DWG version.r   rj   )r[   r\   splitextr   r
   r   openr	   rb   r_   r   r    r   rd   )r\   r_   r   fpinfos        r   ru   ru   6  s0    G
'

4
 
 
#
)
)
+
+C
f}}d## 	4 	4dD!! 4R||%dl34 4 4 4 4 4 4 4 4 4 4 4 4 4 4 
d##?$%JKKK  $$G$G$G$GHHHws   /"BB!$B!rn   r   r   rp   	list[str]c                $    d}|rdnd}||||||| gS )a  ODA File Converter command line format:

    ODAFileConverter "Input Folder" "Output Folder" version type recurse audit [filter]

        - version: output version: "ACAD9" - "ACAD2018"
        - type: output file type: "DWG", "DXF", "DXB"
        - recurse: recurse Input Folder: "0" or "1"
        - audit: audit each file: "0" or "1"
        - optional Input files filter: default "*.DWG,*.DXF"

    01r   )rf   r   r   rp   r_   re   recurse	audit_strs           r   rx   rx   R  s6    & G%#I r   rU   c                   t                      }| t          k    rC|rAt          |                                          r|S t                              d| d           t          j        d          }|s<| t          k    r1t                      }t          |                                          sd}|st          d          |S )zuGet ODAFC application path.

    Raises:
        odafc.ODAFCNotInstalledError: ODA File Converter not installed

    z	command 'z%' not found, using 'ODAFileConverter'rS   Nz~Could not find ODAFileConverter in the path. Install application from https://www.opendesign.com/guestfiles/oda_file_converter)
rP   WINDOWSr   rX   loggerwarningrY   rZ   rM   r   )rU   rO   r\   s      r   _get_odafc_pathr   r  s     ())N^'')) 	!!NNQNQQQ   <*++D Fg%% ""Dzz!!## 	D 
$a
 
 	
 Kr   c               #    K   t          j        d          rdt          j                     } t	          j        d| dddgt          j        t          j                  }t          j        d           | V  	 |	                                 |
                                 d
S # t          $ r Y d
S w xY wt                              d           t          j        d	         V  d
S )zCSee xvbfwrapper library for a more feature complete xvfb interface.Xvfb:z-screenr   
800x600x24)stdoutstderrg?z=Install xvfb to prevent the ODAFileConverter GUI from openingDISPLAYN)rY   rZ   r[   getpid
subprocessPopenDEVNULLtimesleep	terminatewaitOSErrorr   r   environ)displayprocs     r   _linux_dummy_displayr     s       |F $#bikk##Wil;%%
 
 

 	
3	NNIIKKKKK 	 	 	DD	 	WXXXj######s   6(B   
B.-B.commandr   subprocess.Popenc                ~   | t           k    rft                      5 }t          j                                        }||d<   t          j        |g|z   dd|          }ddd           n# 1 swxY w Y   n| t          k    rt          j        |g|z   dd          }n| t          k    rt          j	                    }t
          j
        t
          j        z  |_        t
          j        |_        t          j        |g|z   t
          j        t
          j        |          }|                                 nt%          d|            |S )aT  Execute ODAFC application without launching the GUI.

    Args:
        system: "Linux", "Windows" or "Darwin"
        command: application to execute
        arguments: ODAFC argument list

    Raises:
        odafc.UnsupportedPlatform: for unsupported platforms
        odafc.ODAFCNotInstalledError: ODA File Converter not installed

    r   T)textcapture_outputenvN)r   r   )r   r   startupinfozUnsupported platform: )rV   r   r[   r   r   r   runrW   r   STARTUPINFOCREATE_NEW_CONSOLESTARTF_USESHOWWINDOWdwFlagsSW_HIDEwShowWindowr   PIPEr   r   )rU   r   r   r   r   r   r   s          r   _run_with_no_guir     sq    !## 	w*//##C$C	N>	I%D3  D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
6		~wi)3$tTTT	7		 ,..)J,KK 	 #-"4I	!??#	
 
 
 			 ""C6"C"CDDDKs   A A&&A*-A*
returncodeintr   c                t    |                                 }| t          k    r|dk    o|dk    S |dk    rdS |dk    S )Nr   zQuit (core dumped)r   T)rL   rV   )rU   r   r   s      r   _odafc_failedr     sE    \\^^F|>*> >>	qt|r   Optional[bytes]c                "   t                               d|             t          j                    }t	          |          }t          |||           }|j        }|t          k    rY|j        	                                
                    d          }|j        	                                
                    d          }n|j        }|j        }t          |||          r4d| d| d| }t                               |           t          |          |S )a)  Execute ODAFC application.

    Args:
        arguments: ODAFC argument list

    Raises:
        odafc.ODAFCNotInstalledError: ODA File Converter not installed
        odafc.UnknownODAFCError: execution failed for unknown reasons
        odafc.UnsupportedPlatform: for unsupported platforms

    z)Running ODAFileConverter with arguments: zutf-8z)ODA File Converter failed: return code = z
.
stdout: z	
stderr: )r   debugrT   rU   r   r   r   r   r   readdecoder   r   r   )r   rU   oda_fcr   r   r   r   msgs           r   r{   r{     s    LLHYHHIII_FV$$FFFI66DJ!!##**733!!##**733VZ00 %2
 2 22 2)/2 2 	 	S$$$Mr   )rC   rD   )rC   rQ   )r_   rD   rC   rD   ra   )rf   rg   r_   rh   re   rQ   rC   r   )r   r   rf   rg   r_   rh   re   rQ   r   rQ   rC   r   )r   )r   rg   r   rg   )r\   rD   rC   rD   )rn   r0   F)rf   rD   r   rD   r   rD   rp   rD   r_   rD   re   rQ   rC   r   )rU   rD   rC   rD   )rU   rD   r   rD   r   r   rC   r   )rU   rD   r   r   r   rD   rC   rQ   )r   r   rC   r   )3
__future__r   typingr   loggingr[   rT   rY   r   rv   r   
contextlibr   pathlibr   r   ezdxf.documentr   ezdxf.lldxf.validatorr   r	   r
   r   	getLoggerr   r   r   r   r   r   r   r    rb   r   r   rV   rW   rM   rP   r^   rd   r}   r   r   ru   rx   r   r   r   r   r{   r   r   r   <module>r      s   # " " " " "        				         % % % % % %        " " " " " "            
	7	#	#	 	 	 	 	 	 	 		 	 	 	 	
 	 	 		 	 	 	 	Z 	 	 		 	 	 	 	J 	 	 		 	 	 	 	* 	 	 		 	 	 	 	 	 	 		8	8 
8 Z	
 Z Z Z Z Z g h h h h j  j!" j#$ )  .   	H H H HI I I I/ / / /= = = =
 ;?,EQV,E ,E ,E ,E ,E ,Ed "<S
 <S <S <S <S <S <SB !PK 
PK PK PK PK PK PKf       @     @       F $ $ $*- - - -`	 	 	 	     r   