+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Forum Newbie
    Using
    Inventor 2011
    Join Date
    Sep 2010
    Posts
    7

    Default Use lisp to put text into mtext box

    Registered forum members do not see this ad.

    Hello Again,

    Ive searched all over and cannot find how to do this, but am pretty sure its out there. What code would it take to place information retrieved from lisp program to an empty mtext box. It would be even better if the user could select an existing one and append more information to it.

    Thanks,
    Brian

  2. #2
    Super Member irneb's Avatar
    Computer Details
    irneb's Computer Details
    Operating System:
    Win7 Pro 64bit
    Computer:
    Antec One Hundred
    Motherboard:
    ASUS P8P67-Pro P67
    CPU:
    Intel i7 2600 @ 3.4GHz
    RAM:
    16GB-1600MHz
    Graphics:
    GeForce GT 430 (1GB)
    Primary Storage:
    Seagate1TB SATA2 - 7200rpm
    Monitor:
    Samsung 2333TN 23" 1920 x 1080 Full HD LCD Monitor2GW
    Discipline
    Architectural
    irneb's Discipline Details
    Occupation
    Architectural Technician and Programmer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    Sep 2010
    Location
    Jo'burg SA
    Posts
    1,666

    Default

    Replacing the text inside an MText, there are 2 basic ways. Assume you have the text you want stored in a lisp variable STR and have the MText selected and its ename stored in a lisp variable en:
    1. Through entmod.
    Code:
    (setq ed (entget en)) ;Get the DXF data list
    (setq ed (subst (cons 1 str) (assoc 1 ed) ed)) ;Modify the DXF list
    (entmod ed) ;Modify the entity
    1a. That could cause problems with annotatively scaled text. For that reason I would go with this rather:
    Code:
    (entmod (list (cons -1 en) (cons 1 str)))
    2. The vla methods (also doesn't give annotative problems)
    Code:
    (vl-load-com) ;Ensure VLisp extensions are loaded, only need to do this once per DWG.
    (setq eo (vlax-ename->vla-object en)) ;Get the ActiveX object of the entity
    (vla-put-TextString eo str) ;Change the text inside the MText
    To append to it, you'll need to obtain the existing value first:
    1. Again watch out for annotative scaling
    Code:
    (setq ed (entget en)) ;Get the DXF data list
    (setq ed (subst (cons 1 (strcat (cdr (assoc 1 ed)) str)) (assoc 1 ed) ed)) ;Modify the DXF list
    (entmod ed) ;Modify the entity
    1a. This becomes a bit forced
    Code:
    (setq ed (entget en)) ;Get the DXF data list
    (entmod (list (cons -1 en) (cons 1 (strcat (cdr (assoc 1 ed)) str))))
    2. IMO the easiest way
    Code:
    (setq eo (vlax-ename->vla-object en)) ;Get the ActiveX object of the entity
    (vla-put-TextString eo (strcat (vla-get-TextString eo) str)) ;Change the text inside the MText

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,816

    Default

    Watch out for DXF code 3 strings also when using the entmod method. Also, vla-get-TextString will corrupt any symbols in the string (turns them into '?').
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  4. #4
    Super Member irneb's Avatar
    Computer Details
    irneb's Computer Details
    Operating System:
    Win7 Pro 64bit
    Computer:
    Antec One Hundred
    Motherboard:
    ASUS P8P67-Pro P67
    CPU:
    Intel i7 2600 @ 3.4GHz
    RAM:
    16GB-1600MHz
    Graphics:
    GeForce GT 430 (1GB)
    Primary Storage:
    Seagate1TB SATA2 - 7200rpm
    Monitor:
    Samsung 2333TN 23" 1920 x 1080 Full HD LCD Monitor2GW
    Discipline
    Architectural
    irneb's Discipline Details
    Occupation
    Architectural Technician and Programmer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    Sep 2010
    Location
    Jo'burg SA
    Posts
    1,666

    Default

    Quote Originally Posted by Lee Mac View Post
    Watch out for DXF code 3 strings also when using the entmod method. Also, vla-get-TextString will corrupt any symbols in the string (turns them into '?').
    So actually the vla methods aren't that great. alanjt and myself have been to'ing-an-fro'ing about it for a while. My method of choice is the 1a type as per my previous post. But you make a good point, what about MText with more than 250 characters. That would get a bit complex to do using the entmod method.

  5. #5
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,816

    Default

    Quote Originally Posted by irneb View Post
    But you make a good point, what about MText with more than 250 characters. That would get a bit complex to do using the entmod method.
    Exactly - appending to the correct DXF 3 code, then re-inserting in the correct position...

    EDIT: Although, after a touch of testing, it appears that the last section of the textstring is stored in DXF 1, hence the above methods may still work.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  6. #6
    Luminous Being alanjt's Avatar
    Using
    Civil 3D 2011
    Join Date
    Apr 2008
    Posts
    6,050

    Default

    With MText, I just take the text from entget and put it back with vla-put-textstring. Unless I know the string is going to be shorter than 250c, then if I'll just put it back with entmod.
    DropBox | finding the light...
    Seann: ...it went crazy ex-girlfriend on me...
    eric_monceaux...its pretty funny seeing two AutoCAD Gods give each other flak...

  7. #7
    Super Member irneb's Avatar
    Computer Details
    irneb's Computer Details
    Operating System:
    Win7 Pro 64bit
    Computer:
    Antec One Hundred
    Motherboard:
    ASUS P8P67-Pro P67
    CPU:
    Intel i7 2600 @ 3.4GHz
    RAM:
    16GB-1600MHz
    Graphics:
    GeForce GT 430 (1GB)
    Primary Storage:
    Seagate1TB SATA2 - 7200rpm
    Monitor:
    Samsung 2333TN 23" 1920 x 1080 Full HD LCD Monitor2GW
    Discipline
    Architectural
    irneb's Discipline Details
    Occupation
    Architectural Technician and Programmer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    Sep 2010
    Location
    Jo'burg SA
    Posts
    1,666

    Default

    Actually I don't see anything wrong with using the vla methods. You get the ???? characters in DText whether you use entmod or vla-put-TextString. But with MText you sometimes end up with a blank text entity if you use entmod (I think that's due to the 250 limit).

    As an example:
    Code:
    (defun c:Add250+ (/ en s n)
      (if (setq en (entsel))
        (progn
          (setq n 128 s "")
          (repeat 249
            (setq s (strcat s (chr n))
                  n (1+ n)
                  )
          )
    ;;;      (vla-put-TextString (vlax-ename->vla-object (car en)) s)
          (entmod (list (cons -1 (car en)) (cons 1 s)))
        )
      )
      (princ)
    )

  8. #8
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,816

    Default

    'Putting' is fine, its the 'getting' that causes problems - also we're talking about symols added using the MText Editor, i.e. right-click, Symbol, 'Angle' say
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  9. #9
    Super Member irneb's Avatar
    Computer Details
    irneb's Computer Details
    Operating System:
    Win7 Pro 64bit
    Computer:
    Antec One Hundred
    Motherboard:
    ASUS P8P67-Pro P67
    CPU:
    Intel i7 2600 @ 3.4GHz
    RAM:
    16GB-1600MHz
    Graphics:
    GeForce GT 430 (1GB)
    Primary Storage:
    Seagate1TB SATA2 - 7200rpm
    Monitor:
    Samsung 2333TN 23" 1920 x 1080 Full HD LCD Monitor2GW
    Discipline
    Architectural
    irneb's Discipline Details
    Occupation
    Architectural Technician and Programmer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    Sep 2010
    Location
    Jo'burg SA
    Posts
    1,666

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by Lee Mac View Post
    'Putting' is fine, its the 'getting' that causes problems - also we're talking about symols added using the MText Editor, i.e. right-click, Symbol, 'Angle' say
    Not exactly:
    Code:
    (defun c:AddMText (/ ss m n en str ed oldstr lst)
      (princ "\nSelect MText's: ")
      (if (and (setq ss (ssget '((0 . "MTEXT"))))
               (setq str (getString t "\nType the string to append to each: "))
          )
        (progn
          (setq m (sslength ss))
          (while (>= (setq m (1- m)) 0)
            (setq en (ssname ss m)
                  ed (entget en)
                  oldstr ""
                  )
            (foreach n ed
              (if (member (car n) '(1 3))
                (setq oldstr (strcat oldstr (cdr n)))
              )
            )
            (setq oldstr (strcat oldstr str)
                  lst nil n 1)
            (while (< (+ n 249) (strlen oldstr))
              (setq lst (cons (cons 3 (substr oldstr n 250)) lst)
                    n (+ n 250)
              )
            )
            (setq lst (reverse (cons (cons 1 (substr oldstr n)) lst)))
            (entmod (cons (cons -1 en) lst))
          )
        )
      )
      (princ)
    )
    All you need to do is concatenate the DXF 3 codes and the 1 code into a string variable. Although the above makes the MText blank for some reason (at least on my Vanilla 2008 ). Not sure why.

Similar Threads

  1. Lisp to deal with text/Mtext
    By asos2000 in forum AutoLISP, Visual LISP & DCL
    Replies: 31
    Last Post: 11th Mar 2013, 02:24 pm
  2. Convert text to mtext in lisp?
    By muck in forum AutoLISP, Visual LISP & DCL
    Replies: 19
    Last Post: 27th Jul 2010, 09:31 am
  3. Text to Mtext lisp- add background mask?
    By mawelby in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 1st Jul 2010, 05:58 am
  4. Lisp routine to convert objects into Text or Mtext
    By bsimpson in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 2nd Feb 2009, 01:31 pm
  5. LT 2006- text to mtext lisp,
    By RONQUITECT in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 15th Apr 2006, 03:29 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts