UnFormat String

Function Syntax (LM:UnFormat <str> <mtx>)
Current Version 1.1
Arguments
Symbol Type Description
str String String from which to remove formatting codes
mtx Boolean MText Compatibility Flag: T if string is to be used in MText
Returns
Type Description
String Supplied string with all formatting codes removed

Program Description

This subfunction utilises the RegularExpression (RegExp) Object to remove all MText formatting codes from a supplied string.

MText formatting codes arise when formatting (such as variation in text height, colour, paragraph style, etc) is applied to text from within the MText Editor. This formatting is encoded in the TextString of the MText object.

The UnFormat function has two modes of operation determined by the setting of the 'MText Compatibility Flag' argument. If the resultant string is be used in an MText object (this includes Dimensions, Multileaders & Multiline Attributes) this argument should be set to T, as extra backslashes are required to avoid elements of the string being interpretted as MText formatting codes.

For a more detailed explanation as to why this argument is required, see the description on my Get True Content page.

Example of MText Formatting Codes

Where MText formatting codes are concerned, an ostensibly simple string can contain an surprisingly long text string property; observe the following example:

MText as Displayed:

Unformat Example.png

TextString property for the Above Object:

"\\A1;{\\T1.1;\\C1;a\\C256; \\H1.2x;\\C2;b\\H0.83333x;\\C256; \\C3;c} {\\H0.7x;\\C4;\\S1#2;}"

String returned using UnFormat subfunction:

"a b c 1/2"

UnFormat Function

Select all
;;-------------------=={ 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
            )
        )
    )
)
(vl-load-com)

Example Function Call

_$ (LM:UnFormat "{\\O\\C1;L\\C256;ee} {\\L\\C2;M\\C256;ac}" nil)
"Lee Mac"

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010