In this Article...
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
Let’s see how the leader tool works.
- After we activate leader tool, we need to place first point. The first point will be the point we label.
- Then it will ask next point. We are going to use text location (textloc variable) as second point.
- 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 “”.
- Next, we type the text. In AutoLISP program we can use point coordinate we get from user.
- Leader will ask for another input for the next line. We don’t 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 don’t 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 can’t 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 haven’t 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 don’t 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))
Don’t 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 let’s 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 can’t 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 don’t 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.
I’m 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.
Thanks for useful information.
Similar I want to get X double value in label to get direct CNC lathe diameter programming. Now your program generating only radius value.
Kindly know us how to double one axis value.
Sir, am pleasing you to teach me about AutoLISP becouse am a surveyor, i want to buy a video tips from you about how to use AutoLISP, i hope you Granted my request. Thanks for advance…
Thanks for the great tutorial. I used the setvar for the cnlabel.lsp routine to change the units precision (luprec) to 4 and back to the old precision. Worked great.
How do I get the N/E on two separate lines of an mleader? I thought "" would work, but it doesn't.
(command "MLEADER" p textloc ptcoordN "" ptcoordE)
(defun c:cnlabel (/ p x y ptcoordN ptcoordE textloc oldprec)
(setq oldprec (getvar "LUPREC"))
(setvar "LUPREC" 4)
(while ; start while loop
(setq p (getpoint "
Pick Point to Label:")) ; asking user to click point to label and save it
(setq textloc (getpoint p "
Pick Text Location:")) ;asking user to click text location
(setq x (rtos (car p)))
(setq y (rtos (cadr p)))
(setq ptcoordN (strcat "N=" y))
(setq ptcoordE (strcat "E=" x))
(command "MLEADER" p textloc ptcoordN "" ptcoordE) ;ativate label command and use the input
(setvar "LUPREC" oldprec)
(princ)
)
)
Nancy
Hi Nancy,
You need to add the new line in the variable before you use MLEADER. See how I combine ptcoordN, (chr 10) -as new line-, and ptcoordN. Then I put it in MLEADER.
(setq ptcoordN (strcat "N=" y))
(setq ptcoordE (strcat "E=" x))
(setq ptcoordN (strcat ptcoordN (chr 10) ptcoordE))
(command "mleader" p textloc ptcoordN) ;ativate label command and use the input
HTH :)
Dear Edo,
Adding the tip to the line to emerge that aids in positioning the coordinates was perfect!
Hugs!
Thank you Newton :)
Glad you like it. Now you know how to use multileader with the program right? ;)
Sorry it takes so long to approve the comment. It was national holiday here yesterday.
I have a hard time fighting spam, so I turn on the comment moderation for a while.