(defun c:batch (/ dir files total ct docs doc)
(defun listAllFiles (path ext /)
(apply 'append
(cons
(mapcar
(function (lambda(x) (strcat path "\\" x)))
(vl-directory-files path ext 1)
)
(mapcar
(function (lambda(x) (listAllFiles (strcat path "\\" x) ext)))
(cddr (vl-directory-files path nil -1)) ; to exclude "." and ".."
)
)
)
)
(if (and
(setq dir (getfiled "Select a directory to process..." "Start here" "" 33))
(setq dir (vl-filename-directory dir))
)
(if (setq files (listAllFiles dir "*.dwg"))
(progn
(vl-load-com)
(setq total (itoa (length files))
ct 0
docs (vla-get-documents (vlax-get-acad-object))
)
(foreach file files
(setq doc (vla-open docs file)
ct (1+ ct)
)
(prompt (strcat "\r" (itoa ct) " of " total " drawings processed."))
(command "_.delay" "1000")
(vla-close doc :vlax-false)
)
(vlax-release-object doc)
(vlax-release-object docs)
)
(prompt (strcat "\nNo files of that type found in " dir " !"))
)
(prompt "\nNo path specified!")
)
(princ)
)
The command defines a function called listAllFiles, which recursively lists all files of a certain type in a directory (and its subdirectories, obviously). This was a pain in the ass to create. Anyway, all it does is ask the user for a directory, and then uses Visual Lisp/ActiveX to open each drawing, wait for 1 second, and then close it. All you have to do is add whatever .lsp you want to use into your startup suite.
Procedure:
- Save the above code as a .lsp file
- Add the .lsp file you're going to use to process the drawings to the startup suite (command: APPLOAD or Tools>Load Application). This .lsp file should end with (command "qsave") to save the drawing. DO NOT have (command "close") anywhere in the file, as this will cause an access violation and crash AutoCAD, because AutoCAD sucks.
- Restart AutoCAD and load the .lsp file from step 1. Run the "batch" command, select your directory, sit back and watch it do the work for you.
Uses: Fixing a detail library (or several).
Notes:
I used getfiled to pick the directory because not everyone has Express Tools installed. Otherwise you can use the nicer (acet-ui-pickdir). Also, I don't really think the value for the delay matters that much. It appears that when you call vla-open, it switches to that new drawing long enough for things in the startup suite to do their work, then switch back to the parent drawing. Still, it's a good idea to keep the delay in there.
Hi
ReplyDeleteJust managed to get this to work. Well done!
I thought I knew lisp, but this almost seems like a different language for me. Maybe this visual lisp is worth investigating after all.
After a long search for such an item (free!) yours looks like the best answer.
Keep up the good work!
Regards
Bill Le Couteur