Author Topic: Help joining text together into one piece of mtext  (Read 7020 times)

0 Members and 1 Guest are viewing this topic.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Help joining text together into one piece of mtext
« Reply #15 on: October 27, 2010, 09:31:10 AM »
mine...

Code: [Select]
(defun AT:TextString (Obj)
  ;; Extract textstring (with symbols) from text object
  ;; Works on: Attrib, Attdef, MText, Multileader, Text
  ;; Obj - Object to extract textstring from
  ;; Alan J. Thompson, 11.24.09 / 04.13.10
  (if Obj
    ((lambda (e)
       (cond ((eq (cdr (assoc 0 e)) "MULTILEADER") (cdr (assoc 304 e)))
             ((vl-position (cdr (assoc 0 e)) '("ATTDEF" "ATTRIB" "TEXT")) (cdr (assoc 1 e)))
             ((eq (cdr (assoc 0 e)) "MTEXT")
              (apply (function strcat)
                     (mapcar (function (lambda (x)
                                         (if (vl-position (car x) '(1 3))
                                           (cdr x)
                                           ""
                                         )
                                       )
                             )
                             e
                     )
              )
             )
       )
     )
      (entget (cond ((vl-consp Obj) (car Obj))
                    ((eq (type Obj) 'ENAME) Obj)
                    ((eq (type Obj) 'VLA-ObjECT) (vlax-vla-object->ename Obj))
              )
      )
    )
  )
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Joe Burke

  • Guest
Re: Help joining text together into one piece of mtext
« Reply #16 on: October 27, 2010, 11:11:03 AM »
Agreed, getting the full text string from an mtext ename which contains both DXF 1 and 3 codes is fairly well known operation.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Help joining text together into one piece of mtext
« Reply #17 on: October 27, 2010, 05:30:25 PM »
Agreed, getting the full text string from an mtext ename which contains both DXF 1 and 3 codes is fairly well known operation.
This may very well be, but to be honest, I have taught myself all I know about LISP, with the help of those on this board and others. As a result, I definitely do not know everything and I do know that there are others that are far more skilled than I on this board.

I would like to know more about what the following portion does:
Code: [Select]
(mapcar (function (lambda (x)
     (cond
       ((= (car x) 3)(setq str (strcat str (cdr x)))); I understand strcat and setq, I just don't get the mapcar and such
       ((= (car x) 1)(setq dxf1 (cdr x)))
     )))
    (entget ent)
  )

At any rate, attached is what I have so far....I still need to add some code to allow for selecting multiple pieces of text, I know of one way to accomplish this, I just haven't had the time to implement it yet.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Help joining text together into one piece of mtext
« Reply #18 on: October 27, 2010, 05:32:27 PM »
Mapcar is just there to step through the entity's data. (entget ENAME)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help joining text together into one piece of mtext
« Reply #19 on: October 27, 2010, 11:38:17 PM »
mapcar is like a foreach except the the foreach has a variable that is set with each item in a list, like
Code: [Select]
(foreach pair (entget ent)
  ;;  do something with the variable pair
) ; end foreach

mapcar is set up with the list at the end and here the variable is x, you often see it like this:
Code: [Select]
(mapcar '(lambda(x) <do something with x> ) (entget ent))The form using (function ) executes faster but may be confusing to some.
Code: [Select]
(mapcar (function (lambda(x) <do something with x> )) (entget ent))
So this code can be setup with a foreach
Code: [Select]
(mapcar
  (function
    (lambda (x)
     (cond
       ((= (car x) 3)(setq str (strcat str (cdr x))))
       ((= (car x) 1)(setq dxf1 (cdr x)))
     )))
    (entget ent)
  )

Code: [Select]
(foreach x (entget ent)
     (cond
       ((= (car x) 3)(setq str (strcat str (cdr x))))
       ((= (car x) 1)(setq dxf1 (cdr x)))
     )
)

This code steps through each DXF pair looking for (1 . "some text") and  (3 . "some more text")

We expect the mtext to contain at least one (1 . "some text") and if the text exceeds 255 characters (not sure of the exact number)
then there will be a (3 . "some more text") the order is DXF 3 holds the first 255 characters & then the next 255 but the
remainder is placed in DXF 1. There is a bug in ACAD that sometimes there is more than one DXF 1 and the first one in
the list is not to be used. So my code collects all the DXF 3 in one variable with starcat. The DXF 1 is stored in another variable
but is overwritten each time a DXF 1 is found therefore the last or valid DXF 1 is found and added to the DXF 3 data to
complete the text string.
« Last Edit: October 27, 2010, 11:41:38 PM by CAB »
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.

CHulse

  • Swamp Rat
  • Posts: 504
Re: Help joining text together into one piece of mtext
« Reply #20 on: October 28, 2010, 10:00:08 AM »
Thanks for that explanation CAB, I have never really understood Mapcar and Lambda...
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Help joining text together into one piece of mtext
« Reply #21 on: October 28, 2010, 10:49:21 AM »
Yes thank you CAB, that helps a lot. I can see how I could use it to speed up some of my other routines.