Lisp To AUTOMATICALLY Save And Close All Opened Files

Lisp To AUTOMATICALLY Save And Close All Opened Files

omorah
Collaborator Collaborator
2,418 Views
3 Replies
Message 1 of 4

Lisp To AUTOMATICALLY Save And Close All Opened Files

omorah
Collaborator
Collaborator

MACRO
The below MACRO, I use it when I have lots of drawings opened to change some items in them.
Say I have 5 or 20 sheets opened to change something on each of the sheets.
I added it to my CUIX and added a button for it.

 

1. Open 5 or 20 or more sheets

2. I would use the CTRL/TAB keys to cycle
through the individual sheets for the edits.

3. ^C^C_purge;all;*;n;zoom;e;qsave;CLOSE;

When done with all the sheets, I would use
this MACRO (button on my personal toolbar),
to close the opened sheets, one by one.
-------------------------------------------------------------------------------

LISP
CloseAllButActive.lsp
This will close all opened files, without zooming and saving, just close all opened files except leave the current file opened. If I have 20 files opened, and am on sheet 5, with this LISP, every other opened file will be closed while sheet 5 (current) will remain opened.

 

(defun doc:CloseAllButActive (TrueOrFalse / cnt)
(setq cnt 0)
(vlax-for Item (vla-get-Documents (vlax-get-acad-object))
(if (= (vla-get-Active Item) :vlax-False)
(progn
(vla-close Item TrueOrFalse)
(setq cnt (1+ cnt))
)
)
)
cnt
)

(defun c:cns ( / cnt) ;CLOSES ALL NO SAVE BUT CURRENT ACTIVE
(setq cnt (doc:CloseAllButActive :vlax-False))
(if (> cnt 0)
(princ (strcat "\n(" (itoa cnt) ") document" (if (> cnt 1) "s" "") "
closed without saving."))
(princ "\nNo documents to close.")
)
(princ)
)
-------------------------------------------------------------------------------

My ideal goal would be to kinda combine both the MACRO and the LISP to achieve the following.

 

With say, 20 files opened, edit each file by cycling with the CTRL/TAB, when done with all edits, apply one LISP or MACRO, to do what the above MACRO does, but also automatically save and close the files one by one. That is:

 

_purge;all;*;n;zoom;e;qsave;CLOSE; then go to the next opened file and do the same all over again.

 

I thought I had the LISP to do this, but cannot find it. Seen lots of close functions but not exactly what I am looking for. I would really appreciate if anyone would help me. I hope I am clear enough with my needs, with my English here.
Cheers

0 Likes
2,419 Views
3 Replies
Replies (3)
Message 2 of 4

john.uhden
Mentor
Mentor

@omorah 

How do want to handle any unnamed (new) docs?

John F. Uhden

0 Likes
Message 3 of 4

hak_vz
Advisor
Advisor

Try this

(defun c:savePurgeAndCloseAll () 
	(vlax-for doc (vlax-get (vlax-get-acad-object) 'Documents)
		(cond
			((not (wcmatch  (vlax-get doc 'Name) "Drawing*.dwg"))
				(vla-purgeall doc)
				(vla-save doc)
				(if 
					(not 
						(= 
							(vlax-get (vla-get-activeDocument (vlax-get-acad-object)) 'Name)
							(vlax-get doc 'Name)
						)
					)
					(vla-close doc)
				)
			)
		)
	)
	(princ "\nAll files were saved, purged and closed! New drawings need to be named and saved")
	(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 4 of 4

sander.van.pelt
Advocate
Advocate

Hello Hak_vz

I use a lisp file which, after executing multiple commands (purge, audit, xrefs locks + sent to back, lock viewports and zoom extents the model and all layout tabs), saves and closes a drawing.
How should I process my lisp in your lisp so that it performs these actions on all open drawings and then also closes AutoCAD completely.

All drawings that I have open are existing, previously saved drawings. Because of my set variable, AutoCAD does not start automatically with "Drawing 1".

Therefore, previously unsaved drawings will not be saved.

Thank you

0 Likes