PDA

View Full Version : Script to execute lisp on multiple drawings



jrd.chapman
2005-02-02, 09:58 PM
I am hoping to write a script to run a lisp function on multiple drawings. I'm not sure how to accomplish this though. I need the user to first be prompted to select the directory containing drawings to be processed, and then have the script run on each drawing in that directory.

I have a small function written that allows the user to select the directory. Then all drawings in that directory are compiled into a list.

Now that the user has specified what to process, I'm not sure where to go. I don't know how I can now use a script to open a drawing in this directory, run the Lisp routine that needs to be run, then save, close, and move onto the next drawing in the directory.

Does anyone have any advice? I know this can be accomplished easier in VBA, but I would prefer to use a script.

Any advice?

Thanking you in advance

PellaCAD
2005-02-02, 10:22 PM
http://www.pellacad.com/sm80-password.htm

ScriptMan Pro does pretty much what you need plus a whole lot more!

...and it's fast and easy to use.

Pete
pellacad@kdsi.net


(I need help with DCL files...know anything about them?)

kennet.sjoberg
2005-02-02, 10:42 PM
Hi John, You have to combine the list of drawings and the list of commands in one script file, like :

open
"C:\MyMap\MyDrawing_1.dwg"
commands . . .
.CLOSE Y or N
open
"C:\MyMap\MyDrawing_2.dwg"
commands . . .
.CLOSE Y or N
open
"C:\MyMap\MyDrawing_3.dwg"
commands . . .
.CLOSE Y or N

: ) Happy Computing !

kennet

by the way check out ScriptMan Pro or anything similar

Lemons
2005-02-03, 02:26 PM
Hi, John!!! Long time no see :)

Why don't you just use ScriptPro?

Celie

rmoore
2005-02-03, 03:26 PM
I had done this several years ago by using two text files. One contained a list of the files I was applying the change to. The other was just a counting file that was used to tell the lisp program what line to read in the list.

I did this to read in dxf files and save each as a drawing:



;Program to read a dos text file of a directory listing of dxf files.
;used to read dxf files into AutoCad.
(defun S::STARTUP ()
(readdxf)
)
(defun *error* ()
(close cntfile)
(close dxflist)
(princ)
)
(defun readdxf ()
(setq cntfile (open "i:/testdxf/testcnt.txt" "r"))
(setq filecnt (read-line cntfile))
(setq dxflist (open "i:/testdxf/test.txt" "r"))
(repeat (1- (atoi filecnt))
(setq testread (read-line dxflist))
);endrepeat
(setq testread (read-line dxflist))
(strip)
(princ dxfread)
(command "dxfin" dxfread)
(command "saveas" dwgread)
(setq filecnt (1+ (atoi filecnt)))
(close cntfile)
(close dxflist)
(setq cntfile (open "i:/testdxf/testcnt.txt" "w"))
(princ (rtos filecnt 2 0) cntfile)
(close cntfile)
(command "new" "")
(princ)
)
;Program to strip the file extension and add prefix "I:/testdxf/" to filename. Example: Readme.txt=I:/testdxf/Readme
;also adds prefix "I:/testdxf/dwgs/" to filename. Example: Readme.txt=I:/testdxf/dwgs/Readme
(defun strip ()
(setq cnt 1)
(while (not (equal (substr testread cnt 1) "."))
(setq cnt (1+ cnt))
);endwhile
(setq dxfread (strcat "I:/testdxf/" (substr testread 1 (1- cnt))))
(setq dwgread (strcat "I:/testdxf/dwgs/" (substr testread 1 (1- cnt))))
(princ)
)


The lisp routine was part of the ACAD.lsp file which automatically ran each time a drawing was opened. This was used back in version 13 and I cant guarantee it would even work in todays environment.

jrd.chapman
2005-02-03, 07:32 PM
Thanks for all the replies guys.

Ron, I will try your method when I have a chance, that is an interesting approach!

Hi Celie, yes it has been a while!! As for ScriptPro, I thought that only came with AutoCAD 2000 Migration Assistance....Now, I'm on 2005 and it does not seem to be included anywhere. Is there somewhere I can still get it?

Kennet, Yeah, I know I have to combine evryrhing into one script, but, the list of drawings needs to be created by the user, byt selecting a directory, and then having the script step throught hat directory from one drawing to the next. I'm not in a situation where I can just manually list each drawing in the script. I have hundreds of drawings to process, and I want this to be used in the future. I'm not sure how to incorporate the list (created with LISP), into the script.

naschkeps, thanks for the link, I will check out ScriptMan Pro.

tyshofner
2005-02-03, 11:53 PM
Just to let you know there is a ScriptPro for '05. I'm posting a link but I'm new so I hope it works. If not you can find these on Autodesk's web site, just go to support, choose AutoCAD and then there should be a link on the left called "Data & Downloads" click on that, then on tools, then on Migration Assistance. Here is my link, hope it works.

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=4091668&linkID=2475161

kennet.sjoberg
2005-02-04, 12:08 AM
Hi John, here is a simple and easy code for You to modify. It generate one scriptfile including dwg files in a selected directory and command lines to execute on each of them.


(defun c:MakeMyScript ( / UserFile UserCat FileList File#1 Index FileName )
(vl-load-com )
(setq UserFile (getfiled "Find the the catalog with the drawings to process, and pick one file" "*.*" "*" 16 ) )
(setq UserCat (vl-filename-directory UserFile ) )
(setq FileList (vl-directory-files UserCat "*.dwg" 1 ) )
; ^- those 3 command lines generate a list like ("chroma.dwg" "direct.dwg" "overhead.dwg" "rm_sdb.dwg" "sh_spot.dwg")
; then You need lines, a list or a txt file with the commands to execute on each drawing
; make a while loop and step trough the file list and add the command lines to each file while writing to the script file
(setq File#1 (open "c:/MyTest.scr" "w" ) ) ;; open/make a scriptfile to write to
(setq Index 0 )
(while (setq FileName (nth Index FileList ) ) ;; loop trough every dwg in FilList
(princ (strcat "open " FileName "\n" ) File#1 )
;; write Your command lines here , those lines is NOT tested to run
(princ "._zoom ext \n" File#1 )
(princ "._-layer s 0 \n" File#1 )
(princ "close Yes or No \n" File#1 )
(setq Index (1+ Index ) ) ;; prepare for next file to loop
)
(close File#1 )
(princ "open c:/MyTest.scr and look at it" )
(startapp "NOTEPAD" "c:/MyTest.scr" )
(princ)
)

: ) Happy Computing !

kennet

jrd.chapman
2005-02-07, 02:23 PM
tyshofner, Thanks very much for that link, much appreciated!!

Kennet!!!!! That is wonderful!!! That is exactly what I was trying to accomplish, I was getting hung up on actually writing the script after generating the file list...you have unlocked that mystery for me!!!

Thank you again so much.

Thank you to everyone for their insight. Once again I have learned something from the wonderful people here!

Take care!

jrd.chapman
2005-02-07, 08:46 PM
Just thought I'd share the finished product. Modified Kennet's code (thank you so much again kennet...what I've learned about creating a script from within Lisp is invaluable and I will use it a lot in future applications...)

It is working like a charm so far....Here is what I came up with:


(defun c:BatchLock
(/ UserFile UserCat FileList File#1 DwgName FileName)
(vl-load-com)
(setq UserFile
(getfiled "Select a drawing within the directory to process"
"c:/"
"dwg"
16
)
)
(setq UserCat (vl-filename-directory UserFile))
(setq FileList (vl-directory-files UserCat "*.dwg" 1))
(setq File#1 (open "c:/Lockup.scr" "w"))
;; open/make a scriptfile to write to
(foreach DwgName FileList
(setq FileName (strcat "\"" UserCat "\\" DwgName "\""))
(princ "open\n" File#1)
(princ (strcat FileName "\n") File#1)
;;Below are commands to run on each drawing...
(princ "audit\n" File#1)
(princ "Y\n" File#1)
(princ "(load \"BatchLockRun.vlx\")\n" File#1)
(princ "(C:BATCHLOCKRUN)\n" File#1)
(princ "audit\n" File#1)
(princ "Y\n" File#1)
(princ "_.qsave\n" File#1)
(princ "_.close\n" File#1)
)
(close File#1)
(command "script" "C:\\Lockup.scr")
(princ)
)
(princ "\nBatchLock loaded.")
(princ)


Thanks again to everyone for their help!!

peter
2005-02-07, 10:56 PM
I would suggest creating a dbx routine to change multiple drawings without having to open them. If you are interested I can post some code to get your started.


Peter Jamtgaard

jrd.chapman
2005-02-08, 12:24 PM
I would suggest creating a dbx routine to change multiple drawings without having to open them. If you are interested I can post some code to get your started

That would be great Peter! Always eager to try and learn new things.

kilari_venkatesh693618
2017-07-21, 10:04 AM
I would like to add the following lines after ;; open/make a scriptfile to write to But i see at the command prompt the open command is still active which leads to filedia 0 again
even though i make filedia to 1 before (close File#1)

(setq UserCat (vl-filename-directory UserFile))
(setq FileList (vl-directory-files UserCat "*.dwg" 1))
(setq File#1 (open "c:/Lockup.scr" "w"))
;; open/make a scriptfile to write to
(princ "setvar\n" File#1)
(princ "filedia\n" File#1)
(princ "0\n" File#1)
(foreach DwgName FileList
(setq FileName (strcat "\"" UserCat "\\" DwgName "\""))
(princ "open\n" File#1)
(princ (strcat FileName "\n") File#1)
;;Below are commands to run on each drawing...
(princ "audit\n" File#1)
(princ "Y\n" File#1)
(princ "(load \"BatchLockRun.vlx\")\n" File#1)
(princ "(C:BATCHLOCKRUN)\n" File#1)
(princ "audit\n" File#1)
(princ "Y\n" File#1)
(princ "_.qsave\n" File#1)
(princ "_.close\n" File#1)
)
(princ "setvar\n" File#1)
(princ "filedia\n" File#1)
(princ "1\n" File#1)
(close File#1)
(command "script" "C:\\Lockup.scr")







Just thought I'd share the finished product. Modified Kennet's code (thank you so much again kennet...what I've learned about creating a script from within Lisp is invaluable and I will use it a lot in future applications...)

It is working like a charm so far....Here is what I came up with:


(defun c:BatchLock
(/ UserFile UserCat FileList File#1 DwgName FileName)
(vl-load-com)
(setq UserFile
(getfiled "Select a drawing within the directory to process"
"c:/"
"dwg"
16
)
)
(setq UserCat (vl-filename-directory UserFile))
(setq FileList (vl-directory-files UserCat "*.dwg" 1))
(setq File#1 (open "c:/Lockup.scr" "w"))
;; open/make a scriptfile to write to
(foreach DwgName FileList
(setq FileName (strcat "\"" UserCat "\\" DwgName "\""))
(princ "open\n" File#1)
(princ (strcat FileName "\n") File#1)
;;Below are commands to run on each drawing...
(princ "audit\n" File#1)
(princ "Y\n" File#1)
(princ "(load \"BatchLockRun.vlx\")\n" File#1)
(princ "(C:BATCHLOCKRUN)\n" File#1)
(princ "audit\n" File#1)
(princ "Y\n" File#1)
(princ "_.qsave\n" File#1)
(princ "_.close\n" File#1)
)
(close File#1)
(command "script" "C:\\Lockup.scr")
(princ)
)
(princ "\nBatchLock loaded.")
(princ)


Thanks again to everyone for their help!!