Play It again and again and again, Sam30 Nov, 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 cant 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.
Lets 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.
Press <Enter> at the end of the line to drop down to the next line.
LINE 1,1 2,1 2,2 1,2 C
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.
Wasnt that simple? Now lets 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. Thats 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:
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.
DIR *.DWG /S /B /-P
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. Youll 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 WordPads 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 dont get it right the first time. Also, if you happen to hit a drawing in which the desired commands wont 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.
Lets 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:
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.
(defun s::Startup ()
(if (tblsearch "block" "notes")
(command "insert" "notes=c:/notes")
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. Youll be a hero!
Having saved the day, the Captain went back to checking the tyre pressures and petrol level in the Rolls. . .