
    'jT                       d 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Zddl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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(m)Z) ddl*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 erddl:m;Z; g dZ<dZ= ej>        d          Z?e=ddWdZ@e=ddXd!ZAe=ddYd$ZBdZd*ZCd[d-ZDd\d0ZEd]d4ZFd^d8ZGd_d;ZHd_d<ZId_d=ZJd_d>ZKe3fd`dAZLd_dBZMd_dCZNd_dDZOdadGZPdbdIZQdcdJZRdcdKZSdcdLZTh dMZU G dN dOe+          ZV G dP dQ          ZWdRZXdddUZY G dV d:          ZZdS )ea  
Module to export any DXF document as DXF version R12 without modifying the source
document.

.. versionadded:: 1.1

To get the best result use the ODA File Converter add-on::

    from ezdxf.addons import odafc

    odafc.convert("any.dxf", "r12.dxf", version="R12")

    )annotations)TYPE_CHECKINGTextIOCallableOptionalN)StringIO)constproxygraphicpath)Drawing)BlockRecord	DXFEntityDXFTagStorageEllipseHatchInsert
LWPolylineMPolygonMTextMeshPolyfacePolylineSpline	Textstyle)
DXFPolygon)MTextExplode)EntitySpace)BlockLayoutVirtualLayout)	TagWriterAbstractTagWriter)DXFTagTAG_STRING_FORMAT)Z_AXISVec3NULLVEC)R12NameTranslator)MeshBuilder)TextstyleTable)GenericLayoutType)R12Exporterconvertsaveaswrite{Gz?ezdxfmax_sagittadocr   r2   floatreturnc                   t                      }t          | |          }|                    |           |                    d           t	          j        |          S )zExport and reload DXF document as DXF version R12.

    Writes the DXF document into a temporary file at the file-system and reloads this
    file by the :func:`ezdxf.readfile` function.
    r1   r   )r   r+   r.   seekr0   read)r3   r2   streamexporters       P/DATA/AppData/hermes/venv/lib/python3.11/site-packages/ezdxf/addons/r12export.pyr,   r,   >   sP     ZZF3K888HNN6
KKNNN:f    r9   r   Nonec               R    t          | |          }|                    |           dS )a  Write a DXF document as DXF version R12 to a text stream. The `max_sagitta`
    argument determines the accuracy of the curve flatting for SPLINE and ELLIPSE
    entities.

    Args:
        doc: DXF document to export
        stream: output stream, use :attr:`doc.encoding` as encoding
        max_sagitta: maximum distance from the center of the curve to the
            center of the line segment between two approximation points to
            determine if a segment should be subdivided.

    r1   N)r+   r.   )r3   r9   r2   r:   s       r;   r.   r.   K   s.     3K888HNN6r<   filepathstr | os.PathLikec                   t          |d| j        d          5 }t          | ||           ddd           dS # 1 swxY w Y   dS )a  Write a DXF document as DXF version R12 to a file. The `max_sagitta`
    argument determines the accuracy of the curve flatting for SPLINE and ELLIPSE
    entities.

    Args:
        doc: DXF document to export
        filepath: output filename
        max_sagitta: maximum distance from the center of the curve to the
            center of the line segment between two approximation points to
            determine if a segment should be subdivided.

    wt
dxfreplace)encodingerrorsr1   N)openrD   r.   )r3   r?   r2   r9   s       r;   r-   r-   \   s     
hs|L	I	I	I 
V#	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   9= =spliner   min_segmentsintr   c                   t          j        | j        j        | j        j        | j        j        t          j        d          }|                    | 	                    ||                     |
                                 |S N)layerlinetypecolorflags
dxfattribs)pointsr   newdxfrL   rM   rN   r	   POLYLINE_3D_POLYLINEappend_vertices
flattening
new_seqend)rG   r2   rH   polylines       r;   spline_to_polyliner[   s   s     |Z%
+Z%/	
 
  H F$5$5k<$P$PQQQOr<   ellipser   c                   t          j        | j        j        | j        j        | j        j        t          j        d          }|                    | 	                    ||                     |
                                 |S rK   rS   )r\   r2   rH   rZ   s       r;   ellipse_to_polyliner^      s     |[&,[&/	
 
  H G$6$6{L$Q$QRRROr<   
lwpolyliner   c                l   t          j        | j        j        | j        j        | j        j        d          }|                                 |                    |                                 d           | j	        r|
                                 | j                            d          r$| j        j        }||j        _        ||j        _        t          | j        j                  }|                    t$                    s3||j        _        | j        j        }t          dd|          |j        _        |S )NrL   rM   rN   rP   xyseb)formatconst_widthr   )r   rT   rU   rL   rM   rN   rY   append_formatted_vertices
get_points	is_closedclosehasattrrd   default_start_widthdefault_end_widthr%   	extrusioniscloser$   	elevation)r_   rZ   widthrl   rn   s        r;   lwpolyline_to_polylinerp      s   |^)"/^)
 
  H &&z'<'<'>'>w&OOO ~m,, /*+0().&Z^-..IV$$ 7!*N,	!%aI!6!6 Or<   meshr   r   c                    t          j        |           }|                    t                      | j        j        | j        j        | j        j        d          S )Nra   rP   )r(   	from_meshrender_polyfacer   rU   rL   rM   rN   )rq   builders     r;   mesh_to_polyface_meshrv      sV    #D))G""X^)X^
 
 #   r<   entityr   strc                f    | j         j        J d|                                  d| j         j         S )N
EZDXF_XPL__)rU   handledxftype)rw   s    r;   get_xpl_block_namer~      s8    :(((>((>>6:+<>>>r<   r:   r+   c                    t          |t                    sJ t          |          }t          |j                  r)|                    |                                            d S d S N)
isinstancer   rp   lenvertices
export_dxf	tagwriterr:   rw   rZ   s      r;   export_lwpolyliner      sh    fj)))))%f--H
8 2H..00111112 2r<   c                    t          |t                    sJ t          |          }t          |j                  r)|                    |                                            d S d S r   )r   r   rv   r   r   r   r   )r:   rw   polyface_meshs      r;   export_meshr      sh    fd#####)&11M
=!"" 7  !3!3!5!5666667 7r<   c                    t          |t                    sJ t          || j        | j                  }t          |j                  r)|                    |                                            d S d S r   )	r   r   r[   r2   min_spline_segmentsr   r   r   r   r   s      r;   export_spliner      sy    ff%%%%%!$h&B H 8 2H..00111112 2r<   c                    t          |t                    sJ t          || j        | j                  }t          |j                  r)|                    |                                            d S d S r   )	r   r   r^   r2   min_ellipse_segmentsr   r   r   r   r   s      r;   export_ellipser      sy    fg&&&&&"$h&C H 8 2H..00111112 2r<   namer   c                t    t          j        | |j        j        |j        j        |j        j        |d          S )N)r   rL   rM   rN   insertrP   )r   rT   rU   rL   rM   rN   )r   rw   locations      r;   make_insertr      sC    :Z%
+Z%
 
   r<   c                   t          |j        t                    sJ t          j        |j                  }	 t          |                                          }n# t          j        $ r Y d S w xY w|                     |           d S r   )	r   proxy_graphicbytesr
   ProxyGraphicr   virtual_entitiesProxyGraphicErrorexport_entity_space)r:   rw   pgentitiess       r;   export_proxy_graphicr      s    f*E22222		"6#7	8	8Br224455)     *****s   !A A,+A,c                    t          |t                    sJ t                      }|                     ||           |                     |j                   d S r   )r   r   r   explode_mtextr   entity_space)r:   rw   layouts      r;   export_mtextr      sT    fe$$$$$__F66***  !455555r<   c                    t                      }	 |                                D ]}|                    |           n# t          $ r Y d S w xY w|                     |j                   d S r   )r   r   
add_entity	Exceptionr   r   )r:   rw   r   es       r;   export_virtual_entitiesr      s    __F((** 	! 	!Aa    	!     !455555s   ,= 
A
Ablockr   c                    t          | t                    sJ | j        j        | j        j        d}|                                 D ]\  }}|                    |||           d S )NrL   rN   rP   )r   r   rU   rL   rN   render_pattern_linesadd_line)rw   r   rQ   startends        r;   export_pattern_fillr     sz    fj)))))!! J 1133 : :
sucj9999: :r<   r   c                   | j         j        | j         j        d}t          | j         j                  }|j        s|                    t                    s||d<   |                     ||          D ]}|	                    ||           d S )Nr   rl   rP   )
rU   rL   rN   r%   rl   is_nullrm   r$   triangulate	add_solid)rw   r   r2   rH   rQ   rl   r   s          r;   export_solid_fillr     s     !! J
 VZ)**I ,Y%6%6v%>%> ,"+
; &&{LAA 9 9Z88889 9r<   c                @   t          |t                    sJ |                     |          }t          |j        |          }|                    |                                            |j        rt          ||           d S t          ||| j
        | j                   d S r   )r   r   	new_blockr   r   r   r   has_pattern_fillr   r   r2   r   r:   rw   r   r   s       r;   export_hatchr   $  s    fe$$$$$v&&EV,,F
h((**+++ 
FE*****E8/1M	
 	
 	
 	
 	
r<   c                P   t          |t                    sJ |                     |          }t          |j        |          }|                    |                                            t          j        |t          j	        |t          |j        j                            | j        | j        t          |j        j                  |j        j        |j        j        |j        j        d           |j        rt)          ||           d S t+          ||| j        | j                   d S )N)offsetra   )distancesegmentsrl   rQ   )r   r   r   r   r   r   r   r   render_polylines2dfrom_hatch_ocsr%   rU   r   r2   r   rl   rL   rM   rN   r   r   r   r   s       r;   export_mpolygonr   3  s'   fh'''''v&&EV,,F
h((**+++ 	F4
0A+B+BCCC%-vz+,,Z%
+Z%
 
     
FE*****E8/1M	
 	
 	
 	
 	
r<   c                   ddl m} t          ||          sJ |}|                                }|                                }|                    d          sd S 	 |j                            d          }n# t          j	        $ r Y d S w xY w|
                    dd          }t          j        |||d          }|                    |                                            d S )	Nr   )AcadTableBlockContentz*T
AcDbEntity   0)r   rL   r   rP   )ezdxf.entities.acad_tabler   r   get_insert_locationget_block_name
startswithxtagsget_subclassr	   DXFIndexErrorget_first_valuer   rT   r   r   )	r:   rw   r   tabler   
block_nameacdb_entityrL   r   s	            r;   export_acad_tabler   O  s   ??????f344444#)E((**H%%''J  && k..|<<   ''3//EZ&(KK  F h((**+++++s   A6 6B	B	>                   r   c                  @     e Zd Zd fdZddZdd	ZddZddZ xZS )R12TagWriterr9   r   c                    t                                          |t          j        d           d| _        d| _        t                      | _        d S )NF)
dxfversionwrite_handles )super__init__r	   DXF12
skip_xdatacurrent_entityr'   
translator)selfr9   	__class__s     r;   r   zR12TagWriter.__init__|  sD    EKuMMM +--r<   r5   r=   c                    || _         d S r   )_streamr   r9   s     r;   
set_streamzR12TagWriter.set_stream  s    r<   tagr"   c                @   |\  }}|dk    rt          |          | _        |dk    r	| j        rd S |t          v r:| j                            t          ||                     ||          fz             d S | j                            |                                           d S )Nr     )	rx   r   r   NAME_TAG_CODESr   r.   r#   sanitize_namedxfstr)r   r   codevalues       r;   	write_tagzR12TagWriter.write_tag  s    e199"%e**D#::$/:F>!!L!T4+=+=dE+J+J$KK     Lszz||,,,,,r<   r   rI   c                    |dk    r	| j         rd S |dk    rt          |          | _        |t          v r|                     ||          }| j                            t          ||fz             d S )Nr   r   )r   rx   r   r   r   r   r.   r#   )r   r   r   s      r;   
write_tag2zR12TagWriter.write_tag2  ss    #::$/:F199"%e**D>!!&&tU33E,e}<=====r<   r   rx   c                \    |dk    r| j         dk    r|S | j                            |          S )Nr   	DIMENSION)r   r   	translate)r   r   r   s      r;   r   zR12TagWriter.sanitize_name  s5     199,;;K((...r<   )r9   r   r9   r   r5   r=   )r   r"   r5   r=   )r   rI   r5   r=   )r   rI   r   rx   r5   rx   )	__name__
__module____qualname__r   r   r   r   r   __classcell__)r   s   @r;   r   r   {  s        . . . . . .   - - - -> > > >/ / / / / / / /r<   r   c                  &    e Zd ZddZddZddZdS )SpecialStyleTablestylesr)   extra_stylesc                "    || _         || _        d S r   )r  r  )r   r  r  s      r;   r   zSpecialStyleTable.__init__  s    (r<   r5   list[Textstyle]c                   t          | j        j                                                  }| j        j                                        D ]4\  }}| j                            |          s|                    |           5|S r   )listr  entriesvaluesr  items	has_entryappend)r   r  r   extra_styles       r;   get_text_stylesz!SpecialStyleTable.get_text_styles  sv    t{*113344!%!2!:!@!@!B!B 	, 	,D+;((.. ,{+++r<   r   r!   r=   c                ,   |                                  }|                    dd           |                    dd           |                    dt          |                     |D ]}|                    |           |                    dd           d S )Nr   TABLEr   STYLEF   ENDTAB)r  r   r   r   )r   r   text_stylesstyles       r;   r   zSpecialStyleTable.export_dxf  s    **,,Q(((Q(((R[!1!1222  	( 	(EY''''Q)))))r<   N)r  r)   r  r)   )r5   r  )r   r!   r5   r=   )r   r   r   r   r  r    r<   r;   r   r     sP        ) ) ) )   * * * * * *r<   r   z0
EOF
names	list[str]c                    d}| D ]e}|                                 }|                    d          s,	 t          |dd                    }n# t          $ r Y Qw xY wt	          ||          }f|dz   S )Nr   *r      )upperr   rI   
ValueErrormax)r  
max_numberr   numbers       r;   detect_max_block_numberr     s    J - -zz||s## 		abb]]FF 	 	 	H	V,,

>s   A


AAc                      e Zd Zd0d1dZd2d	Zed3d            Zd4d5dZd6dZd7dZ	d8dZ
d9dZd7dZd7dZd7dZd:dZd;d!Zd<d&Zd7d'Zd=d*Zd>d-Zd:d.Zd?d/ZdS )@r+   r/   r3   r   r2   r4   c                   t          |t                    sJ || _        t          t	                                | _        t          |          | _        d| _        d| _	        t          j        d          | _        t          d |j        D                       | _        t           t"          t$          t&          t(          t*          t*          t*          t*          t,          t.          t0          d| _        d S )N   r   R12c                &    g | ]}|j         j        S r  )rU   r   .0brs     r;   
<listcomp>z(R12Exporter.__init__.<locals>.<listcomp>  s    555RRV[555r<   )
LWPOLYLINEMESHSPLINEELLIPSEMTEXTLEADERMLEADERMULTILEADERMLINEHATCHMPOLYGON
ACAD_TABLE)r   r   _docr   r   
_tagwriterr4   r2   r   r   r0   rT   
_extra_docr   block_records_next_block_numberr   r   r   r   r   r   r   r   r   	exporters)r   r3   r2   s      r;   r   zR12Exporter.__init__  s    #w'''''	&xzz22 --() )*!)E**"9553#4555#
 #
 ,#%!-.2,!'+O
 O
r<   entity_typerx   c                    | j         |= d S r   )r;  )r   r<  s     r;   disable_exporterzR12Exporter.disable_exporter  s    N;'''r<   r5   c                    | j         S r   )r6  r   s    r;   r3   zR12Exporter.doc  s
    yr<   Nr9   Optional[TextIO]r   c                H    || j                             |           | j         S r   )r7  r   r   s     r;   r   zR12Exporter.tagwriter  s&    O&&v...r<   r   r=   c                T    |                     |                                            dS )z"Write DXF document to text stream.N)r.   	to_stringr   s     r;   r.   zR12Exporter.write  s$    T^^%%&&&&&r<   c                    |                                  }|                                 }d                    |                                 |                                 ||t
          f          S )zExport DXF document as string.r   )export_layouts_to_stringexport_blocks_to_stringjoinexport_header_to_stringexport_tables_to_stringEOF_STR)r   r   blockss      r;   rD  zR12Exporter.to_string  sh     0022--//ww,,..,,..
 
 	
r<   charc                >    d| | j          }| xj         dz  c_         |S )Nr  r  )r:  )r   rM  r   s      r;   next_block_namezR12Exporter.next_block_name  s0    2420221$r<   rw   r   r   c                    |                      d          }| j        j                            ||j                            dd          t          j        d          S )NUrL   r   )rL   rO   rP   )rO  r8  rL  rT   rU   getr	   BLK_ANONYMOUS)r   rw   r   s      r;   r   zR12Exporter.new_block  s]    ##C((%))55,  * 
 
 	
r<   c                    t                      }| j        j                            |                     |                     |                                S r   )r   r3   headerr   r   getvaluer   in_memory_streams     r;   rI  z#R12Exporter.export_header_to_string  sC    #::""4>>2B#C#CDDD((***r<   c                <   t                      }| j        j        }|j        }t	          | j        j        | j        j                  |_        |                     |          }d|_        |                    |           ||_        d|_        |	                                S )NTF)
r   r3   tablesr  r   r8  r   r   r   rV  )r   rX  rZ  preserve_tabler   s        r;   rJ  z#R12Exporter.export_tables_to_string"  s    #::)$(/4?;QRRNN#344	#	)$$$&$	((***r<   c                v   t                      }| j                            |           |                     d           | j        j        D ]I}|j        r|j        s|j        j	        
                                }|dv r4|                     |           J|                                 }t          |          r\|D ]6}|                     |           |                     |j        j	                   7|                                 }t          |          \|                                  |                                S )NBLOCKS)z$model_spacez$paper_space)r   r7  r   _write_section_headerr3   r9  is_any_paperspaceis_active_paperspacerU   r   lower_export_block_recordget_extra_blocksr   discard_extra_block_write_endsecrV  )r   rX  block_recordr   extra_blockss        r;   rG  z#R12Exporter.export_blocks_to_string0  sN   #::""#3444""8,,, H2 	4 	4L- l6W #(..00D777 %%l3333,,.., 	3 , @ @)),777(()9)>????0022L , 	3 	((***r<   r   c                D    | j         j                            |           d S r   )r8  r9  discardr   r   s     r;   rd  zR12Exporter.discard_extra_blockJ  s!    %--d33333r<   list[BlockRecord]c                .    d | j         j        D             S )Nc                P    g | ]#}|j         j                            d           !|$S )z*U)rU   r   r   r&  s     r;   r)  z0R12Exporter.get_extra_blocks.<locals>.<listcomp>N  s@     
 
 
"&+:P:PQU:V:V

 
 
r<   )r8  r9  r@  s    r;   rc  zR12Exporter.get_extra_blocksM  s(    
 
6
 
 
 	
r<   mtextr   r   r*   c                    t          || j                  5 }|                    |d           d d d            d S # 1 swxY w Y   d S )NF)destroy)r   r8  explode)r   rn  r   xpls       r;   r   zR12Exporter.explode_mtextR  s    &$/22 	.cKKuK---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   ;??c                   t                      }| j                            |           |                     d           |                     | j                                        j                   |                     | j                                        j                   | 	                                 |
                                S )NENTITIES)r   r7  r   r^  r   r3   
modelspacer   
paperspacere  rV  rW  s     r;   rF  z$R12Exporter.export_layouts_to_stringV  s    #::""#3444"":...  !4!4!6!6!CDDD  !4!4!6!6!CDDD((***r<   rf  r   c                    | j         }|j        J |j                            |           |j        s|                     |j                   |j        J |j                            |           d S r   )r7  r   r   is_any_layoutr   r   endblk)r   rf  r   s      r;   rb  z R12Exporter._export_block_record`  s~    O	!---%%i000) 	@$$\%>???"...&&y11111r<   spacer   c                B   | j         }|D ]}|j        t          j        k    st	          |t
                    rS| j                            |                                          }|r || |           g|j	        rt          | |           |                    |           d S r   )r7  MIN_DXF_VERSION_FOR_EXPORTr	   r   r   r   r;  rR  r}   r   r   r   )r   rz  r   rw   r:   s        r;   r   zR12Exporter.export_entity_spacei  s    O	 	- 	-F05;>>*C C>  >--fnn.>.>?? HT6***' 7(v666!!),,,,	- 	-r<   c                B    | j                             d| d           d S )Nz  0
SECTION
  2

)r7  	write_strrj  s     r;   r^  z!R12Exporter._write_section_headerx  s*    !!"@"@"@"@AAAAAr<   c                <    | j                             dd           d S )Nr   ENDSEC)r7  r   r@  s    r;   re  zR12Exporter._write_endsec{  s     ""1h/////r<   )r/   )r3   r   r2   r4   )r<  rx   )r5   r   r   )r9   rA  r5   r   r   )r5   rx   )rM  rx   r5   rx   )rw   r   r5   r   )r   rx   r5   r=   )r5   rk  )rn  r   r   r*   )rf  r   )rz  r   )r5   r=   )r   r   r   r   r>  propertyr3   r   r.   rD  rO  r   rI  rJ  rG  rd  rc  r   rF  rb  r   r^  re  r  r<   r;   r+   r+     s       
 
 
 
 
8( ( ( (    X    
' ' ' '
 
 
 
"   

 
 
 
+ + + +
+ + + ++ + + +44 4 4 4
 
 
 

. . . .+ + + +2 2 2 2- - - -B B B B0 0 0 0 0 0r<   )r3   r   r2   r4   r5   r   )r3   r   r9   r   r2   r4   r5   r=   )r3   r   r?   r@   r2   r4   r5   r=   )rG   r   r2   r4   rH   rI   r5   r   )r\   r   r2   r4   rH   rI   r5   r   )r_   r   r5   r   )rq   r   r5   r   )rw   r   r5   rx   )r:   r+   rw   r   )r   rx   rw   r   r5   r   )rw   r   r   r   r5   r=   )
rw   r   r   r   r2   r4   rH   rI   r5   r=   )r:   r+   rw   r   r5   r=   )r  r  r5   rI   )[__doc__
__future__r   typingr   r   r   r   osior   loggingr0   r	   r
   r   ezdxf.documentr   ezdxf.entitiesr   r   r   r   r   r   r   r   r   r   r   r   r   r   ezdxf.entities.polygonr   ezdxf.addonsr   ezdxf.entitydbr   ezdxf.layoutsr   r   ezdxf.lldxf.tagwriterr    r!   ezdxf.lldxf.typesr"   r#   
ezdxf.mathr$   r%   r&   ezdxf.r12strictr'   ezdxf.renderr(   ezdxf.sections.tabler)   ezdxf.eztypesr*   __all__MAX_SAGITTA	getLoggerloggerr,   r.   r-   r[   r^   rp   rv   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rK  r   r+   r  r<   r;   <module>r     s    # " " " " " < < < < < < < < < < < < 				         + + + + + + + + + + " " " " " "                                 . - - - - - % % % % % % & & & & & & 4 4 4 4 4 4 4 4 > > > > > > > > 7 7 7 7 7 7 7 7 , , , , , , , , , , - - - - - - $ $ $ $ $ $ / / / / / / 0//////
7
7
7		7	#	# 3> 
 
 
 
 
 
 AL      $ HS
 
 
 
 
 
.   "       4	 	 	 	? ? ? ?
2 2 2 27 7 7 72 2 2 22 2 2 2 8? 	 	 	 	 	+ + + +6 6 6 66 6 6 6: : : :9 9 9 9(
 
 
 

 
 
 
8, , , ,R -,,+/ +/ +/ +/ +/9 +/ +/ +/\* * * * * * * *,    l0 l0 l0 l0 l0 l0 l0 l0 l0 l0r<   