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

Chapter 35 Exploring AutoLISP Further 1075

If the selected object is a circle, you see its radius change to match the value that you specified to be the new text height. This is definitely not what you intended when writing this program.

3. Modify the program so that it reads as follows, and save it as ab35-06.lsp:

;;;modifies text height and content (value) (defun c:chgmytext (/ src_object new_ht new_str)

(terpri)

(setq src_object (entget (car (entsel))))

(if (equal (assoc 0 src_object) ‘(0 . “TEXT”)) (progn

(princ “What is the new height for the text? “) (setq new_ht (getreal))

(princ “What is the new text value? “) (setq new_str (getstring))

(setq src_object

(subst (cons 40 new_ht) (assoc 40 src_object) src_object)

)

(setq src_object

(subst (cons 1 new_str) (assoc 1 src_object)src_object)

)

(entmod src_object)

)

(princ “You must select a text object.”)

)

(princ)

)

4.Load ab35-06.lsp. Start chgmytext and try out the routine again with a circle or other non-text object.

Don’t save your drawing.

Summary

In this chapter, you learned:

How to create variables

How to create AutoLISP functions

How to work with AutoCAD commands and system variables

About extending AutoLISP’s power by using lists and looping

How to modify and get information about drawing objects

How to create selection sets

About obtaining user input

Tools for finishing off your AutoLISP routines by adding some error handling, making sure the routine exits quietly, and adding helpful comments about the routine’s function.

In the next chapter, you read about some of the more-advanced features of Visual LISP.

 

 

 

Exploring Advanced

AutoLISP Topics

This chapter builds on the previous two chapters and introduces you to a few advanced AutoLISP topics, including local and global

variables, ActiveX, and debugging.

AutoCAD LT does not support AutoLISP, and so this entire chapter is for AutoCAD only.

Understanding Local and

Global Variables

In this section, you read how local and global variables are accessed within a function, as well as some common syntax. You also discover what can happen when global variables are not properly documented.

Chapter 35 explained that a variable is a symbolic name that can be operated on in a given program. An important part of using variables is being able to assign values to them. There are two types of variables, global and local.

A global variable is exposed, or available, to all AutoLISP functions that you’ve loaded into your drawing. A global variable retains its value after the program that defined it is finished. You use a global variable when you want its value to be available across an entire project, as opposed to just one function within a project. This allows you to retain a fixed value that might be used and assigned by different functions, or for debugging. Any variable that you don’t specifically define as a local variable is a global variable.

A local variable is temporarily assigned a value during a function’s execution. After the function completes executing, the local variable value is discarded. AutoLISP can now use the memory that was taken up by that local variable. You use a local variable when you want to be sure that you don’t have variable values floating around and interfering with other functions. Local variables are also easier to debug because they affect only the code within their function. In general, most of your variables should be local. You create a local variable and declare it in the DEFUN statement after the slash and a space, as in this example:

36C H A P T E R

In This Chapter

Understanding local and global variables

Working with Visual

LISP ActiveX functions

Debugging code

Using the

Error Trace window

Using the

Watch window

(defun list-objects ( / counter sset)...

1078 Part VII Programming AutoCAD

Caution

Global variables can be tricky. For example, they can easily cause bugs. This is because their val-

 

ues persist and can be hard to debug because the values are hard to find. A common syntax for

 

global variables is to prefix and suffix the variable with an asterisk, as in *aGlobal*. In this way,

 

you can easily identify global variables in your code. Keep your use of global variables to a min-

 

imum, and carefully document those that you do use. Failure to follow these simple rules could

 

result in undesirable and difficult-to-trace bugs.

STEPS: Using Local and Global Variables

1.Start a new drawing using the acad.dwt template. You should not have any other drawing open.

2.Open the Visual LISP Editor.

3.In the Console window, type the following line and then press Ctrl+Enter. You use Ctrl+Enter in the Console window to enter in code of more than one line. This line declares one local variable:

(defun local-variable (/ var1)

4.Type the second line of code in the Console window as follows:

(setq var1 “I’m local”))

This sets the local variable to the string that you typed. The Console returns the name of the function:

LOCAL-VARIABLE

5.Before you test this function, you can check out the current value of the local variable, var1. Type var1 in the Visual LISP Console. The Console returns:

nil

As you can see, the value is nil.

6.Test the local-variable function to check the value that it sets to the var1 variable. In the Console, type (local-variable) . The Console returns:

“I’m local”

You now know that the local-variable function definitely assigns the value of “I’m local” to the variable var1.

7.To create a global variable, type (setq var1 “I’m global”) in the Console. The Console returns:

“I’m global”

You know that the value of var1 is now “I’m global”.

8.Test the local variable again by typing (local-variable) in the Console. The Console returns “I’m local” because it executes the local-variable function.

9.Test the variable var1 to see what its value is now. In the Console, type var1 . The Console returns “I’m global”. The local variable was not retained when the function used the variable because the variable was local to the function. However, the global variable’s value persisted.

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