Author Topic: Close all open drawings  (Read 7799 times)

0 Members and 2 Guests are viewing this topic.

ArgV

  • Guest
Re: Close all open drawings
« Reply #15 on: September 07, 2009, 11:45:08 AM »
Ok you lisp guru's, is there a way to open drawings in a directory using Tim's "mynext" and "myprevious" without vba?
It would be great to have these functions for autocad 2010, since it does not support vba.

Acad 2010 doesn't support VBA?

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Close all open drawings
« Reply #16 on: September 07, 2009, 11:59:42 AM »
Ok you lisp guru's, is there a way to open drawings in a directory using Tim's "mynext" and "myprevious" without vba?
It would be great to have these functions for autocad 2010, since it does not support vba.

Acad 2010 doesn't support VBA?
you have to install it, no longer standard.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Crank

  • Water Moccasin
  • Posts: 1503
Re: Close all open drawings
« Reply #17 on: September 07, 2009, 12:11:41 PM »
Acad 2010 doesn't support VBA?
Autodesk will continue to support VBA now and into the foreseeable future in the AutoCAD product line. They have a transition plan to .NET and VSTA and expect to support VBA until research shows most customers have migrated their code (which could take years.)

At the moment Autodesk is offering VBA for download on their website, but I've read the 64bit version is very slow (haven't tried it).
VB(A) doesn't have a future because everyone is moving to 64bit and Microsoft doesn't support VB any more.
Vault Professional 2023     +     AEC Collection

taner

  • Guest
Re: Close all open drawings
« Reply #18 on: September 11, 2009, 04:34:39 AM »
Here's two for you  :-)

Open All Drawings in a Directory:

Code: [Select]
;; Open_all by Lee McDonnell

(defun c:open_all (/ path dwg)
  (if (setq path (Directory-Dia "Select Directory: "))
    (progn
      (foreach dwg (mapcar
                     (function
                       (lambda (file)
                         (strcat path file)))
                     (vl-directory-files path "*.dwg" 1))
      (if (vl-catch-all-error-p
            (vl-catch-all-apply
              (function
                (lambda ( )
                  (vla-open
                    (vla-get-Documents
                      (vlax-get-acad-object)) dwg :vlax-false)))))
        (princ (strcat "\n** " dwg " Failed to Open **")))))
    (princ "\n*Cancel*"))
  (princ))

(defun Directory-Dia  (Message / sh folder folderobject result)
  ;; By Tony Tanzillo
  (vl-load-com)
  (setq sh (vla-getInterfaceObject
             (vlax-get-acad-object) "Shell.Application"))
  (setq folder (vlax-invoke-method sh 'BrowseForFolder
                 (vla-get-HWND
                   (vlax-get-Acad-Object)) Message 0))
  (vlax-release-object sh)
  (if folder
    (progn
      (setq folderobject (vlax-get-property folder 'Self)
            result (vlax-get-property FolderObject 'Path))
      (vlax-release-object folder)
      (vlax-release-object FolderObject)
      (if (/= (substr result (strlen result)) "\\")
        (setq result (strcat result "\\"))
        result))))

Open Latest Drawing in a Directory:

Code: [Select]
;; Open_last by Lee McDonnell

(defun c:open_last (/ path lst dwg)
  (if (setq path (Directory-Dia "Select Directory: "))
    (progn
      (foreach dwg (mapcar
                     (function
                       (lambda (file)
                         (strcat path file)))
                     (vl-directory-files path "*.dwg" 1))
        (setq lst
               (cons
                 (list dwg (remove_nth (vl-file-systime dwg) 2)) lst)))
      (if (vl-catch-all-error-p
            (vl-catch-all-apply
              (function
                (lambda ( )
                  (vla-open
                    (vla-get-Documents
                      (vlax-get-acad-object))
                    (setq dwg (dSort lst)) :vlax-false)))))
        (princ (strcat "\n** " dwg " Failed to Open **"))))
    (princ "\n*Cancel*"))
  (princ))

(defun Directory-Dia  (Message / sh folder folderobject result)
  ;; By Tony Tanzillo
  (vl-load-com)
  (setq sh (vla-getInterfaceObject
             (vlax-get-acad-object) "Shell.Application"))
  (setq folder (vlax-invoke-method sh 'BrowseForFolder
                 (vla-get-HWND
                   (vlax-get-Acad-Object)) Message 0))
  (vlax-release-object sh)
  (if folder
    (progn
      (setq folderobject (vlax-get-property folder 'Self)
            result (vlax-get-property FolderObject 'Path))
      (vlax-release-object folder)
      (vlax-release-object FolderObject)
      (if (/= (substr result (strlen result)) "\\")
        (setq result (strcat result "\\"))
        result))))

; Stig
(defun remove_nth (lst i / a)
  (setq a -1)
  (vl-remove-if
    (function
      (lambda (n) (= (setq a (1+ a)) i))) lst))

(defun dSort (lst / lst)
  (while
    (progn
      (cond ((not
               (vl-remove-if 'null
                 (mapcar 'cadr lst)))
             (setq lst nil))
            ((apply '=
               (vl-sort
                 (mapcar 'caadr lst)
                   (function
                     (lambda (a b) (< a b)))))
             (setq lst (mapcar
                         (function
                           (lambda (x)
                             (list (car x) (cdadr x)))) lst)))
            (t (setq lst (caar lst)) nil))))
  lst)

Enjoy :P
Can't open the "lt version" dwg.

Joe Burke

  • Guest
Re: Close all open drawings
« Reply #19 on: September 11, 2009, 08:57:14 AM »
I'm not sure this applies to the issue, but it works well for me.

Code: [Select]
;; JB 12/5/2008
;; Save and close all open documents.
(defun c:SaveCloseAll ( / *acad* ad)
  (vl-load-com)
  (setq *acad* (vlax-get-acad-object)
        ad (vla-get-ActiveDocument *acad*)
  )
  (vlax-for x (vla-get-documents *acad*)
    (if (not (equal x ad))
      (progn
        (vlax-invoke x 'Save)
        (vlax-invoke x 'Close)
      )
    )
  )
  (command "._close" "yes")
) ;end

;------------------------------------
;shortcut
(defun c:SCA () (c:SaveCloseAll))
;------------------------------------

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #20 on: September 11, 2009, 11:04:23 AM »
Thanks guys.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

taner

  • Guest
Re: Close all open drawings
« Reply #21 on: September 11, 2009, 08:02:08 PM »
Here is mine.  Pretty much the same thing, but mine doesn't try and save them.  It hasn't locked up my Acad though.

Code: [Select]
(defun c:CloseAllButActive(/ tmpList)
    
    (vl-load-com)
    (vlax-for item (vla-get-documents (vlax-get-acad-object))
        (if (= (vla-get-active item) :vlax-false)
            (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-close (list item :vlax-false)))
                (setq tmpList (cons item tmpList))
            )
        )
    )
    (foreach item tmpList
        (prompt (strcat "\n Drawing could not be closed \"" (vla-get-name item) "\"."))
    )
    (princ)
)
Chang:
Code: [Select]
(defun c:CloseAllButActive (/ tmpList)
  (vl-load-com)
  (vlax-for item (vla-get-documents (vlax-get-acad-object))
    (if
      (vl-catch-all-error-p
(vl-catch-all-apply
  'vla-close
  (list item :vlax-false)
)
      )
       (prompt (strcat "\n Drawing could not be closed \""
       (vla-get-name item)
       "\"."
       )
       )
    )
  )
  (princ)
)
« Last Edit: September 11, 2009, 08:06:39 PM by Tsec2nd »

jxphklibin

  • Guest
Re: Close all open drawings
« Reply #22 on: September 13, 2009, 03:48:20 AM »
Here is my routine:

Code: [Select]
;;不保存关闭所有文档
(defun c:USave-CloseAll ( / ACADOBJ DOCS DOC)
   (vl-load-com)
   (SETVAR "SDI" 0)
   (setq ACADOBJ (vlax-get-acad-object)
  DOCS (vla-get-documents ACADOBJ)
  DOC (vla-get-activedocument ACADOBJ)
   )
   (vlax-for d DOCS
     (if (not (equal DOC d))
       (vla-close d :vlax-false)
     )
   )
   (command "vbastmt"  "AcadApplication.activeDocument.close false ")
   ;;最后关闭当前活动的文档,这样就把所有的文档都关闭了 false 不保存关闭当前文档,true 保存然后关闭当前文档
   (princ)
)

;;保存并关闭所有文档
(defun c:Save-CloseAll ( / ACADOBJ DOCS DOC)
   (vl-load-com)
   (SETVAR "SDI" 0)
   (setq ACADOBJ (vlax-get-acad-object)
  DOCS (vla-get-documents ACADOBJ)
  DOC (vla-get-activedocument ACADOBJ)
   )
   (vlax-for d DOCS
     (if (not (equal DOC d))
       (vla-close d :vlax-true) ;_改为:vlax-true表示保存后关闭
     )
   )
   (command "vbastmt"  "AcadApplication.activeDocument.close true ")
   ;;最后关闭当前活动的文档,这样就把所有的文档都关闭了 false 不保存关闭当前文档,true 保存然后关闭当前文档
   (princ)
)

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Close all open drawings
« Reply #23 on: September 15, 2009, 02:43:13 PM »
2010 has this feature built right in.  :roll: :evil: :lol:

I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

antistar

  • Guest
Re: Close all open drawings
« Reply #24 on: June 27, 2012, 07:14:22 AM »
I'm not sure this applies to the issue, but it works well for me.

Code: [Select]
;; JB 12/5/2008
;; Save and close all open documents.
(defun c:SaveCloseAll ( / *acad* ad)
  (vl-load-com)
  (setq *acad* (vlax-get-acad-object)
        ad (vla-get-ActiveDocument *acad*)
  )
  (vlax-for x (vla-get-documents *acad*)
    (if (not (equal x ad))
      (progn
        (vlax-invoke x 'Save)
        (vlax-invoke x 'Close)
      )
    )
  )
  (command "._close" "yes")
) ;end

;------------------------------------
;shortcut
(defun c:SCA () (c:SaveCloseAll))
;------------------------------------

I want to take the theme of this post to ask:
 Does anyone know how to close all drawings without saving?

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Close all open drawings
« Reply #25 on: June 27, 2012, 07:25:50 AM »
I want to take the theme of this post to ask:
 Does anyone know how to close all drawings without saving?

Using Joe's method:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ca ( / app acd )
  2.           acd (vla-get-activedocument app)
  3.     )
  4.     (vlax-for doc (vla-get-documents app)
  5.         (if (not (equal doc acd))
  6.             (vla-close doc)
  7.         )
  8.     )
  9.     (command "_.close" "_Y")
  10.     (princ)
  11. )

Or, simply the 'closeall' command (Express Tools).