May 10, 2012

Adding New Line in Multiline text

We created an AutoLISP program to create leader to label coordinate before. It will be very useful for surveyors who use vanilla AutoCAD. But you may want to use multileader instead of leader in your AutoLISP program. MLEADER is neat, and you can have more control and flexibility with it.

The problem is it uses multiline text, not single line text in leader. When working with multiline text, we press [enter] when we want to add another text line. But using “” in AutoLISP to simulate pressing enter, it will not work. When we use “” AutoCAD thinks we want to end the command. But not adding new line.

enter

I posted a reply in the comment section, but just in case you miss it I write it as a post. To solve this, we need to use ANSI character to add a new line. chr 10 will add new line (or line feed) to our variable.

Let’s take an example. We add that character to our string variable:

(setq ptcoordN (strcat "N=" y))
(setq ptcoordE (strcat "E=" x))
(setq ptcoordN (strcat ptcoordN (chr 10) ptcoordE))

The first and second line will get x and y value, then add prefix. The 3rd line will combine them both. That AutoLISP code will combine N coordinate, new line, then place E coordinate there.

The complete AutoLISP code will be:
(defun c:cnlabel (/ p x y ptcoordN ptcoordE textloc oldprec)
(setq oldprec (getvar "LUPREC"))
(setvar "LUPREC" 4)
(while ; start while loop
(setq p (getpoint "\nPick Point to Label:")) ; asking user to click point to label and save it
(setq textloc (getpoint p "\nPick 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))
(setq ptcoordN (strcat ptcoordN (chr 10) ptcoordE))
(command "mleader" p textloc ptcoordN) ;ativate label command and use the input
(setvar "LUPREC" oldprec)
(princ)
)
)

I don’t know if there is other solution for this. If you know the other way, please share it here!

Advertisement

You might also interested to

  1. Labeling Coordinate with Easting and Northing in AutoCAD
  2. AutoLISP exercise: creating program to label coordinate
  3. Using AutoLISP Program to label point coordinate
  4. AutoLISP Exercise: Using Block and Conditional If
  5. AutoLISP tutorial: system variable and conditional COND

Search for more articles

Custom Search
About Edwin Prakoso

Edwin works as an Application Engineer in Jakarta, Indonesia. He has 4 years experience in building industry, then start to work for Autodesk reseller.
He is certified as Revit Architecture 2010 certified professional and CATIA V5 part design specialist. Currently he is involved as team leader for implementing SmarTeam as PDM in several companies.
You can reach him on twitter @cad_notes. You can also connect with him on LinkedIn. If you prefer email, reach him at edwin.prakoso@cad-notes.com.