AutoLISP: MTEXT box with a leader

Here is a simple routine that lets you select MTEXT (does not work on DTEXT) and creates a rectangle around the text and then lets you select where you would like a leader to point to. This routine could even be used to simply place a box around  MTEXT by erasing the leader after placing it.

Here’s how:

  • MTEXTLD <enter> to start
  • Select MTEXT <enter> (only one MTEXT object)
  • Place the end point of the leader

;;MTEXTLD.LSP ENCLOSE MTEXT IN A BOX AND PLACE LEADER FOR CALLOUT
(defun C:mtextld (/ ENT FNM APT WDT HGT MTH PT7 PT9 PT3 PT1 TLFO TRTO
BRTO BLFO)
(prompt "PICK MTEXT TO CREATE CALLOUT BOX...")
(setq os (getvar "OSMODE"))
(setvar "OSMODE" 0)
(setq ent (ssget)) ;sets selected mtext as variable "ent"
(setq fnm (ssname ent 0)) ;sets name of selected mtext as variable "fnm"
(setq APT (cdr (assoc 71 (entget fnm)))) ; attachment point
(setq WDT (cdr (assoc 42 (entget fnm)))) ; width
(setq HGT (cdr (assoc 43 (entget fnm)))) ; height
(setq MTH (cdr (assoc 40 (entget fnm)))) ; mtext height
(setq PT7 (cdr (assoc 10 (entget fnm)))) ; locate top left corner
(setq PT9 (list (+ (car PT7) WDT) (cadr PT7) (caddr PT7)))
; locate top right corner
(setq PT3 (list (car PT9) (- (cadr PT9) HGT) (caddr PT9)))
; locate bottom right corner
(setq PT1 (list (car PT7) (- (cadr PT7) HGT) (caddr PT7)))
; locate bottom left corner
(setq TLFO (list (- (car PT7) mth) (+ (cadr PT7) mth)))
; locate top lft w/ offset
(setq TRTO (list (+ (car PT9) mth) (+ (cadr PT9) mth)))
; locate top rt w/ offset
(setq BRTO (list (+ (car PT3) mth) (- (cadr PT3) mth)))
; locate bottom rt w/ offset
(setq BLFO (list (- (car PT1) mth) (- (cadr PT1) mth)))
; locate bottom lft w/ offset
(command "._pline" TLFO TRTO BRTO BLFO "c") ; insertion point
(setvar "OSMODE" os)
(setq pt (getpoint (list (/ (+ (car trto) (car tlfo)) 2)
(/ (+ (cadr trto) (cadr brto)) 2)
) ;_ end of list
"\nSelect Point:"
) ;_ end of getpoint
) ; list middle of box and user selects leader endpoint
(setq d1 (distance pt tlfo))
(setq d2 (distance pt trto)) ; check distance between selected pt & box corners
(setq d3 (distance pt brto))
(setq d4 (distance pt blfo))
(setq md (min d1 d2 d3 d4)) ; find smallest distance from pt to corner
(if (= md d1)
(setq pta tlfo)
) ; if lowest value pta is d1
(if (= md d2)
(setq pta trto)
) ; if lowest value pta is d2;
(if (= md d3)
(setq pta brto)
) ; if lowest value pta is d3;
(if (= md d4)
(setq pta blfo)
) ; if lowest value pta is d4;
(command "qleader" pt pta ^c^c) ; complete qleader using pt and pta
(princ) ;prints nothing to eliminate nil
) ;_ end of defun
;|«Visual LISP© Format Options»
(72 2 40 1 T "end of " 60 9 0 0 0 nil T nil T)
Nov-02-2011 edit by Greg B to save and set osmode
;*** DO NOT add text below the comment! ***|;
Advertisements

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, AutoLISP: Text, Leaders, Text. Bookmark the permalink.

One Response to AutoLISP: MTEXT box with a leader

  1. Mirko says:

    Great!

Leave a 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s