;;-----------------------=={ Copy to Drawings }==-----------------------;;
;;                                                                      ;;
;;  This program enables the user to copy a selection of objects to a   ;;
;;  selected set of drawings, without opening the drawings in the       ;;
;;  AutoCAD Editor.                                                     ;;
;;                                                                      ;;
;;  The program will first prompt the user to make a selection of       ;;
;;  objects residing in the active drawing layout that are to be        ;;
;;  copied. Following a valid selection, the user will be prompted via  ;;
;;  a dialog interface to compile a list of drawings (dwg/dwt/dws) to   ;;
;;  which the selected objects will be copied.                          ;;
;;                                                                      ;;
;;  The program will then proceed to copy every object in the selection ;;
;;  to each selected drawing using an ObjectDBX interface.              ;;
;;                                                                      ;;
;;  The program will retain all information associated with each        ;;
;;  copied object, including the position, scale, rotation, etc.        ;;
;;  Properties such as layers & linetypes will be imported if not       ;;
;;  already present in the external drawing. Similarly, the drawing     ;;
;;  layout in which the source objects reside will be created if not    ;;
;;  already present in the external drawing.                            ;;
;;                                                                      ;;
;;  The program is compatible for use with all drawing objects          ;;
;;  (including XRefs & Dynamic Blocks) with the exception of Viewports. ;;
;;                                                                      ;;
;;  After copying the set of objects to each drawing, the program will  ;;
;;  save the external drawing. Due to a restriction on the saveas       ;;
;;  method when invoked through an ObjectDBX interface, all drawings    ;;
;;  will be saved to the native format, i.e. the latest version         ;;
;;  available - this is unfortunately unavoidable.                      ;;
;;                                                                      ;;
;;  Note that when saving drawings through ObjectDBX, drawing file      ;;
;;  thumbnails will be lost until the next manual save.                 ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright ?2013  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Version 1.2    -    2013-05-17                                      ;;
;;----------------------------------------------------------------------;;
;;  Version 1.3    -    2016-03-21                                      ;;
;;                                                                      ;;
;;  - Updated Get Files Dialog function to fix filename sorting bug     ;;
;;    and incorrect enabling of 'Add Files' button when a directory is  ;;
;;    selected.                                                         ;;
;;  - Refined output message to report unsuccessful drawings.           ;;
;;----------------------------------------------------------------------;;
;复制实体到外部文件[lm_c2dwg]
(defun c:lm_c2dwg ( / old_cmd _getitem acd app dbx doc dwl err lst msg sel tab var vrs cnt dwg )

    (defun _getitem ( col itm )
        (if (not (vl-catch-all-error-p (setq itm (vl-catch-all-apply 'vla-item (list col itm)))))
            itm
        )
    )

    (mini_start nil)
    (mai_last_com "lm_c2dwg")
    (setq app (vlax-get-acad-object)
          acd (vlax-get app 'activedocument)
          tab (if (= 1 (getvar 'cvport)) (getvar 'ctab) "Model")
          cnt 0
          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)
        )
    )
    (setq sel (mai_select_first (list '(0 . "~VIEWPORT") (cons 410 tab))
                                 "选择要复制到外部文件实体 :"
                                "没有找到实体."
              )
          lst (mai_GetFiles "可选择多个dwg文件，选择需要复制实体到的文件" mai_mini_path "dwg,dwt,dws")
    )
    (or lst
        (progn (prompt "\n没有选择到<dwg,dwt,dws>文件 ,命令终止.")
               (exit)
        )
    )
    (vlax-for doc (vlax-get app 'documents)
        (setq dwl (cons (cons (strcase (vlax-get doc 'fullname)) doc) dwl))
    )
    (setq var (mapcar 'vlax-ename->vla-object (mai_ssget_2_list sel))
          var (vlax-make-variant
                  (vlax-safearray-fill
                     (vlax-make-safearray vlax-vbobject (cons 0 (1- (length var)))) var
                  )
              )
    )
    (foreach dwg lst
        (if (or (setq doc (mai_dxf (strcase dwg) dwl))
                (and (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-open (list dbx dwg))))
                     (setq doc dbx)
                )
            )
            (progn
                (vla-copyobjects acd var
                    (vlax-get
                        (cond ((_getitem (vlax-get doc 'layouts) tab))
                              ((vla-add  (vlax-get doc 'layouts) tab))
                        )
						'block
                    )
                )
                (vla-saveas doc dwg)
                (setq cnt (1+ cnt))
            )
            (princ (strcat "\n无法访问文件 < " dwg " >"))
        )
    )
    (setq msg (if (< 0 cnt)
                  (strcat "\n " (itoa (sslength sel)) " 个实体" " 被复制到了 " (itoa cnt) " 个文件." )
                   ""
              )
          err (- (length lst) cnt)
    )
    (if (< 0 err)
        (setq msg (strcat msg "\n不能复制到的文件有 " (itoa err) " 个."))
    )
    (princ msg)
    (if (= 'vla-object (type dbx))
        (vlax-release-object dbx)
    )
    (mai_end)
)
