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

1086 Part VII Programming AutoCAD

In AutoLISP, you first retrieve the space (model or paper), then your drawing, and finally the AutoCAD application. Here you take the simple line command used earlier in this chapter and convert it using ActiveX:

;;;This function creates a line using Visual LISP

;;;ActiveX and returns the line object as a vla-object. (defun ax-make-aLine ()

(vla-AddLine

(vla-get-ModelSpace

; retrieve the model

 

; space object

(vla-get-ActiveDocument

; get the current

 

; active document

(vlax-get-acad-object)

; get the acad object

)

 

)

 

(vlax-3d-point ‘(5 5 0))

; starting point

(vlax-3d-point ‘(15 5 0))

; ending point

)

 

)

You can type this code in the Visual LISP Editor and load it. To try it out, type the following in the Console:

(ax-make-aLine)

Visual LISP responds with the following:

#<VLA-OBJECT IAcadLine 03614934>

 

This might seem a little cumbersome — as if it might be a lot of work to create a line — but

 

quite honestly, it isn’t. It’s actually quite flexible.

 

To locate other ActiveX functions that create objects, you need to look in the AutoCAD

 

ActiveX and VBA Reference. In Visual LISP, choose Help, Visual LISP Help Topics. From the

 

Contents tab, double-click ActiveX and VBA Reference, and then Methods. You immediately

 

see an alphabetical list with the A’s displayed. Here are all of the VBA methods that add draw-

 

ing objects. To create a Visual LISP function, add VLA- before the method. For example, to add

 

a circle, you would use VLA-AddCircle.

Tip

You immediately know whether the function is correct, because it turns from black to blue as

 

you enter it.

Debugging Code

Because few people can write perfect code on their first try, there’s always a need to debug code. Debugging is simply the process of trying to locate and correct mistakes. In this section, you look at some simple examples, using some of the debugging tools that Visual LISP has to offer.

Chapter 36 Exploring Advanced AutoLISP Topics 1087

In the first example, you define a function that is guaranteed to fail. You can type this code in the Visual LISP editor and load it:

;;;The function below will produce an error

;;;because strcat concatenates strings. (defun Error-Prone-Code ()

(strcat “This is will never print the number: “ 1)

)

Before you continue, you need to choose Debug Break on Error. When you select this menu option, Visual LISP enables you to jump to the error in your source code automatically.

Using the function Error-Prone-Code produces an error because strcat concatenates strings, as the following demonstrates. You can type this at the console:

(Error-Prone-Code)

Visual LISP responds with the following:

;error: bad argument type: stringp 1

After you receive the error, choose Last Break on the Debug toolbar. Visual LISP places you in the line of the code where the error occurred.

How can you find the source of the problem in a routine? To answer this question, you often need to perform some detective work. Visual LISP provides a wealth of debugging tools, which you can leverage when you encounter an unplanned “enhancement” or bug.

STEPS: Finding the Last Break

1.Open a drawing in AutoCAD using the acad.dwt template.

2.Start Visual LISP and start a new file.

3.In the Visual LISP Editor, type the following:

;;;The function add-3-numbers will not produce

;;;an error if all of its arguments are numbers. (defun add-3-numbers (num1 num2 num3)

(+ num1 num2 num3)

)

4.Choose Load Active Edit Window on the Tools toolbar.

5.In the Console window, type the following:

(add-3-numbers 1 2 3)

Visual LISP returns 6.

6.However, if you substitute a string for one of its arguments, an error occurs. Type the following in the Console window:

(add-3-numbers 1 “a” 3)

Visual LISP returns the following:

error: bad argument type: numberp: “a”

1088 Part VII Programming AutoCAD

7.Choose Debug Break on Error. Look again to confirm that Break on Error is checked.

8.Choose Last Break on the Debug toolbar. Visual LISP places you in the function add-3- numbers, with (+ num1 num2 num3) as the cause of the error.

To continue after turning on Break on Error, click Reset on the Debug toolbar.

Using the Error Trace window

Any time an error occurs, you can use the last break along with the Visual LISP Error trace window. To display the Error trace window, choose View Error Trace to see an error list, as shown in Figure 36-1.

Figure 36-1: To help you debug your code, the Error Trace window displays every function call before the error, as well as where the error occurred.

The item numbered <5> in Figure 36-1 is what you entered in the Console. The item labeled [2] (+ 1 “a” 3) is where the error occurred, while <1> :ERROR-BREAK signaled the function’s termination because of the error.

Visual LISP enables you to find the source position (the line of code that produced the error) and the call source point (the position in your code where the function was called) for the error. To find them, right-click the third item in the Error Trace window. From the shortcut menu, you can choose either Source Position or Call Source Point. Jumping to the position in your code where the function was called is extremely handy when debugging a large application.

Working with break points

Sometimes when you choose Last Break to highlight an error, you find that a great deal of code is highlighted, which is not very helpful for finding the error. This is where break points come in handy. Break points are locations in your code, specified by you, that signal Visual LISP to stop and wait until you’re ready to continue processing the code. Visual LISP executes your code and stops at each break point to wait for your instructions. Break points enable you to break down your code into smaller pieces and watch what’s going on piece-by-piece so that you can more easily find the error.

To place a break point, position your cursor to the left of an opening parenthesis or to the right of a closing parenthesis, and press F9. Visual LISP places a temporary red mark in your code.

After creating your break points, you need to reload your code to tell Visual LISP about your break points. Then try to execute the function again. Notice that the Debug toolbar buttons become available. Visual LISP executes your code up to the first break point.

Chapter 36 Exploring Advanced AutoLISP Topics 1089

You can now use the Debug toolbar to help find your error. The first three buttons offer three different ways to move through your code:

Step Into: Use this button to “step in” one expression from the innermost nested expression (from opening to closing parentheses) to the outermost, one at a time.

Step Over: Use this button to ignore the nested expressions; that is, step over the highlighted expression.

Step Out: Use this button to move to the end of the function. You can ignore the entire function that is being debugged.

To remove break points, choose View Breakpoints Window. The Breakpoints dialog box opens, as shown in Figure 36-2.

Figure 36-2: Use the Breakpoints dialog box to remove, show, or disable break points.

In the Breakpoints dialog box, you can do the following:

On the

CD-ROM

Delete all break points by clicking Delete all.

Delete individual break points by choosing a break point and clicking Delete.

Jump to a break point by choosing a break point and clicking Show. Visual LISP places a blinking cursor at the break point.

Edit a break point by choosing a break point and clicking Edit. Visual LISP then lets you disable, delete, or show the break point.

The file used in the following exercise on working with break points, ab36-a.lsp, is in the Drawings folder on the CD-ROM.

STEPS: Working with Break Points

1.Open AutoCAD and start a drawing using any template.

2.Start the Visual LISP Editor. Open ab36-a.lsp from the CD-ROM. Choose File Save As and save it as ab36-01.lsp in the AutoCAD 2006\Support folder or in another folder that you’ve added to the support file search path.

1090 Part VII Programming AutoCAD

3.Choose Load Active Edit Window from the Tools toolbar.

4.Read through the code. This routine creates a vertical list of numbers. The new FOREACH function steps through each item in a list. The comments explain why it contains a bug.

5.If you didn’t do the previous exercise, choose Debug Break on Error. (Don’t do anything if the Break on Error item is already checked.)

6.Type the following in the Console window:

(list-beautify-with-bug ‘(1 2 3 4 5 ))

The Console returns the following:

(1

; error: bad argument type: FILE 1

7.Choose Last Break on the Debug toolbar to jump to the error in the source code.

8.To place a break point in the code, place the cursor after (princ (chr 40)). Press F9. Visual LISP marks the break with a red box.

9.Place another break point after (princ (car aList)). Finally, place a third break point after the closing parenthesis on the line that follows (princ item 1). Your code should look like Figure 36-3.

Figure 36-3: Your code should look like this after you place three break points.

10.After the code produces an error, you need to reset. Click Reset on the Debug toolbar.

11.Click the Visual LISP Editor window and reload the function into Visual LISP. (Choose Load Active Edit Window on the Tools toolbar.)

12.Type the same expression that produced the error in the Console:

(list-beautify-with-bug ‘(1 2 3 4 5 ))

Chapter 36 Exploring Advanced AutoLISP Topics 1091

13.Visual LISP highlights the expression (princ (chr 40)). Choose Step Into on the Debug toolbar. Visual LISP highlights (princ (car aList)).

14.Choose Step Into. Now only (car aList)) is highlighted.

15.Choose Step Into until you reach the error, (princ item 1).

16.Edit (princ item 1) so that it reads (princ item).

17.Click Reset again.

18.Choose Debug Clear All Breakpoints. Click Yes to confirm.

19.Activate the Editor window and reload the function.

20.In the Console, type (list-beautify-with-bug '(1 2 3 4 5 )) .

21.Activate the Editor window and save the file.

On the

If you have difficulty in fixing this bug, you can find the “repaired” version in the Results

CD-ROM

folder on the CD-ROM, with the file name list-beautify-without-bug.lsp.

Using the Watch window

The Watch window enables you to examine expressions and variable values as they’re being evaluated. To watch a variable, select it in the code and choose Debug Add Watch,

or press Ctrl+W. Visual LISP opens the Watch window listing the expression or variable and displaying its value after an equal sign. If you select an expression or nothing is selected and you press Ctrl+W, the Add Watch dialog box opens. If it is not already displayed, enter the expression that you want to watch and click OK. For example, if you add a watch for (princ (chr 40)), the Watch window displays (PRINC (CHR 40)) = “(“ because (princ (chr 40)) is another way of telling AutoLISP to print a left parenthesis. After the Watch window is open, you can add expressions or variables by selecting them and choosing Add Watch on the Watch window toolbar.

If you have a routine with arguments, you can execute the function with various arguments and see the results on your watched expressions and variables in the Watch window.

Furthermore, you can add any expression to the Watch window and alter the contents of any variable while debugging a function.

On the

The file used in the following exercise on using the Watch window, ab36-01.lsp, is in the

CD-ROM

Results folder on the CD-ROM.

STEPS: Using the Watch Window

1.Open AutoCAD and start a drawing using the acad.dwt template.

2.Start the Visual LISP Editor. Open ab36-01.lsp from the Results folder of the CD-ROM. If you did the previous exercise, you can open it from your AutoCAD Bible folder.

3.Select the expression (princ (chr 40)). Choose Debug Add Watch. Click OK in the Add Watch dialog box. Visual LISP opens the Watch window and displays the expression, as well as its value.

4.To add a variable to the Watch window, select item to the right of the FOREACH function and choose Add Watch in the Watch window. The Watch window should

look like Figure 36-4.

1092 Part VII Programming AutoCAD

Figure 36-4: The Watch window shows an expression and a variable, and displays their values.

5.Add a break point after (princ item) by placing the cursor there and pressing F9.

6.Choose Load Active Edit Window from the Tools toolbar.

7.To examine the Watch window display as the function list-beautify-with-bug is evaluated, type the following in the Visual LISP Console:

(list-beautify-with-bug ‘(1 2 3 4 5 ))

8.The (princ item) expression should be highlighted. Choose Step Into on the Debug toolbar. The item variable in the Watch window should now display its current value. Continue to click Step Into, watching the value of item increase in the Watch window each time you step through the cycle.

9.In the Visual LISP console, type aList after the last prompt. This is a variable argument that is used in the function list-beautify-with-bug. The Console returns:

(1 2 3 4 5)

As you can see, you can interrogate any variable while debugging the function.

10.To add a new expression to the Watch window, select the variable aList and press Ctrl+W.

11.To change the value of aList, return to the Console window and type the following:

(setq aList (list 6 7 8 9 10))

The Console responds as follows:

(6 7 8 9 10)

Notice the change in the Watch window.

12.To remove the break point, choose View Breakpoints Window to open the Breakpoints dialog box. Click Delete All to delete the break point. Visual LISP automatically closes the dialog box.

13.Click Reset on the Debug toolbar.

14.Close the Watch window and the Visual LISP IDE without saving the file.

As you can see, Visual LISP is not just an editor; it’s a full-featured, integrated development environment for AutoLISP.

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