Author Topic: ==={challenge}=== Find colored text in mtext  (Read 2388 times)

0 Members and 1 Guest are viewing this topic.

Jeremy

  • Guest
==={challenge}=== Find colored text in mtext
« on: November 30, 2012, 10:52:21 PM »
Assume that we have a multiple line piece of mtext and that the only form of formatting that we will allow is changing the color of the text. The challenge is to have a function that takes a piece of mtext and a color number and returns a list of lists each containing

1. the unformatted substring that is colored
2. the line number that the substring appears on
3. the position index of where the substring starts in the unformatted line

I have been unsuccessful with this so far but I'm sure someone will take on the challenge. :-D

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: ==={challenge}=== Find colored text in mtext
« Reply #1 on: December 01, 2012, 12:48:15 AM »
By line-number, do you mean the line shown in the drawing? I.e. the word-wrapped line. Or the physical line - i.e. "paragraph" like in where an Enter is pressed when editing the mtext.

Is the text going to be straight-forward text? I.e. nothing like fractions or super/sub-scripts or %% codes or octal or unicode?

Are we forced to only work through the format codes? Or can we use the unnamed block definition to get these values?

Do you only want to get the formatted pieces? Or would you also want the pieces which have no formatting code to them?
« Last Edit: December 01, 2012, 12:55:27 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Find colored text in mtext
« Reply #2 on: December 01, 2012, 02:23:27 AM »
By line-number, do you mean .......
[size=78%]Is the text going to be .....[/size]
[size=78%]Are we forced to only work .....[/size]
[size=78%]Do you only want to get the....[/size]


What he said  :) 

Jeremy

  • Guest
Re: ==={challenge}=== Find colored text in mtext
« Reply #3 on: December 01, 2012, 09:24:27 PM »
By line-number, do you mean the line shown in the drawing? I.e. the word-wrapped line. Or the physical line - i.e. "paragraph" like in where an Enter is pressed when editing the mtext.

Is the text going to be straight-forward text? I.e. nothing like fractions or super/sub-scripts or %% codes or octal or unicode?

Are we forced to only work through the format codes? Or can we use the unnamed block definition to get these values?

Do you only want to get the formatted pieces? Or would you also want the pieces which have no formatting code to them?

The lines are the ones created by Enters.
Text is straightforward.
You can use either format codes or unnamed block.

If you can break the line into chunks in order and somehow mark the pieces that are colored so they can be identified in some way, that would be fine.

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Find colored text in mtext
« Reply #4 on: December 01, 2012, 11:48:29 PM »
Post an example of the preferred result jeremy
« Last Edit: December 04, 2012, 01:42:51 AM by pBe »

Jeremy

  • Guest
Re: ==={challenge}=== Find colored text in mtext
« Reply #5 on: December 02, 2012, 03:56:03 PM »
In my example here I am using square brackets around the colored text

This is the [first] line
This [is the] second line
[This is the third line]
This is the fourth line

return

(
  ("This is the " '("first") " line")
  ("This " '("is the") " second line")
  ('("This is the third line"))
  ("This is the fourth line")
)



irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: ==={challenge}=== Find colored text in mtext
« Reply #6 on: December 03, 2012, 12:19:28 AM »
If I read your OP, shouldn't that be something like this:
Code: [Select]
(
  ("This is the " 0 0)
  ("first" 0 12)
  (" line" 0 17)
  ("This " 1 0)
  ("is the" 1 5)
  ; ...
)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Find colored text in mtext
« Reply #7 on: December 03, 2012, 05:33:48 AM »
If its the former
Code: [Select]

(defun c:ShowC (/ _split _mark ss str StringList)
  (defun _split   (st ls mod / a)
    (if   (Setq a (vl-string-search mod st))
      (progn
   (setq ls (cons (substr st 1 a) ls))
   (_split (substr st (+ a 3)) ls mod)
      ) ;_ end of progn
      (reverse (cons st ls))
    ) ;_ end of if
  ) ;_ end of defun
  (defun _mark (st_ lst_ / lst_ lst)
    (setq x (mapcar (function (lambda (x)
            (vl-string-search x st_)
               ) ;_ end of lambda
          ) ;_ end of function
          '("{\\C" ";" "}")
       ) ;_ end of mapcar
    ) ;_ end of setq
    (setq lst
      (if (vl-some   '(lambda (j)
            (if (null j)
              t
            ) ;_ end of if
          ) ;_ end of lambda
         x
          ) ;_ end of vl-some
        (list st_)
        (list
          (substr st_ 1 (car x))
          (list
       (substr st_ (+ (cadr x) 2) (1- (- (caddr x) (cadr x))))
          ) ;_ end of list
          (substr st_ (+ (caddr x) 2))
        ) ;_ end of list
      ) ;_ end of if
     
    ) ;_ end of setq
    (if   (vl-string-search "{\\C" (last lst))
      (_mark (last lst) (app_ lst_ (vl-remove (last lst) lst) ))
      (app_ lst_ lst)
    ) ;_ end of if
  ) ;_ end of defun
(setq app_ (lambda (j k)
        (append (if j j '(""))
       k))
      )
  (if (setq ss (ssget '((0 . "MTEXT"))))
    (repeat (sslength ss)
      (Setq str (vla-get-textstring (vlax-ename->vla-object (ssname ss 0))))
      (Setq StringList (_split str ls "\\P"))
      (foreach itm StringList
   (print (vl-remove "" (_mark itm nil)))
      ) ;_ end of foreach
      (ssdel (ssname ss 0) ss)
    ) ;_ end of repeat
  ) ;_ end of if
  (princ)
)

The code needs to be modified in case there are more than one "colored text"  per line

EDIT: Update code to process multiple "colored text" per line
EDIT: Cons to Append
EDIT: Found another bug  :-[
snipp...
Code: [Select]
....(setq app_ (lambda (j k)
        (append (if j j '(""))
       k))
      )
.....

....(_mark (last lst) (app_ lst_ (vl-remove (last lst) lst) ))....
« Last Edit: December 04, 2012, 04:11:11 AM by pBe »

Jeremy

  • Guest
Re: ==={challenge}=== Find colored text in mtext
« Reply #8 on: December 03, 2012, 10:01:14 PM »
Irneb you are close. If you are using this method then only return the data for the colored text.

(
  ("first" 0 12)
  ("is the" 1 5)
  ; ...
)

If you return a list for all then there is no way to know which text was colored and which wasn't. And yes pBe there can be more than one colored text per line.

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Find colored text in mtext
« Reply #9 on: December 03, 2012, 10:37:36 PM »
CODE UPDATED [again]
Too many bugs crawling.....


HTH
« Last Edit: December 04, 2012, 01:26:43 AM by pBe »

pBe

  • Bull Frog
  • Posts: 402
Re: ==={challenge}=== Find colored text in mtext
« Reply #10 on: December 05, 2012, 06:34:29 AM »
CODE UPDATED [again]
Too many bugs crawling.....
...


Today:
Bugs= crickets....