Get True Content

Function Syntax (LM:GetTrueContent <RegExp> <entity> <*dtextstring> <*mtextstring>)
Current Version 1.0
Download GetTrueContent.lsp
View HTML Version GetTrueContent.html
Arguments
Symbol Type Description
RegExp VLA-Object RegularExpressions (RegExp) Object
entity EName Entity for which to retrieve the true text content
*dtextstring Symbol (Output) Unformatted string compatible with Text Objects
*mtextstring Symbol (Output) Unformatted string compatible with MText Objects
Returns
Type Description
Symbol This function always returns nil

Program Description

This subfunction is an evolution of my previous Unformat String subfunction, designed to be more efficient, robust and user-friendly.

The subfunction is designed to retrieve the full textstring associated with a supplied entity and strip the string of all formatting. The string is then manipulated into two formats, compatible with Text & MText objects, and these are bound to the supplied output symbols.

Why two outputs?

Because some strings are displayed differently when used in Text & MText due to the formatting codes utilised by MText objects. These formatting codes are stored within the MText string and hence, should a string inadvertently contain such codes, they will be interpreted as formatting for the MText object.

Consider, for example, the following string:

"{ C:\\Computer\\LISP }"

If used in a Text object, it may be displayed like this:

TextDisplay.png

However, the same string used in MText would be displayed like this:

MTextDisplay.png

This demonstrates why two different outputs are required depending upon whether the string is destined for use in a Text or MText object. My subfunction ensures the string is displayed identically in both objects.

The subfunction requires four arguments: the RegularExpressions (RegExp) object, the entity for which the unformatted text content should be retrieved, and two symbols to house the output compatible with both Text & MText objects.

It is the caller's responsibility to create and release the RegularExpressions (RegExp) object. This object may be created using the programmatic identifier (ProgID): "VBScript.RegExp".

Example Test Function

This test function operates in a similar way to my CopyText program: prompting the user for a selection of a source object for which to retrieve an unformatted textstring, and proceeding to copy this textstring to a destination object.

The result should be identical, whether the user picks, as a source object, formatted Text (using %%U, %%O operators etc), or formatted MText, and either Text or MText as a destination object.

Get True Content.gif
Select all
(defun c:test ( / *error* _AllowsFormatting RegExp src des text mtext )
  (vl-load-com)
  ;; © Lee Mac 2010

  (defun *error* ( msg )
    (if RegExp (vlax-release-object RegExp))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (defun _AllowsFormatting ( entity / object )    
    (or (wcmatch (cdr (assoc 0 (entget entity))) "MTEXT,MULTILEADER")      
      (and
        (eq "ATTRIB" (cdr (assoc 0 (entget entity))))
        (vlax-property-available-p (setq object (vlax-ename->vla-object entity)) 'MTextAttribute)
        (eq :vlax-true (vla-get-MTextAttribute object))
      )
    )
  )

  (while (and (setq src (car (nentsel "\nSelect Source Object: ")))
              (not (wcmatch (cdr (assoc 0 (entget src))) "*TEXT,ATTRIB,MULTILEADER")))
    (princ "\n** Source Object must Contain Text **")
  )

  (while (and (setq des (car (nentsel "\nSelect Destination Object: ")))
              (not (wcmatch (cdr (assoc 0 (entget des))) "*TEXT,ATTRIB,MULTILEADER")))
    (princ "\n** Destination Object must Contain Text **")
  )

  (if (and src des)
    (progn
      (setq RegExp (vlax-get-or-create-object "VBScript.RegExp"))      
      (LM:GetTrueContent RegExp src 'text 'mtext)

      (vla-put-TextString (vlax-ename->vla-object des)
        (if (_AllowsFormatting des)
          mtext
          text
        )
      )

      (vlax-release-object RegExp)
    )
  )

  (princ)
)

textsize

increase · reset · decrease

Designed & Created by Lee Mac © 2010