Understanding the Diesel Macro Language


If you browse through the Acad.mnu file, you'll see many menu options that contain odd-looking text beginning with a dollar sign ( $ ). In some instances, the dollar sign tells AutoCAD to open a shortcut menu. But in many cases, it is used as part of the Diesel macro language. Diesel is one of many macro languages AutoCAD supports, and you can use it to perform some simple operations. Like AutoLISP, it uses parentheses to enclose program code.

In this section, you'll look at different ways that Diesel can be used. You'll start by using Diesel directly from the command line. This will show you how a Diesel macro is formatted and will give you a chance to see Diesel in action. Then you'll go on to see how Diesel can be used as part of a menu option to test AutoCAD's current state. In the third section, you'll see how Diesel can be used as part of the menu label to control what is shown in the menu. Finally, you'll learn how Diesel can be used with Field objects to control text in your drawing.

Using Diesel at the Command Line

You can use Diesel at the AutoCAD command line by using a command called Modemacro. The Modemacro command sends information to the status bar. Diesel can be used with Modemacro to perform some simple tasks .

Try the following exercise to experiment with Diesel:

  1. At the command prompt, type Modemacro .

  2. At the Enter new value for MODEMACRO, or . for none <"">: prompt, enter $(/,25,2) . You'll see the answer to the equation in the far-left side of the status bar.

    The answer to the equation appears here.

    click to expand
  3. To clear the status bar, enter Modemacro . .

The equation you entered in step 2 is referred to as an expression. The structure of Diesel expressions is similar to that of AutoLISP. The dollar sign tells AutoCAD that the information that follows is a Diesel expression.

A Diesel expression must include an operator of some sort , followed by the items to be operated on. An operator is an instruction to take some specific action, such as adding two numbers or dividing one number by another. Examples of mathematical operators include the plus sign (+) for addition and the forward slash (/) for division.

The operator is often referred to as a function and the items to be operated on as the arguments to the function, or simply the arguments. So, in the expression (/,25,2), the / is the function, and the 25 and 2 are the arguments. All Diesel expressions, no matter what size , follow this structure and are enclosed by parentheses.

Parentheses are important elements of an expression. All parentheses must be balanced; for each left parenthesis, there must be a right parenthesis.

You can do other things with Diesel besides performing calculations. The Getvar function is an AutoLISP function that you can use to obtain the drawing prefix and name . Try the following to see how Diesel uses Getvar:

  1. Type Modemacro again.

  2. Type $(getvar,dwgprefix) . The location of the current drawing appears in the status bar.

  3. Press to reissue the Modemacro command; then type $(getvar,dwgname) . Now the name of the drawing appears in the status bar.

In this example, the Getvar function extracts the drawing prefix and name and displays it in the status bar. You can use Getvar to extract any system variable you want. If you've been working through the tutorials in this book, you've seen that virtually all AutoCAD settings are also controlled through system variables. (Appendix C contains a list of all the system variables .) This can be a great tool when you are creating custom menus , because with Getvar, you can "poll" AutoCAD to determine its state. For example, you can find out what command is currently being used. Try the following exercise to see how this works:

  1. Click the Line tool in the Drawing toolbar.

  2. Type ¢ Modemacro . The apostrophe at the beginning of Modemacro lets you use the command while in another command.

  3. Type $(getvar,cmdnames) . The word line appears in the status bar, indicating that the current command is the Line command.

Diesel can be useful in a menu when you want an option to perform a specific task depending on which command is currently active.

Warning  

LT users cannot use AutoLISP to find the location of AutoCAD resource files. However, you can use the Diesel macro language. For example, to find the log file path , enter Modemacro then $(getvar,logfilepath) . The path will be displayed in the far left of the status bar.

Using Diesel in a Menu

So far, you've been experimenting with Diesel through the Modemacro command. To use Diesel in a menu requires a slightly different format. You still use the same Diesel format of a dollar sign followed by the expression, but you don't use the Modemacro command to access Diesel. Instead, you use $M= . You can think of $M= as an abbreviation for Modemacro.

Here's a Diesel expression that you can use in a menu:

[Blipmode on/off]'Blipmode $M=$(-,1,$(getvar,Blipmode))

This menu option turns Blipmode on or off. As you might recall, Blipmode is a feature that displays point selections in the drawing area as tiny crosses. These tiny crosses, or blips as they are called, do not print and can be cleared from the screen with a redraw . They can be helpful when you need to track your point selections.

In this example, the Blipmode command is invoked, and then the $M= tells AutoCAD that a Diesel expression follows. The expression

$(-,1,$(getvar,Blipmode))

returns either a 1 or a 0, which is applied to the Blipmode command to turn it either on or off. This expression shows that you can nest expressions. The most deeply nested expression is evaluated first, so AutoCAD evaluates

$(getvar,blipmode)

to begin with. This returns either a 1 or 0, depending on whether Blipmode is on or off. Next, Auto- CAD evaluates the next level in the expression

$(-,1, getvar_result )

in which getvar_result is either a 1 or 0. If getvar_result is 1, the expression looks like

$(-,1,1)

which returns a 0. If getvar_result is 0, the expression looks like

$(-,1,0)

which returns a 1. In either case, the end result is that the Blipmode command is assigned a value that is opposite of the current Blipmode setting.

Using Diesel as a Menu Option Label

In the previous example, you saw how to use Diesel in a menu to read the status of a command and then return a numeric value to alter that status. You can also use Diesel as part of the menu option label. The following expression shows the same menu listing, with a twist. It includes Diesel code as the menu option label, as follows:

[$(eval,"Blipmode ="$(getvar,blipmode))]'BLIPMODE $M=$(-,1,$ (getvar,blipmode))

Tip  

When Diesel is used as the menu name, you don't need the $M= code.

Normally, you see the menu name within the square brackets at the beginning of this menu listing, but here you see some Diesel instructions. These instructions tell AutoCAD to display the message Blipmode = 1 or Blipmode = 0 in the menu, depending on the current Blipmode setting.

Here's how it works. You see the familiar $(getvar,blipmode) expression, this time embedded within a different expression. You know that $(getvar,blipmode) returns either a 1 or 0 depending on whether Blipmode is on or off. The outer expression

$(eval,"Blipmode =" getvar_result )

displays Blipmode = and then combines this with getvar_result , which, as you've learned, will be either 1 or 0. The eval function evaluates any text that follows it and returns its contents. The end result is the appearance of Blipmode = 1 or Blipmode = 0 in the menu, depending on the status of Blipmode. Here's how the option looks in a menu.

You can get even fancier by setting up the menu option label to read Blipmode On or Blipmode Off by using the if Diesel function. Here's that same menu listing with additional Diesel code to accomplish this:

[$(eval,"Blipmode "$(if,$(getvar,blipmode),"Off","On"))]'BLIPMODE $M=$(,1,$(getvar,blipmode))

In this example, the simple $(getvar,blipmode) expression is expanded to include the if function. The if function reads the result of $(getvar,blipmode) and then returns the Off or On value depending on whether $(getvar,blipmode) returns a 0 or 1. Here's a simpler look at the expression:

$(if, getvar_result , "Off", "On")

If getvar_result returns a 1, the if function returns the first of the two options listed after getvar_result , which is Off . If getvar_result returns a 0, the if function returns On . The second of the two options is optional. Here's how the fancier Blipmode option appears in a menu.

You've really just skimmed the surface of what Diesel can do. To get a more detailed description of how Diesel works, choose Help   AutoCAD Help to open the Help Topics dialog box. Click the Index tab. Enter Diesel in the input box at the top to display a listing of topics related to Diesel.

For fun, try adding this Blipmode menu listing to your Mymenu.mnu file under the [Continue Line] option; then reload the menu file and check the results:

[$(eval,"Blipmode "$(if,$(getvar,blipmode),"Off","On"))]'BLIPMODE $M=$(,1,$(getvar,blipmode))

Make sure the last line in your menu file is followed by .

Table 20.2 shows some of the commonly used Diesel functions. Check the AutoCAD Help Topics dialog box for a more detailed list.

Table 20.2: A Sample of Diesel Functions. To Indicate True or False, Diesel Uses 1 or 0.

Code

Function

Example

result

Comments

+

Add

$ (+,202,144)

346

 

-

Subtract

$ (-,202,144)

58

 

*

Multiply

$ (*,202,144)

29,088

 

/

Divide

$ (/,202,144)

1.4028

 

=

Equal to

$ (=,202,144)

If numbers are equal, 1 is returned.

<

Less than

$ (<,202,144)

If the first number is less than the second, 1 is returned.

>

Greater than

$ (>,202,144)

1

If the first number is less than the second, 0 is returned.

!

Not equal to

$ (!,202,144)

1

If numbers are equal, 0 is returned.

<=

Less than or equal to

$(+,202,144)

If the first number is less than or equal to second, then 1 is returned.

>=

Greater than or equal to

$ (+,202,144)

1

If the first number is less than or equal to the second, 0 is returned.

eq

Equal string

$ (eq,"Yes", "No")

If both text strings are the same, 1 is returned.

eval

Evaluate text

$ (eval,"Here I Am")

Here I Am

Returns text in quotes.

getvar

Get system variable value

$ (getvar,ltscale)

Current line- type scale

 

if

If/Then

$ (if,1,"Yes","No")

Yes

The second argument is returned if the first argument evaluates to 1. Otherwise, the third argument is returned. The third argument is optional.

Using Diesel and Fields to Generate Text

Using Diesel expressions in the status bar or in a menu can be helpful to gather information or to create a more interactive interface, but what if you want the results of a Diesel expression to become part of the drawing? You can employ field objects to do just that.

For example, suppose you want to create a note that shows the scale of the drawing based on the dimension scale. Further, you want the note to automatically update the scale whenever the dimension scale changes. You can add a field object and associate it with a Diesel expression that displays the dimension scale as it relates to the drawing scale. Try the following steps to see how it's done:

  1. In the Draw toolbar, click the Multiline Text tool and select two points to indicate the text location. The Text Formatting dialog box and Text window appear.

  2. Right-click in the Text window and select Insert Field. The Field dialog box appears.

  3. In the Field Category drop-down list, select Other; then in the Field Names list box, select Diesel Expression.

  4. Add the following text in the Diesel Expression box to the right. If you need to expand the width of the dialog box, you can do so by clicking and dragging the right edge of the dialog box.

    $(eval,"Dimension Scale: 1/")$(/,$(getvar, dimscale),12)$(eval,"inch = 1 foot ")

  5. Click OK in the Field dialog box; then click OK in the Text Formatting dialog box. You see the following text displayed in the drawing:

    Dimension Scale: 1 / 0.08333333 inch = 1 foot

The resulting text might not make sense until you change the dimension scale to a value that represents a scale other than 1-to-1. Here's how to do that:

  1. Enter Dimscale at the command prompt.

  2. At the Enter new value for DIMSCALE <1.0000>: prompt, enter 96 . This is the value for a 1 / 8 ² scale drawing.

  3. Choose View   Regen or enter Re . The text changes to read

    Dimension Scale: 1 / 8 inch = 1 foot

In this example, several Diesel operations were used. The beginning of the expression uses the eval operation to tell AutoCAD to display a string of text:

$(eval "Dimension Scale: 1/")

The next part tells AutoCAD to get the current value of the Dimscale system variable and divide it by 12:

$(/,$(getvar, dimscale),12)

Notice that this is a nested expression: $(getvar,dimscale) obtains the value of the Dimscale system variable, which is then divided by 12. Finally, the end of the expression adds the final part to the text:

$(eval,"inch = 1 foot")

When it is all put together, you get the text that shows the dimension scale as an architectural scale. Because it is an AutoCAD text object, this text is part of the drawing.




Mastering AutoCAD 2005 and AutoCAD LT 2005
Mastering AutoCAD 2005 and AutoCAD LT 2005
ISBN: 0782143407
EAN: 2147483647
Year: 2004
Pages: 261
Authors: George Omura

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net