Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Autocad 2005 And Autocad LT 2005 Bible (2004).pdf
Скачиваний:
92
Добавлен:
17.08.2013
Размер:
32.22 Mб
Скачать

Chapter 34 Understanding AutoLISP and Visual LISP Basics

993

Closing a file and Visual LISP

To close a file within Visual LISP, click its Close button. (You can also press Ctrl+F4.) To exit Visual LISP, click its Close button or press Alt+F4. As with AutoCAD, Visual LISP warns you about saving your changes.

On the

The file used in the following Step-by-Step exercise on using the Visual LISP interface,

CD-ROM

ab34-a.lsp, is in the Drawings folder on the CD-ROM.

STEP-BY-STEP: Using the Visual LISP Interface

1.From AutoCAD, choose Tools AutoLISP Visual LISP Editor to open Visual LISP.

2.Choose Open File on the Visual LISP Standard toolbar and open ab34-a.lsp from the CD-ROM. Save the file as ab34-1.lsp in your AutoCAD Bible folder.

3.Choose Format Edit Window on the Tools toolbar. AutoCAD automatically formats the code.

4.Select the line of the code that reads:

(command “_circle” pt “3”)

5.Choose Comment Block. AutoCAD places three semicolons before the text and shades it. Choose Uncomment Block. AutoCAD returns the code to its previous

state.

6.Choose Check Edit Window on the toolbar. AutoCAD opens the Build Output window and issues the Check done message.

7.Click anywhere in the edit window to activate it. Choose Load Active Edit Window on the toolbar. AutoCAD loads the routine.

8.In the Visual LISP Console window, type (c:circle3) and press Enter. Visual LISP returns you to AutoCAD and starts the program. Pick a point. When you’re done, you’re automatically returned to the Visual LISP window.

9.Click the text editor window and choose Save File. Click the Close button at the topright corner of the text editor window to close the routine.

10.Click the Close button at the top-right corner of your screen to close Visual LISP.

Getting Help in Visual LISP

Visual LISP offers two ways for you to get help while writing AutoLISP code — context-sensitive help on any term you select and the overall Help system.

To get help on any AutoLISP function in your code, select the function (either in the edit window or in the Console window) and choose Help on the Visual LISP Tools toolbar.

Figure 34-6 shows the screen that appears when you select setq and choose Help.

994 Part VII Programming AutoCAD

Figure 34-6: When you select any AutoLISP function in your code and choose Help, Visual LISP opens the help page for that function.

To open the entire Visual LISP and ActiveX Help system, choose Help Visual LISP Help Topics on the Visual LISP menu or Press F1. Visual LISP displays the screen shown in Figure 34-7.

Figure 34-7: The main help page of Visual LISP’s Help system.

Chapter 34 Understanding AutoLISP and Visual LISP Basics

995

All these help features can be a bit overwhelming, but for AutoLISP and Visual LISP you basically have three choices:

AutoLISP Reference: This guide is equivalent to the Command Reference in AutoCAD’s Help system. Here you’ll find an alphabetical list of all AutoLISP functions.

AutoLISP Developer’s Guide: This guide is equivalent to the AutoCAD User’s Guide. It contains all the basic topics you need to know to program in Visual LISP.

AutoLISP Tutorial: If you remember the Garden Path tutorial for AutoLISP (which has been around for many years), you’ll recognize this tutorial, now rewritten for Visual LISP.

Working with AutoLISP Expressions

As with any programming language, AutoLISP has its own functions and syntax. This section explains how to start writing AutoLISP code. One big advantage to AutoLISP is that you can test your code immediately, unlike the case of compiled languages for which extra steps are required. For compiled languages, every time a code change is made, the code must be recompiled and re-linked before you can see the changes in AutoCAD. This is the case with ObjectARX, for example.

Understanding AutoLISP syntax

In AutoLISP’s syntax, an operator always comes first, followed by a number of operands. An operator can be thought of as a function that does something, and the operands are what the function operates on. Place a space between each component of the statement.

Another way to describe the syntax of AutoLISP is that, within each set of parentheses, the first item is a function, and what follows are parameters or arguments to that function.

Working with numbers and text

Table 34-1 lists the basic arithmetic functions. You can use these functions on anything that has a value. For example, if you create a variable with a value of 2, you can use that variable with the arithmetic functions.

 

Table 34-1: Basic Arithmetic Functions

 

 

Function

Description

 

 

+

Addition

-

Subtraction

*

Multiplication

/

Division

Sqrt

Square root

 

 

996 Part VII Programming AutoCAD

Table 34-1 lists only a few fundamental arithmetic operators (or functions). Many operators are available to the AutoLISP programmer; you can find them in the AutoLISP Function Synopsis Appendix of the AutoLISP Developer’s Guide, under Basic Functions Arithmetic Functions.

Some fundamental arithmetic operations can provide a feel for the fundamentals. If you type (+ 2 3) on the AutoCAD command line (being careful to put a blank space after the + and the 2), AutoLISP responds with 5. This syntax of having the operator (the plus sign) come before the operands (2 and 3) is different from that of many languages but is important to understand in AutoLISP.

To nest expressions, simply add another set of parentheses to enclose the nested expression. For example, when you enter (* 5 (+ 2 3)), AutoCAD responds with 25. The nested expression is evaluated from the innermost parenthesis pair out; first 2 and 3 are added, and then that sum is multiplied by 5.

Working with floating point numbers (numbers with decimal points) is as easy as working with integers. If you type (/ 7.3 5.84) , AutoLISP responds with 1.25.

Note

If you’re using a number less than 1, you must include the leading 0, as in (+ 2.5 0.5).

Working with text is as easy as working with numbers. In AutoLISP, a string is simply some text. Table 34-2 lists some common string functions.

 

Table 34-2: Basic String Functions

 

 

Function

Description

 

 

STRCAT

Concatenates (attaches) strings to one another.

SUBSTR

Returns (provides you with) a substring (a portion) of a string. The first

 

argument is the string, the second is an integer that specifies the position

 

of the first character you want, and the third (optional) is the number of

 

characters you want.

STRLEN

Calculates string length; returns the number of characters including spaces

 

in the string.

 

 

Here are two examples:

(strcat “Today is “ “a good day” “.”) “Today is a good day.”

(substr “Today is a good day.” 12 4) “good”

STRCAT stands for string concatenate and appends one string with another. Any number of strings can come after the strcat function. (There are three strings enclosed in quotes in the strcat example.)

SUBSTR stands for substring, and in this example, it returns the four characters starting at position 12, as shown in the example.