Jump to content

extract the content of a Mtext...


leonucadomi

Recommended Posts

Good night people

 

I use this lsp to extract the content of a text... but it doesn't work for Mtext

 

somebody could help me...

 

 

I need to select an mtext and its content as shown, enter it into a variable

 

 

 

(defun c:pru2 (/)

(setq e (car (entsel "\nSelection text: ")))
(setq line (cdr (assoc 1 (entget e))))

(princ line)
(princ)

  );fin defun

pru2.lsp

Link to comment
Share on other sites

9 minutes ago, leonucadomi said:

Good night people

 

I use this lsp to extract the content of a text... but it doesn't work for Mtext

 

somebody could help me...

 

 

I need to select an mtext and its content as shown, enter it into a variable

 

 

 

(defun c:pru2 (/)

(setq e (car (entsel "\nSelection text: ")))
(setq line (cdr (assoc 1 (entget e))))

(princ line)
(princ)

  );fin defun

pru2.lsp 160 B · 0 downloads

 

Select 1 object via entsel. 

The result of entsel is a list of pairs. ( entity_name picked_coordinate ) 

coordinates are not the coordinates of an object. 

 

Here we extract the entity name as car. 

Then entget to get the full attribute list of that entity. 

 

Here we get the content of the text as assoc 1. 

Remove the preceding (1 . with cdr and get only the contents. 

 

Both text and mtext have the content of the text in the attribute (1 . 

 

so, Your problem may be something else.

 

If you want to check it out, you can use the dumpit command to see the full entity attributes of that text.

;; Dump all DXF Group Data             
(defun c:DumpIt (/ e)
  (if (setq SS (ssget))
    (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
      (mapcar 'print (entget ent '( "*")))
    )
  )
  (textscr)
  (princ)
)

 

Maybe it's because of the attribute character that exists only in mtext. This can be solved by LM:Unformat. 

or mtext inside a block

Edited by exceed
Link to comment
Share on other sites

Use THIS to get a 'clean' string.

 

Or if these are one line mtext, exploding them will work too.

Edited by ronjonp
Link to comment
Share on other sites

Adding to Ronjonp, the mtext displayed on the screen has invisible formatting data embedded in it, the \A1;{H1..... and so on to say how to display the text, and that is what is shown in the properties. To make useable text you have to remove these formatting codes, which Lee Mac des in the Unformat Lisp above

Link to comment
Share on other sites

In the Mtext Editor highlight all the text you wish to remove formatting for,  then from the [Text Editor] contextual tab's [Formatting] panal there's a drop-down to remove Character Formatting, Paragraph Formatting, or All Formatting.

https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-Core/files/GUID-7B8A0365-D9D6-4113-BC9E-F2C0C823382F-htm.html

Link to comment
Share on other sites

1 hour ago, tombu said:

In the Mtext Editor highlight all the text you wish to remove formatting for,  then from the [Text Editor] contextual tab's [Formatting] panal there's a drop-down to remove Character Formatting, Paragraph Formatting, or All Formatting.

https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-Core/files/GUID-7B8A0365-D9D6-4113-BC9E-F2C0C823382F-htm.html

 

However I don't think that works in a LISP does it? I think the OP needs to retain the original text as it is and to copy it's contents (without formatting) to a LISP variable?

Link to comment
Share on other sites

On 6/1/2022 at 7:05 AM, leonucadomi said:

I tried to extract the content of this text but I don't know why it has other content internally

image.thumb.png.a33b79edb537f510000a4fc87509951c.pngimage.thumb.png.1f971deec67901be7f15d8ef57a24a1b.png

Drawing1.dwg 31.9 kB · 1 download

(defun c:pru2 (/)
  (setq e (car (entsel "\nSelection text: ")))
  (setq line (LM:Unformat (cdr (assoc 1 (entget e))) nil))
  (princ line)

  (princ)
);fin defun

(vl-load-com)

;;-------------------=={ UnFormat String }==------------------;;
;;                                                            ;;
;;  Returns a string with all MText formatting codes removed. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  str - String to Process                                   ;;
;;  mtx - MText Flag (T if string is for use in MText)        ;;
;;------------------------------------------------------------;;
;;  Returns:  String with formatting codes removed            ;;
;;------------------------------------------------------------;;

(defun LM:UnFormat ( str mtx / _replace rx )

    (defun _replace ( new old str )
        (vlax-put-property rx 'pattern old)
        (vlax-invoke rx 'replace str new)
    )
    (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
        (progn
            (setq str
                (vl-catch-all-apply
                    (function
                        (lambda ( )
                            (vlax-put-property rx 'global     actrue)
                            (vlax-put-property rx 'multiline  actrue)
                            (vlax-put-property rx 'ignorecase acfalse) 
                            (foreach pair
                               '(
                                    ("\032"    . "\\\\\\\\")
                                    (" "       . "\\\\P|\\n|\\t")
                                    ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
                                    ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
                                    ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
                                    ("$1"      . "[\\\\]({)|{")
                                )
                                (setq str (_replace (car pair) (cdr pair) str))
                            )
                            (if mtx
                                (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
                                (_replace "\\"   "\032" str)
                            )
                        )
                    )
                )
            )
            (vlax-release-object rx)
            (if (null (vl-catch-all-error-p str))
                str
            )
        )
    )
)

 

2022-06-02%2009;02;46.PNG

 

Like this,

MTEXT can specify more details than specifying font, width, and width in dtext. 

These are stored in text content, not in Properties. 

Therefore, if you want to use the contents of MTEXT in other places, 

you need a format remover such as LM:Unformat.

Edited by exceed
Link to comment
Share on other sites

Just to note that for long mtext strings any characters after the 250th are stored in one or more DXF code 3 (each has 250 characters)

 

For example and very quick this will get up to 500 characters, though it might be better to loop through the mtext entity definition 'strcat'  each (assoc 3 ) to line - but this was quick by way of an example

 

(defun c:pru2 (/)
  (setq e (car (entsel "\nSelection text: ")))
  (setq line (LM:Unformat (cdr (assoc 1 (entget e))) nil))
***
  (setq line (strcat (LM:Unformat (cdr (assoc 3 (entget e))) nil) line))
***
  (princ line)

  (princ)
);fin defun

 

Link to comment
Share on other sites

39 minutes ago, Steven P said:

Just to note that for long mtext strings any characters after the 250th are stored in one or more DXF code 3 (each has 250 characters)

 

For example and very quick this will get up to 500 characters, though it might be better to loop through the mtext entity definition 'strcat'  each (assoc 3 ) to line - but this was quick by way of an example

 

(defun c:pru2 (/)
  (setq e (car (entsel "\nSelection text: ")))
  (setq line (LM:Unformat (cdr (assoc 1 (entget e))) nil))
***
  (setq line (strcat (LM:Unformat (cdr (assoc 3 (entget e))) nil) line))
***
  (princ line)

  (princ)
);fin defun

 

 

Oh it's new to me. thanks, 

I run away to (vl-load-com). Because it is difficult for me to find and think case of multiple (3 . ~

I tested vlax-get-property obj 'textstring by 10000 character mtext. 

get the textstring was fine. but There was an error in vlax-dump-object i don't know why

(defun c:pru2 ( / e obj line )
  (setq e (car (entsel "\nSelection text: ")))
  (setq obj (vlax-ename->vla-object e))
  (setq line (LM:Unformat (vlax-get-property obj 'textstring) nil))
  (princ line)

  (princ)
);fin defun

(vl-load-com)

;;-------------------=={ UnFormat String }==------------------;;
;;                                                            ;;
;;  Returns a string with all MText formatting codes removed. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  str - String to Process                                   ;;
;;  mtx - MText Flag (T if string is for use in MText)        ;;
;;------------------------------------------------------------;;
;;  Returns:  String with formatting codes removed            ;;
;;------------------------------------------------------------;;

(defun LM:UnFormat ( str mtx / _replace rx )

    (defun _replace ( new old str )
        (vlax-put-property rx 'pattern old)
        (vlax-invoke rx 'replace str new)
    )
    (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
        (progn
            (setq str
                (vl-catch-all-apply
                    (function
                        (lambda ( )
                            (vlax-put-property rx 'global     actrue)
                            (vlax-put-property rx 'multiline  actrue)
                            (vlax-put-property rx 'ignorecase acfalse) 
                            (foreach pair
                               '(
                                    ("\032"    . "\\\\\\\\")
                                    (" "       . "\\\\P|\\n|\\t")
                                    ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
                                    ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
                                    ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
                                    ("$1"      . "[\\\\]({)|{")
                                )
                                (setq str (_replace (car pair) (cdr pair) str))
                            )
                            (if mtx
                                (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
                                (_replace "\\"   "\032" str)
                            )
                        )
                    )
                )
            )
            (vlax-release-object rx)
            (if (null (vl-catch-all-error-p str))
                str
            )
        )
    )
)

 

Edited by exceed
  • Like 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...