Creating a Point List from Symbols with autolisp

Many times you will want to create a point list but the coordinate values are contained in AutoLISP symbols such as X, Y, and Z. What happens if we type …

Command: (setq PT4 ‘(X Y Z))
(X Y Z)

But we didn’t want an un-evaluated list (X Y Z), we wanted a list of the values of X, Y, and Z. What if we type:

Command: (setq PT4 (X Y Z))
ERROR: null FUNCTION

(X Y Z)
(SETQ PT4 (X Y Z))

To solve our dilemma, the (list) function enables us to create a list from a series of expressions.

  • The (list) function works better than (quote). While (quote) requires a single list as an argument, (list) can have as many items as needed to add to the list.

Explanation: the (list) function

(list expression1…expressionn)

  • The (list) function returns a single list whose elements are the value of each of the expressions. We have now added the needed function to the list of arguments.

Example

Command: (setq PT4 (list X Y Z))
(3.0 4.0 0.0)

Command:

PRACTICE

Create a program that will prompt the user for coordinates for the X, Y, and Z values of a center point for a circle. Also prompt the user for the radius of the circle. Then create the center point using the values of X, Y, and Z as its respective coordinates. Then draw the circle. Estimated time for completion: 10 minutes.

Steps to the Solution

See also  Executing AutoCAD Commands through AutoLISP

1. Prompt the user for the real number for X, real number for Y, and real number for Z using the setq function and the getreal function.

2. Create and store a list of the values being stored by the symbols X, Y, and Z using the setq function and the list function.

3. Prompt the user for the radius of the circle using the getdist function and store it using the setq function.

4. Use the command function to draw the circle.

Solution

(defun c:CIR ()
(setq X (getreal “\nX value for center point: “))
(setq Y (getreal “\nY value for center point: “))
(setq Z (getreal “\nZ value for center point: “))
(setq RAD (getdist “\nCircle radius: “))
(setq PT (list X Y Z))
(command “circle” PT RAD)
(redraw)
(princ)
)

  • A complete solution to this exercise is on your class disk as CIR-A.LSP.
Back to top button