TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: GDF on July 13, 2009, 11:12:40 AM

Title: Close all open drawings
Post by: GDF 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))
Title: Re: Close all open drawings
Post by: T.Willey 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)
)
Title: Re: Close all open drawings
Post by: GDF 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.
Title: Re: Close all open drawings
Post by: T.Willey 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.
Title: Re: Close all open drawings
Post by: GDF 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.
Title: Re: Close all open drawings
Post by: Lee Mac 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
Title: Re: Close all open drawings
Post by: GDF 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
Title: Re: Close all open drawings
Post by: Lee Mac on July 23, 2009, 06:05:28 PM
Are the routines I posted not suitable for your purpose?
Title: Re: Close all open drawings
Post by: T.Willey 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.
Title: Re: Close all open drawings
Post by: GDF 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.
Title: Re: Close all open drawings
Post by: GDF 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.
Title: Re: Close all open drawings
Post by: T.Willey 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.
Title: Re: Close all open drawings
Post by: Lee Mac 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  :-)
Title: Re: Close all open drawings
Post by: GDF 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.
Title: Re: Close all open drawings
Post by: T.Willey 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:
Title: Re: Close all open drawings
Post by: ArgV 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?
Title: Re: Close all open drawings
Post by: alanjt 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.
Title: Re: Close all open drawings
Post by: Crank 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.
Title: Re: Close all open drawings
Post by: taner 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.
Title: Re: Close all open drawings
Post by: Joe Burke 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))
;------------------------------------
Title: Re: Close all open drawings
Post by: GDF on September 11, 2009, 11:04:23 AM
Thanks guys.
Title: Re: Close all open drawings
Post by: taner 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)
)
Title: Re: Close all open drawings
Post by: jxphklibin 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)
)
Title: Re: Close all open drawings
Post by: Krushert on September 15, 2009, 02:43:13 PM
2010 has this feature built right in.  :roll: :evil: :lol:

Title: Re: Close all open drawings
Post by: antistar 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?
Title: Re: Close all open drawings
Post by: Lee Mac 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).