Author Topic: {Challenge}Get the textstrings in each line of the mtext  (Read 12106 times)

0 Members and 1 Guest are viewing this topic.

xiaxiang

  • Guest
{Challenge}Get the textstrings in each line of the mtext
« on: March 07, 2013, 01:35:04 AM »
HI
I Assume that there was one mtext with group code as follow
Code: [Select]
((-1 . <图元名: 7ed62ec8>) (0 . "MTEXT") (330 . <图元名: 7ed62c10>) (5 . "59")
(100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbMText") (10
56.4981 -14.2706 0.0) (40 . 3.5) (41 . 15.9327) (46 . 20.4843) (71 . 1) (72 .
5) (1 . "abc        abc abc") (7 . "Standard") (210 0.0 0.0 1.0) (11 1.0 0.0
0.0) (42 . 9.33333) (43 . 15.1667) (50 . 0.0) (73 . 1) (44 . 1.0))
Now we know the textstrings content is "abc        abc abc".
Note that there isn't "\\p" Included in the textstrings content .
So the question:how can I get the textstrings in each line of the mtext and return the list of the result?
In this example the return value is ("abc        " "abc " "abc").
I want to know if someone can do that as a Challenge and do help for me.
It seems like  that  we can use explode command ,but any other suggestion?
Sorry for my bad English.
The sample picture and file are as Attachments.
Regards Xia

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #1 on: March 07, 2013, 05:29:57 AM »
This may do the trick .  ( considering the space as a start for another word ) so it is not typical at all .

Code - Auto/Visual Lisp: [Select]
  1. (defun Deconstruct_Strings (string delimiter / pos lst)
  2. ;;;;;   Author  :  Tharwat Al Shoufi            ;;;;;
  3. ;;;;; Sub-Function to separate a string into  ;;;;;
  4. ;;;;; a list acoording to the Delimiter         ;;;;;
  5.   (while (setq pos (vl-string-search delimiter string 0))
  6.     (progn (setq lst (cons (substr string 1 pos) lst))
  7.            (setq string (substr string (+ pos 2) (strlen string)))
  8.     )
  9.   )
  10.   (if string
  11.     (setq lst (cons string lst))
  12.   )
  13.   (setq lst (reverse lst))
  14. )
  15.  
  16. (defun c:Test (/ ss)
  17.   (if (and (setq ss (car (entsel "\n Select Mtext :"))) (eq (cdr (assoc 0 (entget ss))) "MTEXT"))
  18.     (vl-remove "" (Deconstruct_strings (cdr (assoc 1 (entget ss))) " "))
  19.   )
  20. )
  21.  
« Last Edit: March 07, 2013, 05:33:11 AM by Tharwat »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #2 on: March 07, 2013, 06:35:32 AM »
This would be anything but easy. Just consider all the formatting code that can be used in mtexts! The question is: what is your goal?

xiaxiang

  • Guest
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #3 on: March 07, 2013, 08:52:36 AM »
This would be anything but easy. Just consider all the formatting code that can be used in mtexts! The question is: what is your goal?
I want to explode this mtext with no other formats(even no spaces)to single texts and hold their own  locations.This is the real subject of this topic.
Thanks.
xia

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #4 on: March 07, 2013, 09:04:22 AM »
I want to explode this mtext with no other formats(even no spaces)to single texts and hold their own  locations.This is the real subject of this topic.

Why not use the explode command?

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #5 on: March 07, 2013, 09:21:15 AM »
This may do the trick .  ( considering the space as a start for another word ) so it is not typical at all .

Code - Auto/Visual Lisp: [Select]
  1. (defun Deconstruct_Strings (string delimiter / pos lst)
  2. ;;;;;   Author  :  Tharwat Al Shoufi            ;;;;;
  3. ;;;;; Sub-Function to separate a string into  ;;;;;
  4. ;;;;; a list acoording to the Delimiter         ;;;;;
  5.   (while (setq pos (vl-string-search delimiter string 0))
  6.     (progn (setq lst (cons (substr string 1 pos) lst))
  7.            (setq string (substr string (+ pos 2) (strlen string)))
  8.     )
  9.   )
  10.   (if string
  11.     (setq lst (cons string lst))
  12.   )
  13.   (setq lst (reverse lst))
  14. )
  15.  
  16. (defun c:Test (/ ss)
  17.   (if (and (setq ss (car (entsel "\n Select Mtext :"))) (eq (cdr (assoc 0 (entget ss))) "MTEXT"))
  18.     (vl-remove "" (Deconstruct_strings (cdr (assoc 1 (entget ss))) " "))
  19.   )
  20. )
  21.  

@Tharwat

Just getting DXF code 1 will not do the trick if the text string is greater than 250 characters. if the text string length is greater than 250, then you must iterate through DXF code 3, which holds the rest of the string in 250 character chunks. Formatting is a different matter, after retrieving all the text, you have to strip the formatting. Lee Mac's unformat string function: http://www.lee-mac.com/unformatstring.html can be used for that.

It's a whole different matter to preserve the formatting, and put it back into the string afterwards. It can be done - but it is complex, you have to write a new program that replaces the formatting with placeholders in a list along with the string, then replaces the formatting afterwards before recombining the string.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

mkweaver

  • Bull Frog
  • Posts: 352
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #6 on: March 07, 2013, 11:02:49 AM »
Breaking the text down to lines can be done with explode and vl-string-trim. Exploding to individual words is a bit more work.
[ :-Dquote author=xiaxiang link=topic=44128.msg493796#msg493796 date=1362664356]
This would be anything but easy. Just consider all the formatting code that can be used in mtexts! The question is: what is your goal?
I want to explode this mtext with no other formats(even no spaces)to single texts and hold their own  locations.This is the real subject of this topic.
Thanks.
xia
[/quote]

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #7 on: March 07, 2013, 11:26:08 AM »
This what I have been using on Plain Text. (explode Mtext)
Code - Auto/Visual Lisp: [Select]
  1. ;;  CAB 05/12/2011
  2. ;;  Break dText into text words
  3.  
  4. ;; NOTE, will only work with LEFT justification now
  5.  
  6. (defun c:t2w() (c:text2words))
  7. (defun c:text2words (/ ang elst ent wordlst pt10 pt11 spcwid wid)
  8.   ;;  phraser by CAB
  9.   (defun sparser (str delim / ptr lst)
  10.     (while (setq ptr (vl-string-search delim str))
  11.       (setq lst (cons (substr str 1 ptr) lst))
  12.       (setq str (substr str (+ ptr 2)))
  13.     )
  14.     (reverse (cons str lst))
  15.   )
  16.   ;;  Strip these dxf code pairs from original list
  17.   (defun dxfstrip (el)
  18.     (vl-remove-if '(lambda (pr) (member (car pr) '(-2 -1 5 102 300 330 331 340 350 360 410))) el)
  19.   )
  20.  
  21.   (defun getwidth(el)
  22.     (abs (apply '- (mapcar 'car (textbox el))))
  23.   )
  24.  
  25.  
  26.   (if (and (setq ent (entsel "\nSelect the text"))
  27.            (setq elst (entget (car ent)))
  28.            (= (cdr (assoc 0 elst)) "TEXT")
  29.       )
  30.     (progn
  31.       (entdel (car ent))
  32.       (setq elst (dxfstrip elst))
  33.       (setq txt (cdr (assoc 1 elst)))
  34.       (setq wordlst (vl-remove "" (sparser txt " ")))
  35.       (setq ang (cdr (assoc 50 elst))
  36.             pt10 (cdr (assoc 10 elst))
  37.             pt11 (cdr (assoc 11 elst))
  38.       )
  39.       ;;  get approx width a space to seperate new text with
  40.       (setq elst (subst '(1 . "hl") (assoc 1 elst)  elst))
  41.       (setq spcwid (getwidth elst))
  42.  
  43.       (foreach word wordlst
  44.         (entmake (setq elst (subst (cons 1 word) (assoc 1 elst)  elst)))
  45.         (setq wid (getwidth elst)
  46.               pt10 (polar pt10 ang (+ wid spcwid))
  47.               pt11 (polar pt11 ang (+ wid spcwid))
  48.               elst (subst (cons 10 pt10) (assoc 10 elst)  elst)
  49.               elst (subst (cons 11 pt11) (assoc 11 elst)  elst)
  50.         )
  51.       )
  52.     )
  53.     (princ "\nMissed or Not plain TEXT.")
  54.   )
  55.  
  56.   (princ)
  57. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #8 on: March 07, 2013, 01:18:03 PM »
Nice code Charles  8-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #9 on: March 07, 2013, 01:29:19 PM »
CAB , I think the OP is talking about Mtext and not Text .

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #10 on: March 07, 2013, 01:29:43 PM »
Nice one CAB  :-)

This was my version (I'll add it to my site when I get a chance!)


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #11 on: March 07, 2013, 03:16:32 PM »
Thanks fellas, Nice one Lee.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #12 on: March 07, 2013, 05:00:47 PM »
Although it functions as expected, it could be better if all split words were to be counted including " " words...

Code - Auto/Visual Lisp: [Select]
  1. (defun _splitwords ( str / pos )
  2.     (if (setq pos (vl-string-position 32 str))
  3.         (cons (cons 1 (substr str 1 (if (eq pos 0) 1 pos))) (_splitwords (substr str (+ (if (eq pos 0) 1 pos) 1))))
  4.         (list (cons 1 str))
  5.     )
  6. )
  7.  

And at the end of routine, you just erase those empty text entities :

Code - Auto/Visual Lisp: [Select]
  1. (vl-cmdf "_.erase" (ssget "_X" '((0 . "TEXT") (1 . " "))) "")
  2.  

This minor modifications I made for my purposes, just thinking that if the words are separated with more spaces, this variant is better (my opinion)...

Cheers, and thank you Lee...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

xiaxiang

  • Guest
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #13 on: March 07, 2013, 08:45:27 PM »
This may do the trick .  ( considering the space as a start for another word ) so it is not typical at all .
Thanks Tharwat!Your codes work well with the textstrings which contain spaces in them as the Attachments of my topic supplied.
If process mtexts which haven't any spcace in their textstrings,the only way I think is explode command
Regards.

I want to explode this mtext with no other formats(even no spaces)to single texts and hold their own  locations.This is the real subject of this topic.

Why not use the explode command?
Is there any other way to change mtext to texts properly?
It seems like only explode command can do that thing.
Regards.

xiaxiang

  • Guest
Re: {Challenge}Get the textstrings in each line of the mtext
« Reply #14 on: March 07, 2013, 09:05:05 PM »
This what I have been using on Plain Text. (explode Mtext)

Nice one CAB  :-)
This was my version (I'll add it to my site when I get a chance!)

Hi,Charles Hi,Lee
All the codes you supplied are designed to process Dtext.
I hope that Mtext could also be done well.
So please allow me to change the OP to "How to break mText into text words".
You'll tell me that using the explode command.
 :-P