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

Chapter 34 Understanding AutoLISP and Visual LISP Basics 1047

Function Description

=Equal to. Returns T for true if all arguments are numerically equal. (= 3 3.0) returns T. If the arguments are not equal, it returns nil.

/=

Not equal to. Returns T if all arguments are not numerically equal. (/= 5 6) returns T. If

 

they are equal, it returns nil.

<Less than. Returns T if each argument is numerically less than the next argument. Otherwise returns nil.

>Greater than. Returns T if each argument is numerically greater than the next argument. Otherwise returns nil.

>=

Greater than or equal to. Returns T if each argument is numerically greater than or equal to

 

the next argument. Otherwise returns nil.

<=

Less than or equal to. Returns T if each argument is numerically less than or equal to the

 

next argument. Otherwise returns nil.

 

 

Using AutoLISP on the Command Line

You can use AutoLISP on the fly in AutoCAD because it is interpreted. By typing an expression on the command line, you get the result immediately. An interpreted language is one in which a single source statement is translated to machine language and executed, and then each subsequent source statement is operated on in the same way. This enables interactive entry of AutoLISP code into AutoCAD.

The output of an AutoLISP expression can be used in response to AutoCAD prompts as well. For example, you can type (+ 1 7) at the Diameter/<Radius>: prompt of the CIRCLE command for a circle with a radius of 8 units. This capability to place AutoLISP expressions into AutoCAD commands is a very powerful tool.

If you leave out a closing parenthesis, AutoCAD returns (_> when you press Enter. This tells you that one open parenthesis has not been closed. If two open parentheses remain to be closed, AutoCAD responds with ((_>. Just type the correct number of closing parentheses at the command line and press Enter. AutoCAD accepts the expression.

STEPS: Working with Numbers and Text on the Command Line

1.Open a new drawing using any template.

2.Type circle . Follow the prompts:

Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: Pick

any point.

Specify radius of circle or [Diameter]: (– 5 3)

AutoCAD draws a circle with a radius of 2.

3.Type (strcat "This is an example of " "regular text.") . Don’t forget the space between of and the quotation mark. When the two phrases are put together, this creates the space between of and regular. AutoCAD returns “This is an example of regular text.”

1048 Part VII Programming AutoCAD

4.Press the up-arrow key. AutoCAD repeats the last line. Press the left-arrow key to move

to the right of the last r in regular, and press Backspace to delete the word regular. Type italic . AutoCAD returns the new string concatenation.

5.Save the drawing in your AutoCAD Bible folder as ab34-02.dwg.

Note

When you create AutoLISP expressions on the command line or in the Console window, they

 

aren’t saved with the drawing. They have the same status as any input that you type at the

 

command line.

Creating AutoLISP Files

 

If you want to use your AutoLISP expressions more than a couple of times, you should save

 

them in a file. Create the routine in Visual LISP and choose Save File.

Tip

A common practice is to consolidate all AutoLISP routines in one folder for ease of use. To do

 

this, you can create a folder called LISP in any drive or folder where you keep files that you

 

create, and then choose Tools Options. On the Files tab, expand the Support File Search

 

Path and click Add. Add the path by typing it directly in the edit box or by clicking Browse and

 

navigating to it.

 

Because AutoCAD can open multiple drawings, you need to organize your AutoLISP routines

 

based on whether you want them to apply to all drawings or just the first drawing that you

 

open.

 

AutoCAD offers two LSP files that you can use for your AutoLISP routines:

acad.lsp: Use this file for routines that customize the initialization of AutoCAD. acad.lsp is not automatically loaded for every consecutive drawing. For example, if you want to load a special menu for only a specific drawing, you could put the routine in acad.lsp. (You can choose to load acad.lsp with every drawing. Choose Tools Options and click on the System tab. In the General Options section, check the Load acad.lsp with Every Drawing check box.)

acaddoc.lsp: Use this file for routines that customize the initialization of individual drawings. This file is loaded every time a drawing is opened. If you have a menu that contains AutoLISP macros that include variables that you want to be available for all of your drawings, put the AutoLISP routines used by the menu in acaddoc.lsp.

Each drawing contains its own AutoLISP environment. AutoLISP files that you load in one drawing, along with their variables, won’t be accessible to another drawing. To share functions, use

(vl-load-all “filename”) instead of (load “filename”), where filename is the name of the AutoLISP file. This enables you to “populate” every current drawing, as well as any new drawing that you may create. The VL-LOAD-ALL function is equivalent to placing an AutoLISP routine in acaddoc.lsp.

To create a new Visual LISP file, open the Visual LISP Editor (choose Tools AutoLISP Visual LISP Editor) and choose New File on the Standard toolbar. Visual LISP opens an

untitled document, as shown in Figure 34-7. You can now start typing code in the new document. When you start typing, you immediately notice that your code looks different than it would if you had typed it in a text editor — it’s in color! Visual LISP can distinguish certain features in your code, and colors it accordingly. Table 34-4 lists the colors and their meanings.

Chapter 34 Understanding AutoLISP and Visual LISP Basics 1049

Figure 34-7: A new AutoLISP document in the Visual LISP editor.

Table 34-4: Visual LISP Editor Coloring System

 

 

Color

AutoLISP Syntax Element

 

 

Blue

Built-in functions and protected symbols

Magenta

Text strings

Green

Integers

Teal

Real numbers

Purple on gray background

Comments

Red

Parentheses

Black

Unrecognized items, such as variables that you’ve created

 

 

If you want, you can customize these colors. From the Visual LISP menu, choose Tools Window Attributes Configure Current. In the Window Attributes dialog box, choose an element from the drop-down box and pick a color from the color swatches. You can also change the left margin and the tab width in this dialog box.

STEPS: Creating a New Visual LISP File

1.Start a new drawing using any template. You shouldn’t have any other drawings open.

2.Choose Tools AutoLISP Visual LISP Editor.

3. Choose New File from the Visual LISP Standard toolbar.

1050 Part VII Programming AutoCAD

4.Type the following:

draws a horizontal line 3 units long (defun c:line3 (/ pt)

(princ "Please pick a point: ") (setq pt (getpoint))

(command "_line" pt "@3,0" "")

)

5.Select the first line of text and choose Comment Block from the Tools toolbar.

6.Choose Format Edit Window from the Tools toolbar to format the code.

7.Choose Check Edit Window and look at the result in the Build Output window.

8.Click the Visual LISP Editor to activate it again.

9.Choose Load Active Edit Window to load the routine.

10.Choose Activate AutoCAD from the View toolbar.

11.On the command line, type line3 .

12.At the Please pick a point: prompt, use any method to specify a point. AutoCAD draws a horizontal line 3 units long.

13.Use the Windows task bar to return to the Visual LISP window.

14.Click the Visual LISP Editor to activate it. Choose Save File on the Visual LISP Standard toolbar. Save the routine as ab34-02.lsp in your AutoCAD Bible folder.

15.Close the Visual LISP window by clicking its Close button. Do not save your drawing.

Summary

This chapter covers the fundamentals of AutoLISP programming and the Visual LISP environment. You read about:

How to open the Visual LISP interface and load and use AutoLISP files

How to use AutoLISP on the command line

What AutoLISP routines look like

How to get help in Visual LISP

The basic structure of AutoLISP syntax, as well as how to work with numbers and text

In the next chapter, you examine how to create variables, work with AutoCAD commands and system variables, modify AutoCAD objects (or entities, as they’re often still called in AutoLISP), and work with the fundamental units of AutoLISP, known as lists.

 

 

 

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