Lisp Code to Convert MText Selected Objects to Text

Lisp Code to Convert MText Selected Objects to Text

GeryKnee
Advocate Advocate
6,614 Views
6 Replies
Message 1 of 7

Lisp Code to Convert MText Selected Objects to Text

GeryKnee
Advocate
Advocate

Need for a code that

1, Prompts For Selection "select MText objects".

2, For any selected object

   Checks if Selected Object is MT

   if is MText Indeed, Converts to Text

           (Creates a Text Object on the same insertion object, that lies onthe same layer

            end has the same Color

            Its Text Property sould be all the Compination of all MText Lines without

            CR character but if prev line ends to char different to " " or "." a space has to be

            added between those MText Lines)

3. Deletes the MText Object.

Thanks,

Gery

0 Likes
Accepted solutions (1)
6,615 Views
6 Replies
Replies (6)
Message 2 of 7

dlanorh
Advisor
Advisor
Accepted solution
;;MText to Text type "mt2t" to run
;;Author: Ron Harman  Copyright © 2018

(defun rh:regexp ( pattern t_str n_str / regex result rtn)
 (setq regex (vlax-create-object "VBScript.RegExp"))
 (vlax-put-property regex 'Pattern pattern) 
 (vlax-put-property regex 'Global :vlax-true)
 (setq result (vlax-invoke-method regex 'Replace t_str n_str))
 (vlax-release-object regex)
 (setq rtn result) 
);end_defun (rh:regexp)

(defun c:mt2t ( / *error* c_doc ent obj)

	(defun *error* ( msg )	
		(if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
		(if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nOops an Error : " msg " occurred.")))
		(princ)
	);end_defun *error*
	

  (setq c_doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
	
	(if (= 8 (logand 8 (getvar 'UNDOCTL))) (vla-endundomark c_doc))
	(vla-startundomark c_doc)

  (if (setq obj (vlax-ename->vla-object (setq ent (car (entsel "\nSelect MText to convert : ")))))
    (if (eq (vla-get-objectname obj) "AcDbMText")
      (progn
        (vla-put-TextString obj (rh:regexp "\\\\[P]" (vla-get-TextString obj) " "))
        (vl-cmdf "explode" ent "")
      );end_progn
      (alert "Not an MText Entity")
		);end_if
	);end_if
	(if (= 8 (logand 8 (getvar 'UNDOCTL))) (vla-endundomark c_doc))
  (princ)
);end_defun (main)
(princ)

This should accomplish what you require, if i have understood your request correctly.

 

I am not one of the robots you're looking for

0 Likes
Message 3 of 7

dlanorh
Advisor
Advisor

Just noticed it is missing (vl-load-com)

This needs to be above the first line so it reads

(vl-load-com)
;;MText to Text type "mt2t" to run

 

I am not one of the robots you're looking for

0 Likes
Message 4 of 7

zph
Collaborator
Collaborator

The EXPLODE command converts MTEXT to TEXT.

 

Will explode not work for you?

 

~Z

0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant

@zph wrote:

.... 

Will explode not work for you?

....


That leaves multiple  Text objects if there's any word-wrapping in the Mtext original [re-read Message 1].

 

You can just select all Mtext objects you want to do this to, and in the Properties palette change the Columns setting to No Columns, then [in quickie experimentation, it seems I have to unselect and then select them again to be able to] change the Defined width to 0 [zero], which will string each one out into a single line, then  EXPLODE them.

Kent Cooper, AIA
0 Likes
Message 6 of 7

akbarsaidumohamed
Observer
Observer

the lisp showing some error as shown in below. appreciate you able to fix it. 

 

 

Command: MT2T
Select MText to convert :
Oops an Error : no function definition: VLAX-ENAME->VLA-OBJECT occurred.

0 Likes
Message 7 of 7

Kent1Cooper
Consultant
Consultant

@akbarsaidumohamed wrote:

.... no function definition: VLAX-ENAME->VLA-OBJECT ....


See Message 3.  Put this line at the top of the file:
(vl-load-com)

Kent Cooper, AIA
0 Likes