Monday
AutoLISPAutoLISP.5: User input functions
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.
Viewed 8,930 times so far... This week: 58 Today: 2 Latest: 5 July 2009, 1:04Post Tags:
Related Posts
Popular
- Hip tip: Increasing view resolution with VIEWRES system variable
07/03/2009 04:39 am
7 Comments - Sirince – IZMIR
07/02/2009 11:02 am
1 Comment - Visual LISP: Programming user interfaces with OpenDCL
07/01/2009 10:15 pm
10 Comments - 3D Modeling 7: EXTRUDE
06/30/2009 02:14 pm
10 Comments - Ask DailyAutoCAD: Which Notebook to Buy?
06/30/2009 12:17 pm
1 Comment
Featured Articles
- AutoCAD 2009 Update 3 Released
- Speed Up As AutoCAD 2009 Ribbon Menu Helps You!
- MEP Analysis Extension for AutoCAD MEP extended to March 31, 2009
- Recap of AutoCAD-related technologies on Autodesk Labs
- Technology Preview of MEP Analysis Extension extended to 12/31/08
- Autodesk Labs: Forward and Backward utility for AutoCAD
|

Feb 1, 2007
Reply
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
Feb 1, 2007
Reply
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.
Feb 22, 2007
Reply
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 4, 2007
Reply
how rea ascii file or text file then print in the darwing autodesk map2004?
Jul 20, 2007
Reply
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))
Jul 21, 2007
Reply
I think you mean
(setq dis (getdist (getpoint “\nSpecify first point:”) “\nSecond point:”))
Nov 11, 2007
Reply
hi.. i have other methode to draw many entiers from excel….easy & good..
Feb 21, 2008
Reply
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
Feb 22, 2008
Reply
Hello Hugo,
You should use double ‘\\’ or ‘/’ for directory separator.
Regards
Feb 22, 2008
Reply
e.g.
(Setq X “S:\\Transfer\\Templates\\x.dwg”)
(Setq X “S:/Transfer/Templates/x.dwg”)
Mar 25, 2009
Reply
Hello,
Please tell me if it is possible to input the input by way of Excel sheet Or Word into the autolisp?
Best regards
Anil
Jun 8, 2009
Reply
(vl-load-com)
;;;— Main function
(defun getCells (listOfCells fileName sheetName /
newCellList cellList myApp sysDrive
myWBooks myWBook sht mySheets
sheetList cnt shtName mySht
mySheet myAddress myCells
)
;;;— Check for valid cells and split them into separate list < (col row) >
;;;— Create an empty list to hold the cell data
(setq newCellList (list))
;;;— Cycle through each cell location
(foreach a listOfCells
;;;— Set up a counter
(setq cnt 1)
;;;— Get the first character of the cell location
(setq chk (substr a cnt 1))
;;;— While the character is a letter in the alphabet
(while (and (> (ascii chk) 64) (< (ascii chk) 123))
;;;— Increment the counter to get the next character
(setq cnt (+ cnt 1))
;;;— Get the next character
(setq chk (substr a cnt 1))
)
;;;— If the cnt is greater than the length of the cell’s address then
;;; there must not be a number in the address. Make sure the address
;;; had a number in it…
(if (<= cnt (strlen a))
(progn
;;;— Save the col which should be a letter
(setq col (substr a 1 (- cnt 1)))
;;;— Save the row which should be a number represented as a string
(setq row (substr a cnt))
;;;— Convert the string to a number and make sure it is valid…
(if (> (atoi row) 0)
;;;— If it is valid then add the col and row as a list to the newcell list
(setq newCellList (append newCellList (list (list col row))))
;;;— Else alert the user that the cell’s address is bad
(alert
(strcat “Invalid cell number – ” a ” will be skipped.”)
)
)
)
;;;— Else there must not be a number in the address…alert the user
(alert
(strcat “Invalid cell number – ” a ” will be skipped.”)
)
)
)
;;;— Make sure a valid cell list was created
(if newCellList
(progn
;;;— Make sure the XL file exist
(if (findfile fileName)
(progn
;;;— Get the system drive <We will need this to locate the EXCEL OBJECT LIBRARY>
(setq sysDrive (getenv “systemdrive”))
;;;— If the excel object library is not found…load it
(if (null Library)
(progn
;;;— Find out which version we should use by looking in the default install locations
;;; for EXCEL’s Object Library. If it is not found you may need to add the path to
;;; your location for the excel object library.
(setq Library
(cond
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office\\Excel8.olb”
)
)
)
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office\\Excel9.olb”
)
)
)
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office\\Excel10.olb”
)
)
)
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office\\Excel.exe”
)
)
)
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office10\\Excel.exe”
)
)
)
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office11\\Excel.exe”
)
)
)
((findfile
(strcat
sysDrive
“\\Program Files\\Microsoft Office\\Office11\\XL5EN32.OLB”
)
)
)
)
)
;;;— If the library was found…
(if Library
(progn
;;;— Import the library…
(vlax-import-type-library
:tlb-filename Library
:methods-prefix “JXCL-”
:properties-prefix “JXCL-”
:constants-prefix “JXCL-”
)
)
;;;— Else alert the user of failure…
(alert “Excel Object Library was not found!”)
)
)
)
;;;— If an excel application is not loaded, proceed…
(if (null myApp)
(progn
;;;— If Excel executes correctly…
(if (setq myapp
(vlax-get-or-create-object “Excel.Application”)
)
(progn
;;;— Open the workbook
(vlax-invoke-method
(vlax-get-property myapp ‘WorkBooks)
‘Open
fileName
)
;;;— Set it to invisible mode
(vla-put-visible myApp 0)
;;;— Get the workbooks object
(setq myWBooks (vlax-get myApp “Workbooks”))
;;;— Open the excel file
(setq myWBook (vla-open myWBooks fileName))
;;;;— Get the sheets object
(setq mySheets (vlax-get myWBook “Sheets”))
;;;— Get a list of the sheet names
(setq shtCnt (vla-get-count mySheets))
(setq sheetList (list))
(setq cnt 1)
(while (<= cnt shtCnt)
(setq sht (JXCL-get-item mySheets cnt))
(setq shtName (vla-get-name sht))
(setq sheetList (append sheetList
(list (strcase shtName))
)
)
(setq cnt (+ cnt 1))
)
;;;— Make sure the sheet name exist…
(if (member (strcase sheetName) sheetList)
(progn
;;;— Get the worksheet…
(setq mySht (vlax-get-property
mySheets
‘Item
sheetName
)
)
;;;— Make the selected worksheet active
(vlax-invoke-method mysht “Activate”)
;;;— Build an empty list to hold the values of the cells
(setq cellList (list))
;;;— Cycle through the list of cells
(foreach a newCellList
;;;— Get the col and row
(setq col (car a))
(setq row (cadr a))
;;;— Add the address and value to the cell list
(setq cellList
(append
cellList
;;;— Build a list containing the cell’s col:row and value
(list
(list
;;;— First add the col and row as a string
(strcat col row)
;;;— Next, Get the value of the cell
(vlax-variant-value
(JXCL-get-value
(vlax-variant-value
(JXCL-get-item
(JXCL-get-cells
(JXCL-get-ActiveSheet myApp)
)
(vlax-make-variant row)
(vlax-make-variant col)
)
)
)
)
)
)
)
)
)
)
;;;— Else the sheet name was not in the workbook…
(alert (strcat “The sheet – ”
sheetName
” could not be found!”
)
)
)
;;;— Shut Excel down
(cond
(
(not (vlax-object-released-p myApp))
(vlax-invoke-method myApp ‘QUIT)
(vlax-release-object myApp)
)
)
)
(alert “Could not start an EXCEL application! Aborting!”
)
)
)
(alert
“An Excel application must be open. Close it and try again!”
)
)
)
(alert “Could not find the XL file name.”)
)
)
)
;;;— Finally, return the list containing the row,col and values of the cells
cellList
)
This function will help u import data from particular cells of an excel sheet that u mention.