• Home
  • Training Books
  • Subscribe to Our Email Newsletter
  • About
    • Contributors
    • Feedback
    • Contact
    • Privacy policy
    • Cookie Policy

CADnotes

CAD Tutorials and Best Practices for professionals and students

  • Featured
  • AutoCAD
    • AutoLISP
  • Revit
    • Revit Architecture Basic
    • Revit MEP Basic Tutorial
  • Inventor
  • MicroStation
    • MicroStation Basic Tutorial
  • CADnotes on YouTube
You are here: Home / AutoCAD / AutoLISP / AutoLISP exercise: creating program to label coordinate

AutoLISP exercise: creating program to label coordinate

December 6, 2010 by Edwin Prakoso 6 Comments

In this Article...

  • Program Basic
  • Using Leader tool
  • Getting Value from The List
  • Converting Real to String
  • Tips: Seeing Dynamic Lines from Previous Point
  • More Tips: Adding a Loop to Keep the Program Active

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.

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.

  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 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.

basic_AutoLISP_program_result

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.

dynamic_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.

About Edwin Prakoso

I work as a Solution Consultant in Datech Solutions, Tech Data Indonesia. I've been using AutoCAD since R14 and Revit since Revit Building 9. I occasionally write for AUGIWorld magazine and I am also active in Autodesk discussion forum. I'm a member of Autodesk Expert Elite, an appreciation for individuals who give contributions to the Autodesk community.
Connect with me on twitter or LinkedIn.

Filed Under: AutoLISP Tagged With: label coordinate, list, string

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 Comments
Inline Feedbacks
View all comments
yatin
yatin
7 years ago

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.

0
Reply
Jaime Vallejo
Jaime Vallejo
9 years ago

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…

0
Reply
neaton
neaton
12 years ago

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

0
Reply
Edwin Prakoso
Edwin Prakoso
Author
Reply to  neaton
12 years ago

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 :)

0
Reply
Newton Neto
Newton Neto
12 years ago

Dear Edo,

Adding the tip to the line to emerge that aids in positioning the coordinates was perfect!

Hugs!

0
Reply
Edwin Prakoso
Edwin Prakoso
Author
Reply to  Newton Neto
12 years ago

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.

0
Reply
wpdiscuz   wpDiscuz
Join Our Free Email Newsletter
  Thank you for Signing Up
Please correct the marked field(s) below.
1,true,6,Contact Email,2 1,false,1,First Name,2 1,false,1,Last Name,2

Featured

3D model with annotation

What’s new in AutoCAD: Annotations

This is AutoCAD new features roundup in annotation features. Read it if you want to implement AutoCAD new features!

Recent Articles

  • Model-Based Progress Tracking in Autodesk Build
  • Understanding Surface and Cut Patterns in Revit
  • Revit 2024.1 Update is Released

Advertisement

New on CADnotes

  • Model-Based Progress Tracking in Autodesk Build
  • Understanding Surface and Cut Patterns in Revit
  • Revit 2024.1 Update is Released
  • What’s New in Revit 2024: Bending Detail
  • What’s New in Revit 2024: The Dark Theme

Meet the Authors

avatar for
avatar for
avatar for
avatar for
avatar for
avatar for

Get Connected

CADnotes on FacebookCADnotes on InstagramCADnotes on TwitterCADnotes on YouTube

© 2009 – 2023 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz