Você está na página 1de 38

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~ Limitations: No User Input

2006 Autodesk

Writing Scripts
Special Script Functions:
 Backspace key  RESUME ;  DELAY  RSCRIPT  Space in line  New line

Pauses a script Resumes a paused script Remark within a script Delays the next step a specified time Repeats the script when done Enter key Enter key

2006 Autodesk

Writing Scripts
File Format
 All

On One Line Line for Each Command String

 Separate  Save
 

with SCR Extension


Same as Windows screen saver Prior to AutoCAD 2006 change association

2006 Autodesk

Uses for Scripts


Create Entities Test Computer Speed Set Up Layers, Text Styles, Dimension Styles, etc. Change Variable Settings
 Saved  Saved

in drawing in Registry

Extract Block Attributes Make Slides and Run Slide Show Edit Unlimited Number of Drawings

2006 Autodesk

Simple Script
Layer New fl1,fl1-dim,fl1-txt Color 2 fl1 Color 3 fl1-dim Color 4 fl1-txt

2006 Autodesk

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

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

2006 Autodesk

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
2006 Autodesk 10

Automatically Updating Drawing Script Files Batch Files Lisp Files Batch-Editing Drawings

11

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 elseif you know a little AutoLISP

2006 Autodesk

12

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 Isnt 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

2006 Autodesk

13

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 Dont delete the originals until youre done Place new drawings in their own directory

2006 Autodesk

14

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"
2006 Autodesk 15

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

2006 Autodesk

16

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 )
2006 Autodesk 17

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

2006 Autodesk

18

Example One
Reducing Archive File Sizes

19

Reducing Archive File Sizes


Step 1 script file Open Notepad (right-click New Text Document) Type the following: (load "c:\\Sybex\\wbout.lsp") ZOOM All WBOUT QUIT Y Save the file as "C:\Sybex\wbout.scr"

2006 Autodesk

20

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"

2006 Autodesk

21

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"

2006 Autodesk

22

Reducing Archive File Sizes


Step 4 -- RUN the batch file Go to My Computer or Explorer Open folder C:\Sybex Double-click wbout.bat

page 5

2006 Autodesk

23

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

2006 Autodesk

24

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

2006 Autodesk

25

Example Two
Updating a Title Block

26

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"
2006 Autodesk 27

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"

2006 Autodesk

28

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"

2006 Autodesk

29

Updating a Title Block


Step 4 Run the program Go to My Computer Open folder C:\Sybex Double-click TbUpdate.bat

2006 Autodesk

30

Example Three
Creating Drawings from Block Definitions

31

Creating Drawings from Blocks


Step 1 script file:

(load "c:\\Sybex\\blockout.lsp") blockout QUIT Y

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

2006 Autodesk

32

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"

2006 Autodesk

33

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"


2006 Autodesk 34

Dim/Hatch Blocks Break Program!


An * Precedes Anonymous Block Names Cant Use * in Filenames

Dynamic Blocks Saved to Pre-2006 Format Also Have This Problem


2006 Autodesk 35

Creating Drawings from Blocks


Step 4 Run the program Go to My Computer Open folder C:\Sybex Double-click blockout.bat

2006 Autodesk

36

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
2006 Autodesk 37

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.
2006 Autodesk 38

Você também pode gostar