Академический Документы
Профессиональный Документы
Культура Документы
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: "))
it
(setq textloc (getpoint "
Pick Text Location")) ; asking user to click text location
(command "_leader" p textloc "" p "")
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.
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.
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))
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)
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.
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)
(+ a b)
How can we draw polygon by defining the area? I did some searches and find this
formula.
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 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?
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 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 "
How are you doing so far? I would like to know what do you think after you have done
the exercises.