How to run lisp or script on multiple opened files in session

How to run lisp or script on multiple opened files in session

Anonymous
Not applicable
3,501 Views
10 Replies
Message 1 of 11

How to run lisp or script on multiple opened files in session

Anonymous
Not applicable

I have written a routine to plot all layouts in a giving dwg. I want to be able to run the routine on all opened dwg files in the session. I can change the focus of the active dwg in the session, then i lose the routine. and it all stops. i tried doing the same with a script and it all stops after the active dwg changes. I am cant's just add it to the acaddoc.lsp because i am not reopening the dwg, i need it to run after the editing has been competed.

 

any thoughts?

 

thanks in advanced!

0 Likes
3,502 Views
10 Replies
Replies (10)
Message 2 of 11

Moshe-A
Mentor
Mentor

@Anonymous hi,

 

for such kind of tasks Autodesk gave us PUBLISH command.

 

??

 

moshe

 

 

 

0 Likes
Message 3 of 11

Sea-Haven
Mentor
Mentor

Here is a start it makes a list of current dwgs open, I jave somewhere a swap dwgs will try to find.

 

(defun listinopendrawings ()
(vl-load-com)
(setq obj (vlax-get-acad-object))
(setq ACdocuments (vla-get-documents obj))
(setq doccount (vla-get-count acdocuments))
(setq i 0)
(setq pathlist'())
(repeat doccount
(setq item (vla-item acdocuments i))
(setq path (vla-get-fullname item))
(setq pathlist (cons path pathlist))
(setq i (+ i 1))
)
(princ pathlist) )
(listinopendrawings)
Message 4 of 11

maratovich
Advisor
Advisor

Use this:

 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 5 of 11

Sea-Haven
Mentor
Mentor

Very nice copy dwg names from explorer Smiley Very Happy

0 Likes
Message 6 of 11

Anonymous
Not applicable

thanks but my routine does more than just plotting, publish is way to limiting.

0 Likes
Message 7 of 11

Anonymous
Not applicable

thanks, this is what i am already using to gather a list of the current drawings opened

 

(vlax-map-collection (vla-get-documents (vlax-get-acad-object)) '(lambda (x) (setq lst (cons (vla-get-name x) lst) fulllst (cons (vla-get-fullname x) fulllst))) )
(reverse fulllst)(reverse lst)

 

fulllst = list with the paths

lst = just list of dwg names

0 Likes
Message 8 of 11

Anonymous
Not applicable

thanks! looking for a programming solution without the purchase of more software

0 Likes
Message 9 of 11

Anonymous
Not applicable

Let me re-phase the problem.  After i redirect the active document i want to run code in that active document. I have noticed that the focus is redirected but once i go back to the original active document the code was ran in that document and not the active document. How do i change that?

 

below is the code i am using to set the active document, and then the code 

 

(defun f:setactive (x)
(vla-activate (vla-item (vla-get-documents (vlax-get-acad-object)) x))
)

 

(defun C:test ()

 (f:setacvtive "test.dwg")  <----- this sets the document correctly

 (load "fun") <- this only loads in the original active document

)

 

 

 

0 Likes
Message 10 of 11

Anonymous
Not applicable

To my understanding each open dwg has its own lisp environment and you can't send things between them through lisp (could be wrong on that, but I've done a fair bit of research trying to get that to happen and I haven't found anything).

For executing lisp across multiple dwgs I use a lisp that writes an AutoCAD script and then calls it, and the script opens each dwg, loads the desired lisp routine, runs the routine, and then usually saves and closes the dwg. There are some drawbacks to this approach, such as the dwgs must be closed when the script tries to open them (otherwise CAD will ask if you want a read-only copy opened and that will interrupt the script), and scripts are more sensitive to some kinds of errors (I've had situations where the lisp routine wants to update a block definition but not insert an actual copy of the block, so it did a (command "-insert" "blockname=blockfile.dwg" nil) which works fine in a lisp and generates no errors, but in a script it sees the nil on a command and aborts the script). 

So it doesn't quite do what you want in the sense you can't do it on all open drawings (unless you're more proficient at scripting than I and can write a script to change focus and continue executing), but if this approach interests you then you can find a great template dialog box written by Lee-Mac here:

http://www.lee-mac.com/getfilesdialog.html

 

Which I have then modified several times for different purposes, adding various things to the dcl portion so the users can select whatever options are relevant to that routine, and then processing that information at the end by adding a handler similar to this:

(setq scriptFile (open "C:\\Program Files\\AutoCAD\\Support\\batchPlot.scr" "W"))
    (foreach dwgname rtn ;for each drawing on the list of drawings to be processed
		;add to the script commands to open the file then perform a multi plot without prompts
		(princ (strcat "_.open \"" dwgname "\" (load \"W:\\\\RUDDS-CAD\\\\LISP\\\\PLOT-REVIEW.lsp\") (PLOT_REVIEW_MULTI ") scriptFile)
		(cond 
			((= pltA0 "1");if plot A0 was selected add to the script the plotting routine
				(princ "\"A0\" " scriptFile)
			)
			((= pltA1 "1");if plot A1 was selected add to the script the plotting routine
				(princ "\"A1\" " scriptFile)
			)
			((= pltA2 "1");if plot A2 was selected add to the script the plotting routine
				(princ "\"A2\" " scriptFile)
			)
			((= pltA3 "1");if plot A3 was selected add to the script the plotting routine
				(princ "\"A3\" " scriptFile)
			)
			((= pltB1 "1");if plot B1 was selected add to the script the plotting routine
				(princ "\"B1\" " scriptFile)
			)
		)
		(cond
			((= POrange "1") (princ "\"orange\"" scriptFile))
			((= PBlue "1") (princ "\"blue\"" scriptFile))
			((= PGrey "1") (princ "\"grey\"" scriptFile))
			((= PRed "1") (princ "\"red\"" scriptFile))
		)
		(cond
			((= pltMono "1")
				(princ " \"MONO\") " scriptFile)
			)
			((= pltTrueColour "1")
				(princ " \"TRUE\") " scriptFile)
			)
			((= pltFullColour "1")
				(princ " \"FULL\") " scriptFile)
			)
		)
		(if (= tskClose "1");if close the files was selected add to the script to save and close
			(princ "_.qsave _.close " scriptFile)
		)
	)
	(close scriptFile);close the editing of the script file
	(command "_.script" "C:\\Program Files\\AutoCAD\\Support\\batchPlot.scr");run the script file

It's a bit messy, but I've found it incredible as a time saver for processing multiple dwgs quickly and consistently.

If you're interested in this approach and want some examples of a fully modified version of Lee-Mac's program let me know.

Message 11 of 11

Anonymous
Not applicable

Thanks, it helps but i was trying to avoid having to reopen the drawings.  

0 Likes