Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

Creating a 3D Wedge Program

Let's create a new program that draws a wedge shape based on similar information given for the 3d box. Figure 3.9 shows how you might draw a wedge based on the rectangle drawn from the box program.

Figure 3.9: A program to draw a 3D wedge

The procedure outlined in Figure 3.9 was converted into the following AutoLISP program.

61

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

(defun C:3DWEDGE (/ pt1 pt2 pt3 pt4 h) (getinfo)

(setq h (getreal "Enter height of wedge: ")) (procinfo)

(output)

(command "3dface" pt1 pt4 ".xy" pt4 h ".xy" pt1 h pt2 pt3 "" "3dface" pt1 pt2 ".xy" pt1 h pt1 ""

"copy" "L" "" pt1 pt4

)

)

Try the following exercise to see how it works.

1.Exit AutoCAD temporarily either by using the AutoCAD Shell command or by using the End command to exit AutoCAD entirely.

2.Open the box1.lsp file again and add the program listed above to the file.

3.Return to the Chapt3 AutoCAD file.

4.Load Box1.lsp again. If you had to Exit AutoCAD to edit Box1.lsp, Load Rxy.lsp also.

5.Erase the box currently on the screen.

6.Start the C:3DWEDGE program by entering 3dwedge at the command prompt.

7.At the Pick first corner prompt, pick a point at the coordinate 2,3.

8.At the Pick opposite corner prompt, pick a point so the wedge's base is 7 units wide by 5 units wide.

9.At the Enter height prompt, enter 6. A wedge appears.

10.Issue the hide command. The wedge appears as a solid object.

This Wedge program is nearly identical to the C:3DBOX program with some changes to the last expression.

(command "3dface" pt1 pt4 ".xy" pt4 h ".xy" pt1 h pt2 pt3 "" "3dface" pt1 pt2 ".xy" pt1 h pt1 ""

"copy" "L" "" pt1 pt3

)

62

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

The first line of this expression draws a 3d face on the top and vertical face of the wedge. It does this by using AutoCAD's .xy point filter to locate points in the Z coordinate. The second line draws a 3dface on the triangular side of the wedge again using the .xy point filter. The last line copies the triangular face to the opposite side of the wedge. Figure 3.10 shows the entire process.

Figure 3.10: Box1.lsp with the C:3DWEDGE program added

Now you have three programs which use as their basic building blocks the three functions derived from your original Box program. You also have a function, rxy, which can be used independently to dynamically display relative x y coordinates.

63

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Making Your Code More Readable

The box program is quite short and simple. As your programs grow in size and complexity, however, you will find that it becomes more and more difficult to read. Breaking the program down into modules help to clarify your code. There are other steps you can take to help keep your code readable.

Using Prettyprint

The term Prettyprint is used to describe a way to format your code to make it readable. Indents are used to offset portions of code to help the code's readability. Figure 3.12 shows three examples of the C:3DBOX program. The first example is arranged randomly. The second lists each expression as a line while the third makes use of prettyprint to organize the code visually.

(defun C:3DBOX (/ pt1 pt2 pt3 pt4 h) (getinfo)

(setq h (getreal "Enter height of box: "))(procinfo)(output) (command "change" "Last" "" "Properties" "thickness" h

"" "3dface" pt1 pt2 pt3 pt4 "" "3dface" ".xy" pt1 h ".xy" pt2 h ".xy" pt3 h ".xy" pt4 h ""))

(defun C:3DBOX (/ pt1 pt2 pt3 pt4 h) (getinfo)

(setq h (getreal "Enter height of box: ")) (procinfo)

(output)

(command "change" "Last" "" "Properties" "thickness" h "" "3dface" pt1 pt2 pt3 pt4 ""

"3dface" ".xy" pt1 h ".xy" pt2 h ".xy" pt3 h ".xy" pt4 h ""))

Figure 3.12 (continued on next page): Three ways to format the 3dbox program code

64

Copyright © 2001 George Omura,,World rights reserved