Author Topic: MText creation routine  (Read 2362 times)

0 Members and 1 Guest are viewing this topic.

T-Square

  • Guest
MText creation routine
« on: March 19, 2014, 08:08:23 PM »
Hello All,

I was perplexed with the creation of MText, sought an answer and found some code written by Lee Mac. I have added a few things to it to suit my needs. Please feel free to criticize, pick apart and note how it could be done differently and in your "humble oppinion"...better. Ultimately there are numerous ways to accomplish one thing in the wonderful world of AutoCAD. This suits and works for me. If any of you can find use, please do, if not, then don't use it. :-)

I am including the routine and the error trap I use. Again... Please feel free to criticize, pick apart and note how it could be done differently and in your "humble oppinion"...better.

ymg

  • Guest
Re: MText creation routine
« Reply #1 on: March 20, 2014, 04:45:15 AM »
T-Square,

How about this:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:mtx (/ disl p varl)
  2.     (vl-load-com)
  3.     ;;; Error Handler by ElpanovEvgenyi                                       ;
  4.     (defun *error* (msg)
  5.         (mapcar 'eval varl)
  6.         (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
  7.            (princ (strcat "\nError: " msg))
  8.         )
  9.         (princ)
  10.     )
  11.      
  12.     (setq varl '("OSMODE" "CMDECHO"  "CECOLOR" "CELTYPE" "CELWEIGHT" "MTEXTTOOLBAR" "TEXTSTYLE")
  13.           varl (mapcar (function (lambda (a) (list 'setvar a (getvar a)))) varl)
  14.     )
  15.     (setvar 'CMDECHO 0)
  16.     (setvar 'MTEXTTOOLBAR 1)
  17.     (setvar 'OSMODE 0)
  18.     (setvar 'CMDECHO 0)
  19.     (setvar 'CECOLOR "bylayer")
  20.     (setvar 'CELTYPE "bylayer")
  21.     (setvar 'CELWEIGHT -1)
  22.  
  23.     (setq DISL "A-ANNO-TEXT")
  24.     (if (not (tblsearch "layer" DISL))
  25.        (command ".-layer" "thaw" DISL "on" DISL "unlock" DISL "make" DISL "color" "4" DISL "lt" "continuous" DISL "")
  26.        (progn
  27.            (princ (strcat "\nLayer " DISL " Already Exists... Layer Un-Changed... "))
  28.            (setvar "clayer" DISL)
  29.        )
  30.     )
  31.     (if (not (tblsearch "style" "Simplex"))
  32.        (command ".-style" "SIMPLEX" "simplex.shx" "0.00000000" "0.90000000" "0.00000000" "N" "N" "N")
  33.        (princ "\nText Style \"Simplex\" Already Exists... Text Style Un-Changed... ")
  34.     )      
  35.     (setvar 'TEXTSTYLE "Simplex")
  36.    
  37.  
  38.     (while (setq p (getpoint "\nPoint for MText: "))
  39.         (command "_.mtedit" (entmakex
  40.                                  (list
  41.                                     '(0 . "MTEXT")
  42.                                     '(100 . "AcDbEntity")
  43.                                     '(100 . "AcDbMText")
  44.                                     '(1 . "")
  45.                                     (cons 71 9)
  46.                                     (cons 10 (trans p 1 0))
  47.                                     (cons 40 (if (< (getvar 'tilemode) 1 (getvar 'cvport)) 1 (getvar 'DIMSCALE)))
  48.                                     (cons  7 (getvar 'TEXTSTYLE))
  49.                                  )
  50.  
  51.                             )
  52.         )
  53.     )
  54.     (*error* nil)
  55. )  
  56.  

You can change your justification on the MTextToolbar.

Take a good look at Evgenyi's error handler, this a thing of beauty.

Also nice by BlackBox test (< (getvar 'tilemode) 1 (getvar 'cvport))


ymg

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: MText creation routine
« Reply #2 on: March 20, 2014, 05:31:05 PM »
Here is my take on the routine:

Code: [Select]
(defun c:mttl nil (mt 1))
(defun c:mttc nil (mt 2))
(defun c:mttr nil (mt 3))
(defun c:mtml nil (mt 4))
(defun c:mtmc nil (mt 5))
(defun c:mtmr nil (mt 6))
(defun c:mtbl nil (mt 7))
(defun c:mtbc nil (mt 8))
(defun c:mtbr nil (mt 9))

(defun mt ( jus / *error* hgt ins lay sty )

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

    (setq hgt 0.125
          sty "SIMPLEX"
          lay "MT"
    )
    (if (setq ins (getpoint "\nSpecify insertion point: "))
        (progn
            (LM:startundo (LM:acdoc))
            (LM:createlayer lay 4 "Continuous" aclnwtbylwdefault 1)
            (LM:createstyle sty "simplex.shx" 0.0 0.9 0.0)
            (if (/= 1 (getvar 'cvport))
                (setq hgt (* hgt (getvar 'dimscale)))
            )
            (command "_.mtedit"
                (entmakex
                    (list
                       '(000 . "MTEXT")
                       '(100 . "AcDbEntity")
                       '(100 . "AcDbMText")
                       '(001 . "")
                        (cons 008 lay)
                        (cons 010 (trans ins 1 0))
                        (cons 011 (getvar 'ucsxdir))
                        (cons 040 hgt)
                        (cons 071 jus)
                        (cons 007 (if (tblsearch "style" sty) sty "Standard"))
                        (cons 210 (trans '(0.0 0.0 1.0) 1 0 t))
                    )
                )
            )
            (LM:endundo (LM:acdoc))
        )
    )
    (princ)
)

;; Create/Modify Layer  -  Lee Mac

(defun LM:createlayer ( lay col ltp lwt plt )
    (
        (lambda ( _function )
            (_function
                (list
                   '(000 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   '(070 . 0)
                    (cons 002 lay)
                    (cons 006 (if (tblsearch "ltype" ltp) ltp "Continuous"))
                    (cons 062 col)
                    (cons 290 plt)
                    (cons 370 lwt)
                )
            )
        )
        (if (tblsearch "layer" lay)
            (lambda ( lst ) (entmod (cons (cons -1 (tblobjname "layer" lay)) lst)))
            entmakex
        )
    )
)

;; Create/Modify Text Style  -  Lee Mac

(defun LM:createstyle ( sty fnt hgt wid obl )
    (
        (lambda ( _function )
            (_function
                (list
                   '(000 . "STYLE")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbTextStyleTableRecord")
                   '(070 . 0)
                    (cons 002 sty)
                    (cons 003 fnt)
                    (cons 040 hgt)
                    (cons 041 wid)
                    (cons 050 obl)
                )
            )
        )
        (if (tblsearch "style" sty)
            (lambda ( lst ) (entmod (cons (cons -1 (tblobjname "style" sty)) lst)))
            entmakex
        )
    )
)

;; Start Undo  -  Lee Mac
;; Opens an Undo Group.

(defun LM:startundo ( doc )
    (LM:endundo doc)
    (vla-startundomark doc)
)

;; End Undo  -  Lee Mac
;; Closes an Undo Group.

(defun LM:endundo ( doc )
    (while (= 8 (logand 8 (getvar 'undoctl)))
        (vla-endundomark doc)
    )
)

;; Active Document  -  Lee Mac
;; Returns the VLA Active Document Object

(defun LM:acdoc nil
    (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
    (LM:acdoc)
)

(vl-load-com) (princ)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: MText creation routine
« Reply #3 on: March 20, 2014, 10:15:20 PM »
Here is my take on the routine:

Slight mod to account for creating empty text objects...

Code: [Select]
(defun mt (jus / *error* hgt ins lay sty mtext)

  (defun *error* (msg)
    (if (and (eq (type mtext) 'ENT) (eq (cdr (assoc 1 (entget mtext))) "")) ;alanjt edit
      (entdel mtext)
    )
    (LM:endundo (LM:acdoc))
    (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
      (princ (strcat "\nError: " msg))
    )
    (princ)
  )

  (setq hgt 0.125
        sty "SIMPLEX"
        lay "MT"
  )
  (if (setq ins (getpoint "\nSpecify insertion point: "))
    (progn
      (LM:startundo (LM:acdoc))
      (LM:createlayer lay 4 "Continuous" aclnwtbylwdefault 1)
      (LM:createstyle sty "simplex.shx" 0.0 0.9 0.0)
      (if (/= 1 (getvar 'cvport))
        (setq hgt (* hgt (getvar 'dimscale)))
      )
      (command "_.mtedit"
               (setq mtext (entmakex ; alanjt edit
                             (list
                               '(000 . "MTEXT")
                               '(100 . "AcDbEntity")
                               '(100 . "AcDbMText")
                               '(001 . "")
                               (cons 008 lay)
                               (cons 010 (trans ins 1 0))
                               (cons 011 (getvar 'ucsxdir))
                               (cons 040 hgt)
                               (cons 071 jus)
                               (cons 007
                                     (if (tblsearch "style" sty)
                                       sty
                                       "Standard"
                                     )
                               )
                               (cons 210 (trans '(0.0 0.0 1.0) 1 0 t))
                             )
                           )
               )
      )
      (if (eq (cdr (assoc 1 (entget mtext))) "") ;alanjt edit
        (entdel mtext)
      )
      (LM:endundo (LM:acdoc))
    )
  )
  (princ)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: MText creation routine
« Reply #4 on: March 20, 2014, 11:35:58 PM »
That's the way I would do it Lee & great add on Alan.
I might keep some options open with this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:mttl nil (mt 1))
  2. (defun c:mttc nil (mt 2))
  3. (defun c:mttr nil (mt 3))
  4. (defun c:mtml nil (mt 4))
  5. (defun c:mtmc nil (mt 5))
  6. (defun c:mtmr nil (mt 6))
  7. (defun c:mtbl nil (mt 7))
  8. (defun c:mtbc nil (mt 8))
  9. (defun c:mtbr nil (mt 9))
  10. (defun mt ( jus )
  11.     (mt-sub jus
  12.             (if mtDefaultSize mtDefaultSize (setq mtDefaultSize 0.125))
  13.             "SIMPLEX"
  14.             "MT")
  15.  )
  16.  
  17.  
  18. (defun mt-sub ( jus hgt sty lay / *error* hgt ins lay sty )
You can add additional mt routines that allow layer and style options if need be.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: MText creation routine
« Reply #5 on: March 21, 2014, 03:53:21 PM »
Great suggestions guys  8-)