1 autocad: secrets every user should know chapter 7 - autocad scripts

38
1 AutoCAD: Secrets Every User Should Know Chapter 7 - AutoCAD Scripts

Upload: brendan-allison

Post on 30-Dec-2015

235 views

Category:

Documents


1 download

TRANSCRIPT

1

AutoCAD: Secrets Every User Should Know

Chapter 7 - AutoCAD Scripts

2

Scripts

3© 2006 Autodesk

IntroductionASCII Text File

Commands You Can Type at Command Line

Not AliasesCommands and OptionsLisp CodeAutoLISP Functions and CommandsOther ScriptsOpen Dialog Boxes: SAVE~

Limitations: No User Input

4© 2006 Autodesk

Writing ScriptsSpecial 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

5© 2006 Autodesk

Writing ScriptsFile 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

6© 2006 Autodesk

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

7© 2006 Autodesk

Simple ScriptLayerNewfl1,fl1-dim,fl1-txtColor2fl1Color3fl1-dimColor4fl1-txt

8© 2006 Autodesk

Simple Script - Alternate FormatLayer New fl1,fl1-dim,fl1-txt C 2 fl1 C 3 fl1-dim C 4 fl1-txt

...or all on one line

9© 2006 Autodesk

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

10© 2006 Autodesk

Bench-Testing ComputersTIME RBOX 0,0 10,10,10 SPHERE 5,5,5 5 SUBTRACT NON 0,0 LVPOINT 1,-1,1 SLICE L 5,0 5,5 5,5,5 -1,03DARRAY L R 4 4 4 10 10 10ZOOM ALL HIDEVPORTS 4 CVPORT 5 UCS X 90 PLAN CCVPORT 4 UCS W PLAN CCVPORT 2 UCS X 90 UCS Y 90 PLAN CTILEMODE 0 ERASE ALL MVIEW R FMSPACE CVPORT 3 SOLPROF ALL Y Y YCVPORT 4 SOLPROF ALL Y Y YCVPORT 5 SOLPROF ALL Y Y YCVPORT 6 SOLPROF ALL Y Y YTIME

11

Automatically Updating Drawing

Script Files Batch Files Lisp Files Batch-Editing Drawings

12© 2006 Autodesk

IntroductionAutomatically 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

13© 2006 Autodesk

Tools needed for this systemA 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

14© 2006 Autodesk

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

15© 2006 Autodesk

Background - ScriptsCreate with Notepad or WordPad

Save with an .SCR Extension, NOT .txtYou 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"

16© 2006 Autodesk

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

17© 2006 Autodesk

Background – AutoLISP FilesCreate With VLISP, Notepad, or WordPad

Save with an .LSP Extension, NOT .txt

Lisp Code Can Be Included Directly in ScriptsUse parenthesesType as you would at the command line

Learn as Much AutoLISP as You CanGood operator’s programming languageIt is NOT going away – too much existing

codeIt’s fun – really (Friday Morning: Not so

Rapid…)

18© 2006 Autodesk

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

19

Example One

Reducing Archive File Sizes

20© 2006 Autodesk

Reducing Archive File SizesStep 1 – script file

Open Notepad (right-clickNewText Document)Type the following:

(load "c:\\Sybex\\wbout.lsp")ZOOM AllWBOUTQUIT

Y

Save the file as "C:\Sybex\wbout.scr"

21© 2006 Autodesk

Reducing Archive File SizesStep 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"

22© 2006 Autodesk

Reducing Archive File SizesStep 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"

23© 2006 Autodesk

Reducing Archive File Sizes page 5Step 4 -- RUN the batch file

Go to My Computer or ExplorerOpen folder C:\SybexDouble-click wbout.bat

24© 2006 Autodesk

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

25© 2006 Autodesk

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…

26

Example Two

Updating a Title Block

27© 2006 Autodesk

Updating a Title BlockStep 1 – script file:

INSERT border=C:\Sybex\dwg\new-border0,0 1 1 0ERASE L (load "c:\\Sybex\\TbUpdate.lsp")TbUpdateQUITY

•Save the file as "C:\Sybex\ TbUpdate.scr"

28© 2006 Autodesk

Updating a Title BlockStep 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"

29© 2006 Autodesk

Updating a Title BlockStep 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"

30© 2006 Autodesk

Updating a Title BlockStep 4 – Run the program

Go to My Computer

Open folder C:\Sybex

Double-click TbUpdate.bat

31

Example Three

Creating Drawings from Block Definitions

32© 2006 Autodesk

Creating Drawings from BlocksStep 1 – script file:

(load "c:\\Sybex\\blockout.lsp")blockoutQUITY

•Save the file as "C:\Sybex\ blockout.scr"

33© 2006 Autodesk

Creating Drawings from BlocksStep 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"

34© 2006 Autodesk

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"

35© 2006 Autodesk

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

36© 2006 Autodesk

Creating Drawings from BlocksStep 4 – Run the program

Go to My Computer

Open folder C:\Sybex

Double-click blockout.bat

37© 2006 Autodesk

Future ConsiderationsThese 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

38© 2006 Autodesk

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.