Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
George Omura. Lisp programing tutorial for AutoCAD customization / 620.0.The ABC's of AutoLISP - Omura, George.pdf
Скачиваний:
140
Добавлен:
02.05.2014
Размер:
1.55 Mб
Скачать

The ABC’s of AutoLISP by George Omura

Chapter 1: Introducing AutoLISP

Featuring

Understanding the AutoLISP Interpreter and Evaluation

Expressions and Arguments

Variables and Data Types

Manipulating Lists with Functions

Get Functions

If you have never programmed a computer before, you may think that learning AutoLISP will be difficult. Actually, when you use a program such as AutoCAD, you are, in a sense, programming your computer to create and manipulate a database. As you become more familiar with AutoCAD, you may begin to explore the creation of linetypes and hatch patterns, for example. Or you may customize your menu to include your own specialized functions and macros. (Macros are like scripts that the computer follows to perform a predetermined sequence of commands.) At this level, you are delving deeper into the workings of AutoCAD and at the same time programming your computer in a more traditional sense.

Using AutoLISP is really just extending your knowledge and use of AutoCAD. In fact, once you learn the basic syntax of AutoLISP, you need only to familiarize yourself with AutoLISP's built-in functions to start writing useful programs. (AutoLISP's syntax is the standard order of elements in its expressions.) You might look at AutoLISP functions as an extension to AutoCAD's library of commands. The more functions you are familiar with, the better equipped you are for using the program effectively.

AutoLISP closely resembles Common LISP, the most recent version of the oldest artificial intelligence programming language still in use today. AutoLISP is essentially a pared down version of Common LISP with some additional features unique to AutoCAD. Many consider LISP to be one of the easiest programming languages to learn, partly because of its simple syntax. Since AutoLISP is a subset of common LISP, it is that much easier to learn.

In this chapter, you will become familiar with some of the basic elements of AutoLISP by using AutoLISP directly from the AutoCAD command prompt to perform a few simple operations. While doing this, you will be introduced to some of the concepts you will need to know to develop your own AutoLISP applications.

Understanding the Interpreter and Evaluation

AutoLISP is accessed through the AutoLISP interpreter. When you enter data at the AutoCAD command prompt, the interpreter first reads it to determine if the data is an AutoLISP formula. If the data turns out to be intended for AutoLISP, then AutoLISP evaluates it, and returns an answer to the screen. This process of reading the command prompt, evaluating the data, then printing to the screen, occurs whenever anything is entered at the command prompt

5

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

and is an important part of how AutoLISP functions.

In some ways, the interpreter is like a hand-held calculator. Just as with a calculator, the information you wish to have AutoLISP evaluate must follow a certain order. For example, the formula 0.618 plus 1 must be entered as follows:

(+ 0.618 1)

Try entering the above formula at the command prompt. AutoLISP evaluates the formula (+ 0.618 1) and returns the answer, 1.618, displaying it on the prompt line.

This structure-+ 0.618 1-enclosed by parentheses, is called an expression and it is the basic structure for all AutoLISP programs. Everything intended for the AutoLISP interpreter, from the simplest expression to the most complex program, must be written with this structure. The result returned from evaluating an expression is called the value of the expression.

The Components of an Expression

An AutoLISP 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 together or dividing one number by another. Examples of mathematical operators include the plus sign (+)for addition and forward slash (/) for division.

We will often refer to the operator as a function and the items to be operated on as the arguments to the function or simply, the arguments. So, in the expression (+ 0.618 1), the + is the function and the 0.618 and 1 are the arguments. All AutoLISP expressions, no matter what size, follow this structure and are enclosed by parentheses.

Parentheses are important elements of an expression. All parentheses must also be balanced, that is, for each left parenthesis, there must be a right parenthesis. If you enter an unbalanced expression into the AutoLISP interpreter, you get the following prompt:

((_>

where the number of parentheses to the left is the number of parentheses required to complete the expression. If you see this prompt, you must enter the number of closing parentheses indicated in order to return to the command prompt. In this example, you would need to enter two right parentheses to complete the expression.

Double quotation marks enclosing text must also be carefully balanced. If an AutoLISP expression is unbalanced, it can be quite difficult to complete it and exit AutoLISP. Figure 1.1 shows the components of the expression you just entered.

6

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure1.1: The parts of and AutoLISP expression

Note that spaces are used to separate the functions and arguments of the expression. Spaces are not required between the parentheses and the elements of the expression though you can add spaces to help improve the readability of expressions when they become complex. However, it is very important to maintain spaces between the elements of the expression. Spaces help both you and AutoLISP keep track of where one element ends and another begins.

Using Arguments and Expressions

AutoLISP evaluates everything, not just expressions, but the arguments in expressions as well. This means that in the above example, AutoLISP evaluates the numbers 0.618 and 1 before it applies these numbers to the plus operator. In AutoLISP, numbers evaluate to themselves. This means that when AutoLISP evaluates the number 0.618, 0.618 is returned unchanged. Since AutoLISP evaluates all arguments, expressions can also be used as arguments to a function.

For example, enter the following at the command prompt:

(/ 1 (+ 0.618 1))

In this example, the divide function (/) is given two arguments-number 1 and an expression (+ 0.618 1). This type of expression is called a complex or nested expression because one expression is contained within another. So in our example, AutoLISP first evaluates the arguments of the expression, which are the expression (+ 0.618 1) and the number 1. It then applies the resulting value of the expression and the number 1 to the divide function and returns the answer of 0.618047 (see figure 1.2 ).

7

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 1.2: Evaluation of a nested expression

Using Variables

Another calculator-like capability of the interpreter is its ability to remember values. You probably have a calculator that has some memory. This capability allows you to store the value of an equation for future use. In a similar way, you can store values using variables.

A variable is like a container that holds a value. That value can change in the course of a program's operation. A simple analogy to this is the title of a government position. The position of president could be thought of as a variable. This variable can be assigned a value, such as Ronald Reagan or Bill Clinton.

Understanding Data Types

Variables can take on several types of values or data types. Here is what some of these data types look like in AutoLISP.

8

Copyright © 2001 George Omura,,World rights reserved