Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Edit this lisp file

16 REPLIES 16
Reply
Message 1 of 17
thebearjed
597 Views, 16 Replies

Edit this lisp file

I found this lisp file and was wondering how to modify it so that when i select MTEXT instead of TEXT that it first explodes the MTEXT so that it will become TEXT and then the lisp will work. also to change the rectangle that is drawn to the same layer as the selected text?

 

(defun c:tbox()
(setvar "cmdecho" 0)
(while (setq ent (entsel "\nSelect Text: "))
(setq e (car ent) el (entget e))
(if (= (cdr(assoc 0 el)) "TEXT")
(progn
(setq tb (textbox el) ; gets the enclosing box co-ords
margin (* (cdr(assoc 40 el)) 0.4) ; margin assumed as 40% of text height
list1 (list (- margin) (- margin) 0.0)
list2 (list margin margin 0.0)
ipt (cdr(assoc 10 el))
p1 (mapcar '+ (car tb) ipt list1)
p2 (mapcar '+ (cadr tb) ipt list2)
)
(command ".rectangle" p1 p2)
(command "pedit" "last" "w" (* 0.125 (cdr(assoc 40 el))) "")
)
)
)
(prin1)
)

16 REPLIES 16
Message 2 of 17

Hi -

 

Try the attached LISP, instead (TBOX2 is the command to type once it's loaded).  It allows you to draw a box around TEXT AND/OR MTEXT and on the layer of the text/mtext similar to the way it looked in your original post.... no exploding is necessary....

 

Hope this helps you---

 

 

 - Muddy

Message 3 of 17

thank you sir! that is exactly what i was looking for.
Message 4 of 17
Kent1Cooper
in reply to: thebearjed


@thebearjed wrote:

I found this lisp file and was wondering how to modify it so that when i select MTEXT instead of TEXT that it first explodes the MTEXT so that it will become TEXT and then the lisp will work. also to change the rectangle that is drawn to the same layer as the selected text?

....


Something like this, perhaps:

 

(defun c:tbox()
  (setvar "cmdecho" 0)
  (while (setq ent (entsel "\nSelect Text: "))
    (setq e (car ent) el (entget e))

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

      (progn

        (command "_.explode" e)

          ; [no Enter "" to complete selection -- in (command), can Explode

          ; only one entity unless you mess with QAFLAGS System Variable]

        (setvar el (entget (entlast)); replace Mtext's entity data with Text's

      ); progn

    ); if
    (if (= (cdr(assoc 0 el)) "TEXT"); whether original selection or Exploded Mtext
      (progn ; then
; .... your (setq)'s and rectangle ....

        (command

          "pedit" "last" "w" (* 0.125 (cdr(assoc 40 el))) ""

          "_.chprop" "last" "" "_layer" (cdr (assoc 8 el)) ""
      ); progn

      (prompt "\nThat isn't Text or Mtext, you numb-skull!"); else
    ); if
  ); while
  (prin1)
)

 

That assumes that any Mtext selected is of only one line -- otherwise it will box only the last line of it.  It could also set the Polyline width and change the Layer before drawing the Rectangle, but it would then need to set them back.  Consider also whether you want to build entity-type restriction into it, as well as add the usual other stuff [error handling, Osnap control, saving and resetting of the changed System Variable, etc.].

Kent Cooper, AIA
Message 5 of 17

However, there can be unexpected results with MTEXT and the defined width of the mtext entity boundary being much wider than what you actually see of the extents of the text value within, on the screen... 😞

 

 

So..... the attached LISP (TBOX3) will do precisely as you asked in your original post. It will detect if the object is Text or MTEXT, and if MTEXT, it will first explode it down to text and then draw the boundary.

 

🙂

 

-Muddy

Message 6 of 17

[Be aware that the Bounding Box for Mtext goes to the defining width of the Mtext's box, which may be considerably wider than the actual content, especially if there are hard returns in it.  There are threads on this forum about finding the actual width of the content, or reducing the width of the defining box to that, in case you find the resulting rectangle wider than you want in some cases.]  EDIT: you beat me to it!

Kent Cooper, AIA
Message 7 of 17
Lee_Mac
in reply to: thebearjed

Here is another program that will work with both Text & MText constructed in any UCS plane, and at any rotation:

 

Box Text

 

The program could be very easily modified to suit your preferences with regards to a fixed offset & polyline layer.

 

Lee

Message 8 of 17
Lee_Mac
in reply to: Lee_Mac

Here is a modified version of my Box Text program to suit your requirements:

 

;;-----------------------=={ Box Text }==---------------------;;
;;                                                            ;;
;;  Frames Text or MText objects with an LWPolyline, with     ;;
;;  optional offset. Works in all UCS/Views.                  ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Version 1.1    -    26-05-2013                            ;;
;;------------------------------------------------------------;;

;;  2013-12-13: Modified as follows:
;;  - Allows multiple text selection
;;  - Fixed offset factor of 0.4
;;  - Fixed LWPolyline width equal to 1/8 text height
;;  - LWPolyline on same layer as text

(defun c:bt ( / *error* enx idx lst sel )

    (defun *error* ( msg )
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (if (setq sel (ssget '((0 . "TEXT,MTEXT"))))
        (repeat (setq idx (sslength sel))
            (setq enx (entget (ssname sel (setq idx (1- idx))))
                  lst (text-box enx (* 0.4 (cdr (assoc 40 enx))))
            )
            (entmake
                (append
                   '(
                        (000 . "LWPOLYLINE")
                        (100 . "AcDbEntity")
                        (100 . "AcDbPolyline")
                        (090 . 4)
                        (070 . 1)
                    )
                    (list
                        (assoc 8 enx)
                        (cons 38 (caddar lst))
                        (cons 43 (* 0.125 (cdr (assoc 40 enx))))
                    )
                    (mapcar '(lambda ( x ) (cons 10 x)) lst)
                    (list (assoc 210 enx))
                )
            )
        )
    )
    (princ)
)

;; Text Box  -  gile / Lee Mac
;; Returns an OCS point list describing a rectangular frame surrounding
;; the supplied text or mtext entity with optional offset
;; enx - [lst] Text or MText DXF data list
;; off - [rea] offset (may be zero)

(defun text-box ( enx off / b h j l m n o p r w )
    (if
        (setq l
            (cond
                (   (= "TEXT" (cdr (assoc 0 enx)))
                    (setq b (cdr (assoc 10 enx))
                          r (cdr (assoc 50 enx))
                          l (textbox enx)
                    )
                    (list
                        (list (- (caar  l) off) (- (cadar  l) off))
                        (list (+ (caadr l) off) (- (cadar  l) off))
                        (list (+ (caadr l) off) (+ (cadadr l) off))
                        (list (- (caar  l) off) (+ (cadadr l) off))
                    )
                )
                (   (= "MTEXT" (cdr (assoc 0 enx)))
                    (setq n (cdr (assoc 210 enx))
                          b (trans  (cdr (assoc 10 enx)) 0 n)
                          r (angle '(0.0 0.0 0.0) (trans (cdr (assoc 11 enx)) 0 n))
                          w (cdr (assoc 42 enx))
                          h (cdr (assoc 43 enx))
                          j (cdr (assoc 71 enx))
                          o (list
                                (cond
                                    ((member j '(2 5 8)) (/ w -2.0))
                                    ((member j '(3 6 9)) (- w))
                                    (0.0)
                                )
                                (cond
                                    ((member j '(1 2 3)) (- h))
                                    ((member j '(4 5 6)) (/ h -2.0))
                                    (0.0)
                                )
                            )
                    )
                    (list
                        (list (- (car o)   off) (- (cadr o)   off))
                        (list (+ (car o) w off) (- (cadr o)   off))
                        (list (+ (car o) w off) (+ (cadr o) h off))
                        (list (- (car o)   off) (+ (cadr o) h off))
                    )
                )
            )
        )
        (   (lambda ( m ) (mapcar '(lambda ( p ) (mapcar '+ (mxv m p) b)) l))
            (list
                (list (cos r) (sin (- r)) 0.0)
                (list (sin r) (cos r)     0.0)
               '(0.0 0.0 1.0)
            )
        )
    )
)
 
;; Matrix x Vector  -  Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n
 
(defun mxv ( m v )
    (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)

(princ)
Message 9 of 17

@Kent -

 

I didn't realize you could redefine the bounding box width to the content.... I will have to dig around here for some methods to update that TBOX2.LSP to incorporate that feature...  My company's drawings contain a rich (read "RANDOM") mixture of MTEXT and TEXT throughout a drawing.... for my own use, it would be nice to leave the entity as-is and be able to "trim" a mile-long MTEXT bounding box and just leave everything in situ with a tidy rectangle around it.

 

Thanks for the heads up!

 

-Muddy

Message 10 of 17
thebearjed
in reply to: Lee_Mac

Lee...very nice with the addition of selecting multiple mtext!

i wish i could do this as easily as you guys.
Message 11 of 17
Lee_Mac
in reply to: thebearjed

Thank you thebearjed - I'm glad you like it!

 

It all comes with much practice Smiley Happy

Message 12 of 17
thebearjed
in reply to: Lee_Mac

i hat to bother you again but how can i make the lisp repeat and then scale the created box by .03?
Message 13 of 17
Lee_Mac
in reply to: thebearjed

You mean to obtain a double-line box?

Message 14 of 17
thebearjed
in reply to: Lee_Mac

yes, double lined box.
Message 15 of 17
Lee_Mac
in reply to: thebearjed

Try the following - change the noted offset values as required:

 

;;-----------------------=={ Box Text }==---------------------;;
;;                                                            ;;
;;  Frames Text or MText objects with an LWPolyline, with     ;;
;;  optional offset. Works in all UCS/Views.                  ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Version 1.1    -    26-05-2013                            ;;
;;------------------------------------------------------------;;

;;  2013-12-13: Modified as follows:
;;  - Allows multiple text selection
;;  - Fixed LWPolyline width equal to 1/8 text height
;;  - LWPolylines on same layer as text
;;  - Creates multiple LWPolylines with varying text offset factors

(defun c:bt ( / *error* enx idx lst sel )

    (defun *error* ( msg )
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (if (setq sel (ssget '((0 . "TEXT,MTEXT"))))
        (repeat (setq idx (sslength sel))
            (setq enx (entget (ssname sel (setq idx (1- idx)))))
            (foreach off '(0.4 0.8) ;; <-- Change these values as required
                (setq lst (text-box enx (* off (cdr (assoc 40 enx)))))
                (entmake
                    (append
                       '(
                            (000 . "LWPOLYLINE")
                            (100 . "AcDbEntity")
                            (100 . "AcDbPolyline")
                            (090 . 4)
                            (070 . 1)
                        )
                        (list
                            (assoc 8 enx)
                            (cons 38 (caddar lst))
                            (cons 43 (* 0.125 (cdr (assoc 40 enx))))
                        )
                        (mapcar '(lambda ( x ) (cons 10 x)) lst)
                        (list (assoc 210 enx))
                    )
                )
            )
        )
    )
    (princ)
)

;; Text Box  -  gile / Lee Mac
;; Returns an OCS point list describing a rectangular frame surrounding
;; the supplied text or mtext entity with optional offset
;; enx - [lst] Text or MText DXF data list
;; off - [rea] offset (may be zero)

(defun text-box ( enx off / b h j l m n o p r w )
    (if
        (setq l
            (cond
                (   (= "TEXT" (cdr (assoc 0 enx)))
                    (setq b (cdr (assoc 10 enx))
                          r (cdr (assoc 50 enx))
                          l (textbox enx)
                    )
                    (list
                        (list (- (caar  l) off) (- (cadar  l) off))
                        (list (+ (caadr l) off) (- (cadar  l) off))
                        (list (+ (caadr l) off) (+ (cadadr l) off))
                        (list (- (caar  l) off) (+ (cadadr l) off))
                    )
                )
                (   (= "MTEXT" (cdr (assoc 0 enx)))
                    (setq n (cdr (assoc 210 enx))
                          b (trans  (cdr (assoc 10 enx)) 0 n)
                          r (angle '(0.0 0.0 0.0) (trans (cdr (assoc 11 enx)) 0 n))
                          w (cdr (assoc 42 enx))
                          h (cdr (assoc 43 enx))
                          j (cdr (assoc 71 enx))
                          o (list
                                (cond
                                    ((member j '(2 5 8)) (/ w -2.0))
                                    ((member j '(3 6 9)) (- w))
                                    (0.0)
                                )
                                (cond
                                    ((member j '(1 2 3)) (- h))
                                    ((member j '(4 5 6)) (/ h -2.0))
                                    (0.0)
                                )
                            )
                    )
                    (list
                        (list (- (car o)   off) (- (cadr o)   off))
                        (list (+ (car o) w off) (- (cadr o)   off))
                        (list (+ (car o) w off) (+ (cadr o) h off))
                        (list (- (car o)   off) (+ (cadr o) h off))
                    )
                )
            )
        )
        (   (lambda ( m ) (mapcar '(lambda ( p ) (mapcar '+ (mxv m p) b)) l))
            (list
                (list (cos r) (sin (- r)) 0.0)
                (list (sin r) (cos r)     0.0)
               '(0.0 0.0 1.0)
            )
        )
    )
)
 
;; Matrix x Vector  -  Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n
 
(defun mxv ( m v )
    (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)

(princ)
Message 16 of 17
thebearjed
in reply to: Lee_Mac

you are the man!
Message 17 of 17
Lee_Mac
in reply to: thebearjed

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost