AutoLISP.5: User input functions
Print This Post
![]() |
Author | Orhan Toker |
| Profession | Architect M.Sc. | |
| Autodesk Authorized Consultant | ||
| Database Specialist | ||
Hello,
In this lesson, we will learn about methods of receiving input from the user. Commands that we use while drawing in AutoCAD prompts us to input various data. For example, while drawing a circle AutoCAD will ask you to input center point and radius. While developing applications in AutoCAD, you will also ask the user to input data many times. In the table below, you will find different data types that may be asked from the user to input and the corresponding function used to collect this data.

Now, we will shortly examine these functions by small examples.
(getint) and (getreal) functions
These functions are used when real or integer number input is wanted from user. For example,
(setq a (getint "\nEnter the number, square of which you want to calculate?:"))
(princ (* a a))
(setq nrOfFloors (getint "\nHow many number of floors will you copy?:"))
(setq elev (getreal "\nPlease enter the elevation of 1st floor:"))
As you can see, we can also enter an explanation message to ask for data input from the user together with the (getint) and (getreal) functions.
Note: In AutoLISP programs, message that you will write for the user to prompt for data entry is very important as it will guide the user. If you are asking from the user to enter standard AutoCAD data like point, angle, distance etc., then your message should also be in the AutoCAD message standard. It is also preferred for all of your data entry messages in a certain fashion. “\n” code that you can place in front of your messaged enables the message to appear in a new line.
Angle input with (getangle)
You use this function when you want the user to enter angle. (getangle) function uses two optional arguments:
(getangle [point] [message])
If you enter point argument, then angle entry will be made by taking the point that you enter is taken as reference. This is not a very common method. I will give examples to both of them.
(setq angVal (getangle "\nEnter angle:"))
Enters the angle entered by the user to the angle variable in radians. To convert radians into degrees:
(setq angVal (getangle (getvar "LASTPOINT")) "\nEnter direction: "))
;; Convert it to degrees
(setq angVal (* 180.0 (/ angVal pi)))
Second example ask for a direction assignment from the last point.
Get the other corner by (getcorner)
In some cases, we may ask the user to input a frame. For example, you are writing your own rectangle routine or you wanted the selection window to be determined from the beginning. In such cases, (getcorner) is very useful. Let’s write our own rectangle routine:

Code.1 rectangle.lsp file.
Enter this code in VisualLISP editor, which you can open with AutoCAD vlisp command, and save it as rectangle.lsp. Then, load the project into AutoCAD by pressing CTRL+ALT+E buttons. When you write rect in the command line, then your routine that draws a rectangle works. I will explain the car, cadr and list functions that I used in this example, in the following lessons.
Here, the important point is that you should pay attention to the way we are using (getpoint) and (getcorner) functions.
Note: If you examine carefully, you will notice that I have used the taken both corners of the rectangle from the user inside one (setq) value setting function. Thus, you can use (setq) value setting function to set the value of more than one variable at a time. The only thing that matters is that you should be careful about aligning.
Besides, “c:” prefix that I used wwhile defining the c:rect function will enable it to work as a AutoCAD command function. If you don’t put this prefix, then you have to call it from the command line as if you are calling the other lisp functions.
Command:(rect)
In the definition of function, variables that I defined after the (/) symbol shows that they are defined as local only inside the function.
Getting distance by (getdist)
When you use (getdist) function, user can enter distance either directly, or by selecting two points or by taking reference from a certain point. After the entry of first point, (getdist) function pauses for the user to enter another point by any point picking methods of AutoCAD. During this pause, there will be a line drawn from 1st point to the end of your cursor.
(setq distVal (getdist "\nEnter distance:"))
Prompting user to enter a keyword by (getkword)
In some certain cases, in order to choose options, you may ask the user to enter keywords through command line. As an example, AutoCAD CIRCLE command can be given.
Command: circle
Specify center point for circle or [3P /2P /Ttr (tan tan radius)]:
In the example given above, each one of [3P/ 2P/ Ttr (tan tan radius)]: are optional keywords. Let’s make our own example. A code which accepts “Yes” or “No” answer as input for the question we ask:
(initget 1 "Yes No")
(setq answer (getkword "\nDo you want the command to erase all of the entities in the drawing? [Yes No] : "))
The above code will accept only the answers of “Yes” and “No” from the users. (initget) function is used for initialization of data entry prior to the execution of the command. I will explain this function in a more detailed way in our following lessons.
Prompting user to pick a point (getpoint)
If you want the user to enter a single point, then you can use (getpoint) function. Point answer that will be given by the user may be any of AutoCAD’s point picking methods.
(setq pt1 (getpoint "\nClick on the first corner of the rectangle:"))
In the above rectangle.lsp example, we have already used this function.
Prompting user to enter a string (getstring)
It is a function that you will use frequently, especially for routines that works for labeling or location names. It is used as:
(getstring [cr] [message])
cr argument determines if ENTER or SPACE character is accepted or not. İf it is nil then you can not enter space character
(setq locationname (getstring "\nEnter the name of location:"))
(setq locationdescription (getstring T "\nEnter the description of the location:"))
As it can be seen in the example, we are not allowing space in the name of the location; however we are allowing space in the description of the location.
Some exercises that you may work on by yourself:
• Imitate the AutoCAD line command by using (getpoint) function.
• Draw a polar coordinate system by using (getdist) and (getangle) functions. @distance
• Draw a center/radius circle by using (getpoint) and (getdist) functions
See you next lesson…
You can download rectangle.lsp file by pressing on link.
Similar Posts
Rate this article:


February 1st, 2007 09:59
Is there any lisp command with the help of which we can write the area of a room by just picking the coordinates or diagonally picking the corners of the room and the area is written in the room itself
February 1st, 2007 16:08
Hello,
We have an ongoing project that does what you say. I’m using .NET and C# for this project. I’m noting your comment. Keep reading us. Me or Mr.Kocyigit will try to publish such an utilty.
February 22nd, 2007 18:51
Mr Toker,
I red this lesson with interest. But it is not exactly what I am looking for. I am a surveyour in Canada and I make technical description (in french); I would like to know how do I put under or above a line in drawing the distance and azimuth that appear at prompt when using the distance command. Is that routine exist or do I have to write it. Unfortunalety I am not familiar with Lisp. Thank you very much. Yves Sice
May 4th, 2007 09:34
how rea ascii file or text file then print in the darwing autodesk map2004?
July 20th, 2007 16:05
I want to ask for user inputs in AutoCAD by either enter a distance or picking two points.
I know how to do both methods but I don’t know how to put them in the same command
Ask for distance:
(SETQ dis (GETREAL “\nEnter width of parking space:”))
Two points:
(SETQ PT1 (GETPOINT “\nGet First Point”))
(SETQ PT2 (GETPOINT “\nGet Next Point”))
(setq dis (distance pt1 pt2))
July 21st, 2007 09:39
I think you mean
(setq dis (getdist (getpoint “\nSpecify first point:”) “\nSecond point:”))
November 11th, 2007 11:03
hi.. i have other methode to draw many entiers from excel….easy & good..
February 21st, 2008 18:30
Hello Orhan,
Can you please explain to me why I am unable to to assign a \ (backslash) in a SETQ? Is there a way? I have tried to set the code below but I keep on getting an error.
(Setq X “S:\Transfer\Templates\x.dwg”)
Thanks,
Hugo
February 22nd, 2008 08:46
Hello Hugo,
You should use double ‘\\’ or ‘/’ for directory separator.
Regards
February 22nd, 2008 08:47
e.g.
(Setq X “S:\\Transfer\\Templates\\x.dwg”)
(Setq X “S:/Transfer/Templates/x.dwg”)