
    OgK                    ~   U d Z ddlmZ ddlmZmZmZmZ ddlm	Z	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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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,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6m7Z7 g dZ8 ejr                  d      Z:dZ;h dZ<dZ=dZ>ee!ge?f   Z@deAd<   eeBgef   ZCdeAd<   dRdZD G d deE      ZF G d deF      ZG G d deF      ZH G d deF      ZI G d d eF      ZJ G d! d"ej                        ZLdSd#ZMdTdUd%ZNdVd&ZOd'd(d)d$d*	 	 	 	 	 	 	 	 	 	 	 	 	 dWd+ZPdXd,ZQd eR       eLj                  d-	 	 	 	 	 	 	 dYd.ZTd$d/	 	 	 	 	 dZd0ZUd$d/	 	 	 	 	 d[d1ZVd'd2d\d3ZWdeLj                  f	 	 	 	 	 	 	 d]d4ZYdeLj                  f	 	 	 	 	 	 	 d^d5ZZ G d6 d7e	      Z[ G d8 d9e	      Z\ G d: d;      Z] G d< d=e]      Z^d_d>Z_ G d? d@e]      Z` G dA dBe]      Za G dC dDe]      Zb G dE dF      Zcd`dGZd G dH dI      Ze G dJ dK      ZfdadLZgdbdMZhdcdNZidddOZj G dP dQ      Zky)ezN Resource management module for transferring DXF resources between documents.
    )annotations)OptionalSequenceCallableIterable)Protocol	TypeAliasN)const	validatortypes)Tags)DXFInfo)Drawing)
BaseLayout
PaperspaceBlockLayout)is_graphic_entityis_dxf_object	DXFEntityDXFClassfactoryBlockRecordLayerLinetype	TextstyleDimStyleUCSTableEntryMaterial
MLineStyleMLeaderStyleBlockEndBlkInsert	DXFLayoutVisualStyle)CopyStrategyCopySettings)UVecVec3)defineattachembeddetachwrite_blockload_modelspaceload_paperspaceRegistryResourceMapperConflictPolicyLoaderdxf_infor   	XrefErrorXrefDefinitionErrorEntityErrorLayoutErrorezdxf0>   BYBLOCKBYLAYER
CONTINUOUSSTANDARDr	   FilterFunctionLoadFunctionc                <    | D ]  }t         j                  |        y N)loggerdebug)messagesmsgs     O/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/xref.py_log_debug_messagesrI   J   s     S    c                      e Zd ZdZy)r6   z"base exception for the xref moduleN)__name__
__module____qualname____doc__ rJ   rH   r6   r6   O   s    ,rJ   r6   c                      e Zd Zy)r7   NrL   rM   rN   rP   rJ   rH   r7   r7   U       rJ   r7   c                      e Zd Zy)r8   NrR   rP   rJ   rH   r8   r8   Y   rS   rJ   r8   c                      e Zd Zy)r9   NrR   rP   rJ   rH   r9   r9   ]   rS   rJ   r9   c                      e Zd Zy)InternalErrorNrR   rP   rJ   rH   rW   rW   a   rS   rJ   rW   c                  v    e Zd ZdZ ej
                         Z ej
                         Z ej
                         Zy)r3   aO  These conflict policies define how to handle resource name conflicts.

    .. versionadded:: 1.1

    Attributes:
        KEEP: Keeps the existing resource name of the target document and ignore the
            resource from the source document.
        XREF_PREFIX: This policy handles the resource import like CAD applications by
            **always** renaming the loaded resources to `<xref>$0$<name>`, where `xref`
            is the name of source document, the `$0$` part is a number to create a
            unique resource name and `<name>` is the name of the resource itself.
        NUM_PREFIX: This policy renames the loaded resources to `$0$<name>` only if the
            resource `<name>` already exists. The `$0$` prefix is a number to create a
            unique resource name and `<name>` is the name of the resource itself.

    N)	rL   rM   rN   rO   enumautoKEEPXREF_PREFIX
NUM_PREFIXrP   rJ   rH   r3   r3   e   s0    " 499;D$))+KJrJ   r3   c                   t        |       } t        j                  |       r;t        | d      5 }|j	                  d      }ddd       t        j
                        S t        j                  |       r-t        | dd      5 }t        j                  |      cddd       S t        d      # 1 sw Y   kxY w# 1 sw Y   yxY w)a  Scans the HEADER section of a DXF document and returns a :class:`DXFInfo`
    object, which contains information about the DXF version, text encoding, drawing
    units and insertion base point.

    Raises:
        IOError: not a DXF file or a generic IO error

    rbi    Nrtignore)errorszNot a DXF files.)	strr   is_binary_dxf_fileopenreadbinary_dxf_infois_dxf_filer5   IOError)filenamefpdatas      rH   r5   r5   |   s     8}H##H-(D! 	!R774=D	! ((..X&(D2 	*b%%b)	* 	* ())	! 	!	* 	*s   B)?B5)B25B>Fc                    || j                   v rt        d| d      | j                   j                  |t        |      |d       y)a  Add an external reference (xref) definition to a document.

    XREF attachment types:

    - attached: the XREF that's inserted into this drawing is also present in a
      document to which this document is inserted as an XREF.
    - overlay: the XREF that's inserted into this document is **not** present in a
      document to which this document is inserted as an XREF.

    Args:
        doc: host document
        block_name: name of the xref block
        filename: external reference filename
        overlay: creates an XREF overlay if ``True`` and an XREF attachment otherwise

    Raises:
        XrefDefinitionError: block with same name exist

    .. versionadded:: 1.1

    zblock 'z' already exist)flags	xref_path)name
dxfattribsN)blocksr7   newmake_xref_flags)doc
block_namerj   overlays       rH   r*   r*      sL    , SZZ!GJ<"GHHJJNN$W-!
  rJ   c                    | r!t         j                  t         j                  z  S t         j                  t         j                  z  S rC   )r
   BLK_XREF_OVERLAYBLK_EXTERNALBLK_XREFrw   s    rH   rt   rt      s1    %%(:(:::~~ 2 222rJ   )r   r   r         ?g        )insertscalerotationrw   c                   t        | |||       t               }|rt        |      |d<   |dk7  rt        |      }||d<   ||d<   ||d<   t        |      }| j	                         }	|	j                  |||      S )a  Attach the file `filename` to the host document as external reference (XREF) and
    creates a default block reference for the XREF in the modelspace of the document.
    The function raises an :class:`XrefDefinitionError` exception if the block definition
    already exist, but an XREF can be inserted multiple times by adding additional block
    references::

        msp.add_blockref(block_name, insert=another_location)

    .. important::

        If the XREF has different drawing units than the host document, the scale
        factor between these units must be applied as a uniform scale factor to the
        block reference!  Unfortunately the XREF drawing units can only be detected by
        scanning the HEADER section of a document by the function :func:`dxf_info` and
        is therefore not done automatically by this function.
        Advice: always use the same units for all drawings of a project!

    Args:
        doc: host DXF document
        block_name: name of the XREF definition block
        filename: file name of the XREF
        insert: location of the default block reference
        scale: uniform scaling factor
        rotation: rotation angle in degrees
        overlay: creates an XREF overlay if ``True`` and an XREF attachment otherwise

    Returns:
        Insert: default block reference for the XREF

    Raises:
        XrefDefinitionError: block with same name exist

    .. versionadded:: 1.1

    r|   r   r}   xscaleyscalezscale)r~   rq   )r*   dictfloatr)   
modelspaceadd_blockref)
ru   rv   rj   r~   r   r   rw   rq   locationmsps
             rH   r+   r+      s    Z 3
Hg6J!&x
:|e$
8$
8$
8F|H
..
CJxJOOrJ   c                \   t        j                  |       }|j                         r|S |j                  }|D ]`  }|j	                         s|j
                  }|j                         }|| z  }|j                         r|c S ||z  }|j                         s^|c S  t        j                  |       S )zReturns the path of the XREF file.

    Args:
        xref_filename: filename of the XREF, absolute or relative path
        search_paths: search paths where to look for the XREF file

    .. versionadded:: 1.1

    )pathlibPathexistsrp   is_dirparentresolve)xref_filenamesearch_pathsfilepathrp   pathsearch_paths         rH   	find_xrefr   4  s     ||M*H==D {{};;Dlln.??O%??O <<&&rJ   )load_fnr   conflict_policyc                  t        | t              sJ d       | j                  }|J d       | j                  }t        |t              sJ |j
                  st        d      |j                  j                  dd      }|st        d      |D cg c]  }t        j                  |       }}|j                  d|j                                t        ||      }	|	j                         st        d	|	 d
      |r |t!        |	            }
nt#        j$                  |	      }
|
j&                  |j&                  kD  rt)        j*                  d      t-        |
||      }|j/                  |        |j1                  | j2                         |j5                  t(        j6                  t(        j8                  z  d       |
j:                  j                  d      }|rt=        |      |j                  _        yyc c}w )a  Loads the modelspace of the XREF as content into a block layout.

    The loader function loads the XREF as `Drawing` object, by default the
    function :func:`ezdxf.readfile` is used to load DXF files. To load DWG files use the
    :func:`~ezdxf.addons.odafc.readfile` function from the :mod:`ezdxf.addons.odafc`
    add-on. The :func:`ezdxf.recover.readfile` function is very robust for reading DXF
    files with errors.

    If the XREF path isn't absolute the XREF is searched in the folder of the host DXF
    document and in the `search_path` folders.

    Args:
        xref: :class:`BlockLayout` of the XREF document
        load_fn: function to load the content of the XREF as `Drawing` object
        search_paths: list of folders to search for XREFS, default is the folder of the
            host document or the current directory if no filepath is set
        conflict_policy: how to resolve name conflicts

    Raises:
        XrefDefinitionError: argument `xref` is not a XREF definition
        FileNotFoundError: XREF file not found
        DXFVersionError: cannot load a XREF with a newer DXF version than the host
            document, try the :mod:`~ezdxf.addons.odafc` add-on to downgrade the XREF
            document or upgrade the host document

    .. versionadded:: 1.1

    z!expected BLOCK definition of XREFNvalid DXF document requiredz(argument 'xref' is not a XREF definitionro    zno xref path definedr   zfile not found: ''zCcannot embed a XREF with a newer DXF version into the host documentr   )xref_prefixF)state$INSBASE) 
isinstancer   ru   blockr!   is_xrefr7   dxfgetr   r   r~   get_abs_filepathr   r   FileNotFoundErrorrc   r:   readfile
dxfversionr
   DXFVersionErrorr4   r/   executerp   set_flag_stater{   rz   headerr)   
base_point)xrefr   r   r   
target_docr   ro   p_search_pathsr   
source_docloaderorigins                rH   r,   r,   T  s   F dK(M*MM(J!@#@@!JJEeU###==!"LMMYY]];3I!"899.:;W\\!_;M;J779:M2H??"3H:Q ?@@S]+
^^H-
z444##Q
 	
 J
OLF
4 
NNtyyN)	%*<*<<EJ"":.F#F|		 - <s   G5r|   c               l   | j                   }|J d       t        j                  |j                  | j                        }t        ||t        j                        }|j                  | |j                                |j                          | j                  |j                  d<   t        | ||       |S )a  Write the content of `block` into the modelspace of a new DXF document and
    convert `block` to an external reference (XREF).  The new DXF document has to be
    written by the caller: :code:`xref_doc.saveas(xref_filename)`.
    This way it is possible to convert the DXF document to DWG by the
    :mod:`~ezdxf.addons.odafc` add-on if necessary::

        xref_doc = xref.detach(my_block, "my_block.dwg")
        odafc.export_dwg(xref_doc, "my_block.dwg")

    It's recommended to clean up the entity database of the host document afterwards::

        doc.entitydb.purge()

    The function does not create any block references. These references should already
    exist and do not need to be changed since references to blocks and XREFs are the
    same.

    Args:
        block: block definition to detach
        xref_filename: name of the external referenced file
        overlay: creates an XREF overlay if ``True`` and an XREF attachment otherwise

    .. versionadded:: 1.1

    r   r   unitsr   r   r|   )ru   r:   rs   r   r   r4   r3   r[   load_block_layout_intor   r   r   r   block_to_xref)r   r   rw   r   r   r   s         rH   r-   r-     s    8 J!@#@@!j&;&;5;;OJJ
N<O<OPF
!!%)>)>)@A
NN$)$4$4Jj!%8rJ   c                   | j                          | j                  }|J d       t        |      |j                  _        t        |      |j                  _        y)zOConvert a block definition into an external reference.

    (internal API)
    Nzinvalid BlockLayout)delete_all_entitiesr   rc   r   ro   rt   rn   )r   r   rw   block_entitys       rH   r   r     sQ     
;;L#:%::#!$]!3L,W5LrJ   )r   c                  t        |       dk(  rt        j                         S t        d | D              rt	        d      | d   j
                  }|J d       t        j                  |j                  |j                        }t        ||      }|j                  t        | |j                                      |j                          t        |      |j                  d<   |S )a  Write `entities` into the modelspace of a new DXF document.

    This function is called "write_block" because the new DXF document can be used as
    an external referenced block.  This function is similar to the WBLOCK command in CAD
    applications.

    Virtual entities are not supported, because each entity needs a real database- and
    owner handle.

    Args:
        entities: DXF entities to write
        origin: block origin, defines the point in the modelspace which will be inserted
            at the insert location of the block reference

    Raises:
        EntityError: virtual entities are not supported

    .. versionadded:: 1.1

    r   c              3  L   K   | ]  }|j                   j                  d u   y wrC   )r   owner).0es     rH   	<genexpr>zwrite_block.<locals>.<genexpr>  s     
11155;;$
1s   "$z"virtual entities are not supportedz expected a valid source documentr   r   )lenr:   rs   anyr8   ru   r   r   r4   add_commandLoadEntitiesr   r   r)   r   )entitiesr   r   r   r   s        rH   r.   r.     s    * 8}yy{

1
11>??!J!E#EE!j&;&;:CSCSTJJ
+F
|Hj.C.C.EFG
NN$(LJj!rJ   c                d    t        | ||      }|j                  |       |j                          y)a  Loads the modelspace content of the source document into the modelspace
    of the target document.  The filter function `filter_fn` gets every source entity as
    input and returns ``True`` to load the entity or ``False`` otherwise.

    Args:
        sdoc: source document
        tdoc: target document
        filter_fn: optional function to filter entities from the source modelspace
        conflict_policy: how to resolve name conflicts

    .. versionadded:: 1.1

    r   	filter_fnN)r4   r/   r   )sdoctdocr   r   r   s        rH   r/   r/     s-    & D$@F
Y/
NNrJ   c                    | j                   |u rt        d      t        | j                   ||      }|j                  | |       |j	                          y)a  Loads the paperspace layout `psp` into the target document.  The filter function
    `filter_fn` gets every source entity as input and returns ``True`` to load the
    entity or ``False`` otherwise.

    Args:
        psp: paperspace layout to load
        tdoc: target document
        filter_fn: optional function to filter entities from the source paperspace layout
        conflict_policy: how to resolve name conflicts

    .. versionadded:: 1.1

    z8Source paperspace layout cannot be from target document.r   r   N)ru   r9   r4   load_paperspace_layoutr   )pspr   r   r   r   s        rH   r0   r0     sJ    & ww$TUUCGGT?CF
!!#!;
NNrJ   c                  X    e Zd Zef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
)r1   c                     y rC   rP   )selfentity	block_keys      rH   
add_entityzRegistry.add_entity(      rJ   c                     y rC   rP   )r   block_records     rH   	add_blockzRegistry.add_block+  r   rJ   c                     y rC   rP   r   handles     rH   
add_handlezRegistry.add_handle.  r   rJ   c                     y rC   rP   r   rp   s     rH   	add_layerzRegistry.add_layer1  r   rJ   c                     y rC   rP   r   s     rH   add_linetypezRegistry.add_linetype4  r   rJ   c                     y rC   rP   r   s     rH   add_text_stylezRegistry.add_text_style7  r   rJ   c                     y rC   rP   r   s     rH   add_dim_stylezRegistry.add_dim_style:  r   rJ   c                     y rC   rP   r   s     rH   add_block_namezRegistry.add_block_name=  r   rJ   c                     y rC   rP   r   s     rH   	add_appidzRegistry.add_appid@  r   rJ   Nr   r   r   rc   returnNoner   r   r   r   r   zOptional[str]r   r   rp   rc   r   r   )rL   rM   rN   NO_BLOCKr   r   r   r   r   r   r   r   r   rP   rJ   rH   r1   r1   '  s2    =E rJ   r1   c                      e Z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d	Z	 	 	 	 	 	 	 	 dd
Zdd	 	 	 	 	 	 	 ddZy)r2   c                     y rC   rP   r   r   defaults      rH   
get_handlezResourceMapper.get_handleE  r   rJ   c                     y rC   rP   r   s     rH   get_reference_of_copyz$ResourceMapper.get_reference_of_copyH  r   rJ   c                     y rC   rP   r   s     rH   	get_layerzResourceMapper.get_layerK  r   rJ   c                     y rC   rP   r   s     rH   get_linetypezResourceMapper.get_linetypeN  r   rJ   c                     y rC   rP   r   s     rH   get_text_stylezResourceMapper.get_text_styleQ  r   rJ   c                     y rC   rP   r   s     rH   get_dim_stylezResourceMapper.get_dim_styleT  r   rJ   c                     y rC   rP   r   s     rH   get_block_namezResourceMapper.get_block_nameW  r   rJ   c                     y rC   rP   r   r   s     rH   map_resources_of_copyz$ResourceMapper.map_resources_of_copyZ  r   rJ   c                     y rC   rP   )r   tagsnew_owner_handles      rH   map_pointerszResourceMapper.map_pointers]  r   rJ   c                     y rC   rP   )r   	dict_name
entry_namer   s       rH   map_acad_dict_entryz"ResourceMapper.map_acad_dict_entry`       	rJ   Foptionalc                    y rC   rP   )r   sourcecloneattrib_namer  s        rH   map_existing_handlez"ResourceMapper.map_existing_handlee  r  rJ   Nr;   r   rc   r   rc   r   rc   r   Optional[DXFEntity]rp   rc   r   rc   r   r   r   r   r   r	  r   r
  rc   r   r   r  rc   r  rc   r   r   r   ztuple[str, DXFEntity]r  r   r  r   r  rc   r   r   )rL   rM   rN   r   r   r   r   r   r  r  r  r  r  r  rP   rJ   rH   r2   r2   D  s    *-7@	 RW(1@C	rJ   r2   c                      e Zd ZddZddZy)LoadingCommandc                     y rC   rP   )r   registrys     rH   register_resourcesz!LoadingCommand.register_resourcesl      rJ   c                     y rC   rP   )r   transfers     rH   r   zLoadingCommand.executeo  r'  rJ   Nr%  r1   r   r   r)  	_Transferr   r   )rL   rM   rN   r&  r   rP   rJ   rH   r#  r#  k  s    rJ   r#  c                  4    e Zd ZdZ	 	 	 	 	 	 ddZddZddZy)	r   z0Loads all given entities into the target layout.c                n    || _         t        |t              st        dt	        |             || _        y )Ninvalid target layout type: )r   r   r   r9   typetarget_layout)r   r   r1  s      rH   __init__zLoadEntities.__init__v  s6     !-4 <T-=P<QRSS*rJ   c                t    | j                   D ])  }|j                  ||j                  j                         + y N)r   )r   r   r   r   r   r%  r   s      rH   r&  zLoadEntities.register_resources~  s0     	:AQUU[[9	:rJ   c                N   | j                   }| j                  D ]n  }|j                  |      }||j                  dt	        |              4t        |      r|j                  |       Q|j                  dt	        |       d       p t        |t              rt        |       y y )Nzxref:cannot copy found non-graphic entity  as layout content)
r1  r   get_entity_copyrE   rc   r   r   r   r    _reorganize_paperspace_viewports)r   r)  r1  r   r  s        rH   r   zLoadEntities.execute  s    **mm 
	F,,V4E}!23v;-@A '((//E
|;MN
	 mZ0,]; 1rJ   N)r   Sequence[DXFEntity]r1  r   r   r   r*  r+  )rL   rM   rN   rO   r2  r&  r   rP   rJ   rH   r   r   s  s,    :+++<F+	+:<rJ   r   c                   | j                         }|| j                         }| j                         D ]2  }|j                  j                  dk(  s||us"| j                  |       4 | j                  |j                  j                         y )N   )main_viewportadd_new_main_viewport	viewportsr   iddelete_entityset_current_viewport_handler   )
paperspacemain_vpvps      rH   r:  r:    sy    &&(G224""$ )6699>b/$$R() **7;;+=+=>rJ   c                  0    e Zd ZdZddZddZd	dZd
dZy)LoadPaperspaceLayoutzLoads a paperspace layout as a new paperspace layout into the target document.
    If a paperspace layout with same name already exists the layout will be renamed
    to  "<layout name> (x)" where x is 2 or the next free number.
    c                n    t        |t              st        dt        |             || _        || _        y )N invalid paperspace layout type: )r   r   r9   r0  paperspace_layoutr   r   r   r   s      rH   r2  zLoadPaperspaceLayout.__init__  s2    #z* @cLMM!$"rJ   c                    | j                   }|r#| j                  D cg c]  } ||      s| c}S t        | j                        S c c}w rC   )r   rK  list)r   r   r   s      rH   source_entitiesz$LoadPaperspaceLayout.source_entities  sB    NN	#55F!1AFF..// Gs
   AAc                    |j                  | j                  j                         | j                  j                  }| j	                         D ]  }|j                  ||        y r4  )r   rK  
dxf_layout
layout_keyrO  )r   r%  r   r   s       rH   r&  z'LoadPaperspaceLayout.register_resources  sX    D22==>**55	%%' 	8AY7	8rJ   c                   | j                   j                  }|j                  |j                  j                        }t        |t              sJ |j                  j                  j                  |j                  j                        }| j                         D ]P  }|j                  |      }|rt        |      r|j                  |       3|j                  dt!        |       d       R t#        |       y )Nr7  r8  )rK  rQ  r   r   r   r   r$   r%  r   rD  rp   rO  r9  r   r   rE   rc   r:  )r   r)  source_dxf_layouttarget_dxf_layoutr1  r   r  s          rH   r   zLoadPaperspaceLayout.execute  s     22==$::;L;P;P;W;WX+Y777 ))44??!!&&
 **, 	F,,V4E*51((//E
|;MN	 	)7rJ   Nr   r   r   Optional[FilterFunction]r   r   )r   list[DXFEntity]r*  r+  )rL   rM   rN   rO   r2  rO  r&  r   rP   rJ   rH   rH  rH    s    
#088rJ   rH  c                       e Zd ZdZddZddZy)LoadBlockLayoutzLoads a block layout as a new block layout into the target document. If a block
    layout with the same name exists the conflict policy will be applied.
    c                `    t        |t              st        dt        |             || _        y )Ninvalid block layout type: )r   r   r9   r0  block_layout)r   r   s     rH   r2  zLoadBlockLayout.__init__  s+    %- ;DK=IJJ!rJ   c                t    | j                   j                  }t        |t              r|j	                  |       y y rC   )r]  r   r   r   r   )r   r%  r   s      rH   r&  z"LoadBlockLayout.register_resources  s0    ((55lK0- 1rJ   N)r   r   r   r   r*  rL   rM   rN   rO   r2  r&  rP   rJ   rH   rZ  rZ    s    "
.rJ   rZ  c                       e Zd ZdZddZddZy)LoadResourceszLoads table entries into the target document. If a table entry with the same name
    exists the conflict policy will be applied.
    c                    || _         y rC   )r   )r   r   s     rH   r2  zLoadResources.__init__  s	     rJ   c                T    | j                   D ]  }|j                  |t                y r4  )r   r   r   r5  s      rH   r&  z LoadResources.register_resources  s(     	7AX6	7rJ   N)r   r;  r   r   r*  r_  rP   rJ   rH   ra  ra    s    !7rJ   ra  c                      e Zd ZdZej
                  f	 	 	 	 	 ddZddZ	 	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 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dZy)r4   zLoad entities and resources from the source DXF document `sdoc` into the
    target DXF document.

    Args:
        sdoc: source DXF document
        tdoc: target DXF document
        conflict_policy: :class:`ConflictPolicy`

    c                
   t        |t              sJ d       t        |t              sJ d       ||usJ d       |j                  |j                  k  rt        j	                  d       || _        || _        || _        g | _        y )Nz$a valid source document is mandatoryz$a valid target document is mandatoryz-source and target document cannot be the samez>target document has older DXF version than the source document)	r   r   r   rD   warningr   r   r   	_commands)r   r   r   r   s       rH   r2  zLoader.__init__  s     $(P*PP($(P*PP(4P!PP??T__,NNP "	!	./1rJ   c                :    | j                   j                  |       y rC   )rg  append)r   commands     rH   r   zLoader.add_command   s    g&rJ   Nc                   || j                   j                         }n't        |t              st	        dt        |             |j                  | j                   urt	        d      |$t        | j                  j                               }n0| j                  j                         D cg c]  } ||      s| }}| j                  t        ||             yc c}w )a  Loads the content of the modelspace of the source document into a layout of
        the target document, the modelspace of the target document is the default target
        layout. The filter function `filter_fn` is used to skip source entities, the
        function should return ``False`` for entities to ignore and ``True`` otherwise.

        Args:
            target_layout: target layout can be any layout: modelspace, paperspace
                layout or block layout.
            filter_fn: function to filter source entities

        Nr/  :given target layout does not belong to the target document)r   r   r   r   r9   r0  ru   rN  r   r   r   )r   r1  r   r   r   s        rH   r/   zLoader.load_modelspace  s        II002MM:6 <T-=P<QRSSDII-L  DII0023H#'99#7#7#9JaYq\JHJh>? Ks   *C8Cc                    t        |t              s!t        j                  dt	        |             |j
                  | j                  urt        d      | j                  t        ||             y)a  Loads a paperspace layout as a new paperspace layout into the target document.
        If a paperspace layout with same name already exists the layout will be renamed
        to  "<layout name> (2)" or "<layout name> (3)" and so on.  The filter function
        `filter_fn` is used to skip source entities, the function should return ``False``
        for entities to ignore and ``True`` otherwise.

        The content of the modelspace which may be displayed through a VIEWPORT entity
        will **not** be loaded!

        Args:
            psp: the source paperspace layout
            filter_fn: function to filter source entities

        rJ  >given paperspace layout does not belong to the source documentN)
r   r   r
   DXFTypeErrorr0  ru   r   r9   r   rH  rL  s      rH   r   zLoader.load_paperspace_layout!  sc    & #z*$$'GS	{%STT77$))#P  	-c9=>rJ   c                   t        |t              st        dt        |             t        |t              st        dt        |             |j
                  | j                  urt        d      |j
                  | j                  urt        d      |t        |      }n|D cg c]  } ||      s| }}| j                  t        ||             yc c}w )ar  Loads the content of a paperspace layout into an existing layout of the target
        document.  The filter function `filter_fn` is used to skip source entities, the
        function should return ``False`` for entities to ignore and ``True`` otherwise.

        The content of the modelspace which may be displayed through a
        VIEWPORT entity will **not** be loaded!

        Args:
            psp: the source paperspace layout
            target_layout: target layout can be any layout: modelspace, paperspace
                layout or block layout.
            filter_fn: function to filter source entities

        rJ  r/  rn  rl  N)r   r   r9   r0  r   ru   r   r   rN  r   r   )r   r   r1  r   r   r   s         rH   load_paperspace_layout_intoz"Loader.load_paperspace_layout_into<  s    ( #z* @cLMM-4 <T-=P<QRSS77$))#P  DII-L  CyH#&7a)A,7H7h>? 8s   'C5Cc                    t        |t              st        dt        |             |j                  | j
                  urt        d      | j                  t        |             y)a>  Loads a block layout (block definition) as a new block layout into the target
        document. If a block layout with the same name exists the conflict policy will
        be applied.  This method cannot load modelspace or paperspace layouts.

        Args:
            block_layout: the source block layout

        r\  9given block layout does not belong to the source documentN)r   r   r9   r0  ru   r   r   rZ  )r   r]  s     rH   load_block_layoutzLoader.load_block_layoutb  s^     ,4 ;D<N;OPQQ499,K  	67rJ   c                t   t        |t              st        dt        |             t        |t              st        dt        |             |j
                  | j                  urt        d      |j
                  | j                  urt        d      | j                  t        t        |      |             y)a  Loads the content of a block layout (block definition) into an existing layout
        of the target document.  This method cannot load the content of
        modelspace or paperspace layouts.

        Args:
            block_layout: the source block layout
            target_layout: target layout can be any layout: modelspace, paperspace
                layout or block layout.

        r\  r/  rs  rl  N)r   r   r9   r0  r   ru   r   r   r   r   rN  )r   r]  r1  s      rH   r   zLoader.load_block_layout_intov  s     ,4 ;D<N;OPQQ-4 <T-=P<QRSS499,K  DII-L  	d<&8-HIrJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the layers defined by the argument `names` into the target document.
        In the case of a name conflict the conflict policy will be applied.
        N)_get_table_entriesr   layersr   ra  r   namesr   s      rH   load_layerszLoader.load_layers  -     &eTYY-=-=>x01rJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the linetypes defined by the argument `names` into the target document.
        In the case of a name conflict the conflict policy will be applied.
        N)rw  r   	linetypesr   ra  ry  s      rH   load_linetypeszLoader.load_linetypes  -     &eTYY-@-@Ax01rJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the TEXT styles defined by the argument `names` into the target document.
        In the case of a name conflict the conflict policy will be applied.
        N)rw  r   stylesr   ra  ry  s      rH   load_text_styleszLoader.load_text_styles  r|  rJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the DIMENSION styles defined by the argument `names` into the target
        document. In the case of a name conflict the conflict policy will be applied.
        N)rw  r   	dimstylesr   ra  ry  s      rH   load_dim_styleszLoader.load_dim_styles  r  rJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the MLINE styles defined by the argument `names` into the target
        document. In the case of a name conflict the conflict policy will be applied.
        N)rw  r   mline_stylesr   ra  ry  s      rH   load_mline_styleszLoader.load_mline_styles  s-     &eTYY-C-CDx01rJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the MULTILEADER styles defined by the argument `names` into the target
        document. In the case of a name conflict the conflict policy will be applied.
        N)rw  r   mleader_stylesr   ra  ry  s      rH   load_mleader_styleszLoader.load_mleader_styles  s-     &eTYY-E-EFx01rJ   c                x    t        || j                  j                        }| j                  t	        |             y)zLoads the MATERIALS defined by the argument `names` into the target
        document. In the case of a name conflict the conflict policy will be applied.
        N)rw  r   	materialsr   ra  ry  s      rH   load_materialszLoader.load_materials  r  rJ   c                   t        | j                  | j                        }t        j                  j
                  }| j                  D ]  }|j                  |        |rt        |j                         t        | j                        }|j                  |j                         t        ||j                  |j                  |j                   | j"                  |j$                        }|rt'        |      |_        |j+                  |j                         |j-                  |j.                         |j1                          |j3                          |j5                          |j7                          |j9                          |j;                          | j                  D ]  }|j=                  |        |j?                          y)zExecute all loading commands. The `xref_prefix` string is used as XREF name
        when the conflict policy :attr:`ConflictPolicy.XREF_PREFIX` is applied.
        )r%  copiesobjectshandle_mappingr   copy_errorsN) 	_Registryr   r   r:   optionsrE   rg  r&  rI   debug_messagesCopyMachinecopy_blockssource_blocksr,  r  r  r  r   r  rc   r   add_object_copiesregister_classesclassesregister_table_resourcesregister_object_resourcesredirect_handle_mappingmap_object_resourcesmap_entity_resourcescopy_settingsr   finalize)r   r   r%  rE   cmdcpmr)  s          rH   r   zLoader.execute  sX    TYY		2##>> 	-C""8,	-  7 78$))$../::KK-- 00
 #&{#3H ""3;;/!!#++.))+**,((*%%'%%' >> 	"CKK!	"rJ   r   r   r   r   r   r   )rj  r#  r   r   )NN)r1  zOptional[BaseLayout]r   rW  r   r   rC   rV  )r   r   r1  r   r   rW  r   r   )r]  r   r   r   )r]  r   r1  r   r   r   )rz  zSequence[str]r   r   r  )r   rc   r   r   )rL   rM   rN   rO   r3   r[   r2  r   r/   r   rq  rt  r   r{  r  r  r  r  r  r  r   rP   rJ   rH   r4   r4     s#    =K<O<O22#*2	2'
 /3.2@+@ ,@ 
	@B /3?? ,? 
	?> /3	$@$@ "$@ ,	$@
 
$@L8!8 
8(J!J "J 
	J:2222222%rJ   r4   c                    g }| D ]'  }	 |j                  |      }|r|j                  |       ) |S # t        j                  $ r Y Aw xY wrC   )r   ri  r
   DXFTableEntryError)rz  tabler   rp   entrys        rH   rw  rw    sY     "H 	IIdOE&	 O '' 		s   $1AAc                  h    e Zd ZddZddZef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                f    || _         || _        t        i i| _        t	               | _        g | _        y rC   )r   r   r   r  setappidsr  r   r   r   s      rH   r2  z_Registry.__init__  s1     @Hn #)+rJ   c                :    | j                   j                  |       y rC   r  ri  r   rG   s     rH   rE   z_Registry.debug      ""3'rJ   c                    |J d       | j                   j                  |i       }|j                  j                  }||v ry |||<   |j	                  |        y )Nzinternal error: entity is None)r  
setdefaultr   r   r&  )r   r   r   r   entity_handles        rH   r   z_Registry.add_entity  s^    !C#CC!""--i<

))E!%m!!$'rJ   c                   | j                  |       |j                  j                  }| j                  |j                  |       |j                  D ]  }| j                  ||        | j                  |j
                  |       y rC   )r   r   r   r   entity_spaceendblk)r   r   block_handler   s       rH   r   z_Registry.add_block  sj    %#''..**L9"// 	2FOOFL1	2++\:rJ   c                    ||dk(  ry| j                   j                  j                  |      }|| j                  d| d       yt	        |      rt        dt        |             | j                  |       y)zAdd resource by handle (table entry or object), cannot add graphic entities.

        Raises:
            EntityError: cannot add graphic entity

        Nr;   zsource entity #z does not existzcannot add graphic entity: )r   entitydbr   rE   r   r8   rc   r   )r   r   r   s      rH   r   z_Registry.add_handle  st     >Vs]))--f5>JJ@AV$ ;CK=IJJrJ   c                    |t         k(  ry 	 | j                  j                  j                  |      }| j	                  |       y # t
        j                  $ r | j                  d| d       Y y w xY w)Nzsource layer '' does not exist)DEFAULT_LAYERr   rx  r   r   r
   r  rE   )r   rp   layers      rH   r   z_Registry.add_layer/  sf    = 	@OO**..t4EOOE"'' 	@JJv-=>?	@s   6A (A.-A.c                    |j                         t        v ry 	 | j                  j                  j	                  |      }| j                  |       y # t        j                  $ r | j                  d| d       Y y w xY w)Nzsource linetype 'r  )	upperDEFAULT_LINETYPESr   r~  r   r   r
   r  rE   )r   rp   linetypes      rH   r   z_Registry.add_linetype9  sn    ::<,,	C0044T:HOOH%'' 	CJJ*4&0@AB	Cs   6A (A;:A;c                    	 | j                   j                  j                  |      }| j                  |       y # t        j
                  $ r | j                  d| d       Y y w xY w)Nzsource text style 'r  )r   r  r   r   r
   r  rE   )r   rp   
text_styles      rH   r   z_Registry.add_text_styleC  s\    	E//33D9JOOJ''' 	EJJ,TF2BCD	E   69 (A$#A$c                    	 | j                   j                  j                  |      }| j                  |       y # t        j
                  $ r | j                  d| d       Y y w xY w)Nzsource dimension style 'r  )r   r  r   r   r
   r  rE   )r   rp   	dim_styles      rH   r   z_Registry.add_dim_styleJ  s\    	J1155d;IOOI&'' 	JJJ1$7GHI	Jr  c                    	 | j                   j                  j                  |      }| j                  |       y # t        j
                  $ r | j                  d| d       Y y w xY w)Nzsource block 'r  )r   block_recordsr   r   r
   r  rE   )r   rp   r   s      rH   r   z_Registry.add_block_nameQ  s[    	@??88<<TBLOOL)'' 	@JJv-=>?	@r  c                V    | j                   j                  |j                                y rC   )r  addr  r   s     rH   r   z_Registry.add_appidX  s    

%rJ   Nr  rG   rc   r   r   r   r   r   r   r   r   )rL   rM   rN   r2  rE   r   r   r   r   r   r   r   r   r   r   rP   rJ   rH   r  r    sD    ,( >F (; "@CEJ@&rJ   r  c                     e Zd Zej                  d	 	 	 	 	 	 	 	 	 	 	 d,dZd-dZd.d/dZd0dZd1dZ	d1dZ
d1dZd1d	Zd1d
Zd2dZd3dZd4d5dZ	 	 	 	 	 	 	 	 d6dZdd	 	 	 	 	 	 	 d7dZd8dZd8dZd8dZd8dZd8dZd9dZd8dZd8d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"d3d"Z#	 	 	 	 	 	 d@d#Z$dAd$Z%dBd%Z&dCd&Z'd' Z(d( Z)d) Z*d8d*Z+y+)Dr,  r   c                   || _         || _        || _        || _        || _        t        |j                        | _        i | _        i | _	        i | _
        i | _        i | _        || _        i | _        g | _        y rC   )r%  copied_blockscopied_objectsr  r   get_xref_namer   r   layer_mappinglinetype_mappingtext_style_mappingdim_style_mappingblock_name_mappingr  _replace_handlesr  )r   r%  r  r  r  r   r  s          rH   r2  z_Transfer.__init__]  s~     !#%&.()<)<=-/02241324.<02)+rJ   c                :    | j                   j                  |       y rC   r  r  s     rH   rE   z_Transfer.debugw  r  rJ   c                :    | j                   j                  ||      S rC   )r  r   r   s      rH   r   z_Transfer.get_handlez  s    ""&&vw77rJ   c                    | j                   j                  |      }|r/| j                  j                  j                  j                  |      S y rC   )r  r   r%  r   r  )r   r   handle_of_copys      rH   r   z_Transfer.get_reference_of_copy}  s?    ,,008==++4488HHrJ   c                :    | j                   j                  ||      S rC   )r  r   r   s     rH   r   z_Transfer.get_layer  s    !!%%dD11rJ   c                :    | j                   j                  ||      S rC   )r  r   r   s     rH   r   z_Transfer.get_linetype  s    $$((t44rJ   c                :    | j                   j                  ||      S rC   )r  r   r   s     rH   r   z_Transfer.get_text_style      &&**466rJ   c                :    | j                   j                  ||      S rC   )r  r   r   s     rH   r  z_Transfer.get_dim_style  s    %%))$55rJ   c                :    | j                   j                  ||      S rC   )r  r   r   s     rH   r  z_Transfer.get_block_name  r  rJ   c                    	 | j                   |j                  j                     |j                  j                     S # t        $ r Y yw xY w)z%Returns the copy of graphic entities.N)r  r   r   r   KeyErrorr  s     rH   r9  z_Transfer.get_entity_copy  sF    	%%fjj&6&67

8I8IJJ 		s   9< 	AAc                    | j                  |      }|r|j                  ||        y |j                  j                  | j                  v ry t        d| d      )Nzcopy of z
 not found)r9  map_resourcesr   r   r  rW   )r   r   r  s      rH   r  z_Transfer.map_resources_of_copy  sT    $$V,  -ZZ$"2"22(6(* =>>rJ   c                   t        |      D ]  \  }}t        j                  |      s| j                  |j                  d      }t        j
                  |j                  |      ||<   |s_t        j                  |      su| j                  j                  j                  j                  |      }|||j                  _         y )Nr;   )r   )	enumerater   is_translatable_pointerr   valueDXFTagcodeis_hard_ownerr%  r   r  r   r   r   )r   r	  r
  indextagr   copied_objects          rH   r  z_Transfer.map_pointers  s    #D/ 	?JE3,,S1C@#ll388V<U#(;(;C(@$(MM$<$<$E$E$I$I&$QM$, .>M%%+	?rJ   c                r   | j                   j                  }|j                  j                  |      }|j	                  |      }t        |t              r| j                  t        j                  k(  r||fS | j                  t        j                  k(  rt        || j                  |      }n*| j                  t        j                  k(  rt        |d|      }| j                  |j                  j                         }|d|fS |j#                  ||       |j                  j                   |j                  _        ||fS )zdMap and add `entity` to a top level ACAD dictionary `dict_name` in root
        dictionary.
        r   )r%  r   rootdictget_required_dictr   r   r   r   r3   r[   r\   get_unique_dict_keyr   r]   r   r   r   r  r   )r   r  r  r   r   	acad_dictexisting_entryloaded_entrys           rH   r  z_Transfer.map_acad_dict_entry  s
    }}''MM33I>	"z2 ni0##~':'::!>11%%)C)CC0 0 0)
 %%)B)BB0RK
11&**2C2CDv:j,/!*!5!5<''rJ   Fr  c                   |j                   j                  |d      }|sy| j                  |      }|r"|dk7  r|j                   j                  ||       y|r|j                   j	                  |       y|j                   j                  |d       y)zoMap handle attribute if the original handle exist and the references entity
        was really copied.
        r   Nr;   )r   r   r   r  discard)r   r  r  r  r  r   
new_handles          rH   r  z_Transfer.map_existing_handle  so     R0__V,
*+IIMM+z2		!!+.		k3/rJ   c                ~   | j                          | j                  t           j                         D ]  \  }}|j                  j
                  t        |t              r| j                  |       @t        |t              r| j                  |       bt        |t              r0|j                  r| j                  |       | j                  |       t        |t              r| j!                  |       t        |t"              r| j%                  ||       t        |t&              s| j)                  |        y)zHRegister copied table-entries in resource tables of the target document.N)register_appidsr  r   itemsr   r   r   r   add_layer_entryr   add_linetype_entryr   is_shape_fileadd_shape_file_entryadd_text_style_entryr   add_dim_style_entryr   add_block_record_entryr   add_ucs_entry)r   source_entity_handler   s      rH   r  z"_Transfer.register_table_resources  s    ,0,>,>x,H,N,N,P 	+( &zz+ &%($$V,FH-''/FI.''--f5--f5FH-((0FK0++F4HIFM2""6*'	+rJ   c                .   | j                   j                  }| j                  j                         D ]  \  }}t	        |t
              r!| j                  |j                  |h d       7t	        |t              r$| j                  |j                  |t        h       kt	        |t              r$| j                  |j                  |t        h       t	        |t              r| j                  |       t	        |t              s| j!                  |        y)zERegister copied objects in object collections of the target document.>   GLOBALr<   r=   )system_entriesN)r%  r   r  r   r   r   add_collection_entryr  r   r  r?   r    r  r%   add_visualstyle_entryr$   create_empty_paperspace_layout)r   r   _r   s       rH   r  z#_Transfer.register_object_resources  s     }}'',,224 	<IAv&(+))NN#C * 
 FJ/))%% $ *  FL1))'' $ *  FK0**62FI.33F;5	<rJ   c                "    || j                   |<   y rC   )r  )r   
old_target
new_targets      rH   replace_handle_mappingz _Transfer.replace_handle_mapping   s    ,6j)rJ   c                    i }| j                   }| j                  j                         D ]  \  }}||v s||   ||<    |j                         D ]  \  }}|| j                  |<    y)zuRedirect handle mapping from copied entity to a handle of an existing entity
        in the target document.
        N)r  r  r   )r   temp_mappingreplace_handlessource_handletarget_handlenew_target_handles         rH   r  z!_Transfer.redirect_handle_mapping#  s     (*//,0,?,?,E,E,G 	M(M=/.=m.L]+	M
 1=0B0B0D 	C,M,1BD.	CrJ   c                    | j                   j                  }| j                   j                  D ]  }	 |j                  j                  |         y # t        j
                  $ r Y 7w xY wrC   )r%  r   r  rs   r
   r  )r   r   appids      rH   r  z_Transfer.register_appids2  sY    }}'']])) 	E&	 ++ s   AA%$A%c                b    | j                   j                  j                  j                  |       y rC   )r%  r   r  register)r   r  s     rH   r  z_Transfer.register_classes:  s       ((11':rJ   c                8   | j                   j                  j                  }| j                  j	                         D ]]  \  }}|j	                         D ]E  \  }}|j                  |      }|t        d      |'|j                  s4|j                  ||        G _ y )Nz'database error, source entity not found)	r%  r   r  r  r   r   rW   is_aliver  )r   	source_dbr   r   r	  r  source_entitys          rH   r  z_Transfer.map_entity_resources=  s    MM,,55	 $ 2 2 8 8 : 	=Iu/4{{} =+$e ).B C ('(QRR$!//t<=	=rJ   c                   | j                   j                  j                  }| j                  j	                         D ]E  \  }}|j                  |      }|t        d      |'|j                  s4|j                  ||        G y )Nz'database error, source object not found)	r%  r   r  r  r   r   rW   r   r  )r   r!  source_object_handler  r"  s        rH   r  z_Transfer.map_object_resourcesG  sx    MM,,55	+/+>+>+D+D+F 	9' %%MM*>?M$#$MNN U^^++E48	9rJ   c                v   | j                   j                  }|j                  j                  j	                         }|dv st        j                  |      ri	 |j                  j                  |      }|rK| j                  |j                  j                  |j                  j                         |j                          y |j                  j                  }| j                  |j                  |       |j                  r$|j                  j                  | j                   |<   y y # t        j                  $ r d }Y w xY w)N)r;   	DEFPOINTS)r%  r   r   rp   r  r   is_adsk_special_layerrx  r   r
   r  r  r   destroyadd_table_entryr   r  )r   r  r   
layer_namespecialold_names         rH   r  z_Transfer.add_layer_entryP  s    }}''YY^^))+
 ++y/N/N0
++//*5 ++EII,<,<gkk>P>PQ99>>T[[%0>>+099>>Dx(  ++ s   D   D87D8c                <   | j                   j                  }|j                  j                  j	                         t
        v rz|j                  j                  |j                  j                        }| j                  |j                  j                  |j                  j                         |j                          y |j                  j                  }| j                  |j                  |       |j                  r$|j                  j                  | j                  |<   y y rC   )r%  r   r   rp   r  r  r~  r   r  r   r(  r)  r   r  )r   r  r   standardr,  s        rH   r  z_Transfer.add_linetype_entryf  s    }}''<<""$(99~~))(,,*;*;<H''(;(;X\\=P=PQ<<$$T^^X6.6ll.?.?D!!(+ rJ   c                    | j                   j                  }|j                  j                  }| j	                  |j
                  |       |j                  r$|j                  j                  | j                  |<   y y rC   )r%  r   r   rp   r)  r  r   r  )r   r  r   r,  s       rH   r  z_Transfer.add_text_style_entryr  s\    }}''>>&&T[[*50:0C0CD##H- rJ   c                F   |j                   j                  }|sy | j                  j                  }|j                  j                  |      }||j                  j                  |      }| j                  |j                   j                  |j                   j                         y rC   )	r   fontr%  r   r  find_shxadd_shxr  r   )r   r  shape_file_namer   
shape_files        rH   r  z_Transfer.add_shape_file_entryy  sx    $..--}}''[[))/:
,,_=J##JNN$9$9:>>;P;PQrJ   c                    | j                   j                  }|j                  j                  }| j	                  |j
                  |       |j                  r$|j                  j                  | j                  |<   y y rC   )r%  r   r   rp   r)  r  r   r  )r   r  r   r,  s       rH   r  z_Transfer.add_dim_style_entry  s\    }}''==%%T^^Y7/8}}/A/AD""8, rJ   c                T   | j                   j                  }|j                  j                  j	                         }|j                  j                  }t        |      dkD  rQ|d   dk(  rI|j                  j                  |d         |j                  _        |j                  j                  |       n| j                  |j                  |       |j                  rQ|j                  j                  | j                  |<   | j                  ||       |j                  j                  |       y y )Nr=  r   *)r%  r   r   rp   r  r   rr   anonymous_block_namer  	add_entryr)  r   r  restore_block_contentr  )r   r   r   r   rv   r,  s         rH   r  z _Transfer.add_block_record_entry  s    }}''!%%**002
##(( z?Q:a=C#7 %)KK$D$DZPQ]$SL!((6
   !3!3\B  0<0@0@0E0ED##H-&&|V<KKOOL) !rJ   c           	     *   | j                   j                  |t                     }d }d }|j                         D ]  }t	        |t
        t        f      rt	        |t
              r|},|}/t        |      r|j                  |       L|j                  j                  }d| d| dt        |       }t        j                  |       | j                  |        t	        |t
              r#t	        |t              r|j                  ||       y t!        d      )Nz1skipping non-graphic DXF entity in BLOCK_RECORD('z', #z): zinvalid BLOCK_RECORD copy)r  r   r   valuesr   r!   r"   r   r   r   rp   rc   loggingrf  rE   	set_blockrW   )	r   r   r   contentr   r  r   rp   rG   s	            rH   r;  z_Transfer.restore_block_content  s    $$((8!%#'nn& 	 F&5&/2fe,"E#F"6*''/#'',,I$tTZS[[^_bci_j^kl$

3	  eU#
66(B""5&1 ;<<rJ   c                h    | j                   j                  }| j                  |j                  |       y rC   )r%  r   r)  ucs)r   rB  r   s      rH   r  z_Transfer.add_ucs_entry  s%    }}''TXXs+rJ   c                   |j                   j                  }| j                  t        j                  k(  rm|j                  |      r|j                  |      }| j                  |j                   j                  |j                   j                         |j                          y | j                  t        j                  k(  r't        || j                  |      |j                   _        nJ| j                  t        j                  k(  r-|j                  |      rt        |d|      |j                   _        |j                  |       y Nr   )r   rp   r   r3   r[   	has_entryr   r  r   r(  r\   get_unique_table_namer   r]   r:  )r   r  r   rp   r  s        rH   r)  z_Transfer.add_table_entry  s    zz>#6#66t$!&4++JJ%%~'9'9'@'@  !!^%?%??3D$:J:JERFJJO!!^%>%>>t$"7b%"H

rJ   c                   |j                   j                  }|j                         |v rh|j                  j	                  |      }|rK| j                  |j                   j                  |j                   j                         |j                          y | j                  t        j                  k(  r^|j	                  |      }|r| j                  |j                   j                  |j                   j                         |j                          y | j                  t        j                  k(  r't        || j                  |      |j                   _        nJ| j                  t        j                  k(  r-|j                  |      rt        |d|      |j                   _        |j                  j!                  |j                   j                  |       |j                  |j                   _        y rD  )r   rp   r  object_dictr   r  r   r(  r   r3   r[   r\   rF  r   r]   rE  r  r   )r   
collectionr  r  rp   r+  r  s          rH   r  z_Transfer.add_collection_entry  sR    yy~~::<>) ,,006G++EII,<,<gkk>P>PQ>#6#66'^^D1N++EII,<,<n>P>P>W>WX!!^%?%??249I9I:VEIIN!!^%>%>>##D)!6tR!L		""599>>59$++		rJ   c                n   | j                   j                  j                  j                  d      }|j                  j
                  }|j                  |      }|rK| j                  |j                  j                  |j                  j                         |j                          y |j                  ||       y )NACAD_VISUALSTYLE)r%  r   r  r  r   descriptionr   r  r   r(  take_ownership)r   visualstylevisualstyle_dictrp   r  s        rH   r  z_Transfer.add_visualstyle_entry  s    ==33<<NN
 **)--d3''&&(:(:(A(A !++D+>rJ   c                |   | j                   j                  }|j                  j                         }|j                  j                  |      }|j                  |j                  _        |j                  j                  |j                  j                  _
        t        ||      }|j                  j                  |       y rC   )r%  r   layoutsunique_paperspace_namerr   rs   block_record_handler   r   r   layoutr   append_layout)r   rT  r   rv   r]  rD  s         rH   r  z(_Transfer.create_empty_paperspace_layout  s    }}'' \\88:
{{z2)5)I)I

&/5zz/@/@!!%%,-
"":.rJ   c                    | j                   j                  j                  }|j                         D ]&  \  }}|s	|j                  s|j                  |       ( y)zEAdd copied DXF objects to the OBJECTS section of the target document.N)r%  r   r  r   r   
add_object)r   r  r  r  objs        rH   r  z_Transfer.add_object_copies  sG    --**22lln 	(FAss||""3'	(rJ   c                D    | j                          | j                          y rC   )copy_raster_varscopy_wipeout_vars)r   s    rH   r  z_Transfer.copy_settings  s     rJ   c                   | j                   j                  }| j                   j                  }d|j                  vsd|j                  v ry |j                  j                         \  }}}|j                  j                  |||       y )NACAD_IMAGE_VARS)framequalityr   )r%  r   r   r  r  get_raster_variablesset_raster_variables)r   r   r   r^  r_  r   s         rH   rZ  z_Transfer.copy_raster_vars  sm    }}''}}''T]]2 DMM1 $ A A Cw))we)TrJ   c                    | j                   j                  }| j                   j                  }d|j                  vsd|j                  v ry |j                  j                  |j                  j                                y )NACAD_WIPEOUT_VARS)r^  )r%  r   r   r  r  set_wipeout_variablesget_wipeout_frame_settingr  s      rH   r[  z_Transfer.copy_wipeout_vars!  sb    }}''}}''t}}4"dmm3**,,88: 	+ 	
rJ   c                    | j                   j                  j                  j                          | j                  D ]&  }t
        j                  t        j                  |       ( y rC   )	r%  r   r  purger  rD   logr>  INFOr  s     rH   r  z_Transfer.finalize-  sE      ))//1&& 	*CJJw||S)	*rJ   N)r%  r  r  dict[str, dict[str, DXFEntity]]r  dict[str, DXFEntity]r  zdict[str, str]r  set[str]r   r   r  r  r  r  r  r   r   r   r  r  r  r  r   r!  r  )r  zSequence[DXFClass]r   r   )r  r   r   r   )r  r   r   r   )r  r   r   r   )r  r   r   r   )r   r   r   rc   r   r   )rB  r   r   r   )r  r   r  rl  r   r   )rN  r%   r   r   )rT  r$   r   r   )r  rk  r   r   ),rL   rM   rN   r3   r[   r2  rE   r   r   r   r   r   r  r  r9  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  r  rZ  r[  r  rP   rJ   rH   r,  r,  \  sq    '++,, 0, &	,
 ', , 
,4(825767?	?((*-(7@(	(< RW00(10@C0	0$+2$<T7C;=9:,
@D	RB*.=,,
 $,!*,<D,	,B?/(!	U

*rJ   r,  c                n    | j                   r)t        j                  | j                         j                  S yrD  )rj   r   r   stem)ru   s    rH   r  r  4  s%    
||||CLL)...rJ   c                D    t        |       dkD  xr | j                  d      S )Nr=  r8  )r   
startswith)rp   s    rH   is_anonymous_block_namerr  :  s    t9q=1T__S11rJ   c                N    d}	 | d| d|  }|j                  |      s|S |dz  }#Nr   $r=  )rE  )rp   r   r  r  new_names        rH   rF  rF  >  s?    E
V1UG1TF+x(O
	 rJ   c                4    d}	 | d| d|  }||vr|S |dz  }rt  rP   )keyr   
dictionaryr  new_keys        rH   r  r  G  s;    E
F!E7!C5)*$N
	 rJ   c                  4    e Zd ZddZddZd	dZd
dZddZy)r  c                    || _         i | _        g | _        i | _        t	               | _        t        t        d            | _        i | _	        y )NF)set_source_of_copy)
r   r  r  r  r  r  r&   r'   copy_strategyr  )r   r   s     rH   r2  zCopyMachine.__init__Q  sD    79')-/%(U),%*PQ /1rJ   c                p    |j                         D ]#  \  }}| j                  |      | j                  |<   % y rC   )r   
copy_blockr  )r   rr   r   r   s       rH   r  zCopyMachine.copy_blocks\  s2    #\\^ 	9MFE"&//%"8DKK	9rJ   c                   i }| j                   }| j                  }|j                         D ]  \  }}t        |t              r| j                  |       (| j                  |      }|<t        j                  ||       |j                  j                  ||<   t        |d      r*| j                  j                  |j                  |             t        |      r|| j                  |<   |||<    |S )Nget_handle_mapping)r   r  r   r   r   copy_dxf_classcopy_entityr   bindr   r   hasattrupdater  r   r  )r   r   r  r   r  r   r   r  s           rH   r  zCopyMachine.copy_block`  s    '),,#kkm 	'NFF&(+##F+$$V,E}LL%%*YY%5%5N6"v34##**6+D+DU+KLU#',V$!&v!	'" rJ   c                    	 |j                  | j                        S # t        j                  $ r2 | j                  j                  |j                  j                         Y y w xY wN)r~  )copyr~  r
   DXFErrorr  r  r   r   r  s     rH   r  zCopyMachine.copy_entityx  sR    	4;;T-?-?;@@~~ 	4  !2!23	4s    AA#"A#c                n    | j                   j                  |j                  | j                               y r  )r  ri  r  r~  )r   clss     rH   r  zCopyMachine.copy_dxf_class  s%    CHH43E3EHFGrJ   N)r   r   r   r   )rr   rj  r   r   )r   rk  r   rk  rm  )r  r   r   r   )rL   rM   rN   r2  r  r  r  r  rP   rJ   rH   r  r  P  s    	190HrJ   r  )rF   Iterable[str]r   r   )rj   str | os.PathLiker   r   )F)ru   r   rv   rc   rj   rc   r   r   )rw   boolr   int)ru   r   rv   rc   rj   rc   r~   r(   r   r   r   r   r   r#   )r   rc   r   zSequence[pathlib.Path]r   zpathlib.Path)r   r   r   zOptional[LoadFunction]r   zIterable[pathlib.Path | str]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   rW  r   r   )r   r   r   r   r   rW  r   r   )rD  r   r   r   )rz  r  r   rX  )ru   r   r   rc   )rp   rc   r   r  )rp   rc   r   rc   r   rc   )rx  rc   r   rc   r   rc   )lrO   
__future__r   typingr   r   r   r   typing_extensionsr   r	   rY   r   r>  osr:   ezdxf.lldxfr
   r   r   ezdxf.lldxf.tagsr   ezdxf.lldxf.validatorr   ezdxf.documentr   ezdxf.layoutsr   r   r   ezdxf.entitiesr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   ezdxf.entities.copyr&   r'   
ezdxf.mathr(   r)   __all__	getLoggerrD   r   r  r  r?   r  r@   __annotations__rc   rA   rI   	Exceptionr6   r7   r8   r9   rW   Enumr3   r5   r*   rt   r+   r   tupler\   r,   r-   r   r.   r[   r/   r0   r1   r2   r#  r   r:  rH  rZ  ra  r4   rw  r  r,  r  rr  rF  r  r  rP   rJ   rH   <module>r     s1   " 9 9 1    	  / / ! ) " = =     * ; !( 
		7	#8 $i[$%67	 7"C5'>2i 2

		 		) 		) 		) 		I 	TYY .*jB3 8P	8P 8P 	8P
 8P 8P 8P 8Pv'F '+16"..F,
F, $F, /	F, 
F,T FK$$*;$$P FK66'86	6 BK  L +/"''	

 (
 
6 +/"''		
 (
 
4x :$X $N <> <@?(8> (8V.n . 
7N 
7C CL	a& a&HU* U*p20H 0HrJ   