Вы находитесь на странице: 1из 11

http://www.cad-notes.

com/contents/autolisp-articles/
AutoLISP exercise: creating program to label coordinate
By Edwin Prakoso, published on December 6, 2010
In this exercise, we will continue our AutoLISP tutorial. This time we are going to use
lists and strings. We have learned how to use mathematic equation in AutoLISP to
calculate a value. This time we will work with strings.
Let us see what are we going to create. We are going to create a program to place
coordinate label for a point automatically. See the program and try it first if you want.
Program Basic

We are going to use leader tool to label it. It will require us to define two points: point to
label, and where we want to place the label. We already learned how to ask for user
input for a point. So you should already familiar with this code:
(defun c:\cnlabel ()
(setq p (getpoint "
Pick Point to Label: "))

; asking user to click point to label and save

it
(setq textloc (getpoint "
Pick Text Location")) ; asking user to click text location
(command "_leader" p textloc "" p "")

; activate label tool and

use the input


)

In visual LISP editor, you can copy or type it (I suggest you to type it for your exercise)
then click load active edit window.

After you load it, type cnlabel to activate the tool.

Using Leader tool

Lets see how the leader tool works.


1. After we activate leader tool, we need to place first point. The first point will be the point
we label.
2. Then it will ask next point. We are going to use text location (textloc variable) as second
point.
3. It will ask for another point for the leader. If we only want one segment, we can press
[enter]. To simulate [enter] in AutoLISP, we simply use .
4. Next, we type the text. In AutoLISP program we can use point coordinate we get from
user.
5. Leader will ask for another input for the next line. We dont want to add next line. To
finish it, we can press [enter]. Again, we simulate it using .

This line below will do what is described above.


(command "_leader" p textloc "" p "")

What if you dont want to use leader, but would like to use Multileader? Simple, use this
line:
(command "_mleader" p textloc p)

Try to use it first if you want to know how mleader works, pay attention on each step
what you should do.
The program can be used, but the result is not good enough. We cant control the text
appearance, the label will be shown similar like below.

See how many decimal numbers it has?


Getting Value from The List

Let us try the AutoLISP program. We havent define p and textloc as local variables, so
the value should be still stored even after we run the program.
Now after you run it once, type !p to see the p value. It should return something like this:
(268.782 34.178 0.0)
The value is a list. To get the x, y, and z value we can use car, cadr, and caddr.
car will pick the first value from the list. So to get x value, we can do this:
(car p)
To save the value to x, we continue the line like this.
(setq x (car p))
cadr will get the second value from the list, caddr will get the third value from the list. To
separate all the values, we can write lines as below:
(setq x (car p))
(setq y (cadr p))
(setq z (caddr p))

When we work on 2D drawings, we dont need to use z value, so you can delete the last
line. Unless you work in 3D.
Converting Real to String

We already get the x and y value, but they are still in real number. If we want to add
more text, like x=, y= then we need to convert them to string. We can convert them
to string using rtos function. Let us add it to our code.
(setq x (rtos (car p)))
(setq y (rtos (cadr p)))
Now x and y are strings. We can add more texts to those strings. In calculating real or
integer, we can use mathematic function like + or . But in strings, we use strcat to add
more text to the variable.
Let say I want it to look like this:
x = 100, y = 50
I can create it by combining x and y like this:
(setq ptcoord (strcat x= x ; y= y))
Dont forget to save it to a variable! I use ptcoord. You may change the text inside the
double quotes. You may want to use E=, N=, el= etc.
Now lets put it all together:
(defun c:cnlabel ()
(setq p (getpoint "
Pick Point to Label: "))
(setq textloc (getpoint "
Pick Text Location"))
(setq x (rtos (car p)))
(setq y (rtos (cadr p)))
(setq ptcoord (strcat "x=" x ", " "y=" y))

(command "_leader" p textloc "" ptcoord "")


(princ)
)

Tips: Seeing Dynamic Lines from Previous Point

One annoying thing about this program is we cant see dynamic line when we place the
second point. Just like when we place a leader or even a simple line.

To add the dynamic line, we simply add the variable of the first point when we ask the
second point. The variable is p, so now the line become:
(setq textloc (getpoint p
Pick Text Location))
Add it and try it again.
More Tips: Adding a Loop to Keep the Program Active

When we use line tool, the tool will be still active until we press [esc] or [enter]. Some
other tools also have the same behavior. The idea is we can keep using it and dont
have to reactivate it when we still want to use it. Because when labeling coordinate we
usually need to label several points, we can make this program to behave the same.
To do this, we can add a loop with (while). So the complete program will be like this.
(defun c:cnlabel (/

p x y ptcoord textloc)

(while ;start while


(setq p (getpoint "
Pick Point to Label: "))
(setq textloc (getpoint p "
Pick Text Location"))

(setq x (rtos (car p)))


(setq y (rtos (cadr p)))
(setq ptcoord (strcat "x=" x ", " "y=" y))
(command "_leader" p textloc "" ptcoord "")
(princ)
) ;end while
)

We will cover more about loop, but now this will do. Now try it.
Im amazed how many thing I can do with a basic knowledge of AutoLISP, and I believe
many more things can be done. I would like to know what program have you made until
this point? Please share with us!
Next, we are going to modify it a bit further: You will create AutoLISP program to label
coordinate showing Northing, Easting and elevation in multiple lines.

AutoLISP Exercise : Create Regular Polygon by Defining Area


By Edwin Prakoso, published on December 1, 2010
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.
Our challenge now is to create a program that can draw a regular polygon by defining
number of sides and the area. Interesting isnt 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 lets 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)

This one means c = a + b.


Not so difficult, isnt 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.


1. square root can be written as (sqrt a),
2. multiplication as (* a b),
3. division as (/ a b),
4. cosine of an angle as (cos ang),
5. sine of an angle as (sin ang).

With a, b, and ang are variables.


The bad news is AutoLISP doesnt 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.
1. Number of sides is an integer. You dont accept 2.5 as number of sides, dont you? We
can use GETINT to ask for integer.
2. Area can have decimal numbers, so it is a real number. We can use GETREAL for this
purpose.
3. 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.Lets say 3.2. What will AutoCAD say?
Command: (getint)
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.


1. You need to ask the user the number of polygon sides, the expected polygon area.
2. Then you calculate the value.
3. You need to ask one more input: the polygon center.
4. 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: http://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.

Вам также может понравиться