Jump to content

question about batch, lisp, script files


todouble22

Recommended Posts

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

  • todouble22

    25

  • Lee Mac

    19

  • dbroada

    10

  • BIGAL

    4

I write all my scripts in notepad? what are the benefits in doing it in excel? ..ssearching google now...
I quite often have

 

open "file1.dwg" saveas "file1A.dwg"

 

so I can copy my list into a second column but the bulk of the construction is simple find & replace, so whatever works for you.

Link to comment
Share on other sites

I thought I'd invest some time in writing a "Script Maker" to make it easier for you lot :)

 

(defun c:wScript (/ *error* Get_Subs Str-Break Str-Make DirDia

                   FILE FOLD FOLDER OFILE PATH SCRLINE SHELL STRLST)
 
 ;; Lee Mac  ~  11.01.10
 (vl-load-com)
 
 (setq *acad (cond (*acad) ((vlax-get-acad-object)))
       *doc  (cond (*doc)  ((vla-get-ActiveDocument *acad))))
 
 (or *def_opt* (setq *def_opt* "Yes"))
 

 (defun *error* (msg)
   (and ofile (close ofile))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (defun get_subs (folder / file)
   ;; CAB
   (mapcar
     (function
       (lambda (x) (setq file (strcat folder "\\" x))
                   (cons file (apply (function append) (get_subs file)))))
       (cddr (vl-directory-files folder nil -1))))

 
 (defun Str-Break (str del / pos lst)
   ;; Lee Mac  ~  27.05.09
 (while (setq pos  (vl-string-search del str))
   (setq lst (cons (substr str 1 pos) lst)
         str (substr str (+ pos 1 (strlen del)))))
 (reverse (cons str lst)))
 

 (defun Str-Make (lst del / str x)
   ;; Lee Mac  ~  29.12.09
   (setq str  (car lst))
   (foreach x (cdr lst) (setq str (strcat str del x)))
 str)
 

 (defun DirDia (msg dir flag)
   ;; Lee Mac  ~  07.06.09
   (setq Shell (vla-getInterfaceObject *acad "Shell.Application")
         Fold  (vlax-invoke-method Shell 'BrowseForFolder
                 (vla-get-HWND *acad) msg flag dir))
   (vlax-release-object Shell)

   (if Fold
     (progn
       (setq Path (vlax-get-property
                    (vlax-get-property Fold 'Self) 'Path))
       (vlax-release-object Fold)

       (and (= "\\" (substr Path (strlen Path)))
            (setq Path (substr Path 1 (1- (strlen Path)))))))
   
   Path)


 ;;  --=={  Main Function  }==--
 

 (prompt "\n<< Enter Script Operations, use *file* for the Filename >>")
 
 (cond (  (zerop (strlen (setq scrline (getstring t "\n>> Script: "))))

          (princ "\n*Cancel*"))

       (  (< (length (setq StrLst (Str-Break scrline "*file*"))) 2)

          (princ "\n** Delimiter *file* not found in Script String **"))

       (  (not (and (setq Path (DirDia "Select Directory of Files on Which to Operate..." nil 512))
                    
                    (not (initget "Yes No"))
                    (setq *def_opt*
                      (cond ((getkword (strcat "\nProcess SubDirectories? <" *def_opt* "> : ")))
                            (*def_opt*)))
                    
                    (setq file (getfiled "Create Script File" (cond (*load*) ("")) "scr" 1))))
        
          (princ "\n*Cancel*"))

       (t (setq *load* file ofile (open file "w"))

          (foreach filepath (apply (function append)
                                   (vl-remove 'nil
                                     (mapcar
                                       (function
                                         (lambda (Path)
                                           (mapcar
                                             (function
                                               (lambda (File)
                                                 (strcat Path "\\" File)))
                                             (vl-directory-files Path "*.dwg" 1))))
                                       (append (list Path)
                                               (apply (function append)
                                                      (if (= "YES" (strcase *def_opt*))
                                                        (Get_Subs Path)))))))

            (write-line (Str-Make strLst (strcat (chr 34) filepath (chr 34))) ofile))

          (setq ofile (close ofile))
          (princ "\n<< Script Written >>")))

 (princ))

 

You will need to enter a "Script line", which is a list of the operations that you want to perform on the file, but instead of typing the filename, type *file*.

 

This is a Quick purger you may want to call in the script:

 

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] Purger [b][color=BLUE]nil[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]vl-load-com[/color][/b][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]repeat[/color][/b] [b][color=#009900]3[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]vla-purgeall[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]vla-get-ActiveDocument[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]vlax-get-acad-object[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]

 

So your script-line may be something like:

 

_.open *file* (Purger) _.save _.close

 

(Providing the Purger.lsp is loaded from the ACADDOC.lsp file)

 

I hope this helps, please let me know if you have any problems,

 

Lee

Link to comment
Share on other sites

Todouble22

 

Just amend your script file from a single proceedure to a multiple proceedure within the one bat session. Your batch file runs Autocad and calls a script, that script just simply has multiple lines in it calling each drawings as required.

 

The other alternative is, first step of the script could be to run a lisp program that reads the dir.txt file and in turn then writes a script file. The last line is quite simply (command "script" myscript). This is easy to do and the advantage is it reads the most up to date version of your drawing list. The drawing list being created as 1st line in the batch file.

 

I have used this often where a lisp writes a script ! The architectural package I was involved with read a text file containing all the layer specifications by the client hence we did not know what they would use or change so just read it and wrote it to reset when starting a new drawing.

 

So the code could be run from either inside or outside Autocad. You can issue an external Command from within Autocad to run an external operator system command Dir *.DWG > MYLIST.txt /b

 

I will post the code to do it later

Link to comment
Share on other sites

Here is a simple read a file and write the lines out cahnged as a script file (untested also may need a dwg path)

 

[size=2](setvar "cmdecho" 0)[/size]
[size=2](setq infile "yourdwglistfile")[/size]
[size=2](setq scriptfile "MYDWGSCRIPT.SCR")[/size]
[size=2](setq fout (open scriptfile "w"))[/size]
[size=2](setq fo (open infile "r"))[/size]

[size=2](while (setq new_line (read-line fo))[/size]
[size=2](setq ansline (strcat "open" new_line "mynewlisp close y"))[/size]
[size=2](write-line ansline fout)[/size]

[size=2](princ ".")[/size]
[size=2]) [/size]

[size=2](write-line "(princ)" fout)[/size]

[size=2](close fo)[/size]
[size=2](close fout)[/size]
[size=2](princ)[/size]

Link to comment
Share on other sites

I once had to run a batch on 25.000 drawings, (purging, changing the standard text style, renaming layouts).

I bought a program called multi-batch, which basically allows you make a commandline routine, then assign it to any drawing you like. Essentially it makes a scr file for you, no editing in excel or notepad required, it just provides an easy interface for you to do it, one that anyone can understand.

I would simply assign my routine to about 5000 files at a time, hit run, and watch it open AutoCAD and open/edit/save/close all the files, one after the other. I would run it during the day while I was working in another instance of autocad, and would leave it running overnight. It did about 20 drawings per minute, and had all 25.000 finished in a week.

 

http://www.multi-batch.com/

 

I know some take pride in doing things the hard way themselves, but for me this was the best $39 ever spent :)

 

Attached is an example scr file that the program creates when I assign a standard purge routine to a number of files.

Batch.scr

Link to comment
Share on other sites

Hey guys great answers one little problem to be aware of if you have a script file method if you have a directory name with a space in it your script will not work scripts stop at a space expecting it to be next command.

 

Basicly rename your directories to say my_dwgs

Link to comment
Share on other sites

Hi Todd,

 

I thought I'd invest some time in writing a "Script Maker" to make it easier for you lot :)

 

Give this a shot:

 

(defun c:wScript (/ *error* Get_Subs Str-Break Str-Make DirDia

                   FILE FOLD FOLDER OFILE PATH SCRLINE SHELL STRLST)

 ;; Lee Mac  ~  11.01.10
 (vl-load-com)

 (setq *acad (cond (*acad) ((vlax-get-acad-object)))
       *doc  (cond (*doc)  ((vla-get-ActiveDocument *acad))))

 (or *def_opt* (setq *def_opt* "Yes"))


 (defun *error* (msg)
   (and ofile (close ofile))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (defun get_subs (folder / file)
   ;; CAB
   (mapcar
     (function
       (lambda (x) (setq file (strcat folder "\\" x))
                   (cons file (apply (function append) (get_subs file)))))
       (cddr (vl-directory-files folder nil -1))))


 (defun Str-Break (str del / pos lst)
   ;; Lee Mac  ~  27.05.09
 (while (setq pos  (vl-string-search del str))
   (setq lst (cons (substr str 1 pos) lst)
         str (substr str (+ pos 1 (strlen del)))))
 (reverse (cons str lst)))


 (defun Str-Make (lst del / str x)
   ;; Lee Mac  ~  29.12.09
   (setq str  (car lst))
   (foreach x (cdr lst) (setq str (strcat str del x)))
 str)


 (defun DirDia (msg dir flag)
   ;; Lee Mac  ~  07.06.09
   (setq Shell (vla-getInterfaceObject *acad "Shell.Application")
         Fold  (vlax-invoke-method Shell 'BrowseForFolder
                 (vla-get-HWND *acad) msg flag dir))
   (vlax-release-object Shell)

   (if Fold
     (progn
       (setq Path (vlax-get-property
                    (vlax-get-property Fold 'Self) 'Path))
       (vlax-release-object Fold)

       (and (= "\\" (substr Path (strlen Path)))
            (setq Path (substr Path 1 (1- (strlen Path)))))))

   Path)


 ;;  --=={  Main Function  }==--


 (prompt "\n<< Enter Script Operations, use *file* for the Filename >>")

 (cond (  (zerop (strlen (setq scrline (getstring t "\n>> Script: "))))

          (princ "\n*Cancel*"))

       (  (< (length (setq StrLst (Str-Break scrline "*file*"))) 2)

          (princ "\n** Delimiter *file* not found in Script String **"))

       (  (not (and (setq Path (DirDia "Select Directory of Files on Which to Operate..." nil 512))

                    (not (initget "Yes No"))
                    (setq *def_opt*
                      (cond ((getkword (strcat "\nProcess SubDirectories? <" *def_opt* "> : ")))
                            (*def_opt*)))

                    (setq file (getfiled "Create Script File" (cond (*load*) ("")) "scr" 1))))

          (princ "\n*Cancel*"))

       (t (setq *load* file ofile (open file "w"))

          (foreach filepath (apply (function append)
                                   (vl-remove 'nil
                                     (mapcar
                                       (function
                                         (lambda (Path)
                                           (mapcar
                                             (function
                                               (lambda (File)
                                                 (strcat Path "\\" File)))
                                             (vl-directory-files Path "*.dwg" 1))))
                                       (append (list Path)
                                               (apply (function append)
                                                      (if (= "YES" (strcase *def_opt*))
                                                        (Get_Subs Path)))))))

            (write-line (Str-Make strLst (strcat (chr 34) filepath (chr 34))) ofile))

          (setq ofile (close ofile))
          (princ "\n<< Script Written >>")))

 (princ))

You will need to enter a "Script line" which is a list of the operations that you want to perform on the file, but instead of typing the filename, type *file*.

 

This is a Quick purger you may want to call in the script:

 

[b][color=red]([/color][/b][b][color=blue]defun[/color][/b] Purger [b][color=blue]nil[/color][/b]
 [b][color=red]([/color][/b][b][color=blue]vl-load-com[/color][/b][b][color=red])[/color][/b]
 [b][color=red]([/color][/b][b][color=blue]repeat[/color][/b] [b][color=#009900]3[/color][/b]
   [b][color=red]([/color][/b][b][color=blue]vla-purgeall[/color][/b]
     [b][color=red]([/color][/b][b][color=blue]vla-get-ActiveDocument[/color][/b]
       [b][color=red]([/color][/b][b][color=blue]vlax-get-acad-object[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b]
 [b][color=red]([/color][/b][b][color=blue]princ[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b]



So your script-line may be something like:

 

open *file* (Purger) save close

I hope this helps, please let me know if you have any problems,

 

Lee

Lee thank you so much I want to break this code down to figure out how each line works. I'm gonna go over my book to see if I can comment what each line is suppose to do.

Link to comment
Share on other sites

Hey guys great answers one little problem to be aware of if you have a script file method if you have a directory name with a space in it your script will not work scripts stop at a space expecting it to be next command.

 

Basicly rename your directories to say my_dwgs

or add quote marks around the file name

 

my script builder does now add quotes but I have in the past temporarilly had to rename folders to remove spaces - not always easy when other departments are using files elsewhere in the directory :)

Link to comment
Share on other sites

Todouble22

 

Just amend your script file from a single proceedure to a multiple proceedure within the one bat session. Your batch file runs Autocad and calls a script, that script just simply has multiple lines in it calling each drawings as required.

 

The other alternative is, first step of the script could be to run a lisp program that reads the dir.txt file and in turn then writes a script file. The last line is quite simply (command "script" myscript). This is easy to do and the advantage is it reads the most up to date version of your drawing list. The drawing list being created as 1st line in the batch file.

 

I have used this often where a lisp writes a script ! The architectural package I was involved with read a text file containing all the layer specifications by the client hence we did not know what they would use or change so just read it and wrote it to reset when starting a new drawing.

 

So the code could be run from either inside or outside Autocad. You can issue an external Command from within Autocad to run an external operator system command Dir *.DWG > MYLIST.txt /b

 

I will post the code to do it later

can i call the list of the drawings that I want to open in the lisp? by the method dir *.dwg /b > mylist.txt

Link to comment
Share on other sites

can i call the list of the drawings that I want to open in the lisp? by the method dir *.dwg /b > mylist.txt

 

ToDouble, my code as posted should let you select the list of drawings that you want in the Script :)

Link to comment
Share on other sites

Hi Todd,

 

I thought I'd invest some time in writing a "Script Maker" to make it easier for you lot :)

 

Give this a shot:

 

(defun c:wScript (/ *error* Get_Subs Str-Break Str-Make DirDia

                   FILE FOLD FOLDER OFILE PATH SCRLINE SHELL STRLST)

 ;; Lee Mac  ~  11.01.10
 (vl-load-com)

 (setq *acad (cond (*acad) ((vlax-get-acad-object)))
       *doc  (cond (*doc)  ((vla-get-ActiveDocument *acad))))

 (or *def_opt* (setq *def_opt* "Yes"))


 (defun *error* (msg)
   (and ofile (close ofile))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (defun get_subs (folder / file)
   ;; CAB
   (mapcar
     (function
       (lambda (x) (setq file (strcat folder "\\" x))
                   (cons file (apply (function append) (get_subs file)))))
       (cddr (vl-directory-files folder nil -1))))


 (defun Str-Break (str del / pos lst)
   ;; Lee Mac  ~  27.05.09
 (while (setq pos  (vl-string-search del str))
   (setq lst (cons (substr str 1 pos) lst)
         str (substr str (+ pos 1 (strlen del)))))
 (reverse (cons str lst)))


 (defun Str-Make (lst del / str x)
   ;; Lee Mac  ~  29.12.09
   (setq str  (car lst))
   (foreach x (cdr lst) (setq str (strcat str del x)))
 str)


 (defun DirDia (msg dir flag)
   ;; Lee Mac  ~  07.06.09
   (setq Shell (vla-getInterfaceObject *acad "Shell.Application")
         Fold  (vlax-invoke-method Shell 'BrowseForFolder
                 (vla-get-HWND *acad) msg flag dir))
   (vlax-release-object Shell)

   (if Fold
     (progn
       (setq Path (vlax-get-property
                    (vlax-get-property Fold 'Self) 'Path))
       (vlax-release-object Fold)

       (and (= "\\" (substr Path (strlen Path)))
            (setq Path (substr Path 1 (1- (strlen Path)))))))

   Path)


 ;;  --=={  Main Function  }==--


 (prompt "\n<< Enter Script Operations, use *file* for the Filename >>")

 (cond (  (zerop (strlen (setq scrline (getstring t "\n>> Script: "))))

          (princ "\n*Cancel*"))

       (  (< (length (setq StrLst (Str-Break scrline "*file*"))) 2)

          (princ "\n** Delimiter *file* not found in Script String **"))

       (  (not (and (setq Path (DirDia "Select Directory of Files on Which to Operate..." nil 512))

                    (not (initget "Yes No"))
                    (setq *def_opt*
                      (cond ((getkword (strcat "\nProcess SubDirectories? <" *def_opt* "> : ")))
                            (*def_opt*)))

                    (setq file (getfiled "Create Script File" (cond (*load*) ("")) "scr" 1))))

          (princ "\n*Cancel*"))

       (t (setq *load* file ofile (open file "w"))

          (foreach filepath (apply (function append)
                                   (vl-remove 'nil
                                     (mapcar
                                       (function
                                         (lambda (Path)
                                           (mapcar
                                             (function
                                               (lambda (File)
                                                 (strcat Path "\\" File)))
                                             (vl-directory-files Path "*.dwg" 1))))
                                       (append (list Path)
                                               (apply (function append)
                                                      (if (= "YES" (strcase *def_opt*))
                                                        (Get_Subs Path)))))))

            (write-line (Str-Make strLst (strcat (chr 34) filepath (chr 34))) ofile))

          (setq ofile (close ofile))
          (princ "\n<< Script Written >>")))

 (princ))

You will need to enter a "Script line" which is a list of the operations that you want to perform on the file, but instead of typing the filename, type *file*.

 

This is a Quick purger you may want to call in the script:

 

[b][color=red]([/color][/b][b][color=blue]defun[/color][/b] Purger [b][color=blue]nil[/color][/b]
 [b][color=red]([/color][/b][b][color=blue]vl-load-com[/color][/b][b][color=red])[/color][/b]
 [b][color=red]([/color][/b][b][color=blue]repeat[/color][/b] [b][color=#009900]3[/color][/b]
   [b][color=red]([/color][/b][b][color=blue]vla-purgeall[/color][/b]
     [b][color=red]([/color][/b][b][color=blue]vla-get-ActiveDocument[/color][/b]
       [b][color=red]([/color][/b][b][color=blue]vlax-get-acad-object[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b]
 [b][color=red]([/color][/b][b][color=blue]princ[/color][/b][b][color=red])[/color][/b][b][color=red])[/color][/b]



So your script-line may be something like:

 

open *file* (Purger) save close

I hope this helps, please let me know if you have any problems,

 

Lee

LeeMac, I tried this script writer and went through the steps and I am a little confused where I am going wrong in that where I enter the operations that i want to perform it doesnt allow me to enter the autocad commands. do I have to do it a certain way? and what is the part to process sub-directories. When I tried it by entering the operations i wanted it went through the process and saved the file but there was nothing in it for commands?

Link to comment
Share on other sites

LeeMac, I tried this script writer and went through the steps and I am a little confused where I am going wrong in that where I enter the operations that i want to perform it doesnt allow me to enter the autocad commands. do I have to do it a certain way?

 

Could you post what you typed as the Script line?

 

and what is the part to process sub-directories. When I tried it by entering the operations i wanted it went through the process and saved the file but there was nothing in it for commands?

 

The Sub-Directories means that it will include any DWG files that appear in sub-folders within the selected folder.

Link to comment
Share on other sites

Could you post what you typed as the Script line?

 

 

 

The Sub-Directories means that it will include any DWG files that appear in sub-folders within the selected folder.

Ok here goes.. I copied the code you wrote:

(defun Purger nil

(vl-load-com)

(repeat 3

(vla-purgeall

(vla-get-ActiveDocument

(vlax-get-acad-object))))

(princ))

I also tried putting commands in like i would for the script "-pu" "a" "*" "N" . sorry i am completely lost with this.

Link to comment
Share on other sites

Copy the code I wrote into a Notepad doc, and save as a LISP file, and put it in your search path.

 

Then, using my script writer, type this (or something similar) at the prompt for the script operations:

 

_open *file* (load "purger.lsp") (purger) _save _close

Just check whether the script is written correctly. :)

Link to comment
Share on other sites

Copy the code I wrote into a Notepad doc, and save as a LISP file, and put it in your search path.

 

Then, using my script writer, type this (or something similar) at the prompt for the script operations:

 

_open *file* (load "purger.lsp") (purger) _save _close

Just check whether the script is written correctly. :)

LeeMac first off thank you so much for working with me as I struggle through this.. Here is what I have done in an attempt to not be too long-winded and convoluted. I first copied all of the wscript program that you created to write a script and pasted it on the command line, then i called the command wscript. i followed the prompts as you stated above but I directed it to where my purger lisp is located and I all i have for the lisp is as follows:

(defun c:purger()

(command "zoom" "e")

(command "purge" "a" "*" "n")

(princ))

 

I then continue with what you have. it creates the script as to whatever I want to call it and this is what I got.

_open "C:\TEST\DWG\Drawing1.dwg" (load "C:\\TEST\\purger.lsp") purger _save _close

_open "C:\TEST\DWG\Drawing2.dwg" (load "C:\\TEST\\purger.lsp") purger _save _close

_open "C:\TEST\DWG\Drawing3.dwg" (load "C:\\TEST\\purger.lsp") purger _save _close

 

I then ran the script from autocad and it opened the first drawing but wouldnt load the lisp.

ughhh i think my coworkers are concerned from the pounding of my head off my desk that they are hearing . i need to take a step away for a sec..

Link to comment
Share on other sites

Todd,

 

Don't stress, you are almost there, you are just a little confused :)

 

Firstly, you may find it easier to load LISP code using the Visual LISP Editor provided with AutoCAD (just type VLIDE at the command line, and go to File > New File).

 

Now, with your code:

 

To load the LISP you will need to use something like:

 

(load "purger.lsp")

 

Or if the LISP is not in the search path:

 

(load "C:\\test\\purger.lsp")

 

Now, to invoke the LISP, there are two things you need to look at:

 

LISP functions do not have to start:

 

(defun [color=Red][b]c:[/b][/color]...

 

They can just start:

 

(defun ...

 

The c: just means that the command can be invoked from the command line.

 

But when calling the LISP from a Script or from another LISP, you cannot just call it as you would from the command line, as the command will not be recognised as an in-built ACAD command, just as Express Tools commands are not recognised in this way.

 

So, to call the LISP from a script/lisp, you need to use either:

 

(c:purger)

 

Or

 

(purger)

 

(as in my first example).

 

Hope this helps, please let me know if there is anything that you are unsure of.

 

Lee

Link to comment
Share on other sites

So, to use my original Purge lisp:

 

_open *file* (load "C:\\test\\purger.lsp") (purger) _save _close

Or, to use your Purge LISP as you have posted:

 

_open *file* (load "C:\\test\\purger.lsp") (c:purger) _save _close

Lee

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...