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

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Close all open drawings
« on: July 13, 2009, 11:12:40 AM »
I have this function in my next and previous drawing routine. The problem is that it is buggy and doesnot always work, thus locking up AutoCAD. Anybody have any ideas?

Code: [Select]
;;;Here is another one that will close all open documents except the current one.
;;;by Peter Jamtgaard
(defun ARCH:Close_All  (/ DOCOBJ FULL)
  (vl-load-com)
  (vlax-for
         DOCOBJ  (vla-get-documents (vlax-get-acad-object))
    (setq FULL (strcat (vla-get-path DOCOBJ) "\\" (vla-get-name DOCOBJ)))
    (if (= (vla-get-active DOCOBJ) :vlax-false)
      (if (vl-catch-all-error-p
            (vl-catch-all-apply
              '(lambda (X) (vla-close X :vlax-true FULL))
              (list DOCOBJ)))
        (princ (strcat "\nCan not close drawing "
                       FULL
                       " with active command in progress:")))))
  (princ))
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Close all open drawings
« Reply #1 on: July 13, 2009, 11:28:03 AM »
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)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #2 on: July 13, 2009, 12:00:16 PM »
Thanks Tim, seems tobe working pecrfectly. I'm using it with your routines: "MyNext" and MyPrevious".
I use these two routines all of the time. Thanks for sharing them.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Close all open drawings
« Reply #3 on: July 13, 2009, 12:07:02 PM »
Thanks Tim, seems tobe working pecrfectly. I'm using it with your routines: "MyNext" and MyPrevious".
I use these two routines all of the time. Thanks for sharing them.

I use them all the time also.  Good to hear that are still worth using.  You're welcome Gary.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #4 on: July 23, 2009, 05:37:10 PM »
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.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Close all open drawings
« Reply #5 on: July 23, 2009, 05:44:28 PM »
Here's two for you  :-)

Open All Drawings in a Directory:

Code: [Select]
;; Open_all by Lee Mac

(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 Mac

(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
« Last Edit: June 27, 2012, 07:21:31 AM by Lee Mac »

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #6 on: July 23, 2009, 06:02:05 PM »
Thanks Lee

Here is the link to Tim's "Mynext" and "Myprevious" routines. These are the most used routines in our office.
It's just over my head to rework...Maybe I can nudge Tim Willey.
http://www.theswamp.org/index.php?topic=21986.0
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Close all open drawings
« Reply #7 on: July 23, 2009, 06:05:28 PM »
Are the routines I posted not suitable for your purpose?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Close all open drawings
« Reply #8 on: July 23, 2009, 06:31:09 PM »
Do the .Net ones not work anymore?  My .Net stuff works in '09, but haven't tried in '10.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #9 on: July 23, 2009, 06:31:43 PM »
Are the routines I posted not suitable for your purpose?

Lee

Thanks for your routines.

Please refer to Tim's routines which open the next or previous drawings in the current directory.
While you have a drawing open in the current directory, you can open the next drawing and so on.
Very useful for scrolling to the next drawing without having to go with the open command or without having to browse.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #10 on: July 23, 2009, 06:33:52 PM »
Do the .Net ones not work anymore?  My .Net stuff works in '09, but haven't tried in '10.

Hi Tim

May have to refresh my bad memory. I think I remember them, but have not used them.
I think they should work. That right they are the dll routine taht has to be registered.

I will give them a try.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Close all open drawings
« Reply #11 on: July 23, 2009, 06:35:57 PM »
Do the .Net ones not work anymore?  My .Net stuff works in '09, but haven't tried in '10.

Hi Tim

May have to refresh my bad memory. I think I remember them, but have not used them.
I think they should work. Can you post the link to your routines?

They are the ones in the link you posted.  The ones you should be using are the .Net ones, not the Lisp ones.  IIRC that is.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Close all open drawings
« Reply #12 on: July 23, 2009, 06:52:37 PM »
Are the routines I posted not suitable for your purpose?

Lee

Thanks for your routines.

Please refer to Tim's routines which open the next or previous drawings in the current directory.
While you have a drawing open in the current directory, you can open the next drawing and so on.
Very useful for scrolling to the next drawing without having to go with the open command or without having to browse.

I see -

They sound like some top-class functions - nice one Tim  :-)

GDF

  • Water Moccasin
  • Posts: 2081
Re: Close all open drawings
« Reply #13 on: July 23, 2009, 06:57:55 PM »
Tim, yes they work, I had to revise the reg code to the following:

Code: [Select]
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R18.0\ACAD-8001:409\Applications\NextPrev18\Groups]
"Support"="Support"


[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R18.0\ACAD-8001:409\Applications\NextPrev18]
"DESCRIPTION"="Next Previous Drawing in the Directory"
"LOADCTRLS"=dword:00000004
"LOADER"="C:\\Arch_Custom\\Support\\NextPrev18.dll"
"MANAGED"=dword:00000001

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R18.0\ACAD-8001:409\Applications\NextPrev18\Commands]
"MyNext"="MyNext"
"MyPrevious"="MyPrevious"

Thanks again Tim. Works perfectly in version 2010.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Close all open drawings
« Reply #14 on: July 23, 2009, 07:00:35 PM »
Thanks Lee.  I also use them, and find them valuable.

Good to hear Gary.  Thanks for confirming.  That is the good time about .Net over ObjectArx.  Don't have to recompile as often.   :wink:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

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).