Author Topic: clean up the formatting of a MTEXT string  (Read 3851 times)

0 Members and 1 Guest are viewing this topic.

domenicomaria

  • Swamp Rat
  • Posts: 725
clean up the formatting of a MTEXT string
« on: October 24, 2023, 10:23:02 AM »
I need to clean up the formatting of a MTEXT string

For example, I have this string
"{\\Q15.00;DOUBLE BED\nperimeter 31.37336 m\narea 61.14046 m2\nvolume 550.26412 m3\nlateral surface 282.36021 m2\nheight 9.00000 m}"

 and I want to get this list of strings as a result:
("DOUBLE BED" "perimeter 31.37336 m" "area 61.14046 m2" "volume 550.26412 m3" "lateral surface 282.36021 m2" "height 9.00000 m3")

I know (even if it's too tiring to try to understand it) Lee Mac's LM:UNFORMAT function ...
... but it also removes the "\n" characters that I need to divide the string into many sub-strings

It returns :
"DOUBLE BED perimeter 31.37336 m area 61.14046 m2 volume 550.26412 m3 lateral surface 282.36021 m2 height 9.00000 m"

while i need :
"DOUBLE BED\nperimeter 31.37336 m\narea 61.14046 m2\nvolume 550.26412 m3\nlateral surface 282.36021 m2\nheight 9.00000 m"

...

any suggestion ?

thank you



Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: clean up the formatting of a MTEXT string
« Reply #1 on: October 24, 2023, 11:20:26 AM »
Do you need:
("DOUBLE BED" "perimeter 31.37336 m" "area 61.14046 m2" "volume 550.26412 m3" "lateral surface 282.36021 m2" "height 9.00000 m3")
or
"DOUBLE BED\nperimeter 31.37336 m\narea 61.14046 m2\nvolume 550.26412 m3\nlateral surface 282.36021 m2\nheight 9.00000 m"

?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: clean up the formatting of a MTEXT string
« Reply #2 on: October 24, 2023, 11:42:11 AM »
Code: [Select]
(ALE_String2List
  (vl-string-left-trim
    "{\\Q15.00;"
    (vl-string-right-trim
      "}"
      "{\\Q15.00;DOUBLE BED\nperimeter 31.37336 m\narea 61.14046 m2\nvolume 550.26412 m3\nlateral surface 282.36021 m2\nheight 9.00000 m}"
    )
  )
  "\n"
)
==> ("DOUBLE BED" "perimeter 31.37336 m" "area 61.14046 m2" "volume 550.26412 m3" "lateral surface 282.36021 m2" "height 9.00000 m")


; Marc'Antonio Alessi, Italy
;
; Function: ALE_String2List
;
; Version 1.00 - November 2001
;
; Description:
;   convert a string into a list
;
; Arguments:
;   InpStr = string [STR]
;   CarDlm = delimiter [STR] 1 character
;
(defun ALE_String2List (InpStr CarDlm / SttPos EndPos TmpLst)
  (setq
    CarDlm (ascii CarDlm)   SttPos 0
    EndPos (vl-string-position CarDlm InpStr)
  )
  (while EndPos
    (setq
      TmpLst (cons (substr InpStr (1+ SttPos) (- EndPos SttPos)) TmpLst)
      SttPos (1+ EndPos) EndPos (vl-string-position CarDlm InpStr SttPos)
    )
  )
  (reverse (cons (substr InpStr (1+ SttPos)) TmpLst))
)\

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: clean up the formatting of a MTEXT string
« Reply #3 on: October 24, 2023, 12:28:25 PM »
The problem is not converting a string into a list of strings given a delimiter...
(I wrote my STR>STR-LST routine ...)

the format of a Mtext string may be more complex

It doesn't always start with "{\\Q15.00;"

and may internally contain many special characters to remove...

take a look to Lee Mac's LM:UNFORMAT function
and you will discover that the problem is more complex

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: clean up the formatting of a MTEXT string
« Reply #4 on: October 24, 2023, 12:50:47 PM »
if you don't want to modify original Lee's code, then do a temporary substitution with vl-string-translate before unformating and undo it afterwards

p.s. don't use \032 as a substitution character ;)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: clean up the formatting of a MTEXT string
« Reply #5 on: October 25, 2023, 03:19:20 AM »

>> I know (even if it's too tiring to try to understand it) Lee Mac's LM:UNFORMAT function ...
>> ... but it also removes the "\n" characters that I need to divide the string into many sub-strings

(UnFormat2 "{\\Q15.00;DOUBLE BED\nperimeter 31.37336 m\narea 61.14046 m2\nvolume 550.26412 m3\nlateral surface 282.36021 m2\nheight 9.00000 m}" T)
==>                           "DOUBLE BED\nperimeter 31.37336 m\narea 61.14046 m2\nvolume 550.26412 m3\nlateral surface 282.36021 m2\nheight 9.00000 m"
Code: [Select]

(defun UnFormat2 ( str mtx / _replace rx )
; LM:UnFormat original code by Lee Mac modified in ***
    (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|\\t") ; *** ex (" "       . "\\\\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
            )
        )
    )
)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: clean up the formatting of a MTEXT string
« Reply #6 on: October 25, 2023, 03:23:07 AM »
How did you get the \n in the MText? Isn’t it supposed to be \P ?

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: clean up the formatting of a MTEXT string
« Reply #7 on: October 25, 2023, 04:36:57 AM »
How did you get the \n in the MText? Isn’t it supposed to be \P ?
someone gave me a DWG that contains MTEXTs that contain "\n"

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: clean up the formatting of a MTEXT string
« Reply #8 on: October 25, 2023, 04:43:08 AM »
Ah, hate when that happens  :|

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: clean up the formatting of a MTEXT string
« Reply #9 on: October 25, 2023, 04:44:47 AM »
Marco, thank you.

Your modified Lee's code works well,
because you eliminated the removal of "\n"

ciao

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: clean up the formatting of a MTEXT string
« Reply #10 on: October 25, 2023, 04:46:12 AM »
if you don't want to modify original Lee's code, then do a temporary substitution with vl-string-translate before unformating and undo it afterwards

p.s. don't use \032 as a substitution character ;)

I don't understand what you want mean ... sorry!

kozmos

  • Newt
  • Posts: 114
Re: clean up the formatting of a MTEXT string
« Reply #11 on: October 25, 2023, 06:15:45 AM »
if you don't want to modify original Lee's code, then do a temporary substitution with vl-string-translate before unformating and undo it afterwards

p.s. don't use \032 as a substitution character ;)

I don't understand what you want mean ... sorry!

If you want to keep "\n" that Lee's fucntion will remove, you can translate it into a particular character (such as \007, you can not use \032 because it is already used in Lee's function) before passing the content string into Lee's function, after that, you can translate \007 back to "\n". By doing this, you no need to modify Lee's function.

Sure you can modify Lee's function to remove "\n", it will also work.
KozMos Inc.

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: clean up the formatting of a MTEXT string
« Reply #12 on: October 25, 2023, 07:18:36 AM »
@KozMos
ok
thank you !

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: clean up the formatting of a MTEXT string
« Reply #13 on: October 27, 2023, 10:30:22 AM »
I'm pleased that the function is proving useful  :-)

JGA

  • Mosquito
  • Posts: 10
Re: clean up the formatting of a MTEXT string
« Reply #14 on: December 13, 2023, 12:24:52 PM »