Chapter 30. Creating Macros and Slide Shows

Script files are like macros that you create in your word processor or spreadsheet. They automatically execute a series of commands. You can use script files to automate plotting, set up a drawing, clean up a drawing, create a slide show, or do any repetitive task. By running a script file on a number of drawings, you can complete a time-consuming task in a fraction of the time.

You can also record actions on the screen and save them as a macro. Doing so saves you the time of figuring out the exact command-line sequences.

If you need to put together just a few commands that you might use another time for other drawings, you may want to consider creating a menu item or toolbar button instead. Chapter 29 explains how to customize toolbars, and Chapter 33 explains how to customize the ribbon and menus.

Creating Macros with Script Files

A script file contains a list of commands and options. To create a script file, you need to think out the commands that you want to execute, as well as their options and prompts. Then you create the text for the script file. Script files have the following characteristics:

  • They must use the .scr filename extension.

  • They are text-only (ASCII) files.

  • They must use command-line syntax only (which can include AutoLISP expressions if you are creating a script for AutoCAD only and not AutoCAD LT).

Creating the script file

You can create the script file by using a text editor, such as Notepad. For early practice with script files, type each command on its own line. A blank space is equivalent to pressing Enter. End each line by pressing Enter, without extra blank spaces. If you need two returns, one after another, at the end of a line, use a blank line for the second return. Every space is meaningful; getting those spaces and blank lines right is probably the hardest part of creating a script file. One technique is to start your script files in a word-processing program that can display nonprinting characters (blank spaces and returns). You can either save the script file in text format or copy it into Notepad. Some tips to help you create successful script files with the least aggravation are as follows:

  • Before creating the script file, go through the steps once, using only the command line. Turn off the Dynamic Input feature for this purpose, because it doesn't always include all the command-line content. (Click the Dynamic Input button on the status bar.)

  • If the script includes commands that open a dialog box that asks for files, set the system variable FILEDIA to zero (off) before experimenting with the commands that you'll use in the script file. This setting lets you practice the keystrokes without opening dialog boxes. You can also practice using the version of the command with the hyphen in front of it (such as -layer); however, in most cases, you don't need the hyphen in the actual script file.

    Note

    Script files automatically run as if FILEDIA were off, even if it's set to 1 (on). The FILEDIA system variable determines whether dialog boxes appear for commands that let you open or select files.

  • For commands that require inputting text such as layer names or filenames, enclose the names in quotation marks. Then for the next use of Enter, press Enter and go to the next line in the script instead of using a space. Otherwise, AutoCAD may misinterpret a space as a space in the layer name or filename, rather than an Enter.

  • Place comments in your script file to explain what you're doing. A comment is any line that starts with a semicolon.

  • Keep Notepad open as you work. When you've completed a set of keystrokes that you want, open the AutoCAD Text Window (press F2), select the command string that you want, right-click, choose Copy, switch back to Notepad, and paste. Then cut out all the prompts, leaving only the keyboard entry. You'll probably have to readjust the spaces between the words.

  • You can press End to check for blank spaces at the end of a line. Pressing Ctrl+End moves the cursor to the end of the document; this is useful for checking for extra spaces and lines at the end of a script.

Remember, you can open Notepad from within AutoCAD by typing Notepad at the command line. At the File to edit: prompt, press Enter to open a new file. (In AutoCAD LT, you need to start Notepad from Windows.)

Another option is to write down what you type at the command line. As you write, use an underscore to represent each space. It's very hard to remember that you left three spaces between two words unless you see three underscores. Of course, when you create the script file, you must use spaces, not underscores.

As soon as you complete the script file, save it with any name that is meaningful to you, plus an extension of .scr.

Here's an example of a script file that draws a series of circles:

circle 2,2 1.5
circle 6,2 1.5
circle 10,2 1.5
circle 14,2 1.5

This script file starts the CIRCLE command, specifies a center point, and specifies a radius, four times. The results are shown in Figure 30.1.

Running a script file created this drawing.

Figure 30.1. Running a script file created this drawing.

Running a script file

You can run a script file from within a drawing. Use this technique when you want the script to apply only to that drawing. However, you can also start a script within a drawing, and then close the drawing and continue on to open and run the same script in other drawings.

You can also run a script file when loading AutoCAD or AutoCAD LT. You would do this when you want the script file to apply to more than one drawing. For example, you could use script files in the following situations when:

  • You want to use a script file to set up every drawing that you open. Although the script file applies to only one drawing at a time, you use it on a different drawing each time.

  • You want to use a script file to clean up a list of drawings in one batch, such as thawing all layers on all the drawings in a folder.

Running a script file from within a drawing

If you want the script to apply only to the current drawing, then start the script from within your drawing. To run a script from within a drawing, follow these steps:

  1. Running a script file from within a drawing
  2. Choose the script file that you want.

  3. Click Open. AutoCAD or AutoCAD LT runs the script file.

Running a script when starting AutoCAD or AutoCAD LT

To run a script when starting AutoCAD or AutoCAD LT, change the target expression that Windows uses to start AutoCAD or AutoCAD LT. The easiest way to do this is to use the shortcut to AutoCAD or AutoCAD LT on your desktop and modify the target there. Right-click the AutoCAD or AutoCAD LT shortcut and choose Properties. Click the Shortcut tab.

The Target text box displays the command expression that Windows uses to start AutoCAD or AutoCAD LT. Don't make any change to the current expression — just add to it. If you're using AutoCAD LT, substitute acadlt.exe for acad.exe and your AutoCAD LT program location in the following examples. The format for starting a script file is:

drive:pathacad.exe drive:pathdrawingname.dwg /b script_file

For example, if your current target reads C:Program FilesAutoCAD 2010acad.exe and you want to open a drawing named ba-349.dwg in c:drawings and run a script file named pre-plot.scr, your target should read:

″C:Program FilesAutoCAD 2010acad.exe″ c:drawingsa-349.dwg /b pre-plot

You don't need to add the .scr extension after the script filename. If you have long file and folder names that contain spaces, you must enclose them in quotation marks, both in the target and in the actual script file. You need to include the full path of the drawing. If the script file is not in the support-file search path, include the entire path. For example:

″C:Program FilesAutoCAD 2010acad.exe″ ″c:aecdrcDobbs Ferry Apts.dwg″ /b c:aecdrccleanup

If you want to start a new drawing, you might want to specify a template. In the preceding format, replace the drawing filename with:

/t template_name

Note

In Appendix A, I explain more about changing the target expression to open AutoCAD or AutoCAD LT the way you want.

When you've finished typing your additions in the Target text box, click OK. Now, when you start AutoCAD or AutoCAD LT, the drawing or template opens, and the script starts.

From within a script file, you can open (and close) other drawings. In this way, you can run a script file on as many drawings as you want. Figure 30.2 shows a script file, multi-cleanup.scr, that you could use when loading AutoCAD or AutoCAD LT. The target is set to Apt 1A.dwg.

A script file that cleans up three drawings.

Figure 30.2. A script file that cleans up three drawings.

Note

Use the CLOSE command to close each drawing after your script file has finished working on it. If you don't, you may end up with 100 drawings open at once, and probably a major computer crash as well!

Here's how multi-cleanup.scr works:

  1. The CHPROP command selects all objects and sets their color to BYLAYER.

  2. The -LAYER command freezes the layer named no-plot.

  3. The script file saves the drawing.

  4. The script file closes the drawing and opens the next drawing.

  5. This process is repeated until the last drawing is edited and saved. The last drawing is left open.

Tip

It's helpful to leave the last drawing open so that when you return to see the results, you can see that the last drawing has been properly edited. You then feel pretty sure that all the previous drawings were similarly edited.

Notice the quotation marks around the filenames in the script file. These are necessary because the drawing filenames include spaces.

In the following exercise, you practice creating and using a script file similar to the multi-cleanup script file used in the previous example, but for only one drawing.

Note

The drawing used in the following exercise on creating and using a script file, ab30-a.dwg, is in the Drawings folder on the DVD.

STEPS: Creating and Using a Script File

  1. Open ab30-a.dwg from the DVD.

  2. Save the file as ab30-01.dwg in your AutoCAD Bible folder.

  3. In Windows XP, choose Start

    A script file that cleans up three drawings.
  4. Type the following, replacing the underscores with spaces. Note that there should be two spaces between all and c. Press Enter after the qsave line.

    chprop_all__c_bylayer
    
    -layer_f_no-plot
    
    qsave
  5. Save the file as cleanup.scr in your AutoCAD Bible folder. Close Notepad. The script file changes the color property of all objects to ByLayer and freezes the no-plot layer. Notice that the drawing has some text that has been set to a blue color (maybe to make it more readable). The titleblock is on the no-plot layer.

  6. A script file that cleans up three drawings.
  7. In the Select Script File dialog box, find cleanup.scr in your AutoCAD Bible folder and click Open. The script runs, changing the text's color to ByLayer (green) and freezing the no-plot layer. It also saves the drawing.

    If the script file doesn't work, press F2 to open the AutoCAD Text Window and see where the file got hung up. This will help you see where to correct the script file. Re-open it and make the correction. Save the file, close it, and try again.

  8. Save your drawing.

Recording Actions

You can record actions that you perform in a drawing, such as starting commands, using options, and selecting objects, and save them as a macro. You don't need to know any programming. Then you can play the macro to facilitate automating often-repeated tasks.

Note

The Action Recorder is not available in AutoCAD LT.

This feature can relieve you of time-consuming script development. You can edit macros and even create prompts and explanatory messages for users in the Action Tree window, which lists the individual components of the macro.

Creating an action macro

In order to get the most out of the Action Recorder, you need to plan in advance, and experiment. You may want to run through the actions first, before recording. Often, using the command-line form of a command is the best plan, just as it is for scripts. Here are the steps for recording an action macro:

  1. Set up the conditions that you want to apply for the macro. The macro does not record all drawing settings. You can include settings in the macro by using the SETVAR command. For more information, see Chapter 12.

  2. Creating an action macro
  3. Carry out the actions you want to include in the macro. A red circle follows the cursor to remind you that you are recording.

  4. Creating an action macro
  5. The Action Macro dialog box opens. There you do the following:

    • Enter a name for the macro in the Action Macro Command Name text box. The name can be up to 31 letters/numbers, with no spaces. You can use hyphens and underscores.

    • Enter a description in the Description text box.

    • Choose if you want to restore the drawing view as it was before playback. By default, the command restores the view only if the macro asks for user input, because this offers the opportunity for users to change the view.

    • Choose if you want to scan the macro for inconsistencies between the current drawing state and the drawing state when you recorded the macro. This option is on by default, and helps avoid problems due to differences in settings.

  6. Click OK.

The Action Tree window appears beneath the Action Recorder panel, showing the exact sequence of commands in the macro, as shown in Figure 30.3.

Note

Macros are saved with an ACTM filename extension in the SupportActions folder of your AutoCAD installation. To find the exact location on your system, choose Application Button

Creating an action macro

Editing an action macro

When you have completed recording and naming your macro, you can add prompts for user input or user messages. For example, if you drew a circle, the macro recorded the specified center point and radius, but you could add a user prompt for the center so that you could use the macro to create the circle anywhere. To add a prompt for user input, right-click the item in the macro that you want to replace, and choose Pause for User Input. In the example of drawing a circle, you would right-click the item showing the center point of the circle.

The Action Tree window shows you the contents of the macro.

Figure 30.3. The Action Tree window shows you the contents of the macro.

Note

The behavior of pausing for user input has changed in AutoCAD 2010 to automatically request user input when the macro so specifies. AutoCAD no longer displays a task dialog box offering the option of using the original recorded value during playback. This change helps to ensure that action macros play back consistently among workstations by not allowing users to choose the recorded value when they should be providing their own input.

Also, you can now insert a base point in your action macro to establish an absolute coordinate point to use for following actions by choosing the Insert Base Point option from the right-click menu of the Action Tree.

You can add a user message to provide an explanation of the actions in a macro at any point. To add a user message, right-click the item of the macro at the point where you want the message to appear, and choose Insert User Message. You then enter the message you want displayed in the Insert User Message dialog box.

You can rename a macro by right-clicking its name in the Action Tree window and choosing Rename. To delete a macro, right-click the macro's name, and choose Delete.

Note

The Action Tree window shows you the contents of the macro.

You can also edit or delete any item of the macro. To edit the item, right-click it, and choose Edit. You can then change its value. To delete the item, right-click it and choose Delete. If an item is necessary for the command, then you can't delete it. For example, you can't delete the item that sets the radius of a circle, because a circle needs a radius.

Note

An action macro stores coordinate values as absolute. This means that the macro records the exact coordinates that you specified when recording the macro. However, by default, all coordinates except for the first one are treated as relative to the first coordinate of the macro. Let's say that your macro draws a square, and then a circle inside the square. If you add a prompt for user input for the first corner of the square, the circle will be inside the square, no matter where you place the square. To change this default, right-click any item containing coordinates, and choose Relative to Previous on the shortcut menu to deselect it.

Playing back a macro

To use the macro, choose Manage tab

Playing back a macro

Tip

You can also type an action macro's name on the command line to start it.

Creating Slide Shows

You can create an image from the display on your screen and save it as a slide. You can use several of these images to create a slide show. You can then use script files to direct the timing and order of the slide show. You first save a view of a drawing as a slide, create a slide library from the slides, and then show the slides one after another automatically.

Note

You can use the ShowMotion feature to create shots of views and show them one after another. For more information, see Chapter 8.

Tip

You can save any drawing as an image file and import it into a presentation program. You can add text and special effects to create a professional slide show. For more information, see Chapter 27.

Creating slides

Creating a slide is like capturing a screen shot of your drawing. AutoCAD or AutoCAD LT makes a simplified vector file from the current viewport in model space, or from all viewports in paper space layouts. You can create a slide of a wireframe or hidden display. However, you cannot make a slide of a shaded or rendered display.

To create a slide, follow these steps:

  1. Display the view of the drawing that you want to save as a slide.

  2. Type mslide

    Creating slides
  3. In the Create Slide File dialog box, choose a location and name for the slide. Its file extension will automatically be .sld.

  4. Choose Save.

Viewing slides

After you have created your slides, you will want to look at them! To view a slide, follow these steps:

  1. Type vslide

    Viewing slides
  2. In the Select Slide File dialog box, choose the slide that you want to view.

  3. Choose Open to display the slide.

Do a Redraw to return to your drawing. You cannot draw in or edit a slide.

Note

The drawing used in the following exercise on creating and viewing slides, ab30-b.dwg, is in the Drawings folder on the DVD.

STEPS: Creating and Viewing Slides

  1. Open ab30-b.dwg from the DVD.

  2. Save the file as ab30-02.dwg in your AutoCAD Bible folder.

  3. On the command line, type hide

    Viewing slides
  4. On the command line, type mslide

    Viewing slides
  5. On the command line, type ddvpoint

    Viewing slides
  6. Repeat the MSLIDE command. This time, save the slide as ab30-02b.sld.

  7. On the command line, type ddvpoint

    Viewing slides
  8. Repeat the MSLIDE command and save the slide as ab30-02c.sld.

  9. Choose View tab

    Viewing slides
  10. On the command line, type vslide

    Viewing slides
  11. Repeat the VSLIDE command and display ab30-2b.sld. Do the same with ab30-02c.sld.

  12. On the command line, type redraw

    Viewing slides
  13. Save your drawing.

Using scripts to create slide shows

You can create a script file that displays slides one after another, resulting in a slide show. You can use two special script file commands for this purpose:

  • DELAY nnnn pauses the script for the number of milliseconds that you specify. For example, DELAY 3000 pauses the script for 3 seconds.

  • RSCRIPT repeats the script from the beginning. Use this command to create a continuously running script. To stop the script (whether repeating or not), press Esc or backspace, or drop down any menu.

  • RESUME restarts a script file after you've stopped it.

The VSLIDE command, which displays a slide, can also be used to preload the next slide into memory. You use this command to preload a slide while viewers are looking at the previous slide. This reduces the waiting time between slides. To use this feature, put an asterisk (*) before the filename in the VSLIDE command. The next VSLIDE command detects that a slide has been preloaded and displays it without asking for the slide name. Here's how it works:

1 vslide ab30-02a
2 vslide *ab30-02b
3 delay 3000
4 vslide
5 vslide *ab30-02c
6 delay 3000
7 vslide
8 rscript

This script file does the following:

Line 1 displays ab30-02a.sld.

Line 2 preloads ab30-02b.sld.

Line 3 waits 3 seconds, displaying ab30-02a.sld.

Line 4 displays ab30-02b.sld.

Line 5 preloads ab30-02c.sld.

Line 6 waits 3 seconds, displaying ab30-02b.sld.

Line 7 displays ab30-02c.sld.

Line 8 repeats the script from the beginning.

Note

The Dynamic Input feature sometimes interferes with a slide show. If so, turn off Dynamic Input (click the DYN button on the status bar) before running the slide show.

STEPS: Creating a Slide Show

  1. Open Notepad and type the following script:

    vslide ab30-02a
    vslide *ab30-02b
    delay 3000
    vslide
    vslide *ab30-02c
    delay 3000
    vslide
    delay 3000
    rscript
  2. Remember to press Enter at the end of the last line. Save the file as ab30-02.scr in your AutoCAD Bible folder. Close Notepad.

  3. To ensure that AutoCAD can find the slide files, place your AutoCAD Bible folder in the support-file search path. To do this, choose Application Button

    Using scripts to create slide shows
  4. In any drawing, choose Manage tab

    Using scripts to create slide shows
  5. Let the slide show run through twice. The last slide displays a little more quickly the second time. Press Esc to stop the slide show.

  6. Don't save your drawing.

Tip

When running a slide show, you might want to maximize the screen area by reducing menu and command-line space. You can use the CLEANSCREEN command (press Ctrl+0) to toggle the toolbars and palette windows on and off. You can also hide (and redisplay) the command-line window by pressing Ctrl+9.

Creating Slide Libraries

You can organize your slides into slide libraries. Slide libraries have a .slb file extension. One reason for creating slide libraries is to create image tiles when you're customizing your menu. If you're using AutoCAD, then you can see an example of an image tile menu by entering the following AutoLISP code at the command line:

(menucmd "I=ACAD.image_3dobjects") 
Creating Slide Libraries
(menucmd ″I=ACAD.*″)
Creating Slide Libraries

These image tiles are created with slides organized into libraries.

To view slides in a library, use the following format:

library(slidename)

For example, you can place the three slides that you used in the preceding exercise in a slide library called 3dmodel.slb. You can then use the following command in the script file to preload the second slide (the second line of the script file):

vslide *3dmodel(ab30-02b)

To create a slide library, you need to use the DOS prompt. You use the SLIDELIB utility, which you can find in your AutoCAD 2010 or AutoCAD LT 2010 folder.

To get to the DOS prompt, choose Start

Creating Slide Libraries

Follow these steps to create a slide library:

  1. Create a text file (you can use Notepad) that contains the names of the slide files. Include the paths of the slide files if they're not in the support-file search path. Place each slide filename on a new line. Save the file as ab30sld.lst.

    Tip

    SLIDELIB can read a listing that was created by using DOS's dir command with the /b parameter, which creates a simple listing of just the filenames. Therefore, you can place all the slide files in a folder and redirect the dir listing to a file. For example, you can create a list named ab30sld.lst by typing the following at the DOS prompt:

    dir *.sld /b >ab30sld.lst

    This creates the list in the same folder as the slide files.

  2. Assuming that you're still in the same folder where you created the slide file list and you want to create a library called ab30sld.slb in the same folder, type the following at the DOS prompt (substituting the actual path to your AutoCAD or AutoCAD LT program):

    ″c:Program FilesAutoCAD 2010slidelib″ ab30sld < ab30sld.lst

Note

SLIDELIB cannot accept filenames with spaces, but it can handle long filenames, provided that you use a character, such as an underscore, where you might normally have a blank space.

Summary

This chapter explained how to create script files to automate repetitive commands. You read about the following:

  • Creating script files that contain commands, options, and values in command-line format

  • Running script files from within a drawing or when loading AutoCAD or AutoCAD LT

  • Using the Action Recorder to automate the process of creating macros

  • Creating slides from the display in your viewport and creating a script file that displays several slides, one after another, thus resulting in a slide show

  • Organizing your slides into slide libraries

In the next chapter, you read about how to create your own linetypes and hatch patterns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.169