
    Og`                        d dl mZ d dlmZ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mZmZmZmZmZmZmZmZ erd dlmZ d dlmZmZ  ej8                  d	      Zg d
Zh dZ G d d      Z dddZ!dddZ"y)    )annotations)TYPE_CHECKINGIterablecastUnionOptionalN)const)ARROWS)		DXFEntity
DXFGraphicHatchPolylineDimStyle	DimensionViewportLinetypeInsert)Drawing)
BaseLayoutLayoutezdxf)	linetypeslayersstyles	dimstyles>   ARCRAYLINEMESHTEXTHATCHMTEXTPOINTSHAPESOLIDTRACEXLINE3DFACEATTDEFATTRIBCIRCLEINSERTLEADERSPLINEELLIPSEPOLYLINEVIEWPORT	DIMENSION
LWPOLYLINEc                  $   e Zd ZdZddZd dZd!dZd"dZ	 d#	 	 	 d$dZ	 d#	 	 	 	 	 d%dZ	d&dZ
d d	Zd'd
Z	 d(	 	 	 	 	 d)dZd*dZd+dZd,dZd-dZd.dZ	 d(	 	 	 	 	 d/dZd(d0dZd1dZd1dZd2dZd3d4dZd5d6dZd Zd2dZd2dZd Zd Zd2dZy)7ImporteraI  
    The :class:`Importer` class is central element for importing data from
    other DXF documents.

    Args:
        source: source :class:`~ezdxf.drawing.Drawing`
        target: target :class:`~ezdxf.drawing.Drawing`

    Attributes:
        source: source DXF document
        target: target DXF document
        used_layers: Set of used layer names as string, AutoCAD accepts layer
            names without a LAYER table entry.
        used_linetypes: Set of used linetype names as string, these linetypes
            require a TABLE entry or AutoCAD will crash.
        used_styles: Set of used text style names, these text styles require
            a TABLE entry or AutoCAD will crash.
        used_dimstyles: Set of used dimension style names, these dimension
            styles require a TABLE entry or AutoCAD will crash.

    c                   || _         || _        t               | _        t               | _        t               | _        t               | _        t               | _        t               | _        t               | _
        t               | _        t               | _        |j                  d   j                  j                   | _        |j$                  d   j                  j                   | _        y )NNormalGlobal)sourcetargetsetused_layersused_linetypesused_stylesused_shape_filesused_dimstylesused_arrowsdicthandle_mappinglistimported_insertsimported_blocks
plotstylesdxfhandle_default_plotstyle_handle	materials_default_material_handle)selfr9   r:   s      Z/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/addons/importer.py__init__zImporter.__init__O   s    %%%(U(+%(U*-%(+%(U.2f 26 04v)/):):8)D)H)H)O)O&(.(8(8(B(F(F(M(M%    c                   | j                   j                  |j                  dd             | j                  j                  |j                  dd             |j	                  d      r+| j
                  j                  |j                  dd             |j	                  d      r,| j                  j                  |j                  dd             yy)	zRegister used resources.layer0linetypeBYLAYERstyleStandarddimstyleN)r<   addget_dxf_attribr=   is_supported_dxf_attribr>   r@   rM   entitys     rN   _add_used_resourceszImporter._add_used_resourcesd   s    V227C@A 5 5j) LM))'2  !6!6w
!KL))*5##F$9$9*j$QR 6rP   c                   | j                   j                  |j                  dd             | j                  j                  |j                  dd             | j                  j                  |j                  dd             | j                  j                  |j                  dd             | j                  j                  |j                  dd             | j                  j                  |j                  d	d             | j                  j                  |j                  d
d             | j                  j                  |j                  dd             y )NdimtxstyrW   dimltyperU   dimltex1dimltex2dimblk dimblk1dimblk2	dimldrblk)r>   rY   rZ   r=   rA   )rM   rX   s     rN   _add_dimstyle_resourcesz Importer._add_dimstyle_resourcesm   s
   X44ZLM 7 7
I NO 7 7
I NO 7 7
I NOX44XrBCX44YCDX44YCDX44["EFrP   c                   |j                   j                         sy |j                   j                         }| j                  j                  j                  |      }|y |j                  j                  dk(  r0| j                  j                  |j                  j                         y | j                  j                  |j                  j                         y )Nre   )pattern_tagsis_complex_typeget_style_handler9   entitydbgetrH   namer?   rY   fontr>   )rM   rT   style_handlerV   s       rN   _add_linetype_resourcesz Importer._add_linetype_resourcesw   s    $$446,,==?$$((6=99>>R!!%%eiinn5  0rP   c                x    t        |t              r|dk(  rt        }n|f}|D ]  }| j                  |d|        y)a  Import DXF tables from the source document into the target document.

        Args:
            table_names: iterable of tables names as strings, or a single table
                name as string or "*" for all supported tables
            replace: ``True`` to replace already existing table entries else
                ignore existing entries

        Raises:
            TypeError: unsupported table type

        *)entriesreplaceN)
isinstancestrIMPORT_TABLESimport_table)rM   table_namesrw   
table_names       rN   import_tableszImporter.import_tables   sH     k3'c!+*n% 	HJj#wG	HrP   c           	        |t         vrt        d| d      t        | j                  j                  |      }t        | j
                  j                  |      }t        |t              r|dk(  r
d |D        }n|f}|D ]G  }	 |j                  |      }|j                  j                  }||v ra|rBt        j                  d| d	| d
       |j!                  |j                  j                         nt        j                  d| d	| d
       |dk(  r,| j"                  j%                  |j'                  dd             n-|dk(  r| j)                  |       n|dk(  r| j+                  |       | j-                  |      }|j/                  |       |j                  j0                  | j2                  |j                  j0                  <   J y# t        j                  $ r  t        j                  d| d| d       Y ~w xY w)a  
        Import specific table entries from the source document into the
        target document.

        Args:
            name: valid table names are "layers", "linetypes" and "styles"
            entries: Iterable of table names as strings, or a single table name
                or "*" for all table entries
            replace: ``True`` to replace the already existing table entry else
                ignore existing entries

        Raises:
            TypeError: unsupported table type

        zTable "z" import not supported.ru   c              3  H   K   | ]  }|j                   j                    y wN)rH   rp   ).0entrys     rN   	<genexpr>z(Importer.import_table.<locals>.<genexpr>   s     De599>>Ds    "zRequired table entry "z" in table fz not found.z"Replacing already existing entry "z" of z table.z#Discarding already existing entry "r   rT   
Continuousr   r   N)rz   	TypeErrorgetattrr9   tablesr:   rx   ry   ro   r	   DXFTableEntryErrorloggerwarningrH   rp   debugremover=   rY   rZ   ri   rs   _duplicate_table_entry	add_entryrI   rC   )	rM   rp   rv   rw   source_tabletarget_table
entry_nametable_entrynew_table_entrys	            rN   r{   zImporter.import_table   s   $ }$gdV+BCDDt{{1148t{{1148gs##~D|D"*! &	UJ*..z: %--J\)LL<ZL I"V7, !''(<(<=LL=j\ J"V7, x##''..z<H $,,[9$,,[9 #99+FO""?3 ;J:M:M:T:TD 6 67M&	U ++ ,ZLTF K! " s   G/G;:G;c                L   |D ]  }| j                   j                  j                  |      }| j                  j                  j	                  |      }|r8|j
                  j                  | j                  |j
                  j                  <   t        j                  d| d        y)zImport shape file table entries from the source document into the
        target document.
        Shape file entries are stored in the styles table but without a name.

        zRequired shape file entry "" not found.N)
r9   r   find_shxr:   get_shxrH   rI   rC   r   r   )rM   fontsrq   r   r   s        rN   import_shape_fileszImporter.import_shape_files   s      	QD++,,55d;K"kk0088>O>M>Q>Q>X>X##KOO$:$:;!<TF,OP	QrP   c                    | j                   |_        |j                  j                  d      r| j                  |j                  _        |j                  j                  d      r| j                  |j                  _        y y )Nplotstyle_handlematerial_handle)r:   docrH   hasattrrJ   r   rL   r   r\   s     rN   _set_table_entry_dxf_attribsz%Importer._set_table_entry_dxf_attribs   s\    [[
::01*.*H*HFJJ'::/0)-)F)FFJJ& 1rP   c                    t        |      }| j                  |       | j                  j                  j	                  |       |S r   )new_clean_entityr   r:   rn   rY   )rM   r   	new_entrys      rN   r   zImporter._duplicate_table_entry   s:    $U+	))%0 	  +rP   Nc                     fd}| j                   j                         }n.|j                   j                   k7  rt        j                  d      |j                         }|t        vr#t        j                  dt        |       d       y j                  |       	 t        t        t        |            } ||        j                   j                  j!                  |       |j#                  |       	  t%         d|j'                         z         |       y# t        j                  $ r t        j                  d| d       Y yw xY w# t(        $ r Y yw xY w)	a  
        Imports a single DXF `entity` into `target_layout` or the modelspace
        of the target document, if `target_layout` is ``None``.

        Args:
            entity: DXF entity to import
            target_layout: any layout (modelspace, paperspace or block) from
                the target document

        Raises:
            DXFStructureError: `target_layout` is not a layout of target document

        c                    j                   | _        | j                  j                  d       | j                  j                  d       | j                  j                  d       y )Nr   r   visualstyle_handle)r:   r   rH   discard)erM   s    rN   set_dxf_attribsz/Importer.import_entity.<locals>.set_dxf_attribs  sA    KKAEEEMM,-EEMM+,EEMM./rP   NzCTarget layout has to be a layout or block from the target document.z
Import of z not supportedzCopying for DXF type z not supported._import_)r:   
modelspacer   r	   DXFStructureErrordxftypeIMPORT_ENTITIESr   r   ry   r^   r   r   r   DXFTypeErrorrn   rY   
add_entityr   lowerAttributeError)rM   r]   target_layoutr   r   
new_entitys   `     rN   import_entityzImporter.import_entity   s5   "	0   KK224M$++-))X  .."/)LL:c&k].AB  (	j*:6*BCJ
 	
#  ,  ,	7GD*w}}67
C !! 	LL0	IJ	  		s$   D 7#E ,E
	E
	EEc                r    | j                   j                  |       |j                  D ]  }t        |        y r   )rE   appendattribsremove_dependencies)rM   insertattribs      rN   _import_insertzImporter._import_insert3  s1    $$V,nn 	(F'	(rP   c                <    |j                   D ]  }t        |        y r   )verticesr   )rM   polylinevertexs      rN   _import_polylinezImporter._import_polyline9  s    '' 	(F'	(rP   c                :    |j                   j                  d       y )NassociativerH   r   )rM   hatchs     rN   _import_hatchzImporter._import_hatch>  s    		-(rP   c                V   |j                   j                  d       |j                   j                  d       |j                   j                  d       |j                   j                  d       |j                   j                  d       |j                   j                  d       |j                   j                  d       |j                   j                  d       |j                   j                  d	       |j                   j                  d
       |j                   j                  d       y )N
sun_handleclipping_boundary_handle
ucs_handleucs_base_handlebackground_handleshade_plot_handlevisual_style_handleref_vp_object_1ref_vp_object_2ref_vp_object_3ref_vp_object_4r   )rM   viewports     rN   _import_viewportzImporter._import_viewportA  s    \*78\*./010123././././rP   c                    |j                   rZ|j                   D ]J  }t        |t              r'| j                  |j                  j
                  d       | j                  |       L y t        j                  d       y )NFrenamez9The required geometry block for DIMENSION is not defined.)	virtual_block_contentrx   r   import_blockrH   rp   r^   r   error)rM   	dimensionr]   s      rN   _import_dimensionzImporter._import_dimensionN  sb    **#99 1ff-%%fjjooe%D((01
 LLTUrP   c                6    |D ]  }| j                  ||        y)a  Import all `entities` into `target_layout` or the modelspace of the
        target document, if `target_layout` is ``None``.

        Args:
            entities: Iterable of DXF entities
            target_layout: any layout (modelspace, paperspace or block) from
                the target document

        Raises:
            DXFStructureError: `target_layout` is not a layout of target document

        N)r   )rM   entitiesr   r]   s       rN   import_entitieszImporter.import_entitiesW  s#    "  	6Fv}5	6rP   c                Z    | j                  | j                  j                         |       y)a~  Import all entities from source modelspace into `target_layout` or
        the modelspace of the target document, if `target_layout` is ``None``.

        Args:
            target_layout: any layout (modelspace, paperspace or block) from
                the target document

        Raises:
            DXFStructureError: `target_layout` is not a layout of target document

        r   N)r   r9   r   )rM   r   s     rN   import_modelspacezImporter.import_modelspacek  s$     	T[[335]SrP   c                ^     fd}dd}j                         dk(  r j                  j                         S  j                  j                  j                        } |       } ||j                  j                               } j                  j                  j                  ||      }|S )aX  Recreate source paperspace layout `name` in the target document.
        The layout will be renamed if `name` already exist in the target
        document. Returns target modelspace for layout name "Model".

        Args:
            name: layout name as string

        Raises:
            KeyError: if source layout `name` not exist

        c                     } }d}| j                   j                  v r,|t        |      z   } |dz  }| j                   j                  v r,| S )N   )r:   layoutsry   )tname	base_namecountrp   rM   s      rN   get_target_namez8Importer.recreate_source_layout.<locals>.get_target_name  sV    EIE4;;...!CJ.
 4;;... LrP   c                     d
 fd} |d        |d        |d        |d        |d        |d        |d        |d	        S )Nc                ,    	 | = y # t         $ r Y y w xY wr   )KeyError)rp   
dxfattribss    rN   r   z?Importer.recreate_source_layout.<locals>.clear.<locals>.discard  s"    "4( s    	rI   ownertaborderr   block_record_handleviewport_handler   base_ucs_handle)rp   ry    )r   r   s   ` rN   clearz.Importer.recreate_source_layout.<locals>.clear  sT     HGJ'()*%&L!%&rP   model)r   )r   rB   returnrB   )	r   r:   r   r9   r   ro   
dxf_layoutr   new)rM   rp   r   r   source_layouttarget_namer   r   s   ``      rN   recreate_source_layoutzImporter.recreate_source_layouty  s    		" ::<7";;))++++//5%'=33>>@A
++//
/SrP   c                    |j                         dk(  rt        j                  d      | j                  j                  j                  |      }| j                  |      }| j                  ||       |S )a?  Import paperspace layout `name` into the target document.

        Recreates the source paperspace layout in the target document, renames
        the target paperspace if a paperspace with same `name` already exist
        and imports all entities from the source paperspace into the target
        paperspace.

        Args:
            name: source paper space name as string

        Returns: new created target paperspace :class:`Layout`

        Raises:
            KeyError: source paperspace does not exist
            DXFTypeError: invalid modelspace import

        r   z:Can not import modelspace, use method import_modelspace().)r   r	   r   r9   r   ro   r   r   )rM   rp   r   r   s       rN   import_paperspace_layoutz!Importer.import_paperspace_layout  sj    $ ::<7"$$L  ++//533D9]M:rP   c                    | j                   j                  j                         D ]'  }|j                         dk7  s| j	                  |       ) y)zImport all paperspace layouts and their content into the target
        document.
        Target layouts will be renamed if a layout with the same name already
        exist. Layouts will be imported in original tab order.

        r   N)r9   r   names_in_taborderr   r   )rM   rp   s     rN   import_paperspace_layoutsz"Importer.import_paperspace_layouts  sB     KK''99; 	4Dzz|w&--d3	4rP   c                8    |D ]  }| j                  ||        y)a  Import all BLOCK definitions from source document.

        If a BLOCK already exist the BLOCK will be renamed if argument
        `rename` is ``True``, otherwise the existing BLOCK in the target
        document will be used instead of the BLOCK from the source document.
        Required name resolving for imported BLOCK references (INSERT), will be
        done in the :meth:`Importer.finalize` method.

        Args:
            block_names: names of BLOCK definitions to import
            rename: rename BLOCK if a BLOCK with the same name already exist in
                target document

        Raises:
            ValueError: BLOCK in source document not found (defined)

        r   N)r   )rM   block_namesr   
block_names       rN   import_blockszImporter.import_blocks  s&    $ & 	9Jj8	9rP   c                x   dfd}	 | j                      S # t        $ r Y nw xY w	 | j                  j                     }n&# t        j
                  $ r t        d d      w xY w| j                  j                  v r|du r| j                   <   S  |       }|j                  }|J j                  ||j                  j                  |j                  j                  |j                  j                  |j                  j                  d      }| j                  ||       || j                   <   |S )	az  Import one BLOCK definition from source document.

        If the BLOCK already exist the BLOCK will be renamed if argument
        `rename` is ``True``, otherwise the existing BLOCK in the target
        document will be used instead of the BLOCK in the source document.
        Required name resolving for imported block references (INSERT), will be
        done in the :meth:`Importer.finalize` method.

        To replace an existing BLOCK in the target document, just delete it
        before importing data:
        :code:`target.blocks.delete_block(block_name, safe=False)`

        Args:
            block_name: name of BLOCK to import
            rename: rename BLOCK if a BLOCK with the same name already exist in
                target document

        Returns: (renamed) BLOCK name

        Raises:
            ValueError: BLOCK in source document not found (defined)

        c                 H    d} }|v rt        |       z   }| dz  } |v r|S )Nr   r   )ry   )numrp   r  target_blockss     rN   get_new_block_namez1Importer.import_block.<locals>.get_new_block_name  s=    CD-'!CH,q -' KrP   zSource block "r   F)descriptionflags	xref_path)
base_pointr   r   )r   ry   )rF   r   r9   blocksr	   DXFKeyError
ValueErrorr:   blockr   rH   r  r  r  r  r   )	rM   r  r   r
  source_blocknew_block_namer  target_blockr	  s	    `      @rN   r   zImporter.import_block  sC   2		''
33 			H;;--j9L   	H~j\FGG	H **-'fo/9D  ,+-""   $((yy++$yy44"YY00 ) 
 	\F+9Z(s    	&&A #A'c                    | j                   j                  d       | j                   D ]F  }t        j                  |      r| j                  j                  |       4| j                  |d       H y)zCreate or import required arrow blocks, used by the LEADER or the
        DIMSTYLE entity, which are not imported automatically because they are
        not used in an anonymous DIMENSION geometry BLOCK.

        re   Fr   N)rA   r   r
   is_acad_arrowr:   acquire_arrowr   )rM   
arrow_names     rN   _create_missing_arrowszImporter._create_missing_arrows%  se     	  	
 ** 	<J##J/))*5!!*U!;		<rP   c                   t        | j                        rpt        | j                        }g | _        |D ]8  }| j                  |j                  j
                        }||j                  _        : t        | j                        royy)a  Resolve BLOCK names of imported BLOCK reference entities (INSERT).

        This is required for the case the name of the imported BLOCK collides
        with an already existing BLOCK in the target document and the conflict
        resolving method is "rename".

        N)lenrE   rD   r   rH   rp   )rM   insertsr   r  s       rN   _resolve_insertszImporter._resolve_inserts4  sl     $''(4001G$&D!! -!..vzz?
",

-	 $''(rP   c                   t        | j                        r| j                  d| j                         t        | j                        r| j                  d| j                         t        | j                        r| j                  d| j                         t        | j
                        r| j                  d| j
                         t        | j                        r| j                  | j                         | j                          y)zlImport required table entries collected while importing entities
        into the target document.

        r   r   r   r   N)	r  r@   r{   r<   r=   r>   r?   r   update_complex_linetypesrM   s    rN   _import_required_table_entriesz'Importer._import_required_table_entriesD  s     t""#k4+>+>? t h(8(89 t""#k4+>+>? t h(8(89 t$$%##D$9$9: 	%%'rP   c                    | j                   D ]9  }	 | j                  j                  j                  |      }| j                  |       ; y # t        j
                  $ r Y Rw xY wr   )r=   r9   r   ro   r	   r   rs   )rM   
ltype_nameltypes      rN   (_add_required_complex_linetype_resourcesz1Importer._add_required_complex_linetype_resourcesa  s`    -- 	0J--11*= ((/	0 ++ s   %A

A A c                   | j                   j                  j                  d      j                  j                  }| j                   j
                  D ]n  }|j                  j                         s|j                  j                         }| j                  j                  ||      }|j                  j                  |       p y )NSTANDARD)r:   r   ro   rH   rI   r   rk   rl   rm   rC   set_style_handle)rM   
std_handlerT   
old_handle
new_handles        rN   r   z!Importer.update_complex_linetypesi  s    [[''++J7;;BB
-- 	CH$$446%22CCE
!0044ZL
%%66zB		CrP   c                    | j                          | j                          | j                          | j                          y)a  Finalize the import by importing required table entries and BLOCK
        definitions, without finalization the target document is maybe invalid
        for AutoCAD. Call the :meth:`~Importer.finalize()` method as last step
        of the import process.

        N)r  r&  r"  r  r!  s    rN   finalizezImporter.finalizeq  s4     	557++-##%rP   )r9   r   r:   r   )r]   r   r   None)rX   r   r   r/  )rT   r   r   r/  )ru   F)r|   Union[str, Iterable[str]]r   r/  )rp   ry   rv   r0  r   r/  )r   zset[str]r   r/  )r   r   r   r   r   )r]   r   r   Optional[BaseLayout]r   r/  )r   r   )r   r   )r   r   )r   r   )r   r   )r   zIterable[DXFEntity]r   r1  r   r/  )r   r1  r   r/  )rp   ry   r   r   )r   r/  F)r  zIterable[str]r   r/  )T)r  ry   r   ry   ) __name__
__module____qualname____doc__rO   r^   ri   rs   r~   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r"  r&  r   r.  r   rP   rN   r5   r5   8   s    ,N*SG1 EJH4H	H0 LQBUBU";BU	BUHQ G HL220D2	2h((
)0V /36%6 ,6 
	6(T/b6	49*>@<- (:0C
&rP   r5   c                J    | j                         }d|_        t        ||      S )zCopy entity and remove all external dependencies.

    Args:
        entity: DXF entity
        keep_xdata: keep xdata flag

    N)
keep_xdata)copyr   r   )r]   r8  r   s      rN   r   r   ~  s#     JJNzjAArP   c                B    d| _         d| _        d| _        |sd| _        | S )zqRemove all external dependencies.

    Args:
        entity: DXF entity
        keep_xdata: keep xdata flag

    N)appdatareactorsextension_dictxdata)r]   r8  s     rN   r   r     s)     FNFO FMrP   r2  )r]   r   r8  boolr   r   )#
__future__r   typingr   r   r   r   r   loggingezdxf.lldxfr	   ezdxf.render.arrowsr
   ezdxf.entitiesr   r   r   r   r   r   r   r   r   ezdxf.documentr   ezdxf.layoutsr   r   	getLoggerr   rz   r   r5   r   r   r   rP   rN   <module>rI     si    # A A   &
 
 
 &0			7	#>8C	& C	&L
BrP   