Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Open, Purge All, Zoom Extents, then Save, Lisp routine.

61 REPLIES 61
Reply
Message 1 of 62
Ak_homegrown
15753 Views, 61 Replies

Open, Purge All, Zoom Extents, then Save, Lisp routine.

I have thousands of archive drawings that I would like to run a lisp routine on that would Open, Purge All, Zoom Extents, Save, and Close the file, moving onto the next one. I am not very lisp savvy, so I was hoping someone out there could provide me with a lisp that does just that.

(I know there are lots of people out there with lisps for this very purpose, so hopefully one of you can help me out!)

Thanks in advance!
61 REPLIES 61
Message 21 of 62
m_gabra
in reply to: scottbolton

Hi,

 

I tried your script on AutoCAD 2010.
On the C drive file was created bath.scr I loaded the script.
It opens only one first drawing, removes everything, and it stops. Do not zoom, save, close and open the next drawing.
Does anyone have the same problem?

 

Regards
Marcin

Message 22 of 62
sagar.adhau
in reply to: scottbolton

Hi scottbolton,

With reference to your post i have further query.It would be great help if you help me on following,

I have many drawings in one folder and each drawings has multiple layouts.I wanted to run lisp for open, zoom extent, purge all and save all and close all command to this folder.(before closing each drawing it should switch to first layout and then go for save and close command ).

 

I have tried with your above script and run batch file but there it is not working.

 

 

 

Message 23 of 62
roland.r71
in reply to: sagar.adhau


@sagar.adhau wrote:

Hi scottbolton,

With reference to your post i have further query.It would be great help if you help me on following,

I have many drawings in one folder and each drawings has multiple layouts.I wanted to run lisp for open, zoom extent, purge all and save all and close all command to this folder.(before closing each drawing it should switch to first layout and then go for save and close command ).

 

I have tried with your above script and run batch file but there it is not working.

 

 

 


Be aware that an AutoCAD Batchfile (script | *.scr) requires SDI mode (Single Document Interface) to run. You can not use the 'close' command.

Using "fileopen" will close the previous drawing. (& ask to save if it has pending changes) - you can only use qsave or save. (save all has no meaning with just 1 drawing open ...)

 

Any script or lisp invoking SDI mode can only do so if you don't have multiple drawings open at execution. (closing drawings beyond the one the lisp runs in, is not possible)

 

The next code is an adaptation of some code i posted earlier for a similar request and will do what you ask for.

 

After selecting the directory it will create a script to run a purge all, zoom extent, activate the first layout (if present) & qsave on all DWG drawings there (NOT including subdirectories).

 

Note: As it uses 2 ACET functions, you need express tools for it to work.

 

(vl-load-com)
(defun c:scrDir ( / dir_path scr_path path files_list abort msg confirm)

; --- INIT
   (setq dir_path "C:\\My Documents")              ; starting path for file selection
   (setq scr_path "c:\\temp")                      ; path for writing script (scrDir.scr)

; --- Functions
   (defun get_files ( dr / dt tl fp i l filelist)
      (princ "\nCollecting filenames: ")
      (setq filelist nil)
      (setq tl (vl-directory-files dr "*.dwg"))                      
      (setq i 0)
      (while (< i (length tl))
         (setq fp (strcat (parse_path dr) "/" (nth i tl)))
         (setq filelist (append filelist (list fp)))
         (setq i (1+ i))
      )
      (princ (itoa (length filelist)))
      filelist
   )

   (defun parse_path (path / temp i)
      (setq i 1 temp "")
      (repeat (strlen path)
         (if (= (substr path i 1) "\\")
            (setq temp (strcat temp "/"))
            (setq temp (strcat temp (substr path i 1)))
         )
         (setq i (1+ i))
      )
      temp
   )

   (defun scrGen ( scrdir f_list / file_w i f_name iSDI iFILEDIA)
      (setq file_w   (open (strcat scrdir "/scrDir.scr") "w")
            iSDI     (getvar "SDI")
            iFILEDIA (getvar "FILEDIA")
            i 0
      )
      (if (= iSDI 0)(write-line "(setvar \"SDI\" 1)" file_w))
      (if (= iFILEDIA 1)(write-line "(setvar \"FILEDIA\" 0)" file_w))
      (while (< i (length f_list))
         (setq f_name (nth i f_list))
         (write-line (strcat "(command \"_.fileopen\" \"" f_name "\")") file_w)
         (write-line "(command \"-purge\" \"a\" \"\" \"n\")" file_w)
         (write-line "(command \"_.zoom\" \"e\")" file_w)
         (write-line "(if (not (= (nth 0 (layoutlist)) nil))(setvar 'ctab (nth 0 (layoutlist))))" file_w)
         (write-line "(command \"_.qsave\")" file_w)
         (setq i (1+ i))
      )
      (if (= iSDI 0)(write-line "(setvar \"SDI\" 0)" file_w))
      (if (= iFILEDIA 1)(write-line "(setvar \"FILEDIA\" 1)" file_w))
      (close file_w)
   )

; *** Main ***   

   (setq path (acet-ui-pickdir "Select directory:" dir_path))                        
   (if path 
      (setq files_list (get_files path))
      (princ "\n*Cancel*")
   )

   (setq abort T)
   (if files_list
      (progn
         (setq msg (strcat "Ok to start processing " (itoa (length files_list)) " file(s) ?"))
         (setq confirm (acet-ui-message msg "Confirmation" 36))
         (if (= confirm 6)
            (progn
               (scrGen scr_path files_list)
               (setq abort nil)
            )
            (princ "\n*Cancel*")
         )
      )
   )

   (if (= abort nil)
      (command "_.script" (strcat scr_path "/scrDir"))
   )
   (princ)
)

For anything more sophisticated with a lot more options & features, you'll need some patience, as i'm currently still rewriting my 'script generator'.

Message 24 of 62
gmoralesBPE
in reply to: scottbolton

i tried your Lisp 

(defun C:RV ( / COUNT DIR FILENAME FILES SCRIPTNAME)
(if (setq dir (getfiled "Select the drawing directory:" "" "dwg" 0))
(setq dir (vl-filename-directory dir))
)
(if dir
(setq files (vl-directory-files dir "*.dwg" 1))
)
(if files
(progn
(setq scriptname (open "C:\\batch.scr" "w")
count 0
)
(while (setq filename (nth count files))
(setq filename (strcat dir "\\" filename))
(write-line (strcat "_open \"" filename "\"") scriptname)
(write-line "_purge all * no" scriptname)
(write-line "_purge all * no" scriptname)
(write-line "_purge all * no" scriptname)
(write-line "_zoom extents" scriptname)
(write-line "_qsave" scriptname)
(write-line "_close" scriptname)
(setq count (1+ count))
)
(close scriptname)
)
)
(princ)
)

 

and its what i need but it doesnt work for me any suggestions? if you could follow up with an email that would be great gmorales@buholtz.com

Message 25 of 62
crashcup99
in reply to: gmoralesBPE

What part of the script fails for you? You can press F2 and look at the commands for a clue to see where it didn't complete.
Message 26 of 62
stevecouch
in reply to: Ak_homegrown

Try this as it works for me both in LT and Full Autocad.

 

^C^C-layer s 0  qsave xref b * -purge all * n saveas

Message 27 of 62
m_gabra
in reply to: stevecouch

Hi, can I have all the code pasted please. I'm not good at programming and I don't know where to paste this part of the code.

Message 28 of 62
stevecouch
in reply to: m_gabra

^C^C-layer s 0 qsave xref b * -purge all * n saveas
Message 29 of 62
stevecouch
in reply to: m_gabra

^C^C-layer s 0  qsave xref b * -purge all * n saveas

Message 30 of 62
stevecouch
in reply to: m_gabra

create a new button with the desired icon, I use PBS for purge bind and save

then under macro setting paste the routine

Message 31 of 62

Good afternoon all.

I have added a line to to this routine, but it is not adding it to the batch.scr.
The line I have added is the audit line:

(write-line (strcat "_open \"" filename "\"") scriptname)
(write-line "_audit y" scriptname)
(write-line "_purge all * no" scriptname)

I have in the past added some lines that did not work also, but cannot recall what they were, but why is this not placing the command in the batch.scr?

Message 32 of 62

Nevermind about the above.
I restarted autocad and it is now working.
I am assuming since the routine was already loaded, it wasn't seeing the changes made.

Message 33 of 62

That's good, always worked for me and a brilliant little routine, especially
when sending drawings out to third parties and you need to strip out quick
Message 34 of 62
m_gabra
in reply to: stevecouch

Hi,

should I paste this code

 

^C^C-layer s 0  qsave xref b * -purge all * n saveas

 

in AutoCAD into the command window?

Message 35 of 62
stevecouch
in reply to: m_gabra

Yes you should
Message 36 of 62

Ran the lisp and it seems to show in the command line but does not create the scr. I am getting a (; error: bad argument type: streamp nil) at the end in the command line. Thoughts? This is exactly what I'm looking for. I was wondering if you could add to the lisp to run the scr what done creating the scr? 

Message 37 of 62
crashcup99
in reply to: williamgodette

What version of Windows are you using?

 

Message 38 of 62


@williamgodette wrote:

.... I am getting a (; error: bad argument type: streamp nil) at the end in the command line. Thoughts? .... 


Maybe >this< will give you some clues as to where that could be coming from.  You can Search for more such topics.

Kent Cooper, AIA
Message 39 of 62
williamgodette
in reply to: crashcup99

Windows 11. I changed the location of the scr dir and it worked. Now I just need to add the run scr at the end of the lisp.

Message 40 of 62
roland.r71
in reply to: Ak_homegrown

My version should have done all that, already 😋

You could also use my Multifile tool (MFT) https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/running-a-lisp-on-multiple-drawings/...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost