I hope you are having fun with our AutoLISP exercises. Last time, we were introduced to use AutoLISP variables and asking users for input. This time we will learn about asking for more user input, then using the input in mathematical equation. The calculation result result will be used to draw an object.
Re: LISP routine for Area Command Yes, the field command will do the trick, Ideally looking for a routine that would prompt to draw a polyline or a rectangle, then calculate the area and place the field / data in the center. 1 move instead of 4, since I have many objects to calculate. Re: LISP Routine that works with AREA command Here's a routine I use that adds the square footage as Text. It wouldn't be too difficult to modify it to use an attributed block though.
Our challenge now is to create a program that can draw a regular polygon by defining number of sides and the area. Interesting isn’t it?
writing Calculation in AutoLISP
Writing calculation in AutoLISP is quite different. But should not be difficult to understand.
If we want to calculate 2+5, then we write:
(+ 2 5)
Let us try it in AutoCAD command line. Type in AutoCAD the line above. Yes you can use AutoLISP command in AutoCAD. You need to type it exactly like above.
Now let’s try another one. Type each line then press [enter].
(setq a 2)
(setq b 5)
(+ a b)
What we did is set variable a value to 2, b to 5, then calculate a+b.
(setq c (+ a b)
![Autolisp autocad Autolisp autocad](/uploads/1/2/6/2/126242633/238990927.png)
This one means c = a + b.
Not so difficult, isn’t it? Refer to other calculation function in AfraLISP site here. Now let us see the polygon formula.
Drawing Polygon Method
How can we draw polygon by defining the area? I did some searches and find this formula.
source: Math Open Reference page.
If we know the area and number of sides, we can calculate the apothem. What is apothem? See image below.
How can we draw a polygon when we know the apothem and number of sides? Using polygon tool of course. We create it using circumscribed method.
Calculating Apothem Length
We need to change the formula to get the apothem like below:
Functions in Our Formula
We will use these function in our formula.
- square root can be written as (sqrt a),
- multiplication as (* a b),
- division as (/ a b),
- cosine of an angle as (cos ang),
- sine of an angle as (sin ang).
With a, b, and ang are variables.
The bad news is AutoLISP doesn’t have tan function. But there is work around. Tangen is sin/cos. Tan (pi/N) can be written as:
(setq ang (/ pi N))
(/ (sin ang) (cos ang))
Pi is built in variable, so we will just use it.
Besides the sequence, you should be familiar with the command. If you want to try writing the equation by yourself, go ahead. It can be a good exercise to get familiar with mathematical function in AutoLISP. You can find the equation below later.
The complete equation can be written. I use variable a for area and n for number of sides. I also use apt to keep the equation value (the apothem length), and ang to keep (pi/n).
(setq ang (/ pi n))
(setq apt (sqrt (/ (/ a (/ (sin ang) (cos ang))) n)))
Or you can write in a single line, which looks more confusing.
(setq apt (sqrt (/ (/ a (/ (sin(/ pi n)) (cos(/ pi n)))) n)))
Asking For User Input
We already use getpoint to ask user to pick a point. Or they can type the coordinate. Now we need three user input: number of sides, area, and center point.
- Number of sides is an integer. You don’t accept 2.5 as number of sides, don’t you? We can use GETINT to ask for integer.
- Area can have decimal numbers, so it is a real number. We can use GETREAL for this purpose.
- You already use GETPOINT to ask for a point right?
Let us try. In AutoCAD command line, type
(GETINT)
type integer number. It should return the number you entered. Try again. This time type a decimal number.Let’s say 3.2. What will AutoCAD say?
Command: (getint)
3.2
3.2
Requires an integer value.
Using the right user input function will also reduce probability of errors.
Writing the Complete Code
Now we can write the complete code.
- You need to ask the user the number of polygon sides, the expected polygon area.
- Then you calculate the value.
- You need to ask one more input: the polygon center.
- Finally you can write the polygon command.
Now we have complete data, what we will put in our program. I strongly suggest you to try writing the program first. You can check and compare the code later.
Below is the complete code I created. If you have problem, you can copy the code below.
; This LISP will create regular polygon by defining polygon area and number of sides
; Created by Edwin Prakoso
; Website: https://www.cad-notes.com
(defun c:pba (/ a n apt ptloc)
(setq n (getint '
Number of Polygon Sides: '))
(setq a (getreal '
Expected Polygon Area: '))
(setq ang (/ pi n))
(setq apt (sqrt (/ (/ a (/ (sin ang) (cos ang))) n))) ;calculating apothem for circumscribed polygon
(setq ptloc (getpoint '
Pick Location: '))
(command '_POLYGON' n ptloc 'C' apt)
)
How are you doing so far? I would like to know what do you think after you have done the exercises.
Next, we are going to create an AutoLISP program to label coordinate.
AutoCAD, from AutoDesk Software, is one of the most common software programs in use today in the engineering industry. It is used to create blueprints, schematics and other engineering drawings and 3D models. If you use AutoCAD every day, and find there are routines that you would like to automate, AutoCAD offers a way to do this: LISP routines. LISP, the name of which is derived from LISt Processing, is a computer programming language that is interfaced with AutoCAD using VLISP, or Visual LISP. You can use the VLISP interface to write and run LISP routines in AutoCAD.
Launch the VLISP window. From the AutoCAD window, in the 'Tools' menu, select 'AutoLISP' and 'Visual LISP Editor.' In the VLISP window, you can type LISP commands in the console window.
Launch the VLISP text editor by selecting 'File', then 'New.' The text editor enables you to write and save complete LISP programs, or routines.
Begin your VLISP routine. To create a 'Hello, world' application to work at the AutoCAD command prompt, enter the following code:
(defun c:hello () (prompt 'Hello, world!') (princ) )
The first line defines the 'Hello, world' function. The rest of the program displays the string at the command prompt.
Save the routine by choosing 'Save As...' from the 'File' menu, then save the file as 'hello.lsp'.
Load the program, and from the 'Tools' menu, select 'Load Text in Editor.' The VLISP window should display a confirmation message. If it displays an error code, fix the listed errors in your code and save and load the code again.
Switch to your AutoCAD window. From the 'Tools' menu, select 'AutoLISP' and 'Load.' Select the hello.lsp file, and click 'Load.' The file is now ready to run.
Type 'hello' (without quotes) at the AutoCAD command prompt. You will immediately see the string 'Hello, world' displayed at the prompt.
Tips
LISP is a programming language, and the best way to learn any programming language is by practicing its use.
- 'Harnessing AutoCAD 2006'; Thomas Stellman & G.V. Krishnan; 2006