;;-----------------------=={ 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 )

    (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)
    (setq app (vlax-get-acad-object)
          acd (vla-get-activedocument app)
          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 (vla-get-documents app)
        (setq dwl (cons (cons (strcase (vla-get-fullname doc)) 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
                    (vla-get-block
                        (cond ((_getitem (vla-get-layouts doc) tab))
                              ((vla-add  (vla-get-layouts doc) tab))
                        )
                    )
                )
                (vla-saveas doc dwg)
                (setq cnt (1+ cnt))
            )
            (princ (apply 'strcat (cons "\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)
)
