410 likes | 1.09k Views
AutoCAD: Secrets Every User Should Know. Chapter 7 - AutoCAD Scripts. Scripts. Introduction. ASCII Text File Commands You Can Type at Command Line Not Aliases Commands and Options Lisp Code AutoLISP Functions and Commands Other Scripts Open Dialog Boxes: SAVE~
E N D
AutoCAD: Secrets Every User Should Know Chapter 7 - AutoCAD Scripts
Introduction • ASCII Text File • Commands You Can Type at Command Line • Not Aliases • Commands and Options • Lisp Code • AutoLISP Functions and Commands • Other Scripts • Open Dialog Boxes: SAVE~ • Limitations: No User Input
Writing Scripts • Special Script Functions: • Backspace key Pauses a script • RESUME Resumes a paused script • ; Remark within a script • DELAY Delays the next step a specified time • RSCRIPT Repeats the script when done • Space in line Enter key • New line Enter key
Writing Scripts • File Format • All On One Line • Separate Line for Each Command String • Save with SCR Extension • Same as Windows screen saver • Prior to AutoCAD 2006 change association
Uses for Scripts • Create Entities • Test Computer Speed • Set Up Layers, Text Styles, Dimension Styles, etc. • Change Variable Settings • Saved in drawing • Saved in Registry • Extract Block Attributes • Make Slides and Run Slide Show • Edit Unlimited Number of Drawings
Simple Script • Layer • New • fl1,fl1-dim,fl1-txt • Color • 2 • fl1 • Color • 3 • fl1-dim • Color • 4 • fl1-txt
Simple Script - Alternate Format • Layer • New fl1,fl1-dim,fl1-txt • C 2 fl1 C 3 fl1-dim C 4 fl1-txt • ...or all on one line
Setup Script • LAYER New obj,hid,cen,txt,dim Color 1 hid Color 3 cen • Color 4 txt Color 5 dim L hidden hid L center cen S obj • STYLE romans romans 0 1 0 N N N • APERTURE 5 ATTDIA 1 AUNITS 0 AUPREC 1 BLIPMODE 0 • CECOLOR bylayer CELTSCALE 1 CELTYPE bylayer • CMDDIA 1 CMDECHO 1 CURSORSIZE 5 DRAGMODE A ELEVATION 0 EXPERT 0 FACETRES 1 FILEDIA 1 FILLETRAD 0 GRIPCOLOR 5 GRIPHOT 1 GRIPS 1 GRIPSIZE 3 HIGHLIGHT 1 LTSCALE 1 MBUTTONPAN 1 MIRRTEXT 0 OSMODE 4133 PELLIPSE 0 PICKADD 1 • PICKAUTO 1 PICKBOX 3 PICKFIRST 1 PICKSTYLE 1 PLINEGEN 1 PSLTSCALE 1 SAVETIME 15 SDI 0 SORTENTS 23 THICKNESS 0 UCSICON off UCS w UCSVP 1
Bench-Testing Computers • TIME R • BOX 0,0 10,10,10 SPHERE 5,5,5 5 SUBTRACT NON 0,0 L • VPOINT 1,-1,1 SLICE L 5,0 5,5 5,5,5 -1,0 • 3DARRAY L R 4 4 4 10 10 10 • ZOOM ALL HIDE • VPORTS 4 CVPORT 5 UCS X 90 PLAN C • CVPORT 4 UCS W PLAN C • CVPORT 2 UCS X 90 UCS Y 90 PLAN C • TILEMODE 0 ERASE ALL MVIEW R F • MSPACE CVPORT 3 SOLPROF ALL Y Y Y • CVPORT 4 SOLPROF ALL Y Y Y • CVPORT 5 SOLPROF ALL Y Y Y • CVPORT 6 SOLPROF ALL Y Y Y • TIME
Automatically Updating Drawing Script Files Batch Files Lisp Files Batch-Editing Drawings
Introduction • Automatically Updating Multiple Drawings • Possibilities: • Reducing file size of all drawings in a directory • Replacing existing title blocks with a new one • Creating drawing files from existing block definitions • Adding attributes to existing block definitions • Redefining existing block definitions • Extracting attribute information into a text file • Repathing all XRefs and images to relative • Almost anything else…if you know a little AutoLISP
Tools needed for this system • A Script File • Can be run on startup of AutoCAD • Can makes changes directly or load Lisp program • Can exit AutoCAD to restart cycle • A Batch File – DOS Isn’t Dead Yet for Me • Can create a new folder for replacement drawings • Can use the FOR function to process multiple files • Can use the /r switch to include subdirectories • A Lisp program • Can do nearly anything
Two Cautions! • Try Out Programming in Small Pieces on a Test File • Does the script do what you want it to? • Does the Lisp do what you want it to? • Does the batch file work with a small group of files? • Protect Your Files! • Back up your original files • Make NEW files with this process • Don’t delete the originals until you’re done • Place new drawings in their own directory
Background - Scripts • Create with Notepad or WordPad • Save with an .SCR Extension, NOT .txt • You may have to quote filename “myscript.scr” • A Script Can Run on Startup Using the Switch /b • Scripts Contain Command-Line Input • Try out commands at the command line • Use a dash with commands, like -LAYER, -INSERT • Turn FILEDIA off to try options of SAVEAS • AutoCAD Can Open a Drawing on Startup • …AutoCAD 2007\acad.exe "path\drawing name"
Background – Batch Files • Create with Notepad or WordPad • Save with a .BAT extension, NOT .txt • OPEN Batch File to Run It • Two DOS Commands Are Key: • "FOR" function and "%%f" parameter allow processing of multiple files (DWG in this example) • "START /WAIT" starts a windows application and returns to the batch file when application closes
Background – AutoLISP Files • Create With VLISP, Notepad, or WordPad • Save with an .LSP Extension, NOT .txt • Lisp Code Can Be Included Directly in Scripts • Use parentheses • Type as you would at the command line • Learn as Much AutoLISP as You Can • Good operator’s programming language • It is NOT going away – too much existing code • It’s fun – really (Friday Morning: Not so Rapid…)
AutoLISP Files Important Note! • These AutoLISP Files Have Been Simplified for This Presentation. They Do Not: • Include Annotation • Include Error Handling • Define Program Variables as Local • Account for Anonymous Blocks
Example One Reducing Archive File Sizes
Reducing Archive File Sizes • Step 1 – script file • Open Notepad (right-clickNewText Document) • Type the following: (load "c:\\Sybex\\wbout.lsp") ZOOM All WBOUT QUIT Y Save the file as "C:\Sybex\wbout.scr"
Reducing Archive File Sizes • Step 2 – batch file • Open Notepad (NOT a word processor) • Type the following in TWO lines: md C:\Sybex\dwg\wb FOR %%f in (C:\Sybex\dwg\*.dwg) do start /wait C:\"program files"\"AutoCAD 2007"\ acad.exe "%%f" /b C:\Sybex\wbout.scr Save the file as "C:\Sybex\wbout.bat"
Reducing Archive File Sizes • Step 3 – Lisp file • Open Notepad or VLISP (NOT a word processor) • Type the following: (defun c:wbout() (setq dn (getvar"dwgname")) (setq pa (getvar"dwgprefix")) (setq pawbdn (strcat pa "wb\\" dn)) (command"wblock" pawbdn "*") ) Save the file as "C:\Sybex\wbout.lsp"
Reducing Archive File Sizes page 5 • Step 4 -- RUN the batch file • Go to My Computer or Explorer • Open folder C:\Sybex • Double-click wbout.bat
Caution with Batch Files! • Double-clicking a Batch File… • RUNS the file, doesn’t open it for editing! • To change the text in the file • Select Edit after right-clicking
Processing Files in Subdirectories The FOR Function in DOS Can Be Modified to Include Subdirectories The Text Changes as Follows: FOR /R C:\Sybex\dwg\%%f in (*.dwg) do…
Example Two Updating a Title Block
Updating a Title Block • Step 1 – script file: INSERT border=C:\Sybex\dwg\new-border 0,0 1 1 0 ERASE L (load "c:\\Sybex\\TbUpdate.lsp") TbUpdate QUIT Y • Save the file as "C:\Sybex\ TbUpdate.scr"
Updating a Title Block • Step 2 – batch file in TWO lines: md C:\Sybex\dwg\NewBorder FOR %%f in (C:\Sybex\dwg\D5*.dwg) do START /WAIT C:\"program files"\"AutoCAD 2007"\acad.exe "%%f" /b C:\Sybex\TbUpdate.scr Save the file as "C:\Sybex\TbUpdate.bat"
Updating a Title Block • Step 3 – Lisp file: (defun c:TbUpdate() (setq dn (getvar"dwgname")) (setq pa (getvar"dwgprefix")) (setq panbdn (strcat pa "NewBorder\\" dn)) (command"SAVE" panbdn ) ) Save the file as "C:\Sybex\ TbUpdate.lsp"
Updating a Title Block • Step 4 – Run the program • Go to My Computer • Open folder C:\Sybex • Double-click TbUpdate.bat
Example Three Creating Drawings from Block Definitions
Creating Drawings from Blocks • Step 1 – script file: (load "c:\\Sybex\\blockout.lsp") blockout QUIT Y • Save the file as "C:\Sybex\ blockout.scr"
Creating Drawings from Blocks • Step 2 – batch file in TWO Lines: md C:\Sybex\dwg\NewBlocks for %%f in (C:\Sybex\dwg\sym*.dwg) do START /WAIT c:\"program files"\"AutoCAD 2007"\acad.exe "%%f" /b C:\Sybex\blockout.scr • Save the file as "C:\Sybex\blockout.bat"
Creating Drawings from Blocks Step 3 – Lisp file: (defun c:blockout () (setq dn (getvar"dwgname")) (setq pa (getvar"dwgprefix")) (setq s1 (strcat pa "NewBlocks\\" dn)) (setq blkdata (tblnext"BLOCK"T)) (while blkdata (setq blname (cdr(assoc 2 blkdata))) (setq fullname (strcat s1 blname)) (command"WBLOCK" fullname blname) (setq blkdata (tblnext"BLOCK")) ) ) Save the file as "C:\Sybex\ blockout.lsp"
Dim/Hatch Blocks Break Program! An * Precedes Anonymous Block Names – Can’t Use * in Filenames Dynamic Blocks Saved to Pre-2006 Format Also Have This Problem
Creating Drawings from Blocks • Step 4 – Run the program • Go to My Computer • Open folder C:\Sybex • Double-click blockout.bat
Future Considerations • These Programs Were Simplified as Much as Possible • Add Documentation to Each File • ; before documentation lines in script files • REM before documentation lines in batch files • ; before documentation lines in Lisp files • Use VLISP for Creating Lisp Programs • Add Error-Trapping to All Lisp Programs
Error-Trapping Example (defun da_error (msg) (command) (setvar"aperture" ap)(setvar"osmode" os) (setq *error* old_error) (alert"Returning drawing to original status.") ) (defun C:MID (/ p1 p2 old_error) (setq old_error *error*) (setq *error* da_error) …etc.