Jump to content

Edit Mtext via LISP


jmerch

Recommended Posts

I've searched high and low for a LISP to edit Mtext via LISP and have only found one (thanks Mike Weaver) that is almost what I want. It is posted below but I want it to autoselect a piece of MTEXT that I designate (via ssget). I have modified this code but don't understand Visual LISP enough to know why it's erroring...any help?

 

Original:

(defun c:mtval( / ent objmtext stroldval 
strnewval)
 (setq
ent (car (entsel))
objMText 
(vlax-ename->vla-object ent)
strOldval (vlax-get-property objMText 
"TEXTSTRING")
strnewval (getstring T (strcat "\nNew text value<" 
stroldval ">: "))
)
 (if 
strnewval
(vlax-put-property objmtext "TEXTSTRING" 
strnewval)
)
 (vlax-release-object objmtext)
 
)

 

My modified code:

(defun c:mtval( / getmtext objMText strnewval)
 (setq getmtext (ssget "_X" '((0 . "MTEXT")(1 . "TEST")))
       objMText (vlax-ename->vla-object (entlast))
       strnewval (getstring T "Enter New Text: ")
 )
 (if strnewval (vlax-put-property objmtext "TEXTSTRING" strnewval)
)
 (vlax-release-object objmtext)

)

Link to comment
Share on other sites

  • Replies 44
  • Created
  • Last Reply

Top Posters In This Topic

  • jmerch

    17

  • alanjt

    10

  • Tharwat

    10

  • Lee Mac

    8

Top Posters In This Topic

Be careful that the text string is case sensitive .

 

So here it goes buddy.:)

(defun c:test (/ new ss)
 ; Tharwat 24.02. 2010
 (if (and (setq new (getstring T "Enter New Text: "))
          (setq ss (ssget "_X" '((0 . "MTEXT") (1 . "test"))))
     )
   ((lambda (i / ss1 e)
      (while
        (setq ss1 (ssname ss (setq i (1+ i))))
         (entupd
           (cdr
             (assoc
               -1
               (entmod
                 (subst (cons 1 new) (assoc 1 (setq e (entget ss1))) e)
               )
             )
           )
         )
      )
    )
     -1
   )
   (Alert
     "\n Your replaced text is not found in the drawing....."
   )
 )
 (princ)
)

 

Tharwat

Link to comment
Share on other sites

  (setq getmtext (ssget "_X" '((0 . "MTEXT")(1 . "TEST"))) [color=red]<-Select [u]all[/u] MText with "TEST" value[/color]
       objMText (vlax-ename->vla-object (entlast)) [color=red]<- converts the last created object to a vla-object (not what you want to do here)[/color]

Link to comment
Share on other sites

  (setq getmtext (ssget "_X" '((0 . "MTEXT")(1 . "TEST"))) [color=red]<-Select [u]all[/u] MText with "TEST" value[/color]
       objMText (vlax-ename->vla-object (entlast)) [color=red]<- converts the last created object to a vla-object (not what you want to do here)[/color]

 

I know I'm selecting all Mtext with "Test" value, I understand the ssget function. But on the entlast, i thought that just grabbed the entity data from the last object, I didn't realize it converts the last created object.

 

In order to edit the Mtext, from what I can tell it has to be a vla-object which is why it's getting converted, right?

Link to comment
Share on other sites

I know I'm selecting all Mtext with "Test" value, I understand the ssget function. But on the entlast, i thought that just grabbed the entity data from the last object, I didn't realize it converts the last created object.

 

In order to edit the Mtext, from what I can tell it has to be a vla-object which is why it's getting converted, right?

Your selection with ssget has no bearing on (entlast). You need to step through the selectionset with repeat/while or vla-get-activeselectinset.

Link to comment
Share on other sites

Check this out in VL .

 

(defun c:test (/ new ss)(vl-load-com)
 ; Tharwat 24.02. 2010
 (if (and (setq new (getstring T "Enter New Text: "))
          (setq ss (ssget "_X" '((0 . "MTEXT") (1 . "test"))))
     )
   ((lambda (i / ss1)
      (while
        (setq ss1 (ssname ss (setq i (1+ i))))
         (vla-put-textstring (vlax-ename->vla-object ss1) new)
      )
    )
     -1
   )
   (Alert
     "\n Your replaced text is not found in the drawing....."
   )
 )
 (princ)
)

 

Tharwat

Link to comment
Share on other sites

@alan: Maybe I'm not understanding you. I was doing the ssget to "grab" the mtext on my drawing, and thought I needed to convert this to vla-obect. What contains the properties (text string) of mtext? vla-get-activeselectionset isn't a function.

 

@Tharwat: Same result, it asks 'Select Objects in Current Drawing or Xref'...then isolates it. Why is it doing that? it's not asking me to enter any text or anything...

 

I appreciate the help from both of you.

Link to comment
Share on other sites

The two routines that I already posted for you are working normally in Arch. Autocad , but I do not have MEP cad to check these codes for it .

 

Anyway , can you please copy your command line after fails of the routine with your version ?

 

Tharwat

Link to comment
Share on other sites

it's not really "failing", just not doing what I want...I wonder what in your code has dependency on the ACAD version...

 

Command: test

Select Objects in Current Drawing or Xref:

Select Objects in Current Drawing or Xref:

Regenerating model.

Link to comment
Share on other sites

it's not really "failing", just not doing what I want...I wonder what in your code has dependency on the ACAD version...

 

Command: test

Select Objects in Current Drawing or Xref:

Select Objects in Current Drawing or Xref:

Regenerating model.

LoL, that's a different program all together. Make sure you are actually loading the routine.

Link to comment
Share on other sites

LMAO....WTF...I've been loading it correctly and am pretty sure I don't have any commands called TEST....but I changed this command to something else and it seems to be working now....so thank you very much Tharwat!!!

 

And I'm still curious about my questions to you alan, just so I can understand it better.

Link to comment
Share on other sites

....but I changed this command to something else and it seems to be working now....so thank you very much Tharwat!!!

 

 

Could you please post what you have changed to let routines working with your MEP cad ?

 

Just curious about other cad versions.

Link to comment
Share on other sites

LMAO....WTF...I've been loading it correctly and am pretty sure I don't have any commands called TEST....but I changed this command to something else and it seems to be working now....so thank you very much Tharwat!!!

 

And I'm still curious about my questions to you alan, just so I can understand it better.

(defun c:Test (/ ss str)
 (if (and (setq ss (ssget "_X" '((0 . "MTEXT") (1 . "TEST")))) ; select mtext
          (/= "" (setq str (getstring T "\nSpecify string: "))) ; specify string & /= ""
     )
   (progn
     ;; below steps through active selectionset
     (vlax-for obj (setq ss (vla-get-activeselectionset ; active selectionset 
                              (vla-get-activedocument (vlax-get-acad-object))
                            )
                   )
       (vla-put-textstring obj str)
     )
     (vla-delete ss) ; delete selection set (not acutal objects, just the selection set)
   )
 )
 (princ)
)

Link to comment
Share on other sites

Could you please post what you have changed to let routines working with your MEP cad ?

 

Just curious about other cad versions.

 

I was just saying I changed your C:test to something like C:test1 and it worked....so somewhere I must have a routine loaded that uses TEST for the command...other than that the last code you posted works as is.

Link to comment
Share on other sites

I was just saying I changed your C:test to something like C:test1 and it worked....so somewhere I must have a routine loaded that uses TEST for the command...other than that the last code you posted works as is.

 

Yeah.... that's it .... .

 

Tharwat

Link to comment
Share on other sites

Please note jmerch that if your intended to use any of my routines that I have already posted for you, you should modify the first line of

routines to ...... which is .

 

([color=red][b]/=[/b][/color] (setq new (getstring T "Enter New Text: ")) [b][color=red]""[/color][/b])

 

Because if a user used the routines and pressed enter instead of supporting a string , the routine would replace the found Mtexts to nil string (invisible).

 

Tharwat

Link to comment
Share on other sites

Please note jmerch that if your intended to use any of my routines that I have already posted for you, you should modify the first line of

routines to ...... which is .

 

([color=red][b]/=[/b][/color] (setq new (getstring T "Enter New Text: ")) [b][color=red]""[/color][/b])

 

Because if a user used the routines and pressed enter instead of supporting a string , the routine would replace the found Mtexts to nil string (invisible).

 

Tharwat

Look who's learning.
Link to comment
Share on other sites

To add:

 

  1. In these situations, I would stay clear of using the 1 dxf code for finding text. It's case sensitive and only searches the first 250 characters.
  2. I would avoid the use of vla-get-textstring since there is a huge bug in that it will not return a symbol properly, and instead give you a "?".

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...