Jump to content

MText command in LISP


Arepo

Recommended Posts

I am trying to write a routine to "simulate" MText command in lisp using predefined Text Height (2.0mm in this case for 1:1 scale) and text Style. I started with this code:

 

(defun C:test (/ Height Style)
(setvar "cmdecho" 1)
(setq Height (* (getvar "DIMSCALE") 2.0 ))
(setq Style "My_Style")
(command "._MTEXT" pause "H" Height "S" Style pause)
	(princ)
)

 

The text style seems to work but the Text Height doesn't change.

Could anyone help please?

Edited by Arepo
Link to comment
Share on other sites

Is (getvar "textsize") what you need instead of dimscale? You also have to ensure the text style used does not have a predefined height.

From F1 help for textsize:

Sets the default height for new text objects drawn with the current text style.

TEXTSIZE has no effect if the current text style has a fixed height.

 

-edit- if you just want the height to be 2.0, enter "2.0" instead of Height in your (command... ) line. Right now it is setting the value of Height to 2 times the current dimscale.

Link to comment
Share on other sites

(defun c:Test (/ str p)
 (if (and (not (eq (setq str (getstring t "\n Type text string :")) ""))
      (setq p (getpoint "\n Specify Text location :"))
     )
   (entmake (list '(0 . "MTEXT") '(100 . "AcDbEntity") '(100 . "AcDbMText")
          (cons 10 p)
          (cons 40 (* (getvar "DIMSCALE") 2.0))
          (cons 7 (if (tblsearch "STYLE" "My_Style")  "My_Style"   "Standard" ))
          (cons 1 str)
        )
   )
 )
 (princ)
)

Link to comment
Share on other sites

What I am trying is to write a set of commands for different text styles and sizes. The text styles to be used are already loaded in the drawing template. Of course, text size should depend on DIMSCALE. I want the command to look as close to AutoCAD Mtext command as possible (have the Text Formatting box and the ability to write text on multiple lines). Something simple like MTEXT (MT) command I tried above, only having text style and text size predefined so I won't have to change them for different texts I'm using.

Rickh: The text styles I'm using don't have a predefined text height and I need text height depending on DIMSCALE so I have the right text size in Paper Space.

Tharwat: thank you for your code, works well but gives me the option to write text on one line only. Could you tell me why the code I tried doesn't work properly?

Link to comment
Share on other sites

Tharwat: thank you for your code, works well but gives me the option to write text on one line only.

 

Try this .... :)

 

(defun c:Test (/ lst st p)
 (while (not (eq (setq st (getstring t "\n Type text string :")) "")
        )
   (setq lst (cons (strcat st "\\P") lst))
 )
 (if (setq p (getpoint "\n Specify Text location :"))
   (entmake (list '(0 . "MTEXT")
                  '(100 . "AcDbEntity")
                  '(100 . "AcDbMText")
                  (cons 10 p)
                  (cons 40 (* (getvar "DIMSCALE") 2.0))
                  (cons 7
                        (if (tblsearch "STYLE" "My_Style")
                          "My_Style"
                          "Standard"
                        )
                  )
                  (cons 1 (apply 'strcat (reverse lst)))
            )
   )
 )
 (princ)
)

Link to comment
Share on other sites

An alternative method:

 

(defun c:mtx ( / e h p s )

   (setq s "My_Style"
         h (* 2 (getvar 'dimscale))
   )    
   (if (setq p (getpoint "\nPoint for MText: "))
       (progn
           (setq e
               (entmakex
                   (list
                      '(0 . "MTEXT")
                      '(100 . "AcDbEntity")
                      '(100 . "AcDbMText")
                      '(1 . "")
                       (cons 10 (trans p 1 0))
                       (cons 40 h)
                       (cons 07 (if (tblsearch "STYLE" s) s "Standard"))
                   )
               )
           )
           (command "_.mtedit" e)
       )
   )
   (princ)
)

Link to comment
Share on other sites

An alternative method:

 

That's exactly what I was looking for. I like having the Text Formatting window. It turned out to be more complicated code than I thought :)

Thank you very much!

Link to comment
Share on other sites

  • 1 year later...

I have recently discovered by accident that the mtext editor command (as in mtedit) can be called as a function in LISP. Since I can't find this information mentioned anywhere else, I am posting it here purely in the interests of sharing the information with those who have an interest. I am not at this point suggesting that it be used instead of (command). :roll:

 

What I have discovered so far is that the function is defined in "acmted.arx", and that it is demand loaded when the command is invoked. Which means if you want to call it as a function you would need to check if it is loaded first. The function appears to be called with just the one variable, the ename of the MTEXT entity, as in

 

(mtedit ename)

 

I haven't nutted much else out yet, but it returns a symbol.

Link to comment
Share on other sites

  • 2 years later...

Lee Mac, I've been using the code you suggested for a while in my program and it worked fine as long as I'd been using World UCS. It does not work when I use a different UCS, the insertion point translates into world UCS, I guess and my picked point "p" jumps elsewhere. I know the culprit is "(cons 10 (trans p 1 0))" and I tried different ways to make it work, but unsuccessfully. Basically I'm trying to get some code that keeps the point where I pick it regardless of the UCS I use, because sometimes I have to change the UCS often. I would appreciate if you could suggest anything. Thank you!

Link to comment
Share on other sites

Lee Mac, I've been using the code you suggested for a while in my program and it worked fine as long as I'd been using World UCS. It does not work when I use a different UCS, the insertion point translates into world UCS, I guess and my picked point "p" jumps elsewhere.

 

Please try the following instead:

(defun c:mtx ( / e h p s )

   (setq s "My_Style"
         h (* 2 (getvar 'dimscale))
   )    
   (if (setq p (getpoint "\nSpecify point for mtext: "))
       (if
           (setq e
               (entmakex
                   (list
                      '(000 . "MTEXT")
                      '(100 . "AcDbEntity")
                      '(100 . "AcDbMText")
                      '(001 . "")
                       (cons 010 (trans p 1 0))
                       (cons 011 (getvar 'ucsxdir))
                       (cons 040 h)
                       (cons 007 (if (tblsearch "style" s) s "Standard"))
                       (cons 210 (trans '(0 0 1) 1 0 t))
                   )
               )
           )
           (command "_.mtedit" e)
       )
   )
   (princ)
)

I'm pleased that you find this old program useful!

Link to comment
Share on other sites

  • 7 years later...
On 9/30/2016 at 7:36 PM, Lee Mac said:

 

Please try the following instead:

(defun c:mtx ( / e h p s )

   (setq s "My_Style"
         h (* 2 (getvar 'dimscale))
   )    
   (if (setq p (getpoint "\nSpecify point for mtext: "))
       (if
           (setq e
               (entmakex
                   (list
                      '(000 . "MTEXT")
                      '(100 . "AcDbEntity")
                      '(100 . "AcDbMText")
                      '(001 . "")
                       (cons 010 (trans p 1 0))
                       (cons 011 (getvar 'ucsxdir))
                       (cons 040 h)
                       (cons 007 (if (tblsearch "style" s) s "Standard"))
                       (cons 210 (trans '(0 0 1) 1 0 t))
                   )
               )
           )
           (command "_.mtedit" e)
       )
   )
   (princ)
)
 

I'm pleased that you find this old program useful!

After hours of frustration, I found this bit of code and it works. Of course.

 

Lee Mac is da bomb. We're not worthy!!

 

 

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...