AutoCAD

Play It again and again and again, Sam

1 Dec, 1998 By: Bill Fane


It was a warm and mild evening. Captain LearnCurve finished polishing the final coat of wax on his 1937 Rolls-Royce Phantom III limousine. He flicked one last speck of dust off the boot (the trunk to you colonials) and ruminated on the wonders of older technology when the cry of an anguished e-mail came wafting over the breeze:

Help me! Help me! I have more than 1,000 drawings, each of which contains a standard set of notes held in a block definition called NOTES. Note number 6 is misleading and is confusing our vendors. I can’t update all 1,000 drawings! Oh, woe is me! Whatever shall I do!

By an amazing coincidence, the answer to this plaintive cry lies in older technology. All it needs is a dash of DOS, a touch of text editor, a ladle of AutoLISP, and a serving of scripts. In keeping with my usual tradition, I will start with the last item.

Scripts are simple
Scripts have been around almost since Day One in AutoCAD, but few people realize their simplicity and power, or even know that they exist.

Instead of typing in AutoCAD commands one at a time at the Command prompt, scripts let you do all the typing in advance and have AutoCAD play them back later. A simple text editor creates a separate file of the desired commands and all the responses to any prompts that come up.
Let’s try a very simple example, then move on to fancier stuff.

Start AutoCAD and begin a new drawing. While you are at it, start NotePad. Type in a single line that looks exactly like the line below. Start at the left edge of the screen (no tabs or spaces) and be sure to put a single space between each pair of numbers and before the C.
LINE 1,1 2,1 2,2 1,2 C
Press <Enter> at the end of the line to drop down to the next line.
Before you save the file using the file name BOX.SCR, note this possible gnarly bit: AutoCAD insists that all script files use an SCR extension.

NotePad saves all files in text format but adds a TXT extension to any filename you supply. To prevent this, you can either rename it later or save as All files. Or put the desired filename in quotes when you type it in the File Name box.
Now go back to AutoCAD. Type in Script at the Command prompt or pick Tools|Run Script from the menu bar. AutoCAD pops up a file dialog box. Type in the name or browse to find your BOX.SCR file.

Click Open and watch AutoCAD automatically run through the Line command to draw a one-unit square that starts at 1,1 and closes back to the start, exactly as you typed the script file.

It's automatic
Wasn’t that simple? Now let’s automate the processing of many drawings. Scripts serve us nicely here because they have one very significant property.
AutoLISP reloads itself and starts from scratch every time you open or start another drawing. This means that you cannot write an AutoLISP routine that opens another drawing and then works within it when it opens. The running program dies when the next drawing opens.

On the other hand, scripts keep running from one drawing to the next. All you need is a script that includes the Open command and a filename, line after line, with a different filename in each line.

Oh, great! Now I have to type in a 1,000-line script file!

Of course not. That’s why I mentioned the dash of DOS.

DOS is never dead
For all the power and the glory of Windows, there are still a few tasks that only DOS can perform.

From your desktop, click on the appropriate icon or select MS-DOS Prompt from the Windows’ Start menu. Enter the following DOS command at the Command prompt:
DIR *.DWG /S /B /-P
Like magic, a listing of all DWG files scrolls down your screen. It includes all subdirectories (/S) and is a brief (/B) listing that does not include file size and date information. It does not stop to display one page (/P) at a time. You can always dig up an old DOS user to find out how to specify specific directories or groups of files.

Now the cunning bit. Repeat the last DOS command, but add a bit to the end so it looks like this:
DIR *.DWG /S /B /-P > TEST.SCR

You may think that nothing has happened. No directory listing appears on screen. A little exploring reveals a file called test.scr on your hard drive. Use WordPad (not NotePad) to open it. You’ll see a complete directory listing of all your drawings, complete with drive letter and full directory path, one per line.
If you think that was magic, just wait until the next step.

This is magic
In WordPad, click Edit|Replace, then click the Find What box. Type in C:\
Now click the Replace With box, type in OPEN C:\, and click Replace All. WordPad sweeps down through the entire file listing and makes the change. You must use WordPad because NotePad does not have a replace function.

Next pick File|Save As. Click on the arrow at the right end of the Save As type box. Scroll down and select Text document, then save. You must save the file as a text (TXT) document. If you use any other format, WordPad sticks in font and formatting codes that confuse AutoCAD to no end.

Go back to AutoCAD and start a new blank drawing. Run the Script command and select your TEST.SCR file. Watch in amazement as AutoCAD automatically runs through the script and systematically opens every drawing on your hard drive, one after the other.

That is magic! But now that I have it opening every drawing, how do I get it to update?

The obvious way is to use WordPad’s Replace function again. Replace DWG at the end of each line with the specific commands and prompt responses that you want to enact. This can be messy to edit if you don’t get it right the first time. Also, if you happen to hit a drawing in which the desired commands won’t work, perhaps because of a missing layer or block name, the entire script stalls.

The better way is to use a bit of AutoLISP. Specifically, if you create a file called acad.lsp and put it in your C:\…\ ACADR14\SUPPORT directory, AutoCAD automatically loads it whenever it opens an existing drawing. This one file can include logic to make sure the drawing needs editing and that only one copy exists if you need to experiment and make changes.

Get specific
Let’s go back to the specific e-mail that started all this. Your situation will be different, but you will get the general idea from the example.
First, open one of the existing drawings and Wblock the Notes block out to disk in a file called C: \NOTES.DWG. This creates a new drawing file in the root directory C: that consists only of the notes. Edit it to make the desired changes, then save.

Now create an acad.lsp file that looks like this:

;;;ACAD.LSP
(defun s::Startup ()
(if (tblsearch "block" "notes")
(progn
(command "insert" "notes=c:/notes")
(command)
(command "qsave")
)
)
)
When your script file opens a drawing, the ACAD.LSP routine automatically loads. It defines a function called S::STARTUP, which AutoCAD runs automatically after it loads.

The (if …) statement checks to see if the drawing contains a block definition called Notes. If it does, the routine inserts another copy of it. The equal sign followed by the file name tells AutoCAD to go get the drawing from disk and use it to redefine the existing block definition. A forward slash after the C: indicates the root directory.

The (command) line tells AutoCAD to cancel the command in process, so it never actually completes the Insert command. It has, however, updated the definition.

To finish, Qsave saves the revised drawing back to disk, the script resumes control, the next drawing opens, and the cycle repeats.

There you have it. Twenty minutes to create the script, the modified block drawing, and the ACAD.LSP routine. Start running it Friday afternoon and go home. Come back Monday morning and convince your boss that you slaved all weekend on the revisions. You’ll be a hero!

Having saved the day, the Captain went back to checking the tyre pressures and petrol level in the Rolls. . .


AutoCAD Tips!

Lynn Allen

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter



Poll
Which file format do you use most often for CAD drawing/model exchange?
Native format
PDF
3D PDF
DWF
STEP or IGES
JT
IFC
Other
Submit Vote