
;;-------------------------=={ Copy to XRef }==-------------------------;;
;;                                                                      ;;
;;  This program enables the user to copy a selection of objects to a   ;;
;;  selected xref, without opening the xref source drawing.             ;;
;;                                                                      ;;
;;  Upon calling the program with 'c2x' at the command-line, the user   ;;
;;  is prompted to make a selection of objects to copy. Following a     ;;
;;  valid response, the user is then prompted to select an External     ;;
;;  Reference (xref) to which the objects are to be copied.             ;;
;;                                                                      ;;
;;  The program will then proceed to copy the selected objects to the   ;;
;;  source drawing of the selected xref using a deep-clone method,      ;;
;;  coupled with an ObjectDBX interface should the xref source drawing  ;;
;;  be unopened in the current drawing session.                         ;;
;;                                                                      ;;
;;  Upon copying the selection, the xref source drawing is saved and    ;;
;;  the xref is reloaded in the current drawing; the selected objects   ;;
;;  are then deleted from the current drawing.                          ;;
;;                                                                      ;;
;;  The program will account for the position, scale, rotation &        ;;
;;  orientation of the xref relative to the selection of objects and    ;;
;;  will perform successfully under all UCS & View settings.            ;;
;;                                                                      ;;
;;  Please Note:                                                        ;;
;;  ------------------------------                                      ;;
;;  The act of copying objects to the xref source drawing involves      ;;
;;  saving the external drawing remotely - this action cannot be        ;;
;;  undone within the current drawing and changes to the external       ;;
;;  drawing must be reset manually.                                     ;;
;;                                                                      ;;
;;  Note that when saving drawings through ObjectDBX, drawing file      ;;
;;  thumbnails will be lost until the next manual save.                 ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright ?2014  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Version 1.2    -    2014-06-21                                      ;;
;;----------------------------------------------------------------------;;
;复制实体到外部参照文件[lm_c2xref]
(defun c:lm_c2xref ( / old_cmd acd app dbx doc dwg dwl ent enx err lst mat obj sel vrs xrl )
   (mini_start nil)
   (mai_last_com "lm_c2xref")
   (setq xrl (mai_lst2str (mai_tblsearch "xref") ","))
    (if (= 1 (getvar 'xloadctl))
        (progn (princ "\nXLOADCTL系统变量被设定为 1, 外部参照文件被锁定.")
               (exit)
        )
    )
    (setq sel (mai_select_first
                    (list '(0 . "~VIEWPORT")
                          '(-4 . "<NOT")
                          '(-4 . "<AND") '(0 . "INSERT") (cons 2 xrl) '(-4 . "AND>")
                          '(-4 . "NOT>")
                           (cons 410 (if (= 1 (getvar 'cvport)) (getvar 'ctab) "Model"))
                    )
                    "选择要复制到外部参照文件实体 :"
                    "没有找到实体或实体不是外部参照."
              )           
         ent (car (mai_entsel nil "选择外部参照块 :" "选择的不是外部参照块." (list (cons 0 "insert") (cons 2 xrl)) nil))
    )
    (or ent (exit))
    (setq app (vlax-get-acad-object)
          vrs (atoi (getvar 'acadver))
          vrs (if (< vrs 16) "" (strcat "." (itoa vrs)))
          vrs (strcat "objectdbx.axdbdocument" vrs)
          dbx (vl-catch-all-apply 'vla-getinterfaceobject (list app vrs))
    )
    (if (or (null dbx) (vl-catch-all-error-p dbx))
        (progn (prompt "\n无法访问 ObjectDBX.")
               (exit)
        )
    )
    (or (and (setq dwg (mai_dxf 1 (tblsearch "block" (mai_dxf 2 enx))))
             (setq dwg (findfile dwg))
        )
        (progn (prompt "\n无法定位外部参照文件.")
               (exit)
        )
    )
    (vlax-for doc (vlax-get app 'documents)
        (setq dwl (cons (cons (strcase (vlax-get doc 'fullname)) doc) dwl))
    )
    (or (setq doc (mai_dxf (strcase dwg) dwl))
        (and (not (vl-catch-all-error-p (setq err (vl-catch-all-apply 'vla-open (list dbx dwg)))))
             (setq doc dbx)
        )
        (progn (prompt (strcat "\n无法打开外部参照文件:\n" (vl-catch-all-error-message err)))
               (exit)
        )
    )
    (setq mat (mai_blk_RevMatrix ent)
          mat (vlax-tmatrix (append (mapcar 'append (car mat) (mapcar 'list (cadr mat))) '((0. 0. 0. 1.))))
          acd (vlax-get app 'activedocument)
    )
    (setq lst (mapcar 'vlax-ename->vla-object (mai_ssget_2_list sel)))
    (foreach obj lst (vla-transformby obj mat))
    (vlax-invoke acd 'copyobjects lst (vlax-get doc 'modelspace))
    (vla-saveas doc dwg)
    (vla-reload (vla-item (vlax-get acd 'blocks) (mai_dxf 2 enx)))
    (foreach obj lst (vla-delete obj)) ;; Comment this line to retain original objects
    (and (= 'vla-object (type dbx))
         (not (vlax-object-released-p dbx))
         (vlax-release-object dbx)
    )
    (mai_end)
)

