Yeah, it's doable. It takes a few steps.
The basic idea:
- 2 lisp files; "parent_lisp.lsp" and "batch_execute.lsp"
- Open a drawing, , load parent_lisp.lsp, execute BOF (batch open files)
- "batch_execute.lsp" should be included in the files that get auto loaded any time a drawing is opened, see:
https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Automatically-load-AutoLISP-routines.html
now, "batch_execute.lsp" holds a variable
(setq specified_folder "C:\\Data\\desktop\\lisp\\CADTUTOR\\batch_execute\\")
If the drawing is saved in that folder, then a function gets executed. Else it's ignored.
Obviously set this specified_folder to your folder. don't forget each \ must get doubled to \\, and finish with \\.
When you no longer need this batch auto execute, then remove "batch_execute.lsp" from the list of auto loaded lisp files.
I tried it, it works.
You might try it for a limited number of dwg's. You may get a RAM overload or something.
-------
"parent_lisp.lsp"
;; @file this file opens all drawings in a specified folder.
(vl-load-com)
(setq specified_folder "C:\\Data\\desktop\\lisp\\CADTUTOR\\batch_execute\\") ;; don't forget the \\ at the end
;; substring, like php substr
(defun substring ( str idx len )
(substr str (if (< idx 0) (+ 1 (strlen str) idx) (1+ idx)) len)
)
(defun open_file (FileName ReadOnly / )
(vla-Open
(vla-get-Documents
(vlax-get-Acad-Object)
)
FileName
(if ReadOnly
:vlax-true
:vlax-false
)
)
)
;; command BOF, for Batch Open Files
(defun c:bof ( / path allfiles FileName)
;; path of the drawings that need to be opened, and the batch function executed on each drawing
(setq path specified_folder)
;; find all the drawings in that folder
;; @see https://www.cadtutor.net/forum/topic/50837-list-of-drawings-in-a-folder/
(setq allfiles (vl-directory-files path "*.DWG" 0))
(princ allfiles)
(foreach FileName allfiles
(open_file (strcat path FileName) ReadOnly)
(Command "DELAY" "2000") ; Pause a couple seconds
)
)
"batch_execute.lsp":
;; @file this file is to be auto loaded when a drawing opens: see (1). Then check the folder in which the drawings are saved.
;; If that folder is the specified foler (setq specified_folder ...), then execute a function.
;; Then close the drawing
;; (1) https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Automatically-load-AutoLISP-routines.html
(vl-load-com)
(setq specified_folder "C:\\Data\\desktop\\lisp\\CADTUTOR\\batch_execute\\") ;; don't forget the \\ at the end
(defun auto_execute ( / dwg_path)
(setq dwg_path (getvar "DWGPREFIX"))
(if (= specified_folder dwg_path)
(do_batch_function)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; as an example, do_batch_function will write the filename of the dwg on the drawing, on x,y = 0,0
;; https://www.cadtutor.net/forum/topic/18257-entmake-functions/
(defun drawText (pt hgt str)
(entmakex (list (cons 0 "TEXT")
(cons 10 pt)
(cons 40 hgt)
(cons 1 str))))
(defun do_batch_function ( / )
(drawText (list 0.0 0.0) 2.5 (getvar "dwgname"))
(command "QSAVE")
;;(command "CLOSE")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(auto_execute)