AutoLISP: Add Leader to MTEXT or DTEXT

Here is a quickie that lets you attach a leader to an existing MTEXT or DTEXT object.

Here’s How:

  • TL <enter> to start “Text Leader”
  • Select Text object
  • Specify start point (arrow) of leader
  • Specify the angle of the leader

~enjoy

;;; ------------------------------------------------------------------------

;;; TEXTLEADER.LSP Version 1.2

;;;

;;; Copyright© August, 2007

;;; Timothy G. Spangler

;;;

;;; Permission to use, copy, modify, and distribute this software

;;; for any purpose and without fee is hereby granted, provided

;;; that the above copyright notice appears in all copies and

;;; that both that copyright notice and the limited warranty and

;;; restricted rights notice below appear in all supporting

;;; documentation.

;;;

;;; Add leader to text (Non Associating Leader).

;;;

;;; ------------------------------------------------------------------------

(defun C:TL (/) (C:TEXTLEADER)); Program Shortcut

;;; MAIN FUNCTION ;;;;;;;;;;;;;;;;;;;;;;;;;

(defun C:TEXTLEADER (/ *error* OldCmdecho OldOsmode OldOrthoMode OldClayer ExludeList)

;; Set Env

(TEXT_LEADER_SET_ENV)

;;; Error Handling Routine ;;;

(defun *error* (MSG)

(if(not(member MSG '("Function cancelled" "quit / exit abort")))

(princ (strcat "\n*** Program Error: " (strcase MSG) " ***"))

(princ "\n... Program Cancelled ...")

)

(while (< 0 (getvar "CMDACTIVE"))

(command)

)

(TEXT_LEADER_RESET_ENV)

(princ)

)

;; Main Code

(TEXT_LEADER_RUN)

)

;;; ------------ Begin Main Routine

(defun TEXT_LEADER_RUN (/ TextEnt TextEntList TextLayer DtextBox DtextEntList TextRotate MtextLine

DtextInsPoint TempPoint LeaderEnd)

;; Get text object from selection

(while (null (setq TextEnt (entsel "\n Select Top or Bottom Line of Text: ")))

(princ "\n Nothing Selected...")

)

;; Get entity list from text

(setq TextEntList (entget (car TextEnt)))

;; Get text layer

(setq TextLayer (cdr(assoc 8 TextEntList)))

;; Set layer to text layer

(setvar "CLAYER" TextLayer)

;; If selected text is "TEXT"

(if (= (cdr (assoc 0 TextEntList)) "TEXT")

(progn

(setq DtextBox (textbox (entget (car TextEnt))))

(setq DtextEntList (entget (car TextEnt)))

(setq TextRotate (cdr(assoc 50 DtextEntList)))

)

)

;; If selected text is "MTEXT"

(if (= (cdr (assoc 0 TextEntList)) "MTEXT")

(progn

(command "explode" TextEnt)

(setq MtextLine (ssget (cadr TextEnt)))

(setq DtextEntList (entget (ssname MtextLine 0)))

(setq DtextBox (textbox (entget (ssname MtextLine 0))))

(setq TextRotate (cdr(assoc 50 DtextEntList)))

(command "u")

)

)

;; If selected entity is not "TEXT" or "MTEXT"

(if (member (cdr (assoc 0 TextEntList)) ExludeList)

(progn

(alert "Selected entity is not TEXT or MTEXT")

(TEXT_LEADER_RUN)

)

)

;; Get insertion point of text

(setq DtextInsPoint (cdr (assoc 10 DtextEntList)))

;; Check the rotation of the text

(cond

((equal TextRotate 1.5708 0.0001)

;; Get center point of textbox (from 0,0)

(setq TempPoint

(list

(/ (+ (cadar DtextBox)(cadadr DtextBox)) 2.0)

(/ (+ (caadr DtextBox)(caar DtextBox)) 2.0)

(cadddr (assoc 10 DtextEntList))

)

)

;; Get the center point of the selected text object

(setq InsertPoint

(list

(- (car DtextInsPoint)(car TempPoint))

(+ (cadr DtextInsPoint)(cadr TempPoint))

(+ (caddr DtextInsPoint)(caddr TempPoint))

)

)

;; Set the leader end point

(setq LeaderEnd

(+ (/ (- (caadr DtextBox) (caar DtextBox)) 2.0)

(* 0.0625 (getvar "dimscale")) ;CHANGE THIS TO CHANGE GAT BETWEEN TEXT AND LEADER

)

)

;; Prompt to create the leader

(prompt "\n Select Leader Start and Bend Points: ")

;; Run the leader command with the point filter

(command "leader"

PAUSE

".X"

InsertPoint

PAUSE

(polar InsertPoint (angle InsertPoint (getvar "lastpoint")) LeaderEnd)

""

""

"n"

)

)

((equal TextRotate 0.0 0.0001)

;; Get center point of textbox (from 0,0)

(setq TempPoint

(list

(/ (+ (caadr DtextBox) (caar DtextBox)) 2.0)

(/ (+ (cadar DtextBox) (cadadr DtextBox)) 2.0)

(cadddr (assoc 10 DtextEntList))

)

)

;; Get the center point of the selected text object

(setq InsertPoint

(list

(+ (car DtextInsPoint) (car TempPoint))

(+ (cadr DtextInsPoint) (cadr TempPoint))

(+ (caddr DtextInsPoint) (caddr TempPoint))

)

)

;; Set the leader end point

(setq LeaderEnd

(+ (/ (- (caadr DtextBox) (caar DtextBox)) 2.0)

(* 0.0625 (getvar "dimscale")) ;CHANGE THIS TO CHANGE GAP BETWEEN TEXT AND LEADER

)

)

;; Prompt to create the leader

(prompt "\n Select Leader Start and Bend Points: ")

;; Run the leader command with the point filter

(command "leader"

PAUSE

".Y"

InsertPoint

PAUSE

(polar InsertPoint (angle InsertPoint (getvar "lastpoint")) LeaderEnd)

""

""

"n"

)

)

((/= (or (equal TextRotate 0.0 0.0001)(equal TextRotate 1.5708 0.0001)))

(alert "Selected text not at a suitable angle")

(TEXT_LEADER_RUN)

)

)

(TEXT_LEADER_RESET_ENV)

)

;;; ------------ Set Environment Settings

(defun TEXT_LEADER_SET_ENV (/)

&nbsp;

;; Set sysetem variables

(setq OldCmdecho (getvar "CMDECHO"))

(setq OldOsmode (getvar "OSMODE"))

(setq OldOrthoMode (getvar "ORTHOMODE"))

(setq OldClayer (getvar "CLAYER"))

(setvar "CMDECHO" 0)

(setvar "ORTHOMODE" 0)

(setvar "OSMODE" 513)

;;; Undo marker

(command "_UNDO" "BEGIN")

;; Set the exclusion list

(setq ExludeList (list "3DFACE" "3DSOLID" "ARC" "ATTDEF" "ATTRIB" "BODY" "CIRCLE" "DIMENSION" "ELLIPSE"

"HATCH" "IMAGE" "INSERT" "LEADER" "LINE" "LWPOLYLINE" "MLINE" "OLEFRAME" "OLE2FRAME" "POINT" "POLYLINE"

"RAY" "REGION" "SEQUEND" "SHAPE" "SOLID" "SPLINE" "TOLERANCE" "TRACE" "VERTEX" "VIEWPORT" "XLINE"))

;; Add program description to status line

(grtext -2 (strcat "Text Leader " "v1.2" " Copyright© 2007"))

)

;;; ------------ Reset Environment Settings

(defun TEXT_LEADER_RESET_ENV (/)

;;; Undo marker

(command "_UNDO" "END")

;; Reset system variable

(grtext -2 "")

(setvar "CLAYER" OldClayer)

(setvar "OSMODE" OldOsmode)

(setvar "ORTHOMODE" OldOrthoMode)

(setvar "CMDECHO" OldCmdecho)

(princ)

)

;;;

;;; Echos to the command line

(princ "\n Text Leader v1.2© \n Timothy Spangler, \nAugust, 2007....loaded.")

(terpri)

(princ "Type \"TL\" to run")

(print)

;;; End echo
Advertisement

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Leaders, Modifying, Text. Bookmark the permalink.

7 Responses to AutoLISP: Add Leader to MTEXT or DTEXT

  1. saif says:

    is there a way to attach the lsp file?

    Thanks

    • AutoCAD Tips says:

      Sure thing. I just emailed it to you.
      I am not able to attach .lsp files in a wordpress blog though…
      Also, there is a link that shows how to copy code from this blog under the “about” tab at the top of the blog.
      Hope this helps

      ~Greg

  2. Nicolas MARIE says:

    Thank for the lsp. I am looking for something similar that works with multi leader. Even more, having a pre-defined list of mtext offered when applying mleader. Thanks in advanced.

    • AutoCAD Tips says:

      I am looking for a simple routine that I can post on my blog that helps to easily insert common notes into a drawing. In the mean time, check out the link below. It is pretty robust in letting you insert common notes and whether or not you use a leader or just text.

      You need to get access to theswamp.org to be able to access it though. It is free, but it takes a day or two to get approved.
      http://www.theswamp.org/index.php?topic=1392.240

  3. Pingback: Text to Multileader | AutoCAD Tips

  4. anthonylopez says:

    do you have anything the select the text and highlighted it ready to be updated in multileader?

  5. syed says:

    Sir how I can download these lisp. Pls advise

Leave a Reply to AutoCAD Tips Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s