Author Topic: mText to Single line mText?  (Read 11142 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: mText to Single line mText?
« Reply #15 on: July 28, 2014, 01:31:41 PM »
Here's my attempt, as we could use this as well, but you will notice it only works with the first two line for some reason, not sure why:


cmwade77 , Thanks for sharing!

I  publish your question in Chinese forum , and my boss give a answer.You can reference.
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=110871&page=1#pid646141
The bug modfiy by: edata  @mjtd.com 2014-7-26


Thank you, also I found a slight bug in your code, any text after a blank line would not remain in the correct spot.

This is my corrected code, also this code will now copy all properties, as it simply makes a copy of the text and modifies it instead.

Code: [Select]
;Explode MText
;Explodes Mtext into single lines of mtext
;Written by: cmwade77 @ theswamp.org - July 2014
;Bug: Currenlty Only works on the first two lines of mtext
;Work Around: Repeat use until all lines are exploded
;Bug Fix by: edata  @mjtd.com 2014-7-26

(defun c:explodemtext (/ SS Ent Object ObjectType Text NEWTEXT pt1 textlst txtobj LineCt Pt2 LSpace rot)
(defun Degrees->Radians (numberOfDegrees)
  (* pi (/ numberOfDegrees 180.0))
)
  (vl-load-com)
  (while (= SS nil)
(princ "\rSelect MText to explode: ")
(setq Filter '((0 . "MTEXT"))
  SS (ssget Filter)
)
  )
  ((lambda (i / Ent)
(while (setq Ent (ssname SS (setq i (1+ i))))
(setq Object (vlax-ename->vla-object Ent)
  ObjectType (vla-get-ObjectName Object)
)

(setq Text (vla-get-textstring Object)
    pt1 (vla-get-InsertionPoint Object)
    LSpace (vla-get-LineSpacingDistance Object)
    pt1 (trans (vlax-safearray->list (vlax-variant-value pt1)) 0 1)
    rot (vla-get-rotation Object)
)     
(setq LineCt 0)           
(setq textlst(parse12 Text "\\P"))     
(while (setq NewText(car textlst))
  (setq pt2 (trans (polar pt1 (- rot (Degrees->Radians 90.0)) (* LineCt LSPace)) 1 0)
        pt2 (vlax-3d-point pt2)
        LineCt (+ LineCt 1))
  (setq txtobj (vla-copy Object))
  (vla-put-textstring txtObj NewText)
  (vla-put-InsertionPoint txtObj pt2)
  (setq textlst(cdr textlst))
)
(vla-delete Object)
)
    )
-1)
 
)
;;string delimiter
;;code By st788796
(defun parse12 (str delimiter / POST STRL STRLST)
  (setq strl (strlen delimiter))
  (while (vl-string-search delimiter str)
    (setq post (vl-string-search delimiter str))
    (setq strlst (append strlst (list (substr str 1 post))))
    (setq str (substr str (+ post (1+ strl))))
  )
  (append strlst (list str));Code modified by cmwade77 to allow text after blanks lines to remain in place.
)

This should also account for all possible alignments and should work more reliably with versions older than 2009, as there were some properties that may not have been available.

NOTE: For those that have AutoCAD 2015, if the piece of mText was created in AutoCAD 2015, it will explode correctly with the normal explode command; however, if the text was created in 2014 or earlier, the mText will explode as CAB has described in his first post and one of the routines posted here is needed.

EDIT: Fixed problem with the code that was posted.

EDIT: Changed code to allow for rotated text, rotated UCS and to allow selecting multiple pieces of text.
« Last Edit: July 28, 2014, 04:53:26 PM by cmwade77 »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mText to Single line mText?
« Reply #16 on: July 28, 2014, 01:43:40 PM »
How about the one from this post:
http://www.theswamp.org/index.php?topic=29430.msg463814#msg463814

They all return plain text objects, I wanted Single individual objects by lines of Mtext.
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mText to Single line mText?
« Reply #17 on: July 28, 2014, 01:48:17 PM »
Sorry again, I replaced gile's function with mine & forgot to update the call.
Code is updated again & should work.

Thanks for testing  8)

when appload , Command: ; error: malformed list on input

I copy & pasted from forum without error.
Perhaps the file attached will work for you.
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.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: mText to Single line mText?
« Reply #18 on: July 28, 2014, 01:52:46 PM »
How about the one from this post:
http://www.theswamp.org/index.php?topic=29430.msg463814#msg463814

They all return plain text objects, I wanted Single individual objects by lines of Mtext.
Yeah, but if you wanted the wrapped text to be accounted for, I thought it might be a good starting place. In my case I don't really care about the wrapped text, so what I posted a little bit ago works as well.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mText to Single line mText?
« Reply #19 on: July 28, 2014, 01:56:01 PM »
Thank you, also I found a slight bug in your code, any text after a blank line would not remain in the correct spot.

This is my corrected code, also this code will now copy all properties, as it simply makes a copy of the text and modifies it instead.

No problem with my code here in ADAC2004, blank lines work fine.

Your code produces plain text not Mtext that I desire.

Thanks
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mText to Single line mText?
« Reply #20 on: July 28, 2014, 01:57:57 PM »
How about the one from this post:
http://www.theswamp.org/index.php?topic=29430.msg463814#msg463814

They all return plain text objects, I wanted Single individual objects by lines of Mtext.
Yeah, but if you wanted the wrapped text to be accounted for, I thought it might be a good starting place. In my case I don't really care about the wrapped text, so what I posted a little bit ago works as well.

My priority is to maintain formatting of fractions! Word wrap is not an issue it is just something that would be nice to add to the code for future users.

Thanks
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.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: mText to Single line mText?
« Reply #21 on: July 28, 2014, 02:07:22 PM »
Thank you, also I found a slight bug in your code, any text after a blank line would not remain in the correct spot.

This is my corrected code, also this code will now copy all properties, as it simply makes a copy of the text and modifies it instead.

No problem with my code here in ADAC2004, blank lines work fine.

Your code produces plain text not Mtext that I desire.

Thanks
Huh? It produced Mtext for me. Literally, all I do is copy the mText that you are exploding, changing the text and insertion point. So there is no reason (as long as mText is selected) that it shouldn't produce mText.

I have attached a sample of what I get, perhaps you could attach something similar? I just don't understand how it's producing plain text and want to figure that out.

I would appreciate the help, as I really don't understand what is going on here. Also, the bug I mentioned was not in your code, but in the bug fix that emk2012 provided for my original code.

I unfortunately have not been able to get your code to run on 2015 yet.

Also, please try the code from http://www.theswamp.org/index.php?topic=47467.msg524898#msg524898 again, as I did find an error in what I posted.
« Last Edit: July 28, 2014, 02:23:27 PM by cmwade77 »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mText to Single line mText?
« Reply #22 on: July 28, 2014, 02:54:27 PM »
OK that code did work as long as the UCS or the mtext is not rotated.

Thanks
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.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: mText to Single line mText?
« Reply #23 on: July 28, 2014, 02:58:15 PM »
OK that code did work as long as the UCS or the mtext is not rotated.

Thanks

Ok, just making sure, that was the next item to look at, but I had to make sure the basics worked first.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: mText to Single line mText?
« Reply #24 on: July 28, 2014, 04:54:36 PM »
OK that code did work as long as the UCS or the mtext is not rotated.

Thanks

Please try the code again, I have modified it to work with rotated text, rotated UCS and to be able to select multiple pieces of mtext at once, filtering out any non-mtext entities.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: mText to Single line mText?
« Reply #25 on: July 29, 2014, 03:00:30 PM »
Here's another one that will let you adjust the text spacing to match the source text.
Code: [Select]
(defun c:mtbreak (/ *error* _blank _foo _parse a co d e gr i inc l lines lsd o p x)
  (vl-load-com)
  (defun *error* (msg)
    (and l (mapcar '(lambda (x) (vl-catch-all-apply 'vla-delete (list x))) l))
    (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
      (princ (strcat "\nError: " msg))
    )
    (princ)
  )
  (defun _parse (s / p)
    (if (setq p (vl-string-search "\\P" s))
      (cons (substr s 1 p) (_parse (substr s (+ p 1 (strlen "\\P")))))
      (list s)
    )
  )
  (defun _blank (s)
    (and s (or (null (vl-string->list s)) (vl-every '(lambda (x) (= x 32)) (vl-string->list s))))
  )
  (if (and (setq e (car (entsel "\nPick MTEXT: ")))
   (setq o (vlax-ename->vla-object e))
   (= (vla-get-objectname o) "AcDbMText")
   (> (length (setq lines (_parse (vla-get-textstring o)))) 1)
      )
    (progn
      (setq p (vlax-get o 'insertionpoint))
      (setq a (- (vla-get-rotation o) (/ pi 2.)))
      (setq lsd (vla-get-linespacingdistance o))
      (setq d 0)
      (foreach line lines
(if (_blank line)
  (setq d (+ d lsd))
  (progn (setq l (cons (setq co (vla-copy o)) l))
(vla-put-textstring co line)
(vlax-put co 'insertionpoint (polar p a d))
(setq d (+ d lsd))
  )
)
      )
      (vla-put-color o 1)
      (setq i 1)
      (setq inc 0.0001)
      (alert "Press 'A' or 'S' to Adjust Text Spacing, Any other key to Exit...")
      (while (or (= 5 (car (setq gr (grread t 4 1))))
(and (= 2 (car gr)) (member (cadr gr) '(65 97 83 115)))
     )
(if (= 2 (car gr))
  (progn (cond ((member (cadr gr) '(65 97)) (setq i (- i inc)))
       ((member (cadr gr) '(83 115)) (setq i (+ i inc)))
)
(and (<= i 0) (setq i 1))
(mapcar '(lambda (x)
    (vlax-put x
      'insertionpoint
      (polar p a (* i (distance p (vlax-get x 'insertionpoint))))
    )
  )
l
)
(mapcar 'vla-update l)
  )
)
;; (princ (strcat "\n" (vl-prin1-to-string gr)))
(princ "\rPress 'A' or 'S' to Adjust Text Spacing, Any other key to Exit...")
      )
      (vla-delete o)
    )
  )
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mText to Single line mText?
« Reply #26 on: July 29, 2014, 04:42:44 PM »
Isn't that special.  8)

Nice job Ron and like the vla-copy  :)

The issue with ACAD2006 is that the spacing varies from line to line due to fractions etc. but that is close.
It does not account for Middle or Bottom insert points.
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: mText to Single line mText?
« Reply #27 on: July 29, 2014, 07:03:45 PM »
Isn't that special.  8)

Nice job Ron and like the vla-copy  :)

The issue with ACAD2006 is that the spacing varies from line to line due to fractions etc. but that is close.
It does not account for Middle or Bottom insert points.


Thanks Charles. I tried to get the "true" height of each text line but proved difficult with mtext format ( {}\ etc.. ). The grread version was more of a hack, but still faster than doing it manually  8) . If I get some time, I'll try to adjust for varying insertion points.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: mText to Single line mText?
« Reply #28 on: July 29, 2014, 07:37:40 PM »
Isn't that special.  8)

Nice job Ron and like the vla-copy  :)

The issue with ACAD2006 is that the spacing varies from line to line due to fractions etc. but that is close.
It does not account for Middle or Bottom insert points.


Thanks Charles. I tried to get the "true" height of each text line but proved difficult with mtext format ( {}\ etc.. ). The grread version was more of a hack, but still faster than doing it manually  8) . If I get some time, I'll try to adjust for varying insertion points.
I need to dust off my older hard drives, I know I have some very old code laying around that I used to use to get the true height of each line of text.

Here's the basics of how it worked:
  • Get bounding box of mtext
  • Make a copy of the Mtext and freeze it
  • Explode the MText
  • Get the bounding box for each line of text and process accordingly
  • Erase the individual pieces of text
  • Thaw the mText copy and move it back to the original layer
This is a bit of over simplification, but it gives the gist.

I had originally used it for inserting number bubbles for reference notes; however, I have since found ways of doing so without exploding the text. But I would imagine that this method could be adapted to the purpose of this discussion.
« Last Edit: July 29, 2014, 07:42:09 PM by cmwade77 »

77077

  • Guest
Re: mText to Single line mText?
« Reply #29 on: September 28, 2014, 08:47:27 PM »
Nice routine ,ronjonp .