
    OgT                       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d6dZ@e=dd7dZAe=d	 	 	 	 	 	 	 d8dZB	 	 	 	 	 	 	 	 d9dZC	 	 	 	 	 	 	 	 d:dZDd;dZEd<dZFd=dZGd>d!ZHd>d"ZId>d#ZJd>d$ZKe3fd?d%ZLd>d&ZMd>d'ZNd>d(ZOd@d)ZP	 	 	 	 	 	 	 	 	 	 dAd*ZQdBd+ZRdBd,ZSdBd-ZTh d.ZU G d/ d0e+      ZV G d1 d2      ZWd3ZXdCd4ZY G d5 d       ZZy)Da  
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_sagittac                   t               }t        | |      }|j                  |       |j                  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)docr2   streamexporters       [/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/addons/r12export.pyr,   r,   >   s=     ZF3K8HNN6
KKN::f    c               @    t        | |      }|j                  |       y)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.   )r6   r7   r2   r8   s       r9   r.   r.   K   s     3K8HNN6r:   c               |    t        |d| j                  d      5 }t        | ||       ddd       y# 1 sw Y   yxY w)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)openr?   r.   )r6   filepathr2   r7   s       r9   r-   r-   \   s>     
hs||L	I 
V#	

 
 
s   2;c                4   t        j                  | j                  j                  | j                  j                  | j                  j
                  t        j                  d      }|j                  | j                  ||             |j                          |S N)layerlinetypecolorflags
dxfattribs)pointsr   newdxfrE   rF   rG   r	   POLYLINE_3D_POLYLINEappend_vertices
flattening
new_seqend)spliner2   min_segmentspolylines       r9   spline_to_polylinerV   s   sx     ||ZZ%%

++ZZ%%//	
H F$5$5k<$PQOr:   c                4   t        j                  | j                  j                  | j                  j                  | j                  j
                  t        j                  d      }|j                  | j                  ||             |j                          |S rD   rL   )ellipser2   rT   rU   s       r9   ellipse_to_polylinerY      sx     ||[[&&,,[[&&//	
H G$6$6{L$QROr:   c                   t        j                  | j                  j                  | j                  j                  | j                  j
                  d      }|j                          |j                  | j                         d       | j                  r|j                          | j                  j                  d      r8| j                  j                  }||j                  _        ||j                  _        t        | j                  j                         }|j#                  t$              sC||j                  _        | j                  j&                  }t        dd|      |j                  _        |S )NrE   rF   rG   rI   xyseb)formatconst_widthr   )r   rM   rN   rE   rF   rG   rR   append_formatted_vertices
get_points	is_closedclosehasattrr^   default_start_widthdefault_end_widthr%   	extrusioniscloser$   	elevation)
lwpolylinerU   widthrf   rh   s        r9   lwpolyline_to_polylinerk      s   ||^^))"//^^))
H &&z'<'<'>w&O~~m,**+0().&Z^^--.IV$!*NN,,	!%aI!6 Or:   c                    t        j                  |       }|j                  t               | j                  j
                  | j                  j                  | j                  j                  d      S )Nr[   rI   )r(   	from_meshrender_polyfacer   rN   rE   rF   rG   )meshbuilders     r9   mesh_to_polyface_meshrq      sW    ##D)G""XX^^))XX^^
 #  r:   c                    | j                   j                  J d| j                          d| j                   j                   S )N
EZDXF_XPL__)rN   handledxftype)entitys    r9   get_xpl_block_namerx      s>    ::(((()6::+<+<*=>>r:   r+   c                    t        |t              sJ t        |      }t        |j                        r |j                  | j                                y y N)
isinstancer   rk   lenvertices
export_dxf	tagwriterr8   rw   rU   s      r9   export_lwpolyliner      sF    fj)))%f-H
8H..01 r:   c                    t        |t              sJ t        |      }t        |j                        r |j                  | j                                y y rz   )r{   r   rq   r|   r}   r~   r   )r8   rw   polyface_meshs      r9   export_meshr      sF    fd###)&1M
=!!"  !3!3!56 #r:   c                    t        |t              sJ t        || j                  | j                        }t        |j                        r |j                  | j                                y y rz   )	r{   r   rV   r2   min_spline_segmentsr|   r}   r~   r   r   s      r9   export_spliner      s[    ff%%%!$$h&B&BH 8H..01 r:   c                    t        |t              sJ t        || j                  | j                        }t        |j                        r |j                  | j                                y y rz   )	r{   r   rY   r2   min_ellipse_segmentsr|   r}   r~   r   r   s      r9   export_ellipser      s[    fg&&&"$$h&C&CH 8H..01 r:   c                    t        j                  | |j                  j                  |j                  j                  |j                  j
                  |d      S )N)namerE   rF   rG   insertrI   )r   rM   rN   rE   rF   rG   )r   rw   locations      r9   make_insertr      sE    ::ZZ%%

++ZZ%%
 r:   c                   t        |j                  t              sJ t        j                  |j                        }	 t        |j                               }| j                  |       y # t        j                  $ r Y y w xY wrz   )	r{   proxy_graphicbytesr
   ProxyGraphicr   virtual_entitiesProxyGraphicErrorexport_entity_space)r8   rw   pgentitiess       r9   export_proxy_graphicr      sp    f**E222		"	"6#7#7	8Br2245   * )) s   A( (A>=A>c                    t        |t              sJ t               }| j                  ||       | j	                  |j
                         y rz   )r{   r   r   explode_mtextr   entity_space)r8   rw   layouts      r9   export_mtextr      s=    fe$$$_F66*  !4!45r:   c                    t               }	 |j                         D ]  }|j                  |        	 | j	                  |j
                         y # t        $ r Y y w xY wrz   )r   r   
add_entity	Exceptionr   r   )r8   rw   r   es       r9   export_virtual_entitiesr      s^    _F((* 	!Aa 	!   !4!45  s   &A 	AAc                    t        | t              sJ | j                  j                  | j                  j                  d}| j                         D ]  \  }}|j                  |||        y )NrE   rG   rI   )r{   r   rN   rE   rG   render_pattern_linesadd_line)rw   blockrJ   startends        r9   export_pattern_fillr     sc    fj)))!!!!J 113 :
sucj9:r:   c                <   | j                   j                  | j                   j                  d}t        | j                   j                        }|j
                  s|j                  t              s||d<   | j                  ||      D ]  }|j                  ||        y )Nr   rf   rI   )
rN   rE   rG   r%   rf   is_nullrg   r$   triangulate	add_solid)rw   r   r2   rT   rJ   rf   r}   s          r9   export_solid_fillr     s     !!!!J
 VZZ))*IY%6%6v%>"+
; &&{LA 9Z89r:   c                *   t        |t              sJ | j                  |      }t        |j                  |      }|j                  | j                                |j                  rt        ||       y t        ||| j                  | j                         y rz   )r{   r   	new_blockr   r   r~   r   has_pattern_fillr   r   r2   r   r8   rw   r   r   s       r9   export_hatchr   $  sy    fe$$$v&EV,F
h((*+FE*E8//1M1M	
r:   c                   t        |t              sJ | j                  |      }t        |j                  |      }|j                  | j                                t        j                  |t        j                  |t        |j                  j                              | j                  | j                  t        |j                  j                        |j                  j                   |j                  j"                  |j                  j$                  d       |j&                  rt)        ||       y t+        ||| j                  | j                         y )N)offsetr[   )distancesegmentsrf   rJ   )r{   r   r   r   r   r~   r   r   render_polylines2dfrom_hatch_ocsr%   rN   r   r2   r   rf   rE   rF   rG   r   r   r   r   s       r9   export_mpolygonr   3  s    fh'''v&EV,F
h((*+ 	F4

0A0A+BC%%--vzz++,ZZ%%

++ZZ%%
 FE*E8//1M1M	
r:   c                   ddl m} t        ||      sJ |}|j                         }|j	                         }|j                  d      sy 	 |j                  j                  d      }|j                  dd      }t        j                  |||d      }|j                  | j                                y # t        j                  $ r Y y w xY w)	Nr   )AcadTableBlockContentz*T
AcDbEntity   0)r   rE   r   rI   )ezdxf.entities.acad_tabler   r{   get_insert_locationget_block_name
startswithxtagsget_subclassr	   DXFIndexErrorget_first_valuer   rM   r~   r   )	r8   rw   r   tabler   
block_nameacdb_entityrE   r   s	            r9   export_acad_tabler   O  s    ?f3444#)E((*H%%'J  &kk..|< ''3/EZZ&(KF h((*+  s   
B1 1CC>                   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 )R12TagWriterc                    t         |   |t        j                  d       d| _        d| _        t               | _        y )NF)
dxfversionwrite_handles )super__init__r	   DXF12
skip_xdatacurrent_entityr'   
translator)selfr7   	__class__s     r9   r   zR12TagWriter.__init__|  s4    EKKuM +-r:   c                    || _         y rz   )_streamr   r7   s     r9   
set_streamzR12TagWriter.set_stream  s	    r:   c                (   |\  }}|dk(  rt        |      | _        |dkD  r| j                  ry |t        v r5| j                  j                  t        || j                  ||      fz         y | j                  j                  |j                                y )Nr     )	strr   r   NAME_TAG_CODESr   r.   r#   sanitize_namedxfstr)r   tagcodevalues       r9   	write_tagzR12TagWriter.write_tag  s|    e19"%e*D#:$//>!LL!T4+=+=dE+J$KK LLszz|,r:   c                    |dkD  r| j                   ry |dk(  rt        |      | _        |t        v r| j	                  ||      }| j
                  j                  t        ||fz         y )Nr   r   )r   r   r   r   r   r   r.   r#   )r   r   r   s      r9   
write_tag2zR12TagWriter.write_tag2  s[    #:$//19"%e*D>!&&tU3E,e}<=r:   c                d    |dk(  r| j                   dk7  r|S | j                  j                  |      S )Nr   	DIMENSION)r   r   	translate)r   r   r   s      r9   r   zR12TagWriter.sanitize_name  s2     19,,;K((..r:   )r7   r   r7   r   returnNone)r   r"   r   r   )r   intr   r   )r   r   r   r   r   r   )	__name__
__module____qualname__r   r   r   r   r   __classcell__)r   s   @r9   r   r   {  s    .->/r:   r   c                  $    e Zd ZddZddZddZy)SpecialStyleTablec                     || _         || _        y rz   )stylesextra_styles)r   r   r  s      r9   r   zSpecialStyleTable.__init__  s    (r:   c                   t        | j                  j                  j                               }| j                  j                  j                         D ]2  \  }}| j                  j                  |      r"|j                  |       4 |S rz   )listr   entriesvaluesr  items	has_entryappend)r   r  r   extra_styles       r9   get_text_stylesz!SpecialStyleTable.get_text_styles  sm    t{{**1134!%!2!2!:!:!@!@!B 	,D+;;((.{+	, r:   c                    | j                         }|j                  dd       |j                  dd       |j                  dt        |             |D ]  }|j                  |        |j                  dd       y )Nr   TABLEr   STYLEF   ENDTAB)r
  r   r|   r~   )r   r   text_stylesstyles       r9   r~   zSpecialStyleTable.export_dxf  sr    **,Q(Q(R[!12  	(EY'	(Q)r:   N)r   r)   r  r)   )r   zlist[Textstyle])r   r!   r   r   )r   r   r   r   r
  r~    r:   r9   r   r     s    )*r:   r   z0
EOF
c                    d}| D ]?  }|j                         }|j                  d      s%	 t        |dd        }t	        ||      }A |dz   S # t        $ r Y Rw xY w)Nr   *r      )upperr   r   
ValueErrormax)names
max_numberr   numbers       r9   detect_max_block_numberr    sr    J -zz|s#	ab]F V,
- >  		s   A	AAc                      e Zd ZdddZddZedd       ZdddZddZddZ	ddZ
dd	Zdd
ZddZddZddZd dZd!dZddZd"dZd#dZddZd$dZy)%r+   c                   t        |t              sJ || _        t        t	                     | _        t        |      | _        d| _        d| _	        t        j                  d      | _        t        |j                  D cg c]  }|j                  j                    c}      | _        t$        t&        t(        t*        t,        t.        t.        t.        t.        t0        t2        t4        d| _        y c c}w )N   r   R12)
LWPOLYLINEMESHSPLINEELLIPSEMTEXTLEADERMLEADERMULTILEADERMLINEHATCHMPOLYGON
ACAD_TABLE)r{   r   _docr   r   
_tagwriterfloatr2   r   r   r0   rM   
_extra_docr  block_recordsrN   r   _next_block_numberr   r   r   r   r   r   r   r   r   	exporters)r   r6   r2   brs       r9   r   zR12Exporter.__init__  s    #w'''	&xz2 -() )*!))E*"9#&#4#45RRVV[[5#
 ,#%!-.2,!'+O
	 6s   =C+c                    | j                   |= y rz   )r3  )r   entity_types     r9   disable_exporterzR12Exporter.disable_exporter  s    NN;'r:   c                    | j                   S rz   )r-  r   s    r9   r6   zR12Exporter.doc  s    yyr:   Nc                T    || j                   j                  |       | j                   S rz   )r.  r   r   s     r9   r   zR12Exporter.tagwriter  s$    OO&&v.r:   c                B    |j                  | j                                y)z"Write DXF document to text stream.N)r.   	to_stringr   s     r9   r.   zR12Exporter.write  s    T^^%&r:   c                    | j                         }| j                         }dj                  | j                         | j	                         ||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      r9   r<  zR12Exporter.to_string  sW     002--/ww,,.,,.
 	
r:   c                R    d| | j                    }| xj                   dz  c_         |S )Nr  r  )r2  )r   charr   s      r9   next_block_namezR12Exporter.next_block_name  s/    4&00121$r:   c                    | j                  d      }| j                  j                  j                  ||j                  j                  dd      t        j                  d      S )NUrE   r   )rE   rH   rI   )rG  r0  rD  rM   rN   getr	   BLK_ANONYMOUS)r   rw   r   s      r9   r   zR12Exporter.new_block  sW    ##C(%%))5,, * 
 	
r:   c                    t               }| j                  j                  j                  | j	                  |             |j                         S rz   )r   r6   headerr~   r   getvaluer   in_memory_streams     r9   rA  z#R12Exporter.export_header_to_string  s9    #:""4>>2B#CD((**r:   c                Z   t               }| j                  j                  }|j                  }t	        | j                  j                  | j
                  j                        |_        | j                  |      }d|_        |j                  |       ||_        d|_        |j                         S )NTF)
r   r6   tablesr   r   r0  r   r   r~   rN  )r   rP  rR  preserve_tabler   s        r9   rB  z#R12Exporter.export_tables_to_string"  s    #:)$((//4??;Q;QRNN#34	#	)$&$	((**r:   c                r   t               }| j                  j                  |       | j                  d       | j                  j
                  D ]U  }|j                  r|j                  s|j                  j                  j                         }|dv rE| j                  |       W | j                         }t        |      rY|D ]8  }| j                  |       | j                  |j                  j                         : | j                         }t        |      rY| j                          |j!                         S )NBLOCKS)z$model_spacez$paper_space)r   r.  r   _write_section_headerr6   r1  is_any_paperspaceis_active_paperspacerN   r   lower_export_block_recordget_extra_blocksr|   discard_extra_block_write_endsecrN  )r   rP  block_recordr   extra_blockss        r9   r?  z#R12Exporter.export_blocks_to_string0  s   #:""#34""8, HH22 	4L--l6W6W##((..0D77 %%l3	4 ,,., , @)),7(()9)9)>)>?@  002L , 	((**r:   c                N    | j                   j                  j                  |       y rz   )r0  r1  discardr   r   s     r9   r\  zR12Exporter.discard_extra_blockJ  s    %%--d3r:   c                    | j                   j                  D cg c]*  }|j                  j                  j	                  d      s)|, c}S c c}w )Nz*U)r0  r1  rN   r   r   )r   r4  s     r9   r[  zR12Exporter.get_extra_blocksM  s?    66
"&&++:P:PQU:VB
 	
 
s   *AAc                    t        || j                        5 }|j                  |d       d d d        y # 1 sw Y   y xY w)NF)destroy)r   r0  explode)r   mtextr   xpls       r9   r   zR12Exporter.explode_mtextR  s7    &$//2 	.cKKuK-	. 	. 	.s   4=c                z   t               }| j                  j                  |       | j                  d       | j	                  | j
                  j                         j                         | j	                  | j
                  j                         j                         | j                          |j                         S )NENTITIES)r   r.  r   rV  r   r6   
modelspacer   
paperspacer]  rN  rO  s     r9   r>  z$R12Exporter.export_layouts_to_stringV  s    #:""#34"":.  !4!4!6!C!CD  !4!4!6!C!CD((**r:   c                   | j                   }|j                  J |j                  j                  |       |j                  s| j	                  |j
                         |j                  J |j                  j                  |       y rz   )r.  r   r~   is_any_layoutr   r   endblk)r   r^  r   s      r9   rZ  z R12Exporter._export_block_record`  st    OO	!!---%%i0))$$\%>%>?""...&&y1r:   c                D   | j                   }|D ]  }|j                  t        j                  kD  st	        |t
              rO| j                  j                  |j                               }|r
 || |       e|j                  srt        | |       |j                  |        y rz   )r.  MIN_DXF_VERSION_FOR_EXPORTr	   r   r{   r   r3  rJ  rv   r   r   r~   )r   spacer   rw   r8   s        r9   r   zR12Exporter.export_entity_spacei  s    OO	 	-F005;;>*C  >>--fnn.>?T6*''(v6!!),	-r:   c                B    | j                   j                  d| d       y )Nz  0
SECTION
  2

)r.  	write_strrb  s     r9   rV  z!R12Exporter._write_section_headerx  s    !!$7vR"@Ar:   c                <    | j                   j                  dd       y )Nr   ENDSEC)r.  r   r9  s    r9   r]  zR12Exporter._write_endsec{  s    ""1h/r:   )r/   )r6   r   r2   r/  )r6  r   )r   r   rz   )r7   zOptional[TextIO]r   r   r   )r   r   )rF  r   r   r   )rw   r   r   r   )r   r   r   r   )r   zlist[BlockRecord])rg  r   r   r*   )r^  r   )rr  r   )r   r   )r   r   r   r   r7  propertyr6   r   r.   r<  rG  r   rA  rB  r?  r\  r[  r   r>  rZ  r   rV  r]  r  r:   r9   r+   r+     sq    
8(  
'
"

+
++44

.+2-B0r:   )r6   r   r2   r/  r   r   )r6   r   r7   r   r2   r/  r   r   )r6   r   rB   zstr | os.PathLiker2   r/  r   r   )rS   r   r2   r/  rT   r   r   r   )rX   r   r2   r/  rT   r   r   r   )ri   r   r   r   )ro   r   r   r   )rw   r   r   r   )r8   r+   rw   r   )r   r   rw   r   r   r   )rw   r   r   r   r   r   )
rw   r   r   r   r2   r/  rT   r   r   r   )r8   r+   rw   r   r   r   )r  z	list[str]r   r   )[__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-   rV   rY   rk   rq   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rC  r  r+   r  r:   r9   <module>r     s   # < < 	    + + "     . % & 4 > 7 , , - $ //
7			7	# 3> 
 AL $ HS
	
-
?D
	
.!&69"#(8; 4	?
2722 8? 	+66:999 9 	9
 
9(

8,R -+/9 +/\* *, l0 l0r:   