Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Книги_AutoCad_2 / AutoCAD 2006 and AutoCAD LT 2006 Bible_2004г__.pdf
Скачиваний:
122
Добавлен:
09.04.2015
Размер:
17.83 Mб
Скачать

Chapter 34 Understanding AutoLISP and Visual LISP Basics 1043

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 top-right 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 that 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-5 shows the screen that appears when you select setq and choose Help.

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

1044 Part VII Programming AutoCAD

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-6.

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

All of 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 of the basic topics that you need to know to program in Visual LISP.

AutoLISP Tutorial: This is a tutorial that creates a garden path.

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. For example, this is the case with ObjectARX.

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 the elements on which the function operates. Place a space between each component of the statement.

Chapter 34 Understanding AutoLISP and Visual LISP Basics 1045

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

 

 

Table 34-1 lists only a few fundamental arithmetic operators (or functions). Many operators are available to the AutoLISP programmer. You can find more operators 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) , then 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.

1046 Part VII Programming AutoCAD

 

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 that you want, and the third (optional) is the number of

 

characters that 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 above STRCAT example.)

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

Note

The first character of the string for SUBSTR is number 1. However, other functions that process

 

elements of a list (such as NTH) count the first element as 0.

Table 34-3 offers you a few more functions that work with numbers and strings.

 

Table 34-3: AutoLISP Functions for Numbers and Strings

 

 

Function

Description

 

 

ABS

Returns the absolute value of the argument. (ABS -76) returns 76.

ASCII

Returns the ASCII code of a character. (ASCII “B”) returns 66.

CHR

Returns the text string for an ASCII code. (CHR 66) returns “B”. (Stands for character.)

ATOI

Converts a string to an integer. (ATOI “2.7”) returns 2. (Stands for ASCII to integer.)

ATOF

Converts a string to a real number. (ATOF “7.6”) returns 7.6. (Stands for ASCII to float, as in

 

floating point — that is, a real number.)

ITOA

Converts an integer to a text string. (ITOA 76) returns “76”. (Stands for integer to ASCII.)

RTOS

Converts a real number to a text string, enclosed in quotes. You can add a mode

 

(1=scientific, 2=decimal, 3=engineering, 4=architectural, 5=fractional) and a precision.

 

Otherwise, RTOS uses the current settings. (RTOS 87.3 2 2) returns “87.30”. (RTOS stands for

 

real to string.)

Соседние файлы в папке Книги_AutoCad_2