text concatenate

text concatenate

sudarsann
Advocate Advocate
4,831 Views
11 Replies
Message 1 of 12

text concatenate

sudarsann
Advocate
Advocate

hi,

 

Is any lisp to concatenate two or more text strings using ',' between those strings.

 

Sudarsan

0 Likes
Accepted solutions (1)
4,832 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Accepted solution

Something like this?

 

(defun c:conc (/ a ans b c d e f g h j)
   (graphscr)
   (command ".redraw") (command)
(initget "Yes No")
(if (= "Yes" (getkword "\nRetain Append Text? [Y/N] <N>: "))
(setq j 0); Yes
(setq j 1); No
)
   (prompt "\nSelect Text to Remain->")
   (setq a (entsel))
   (while (setq b (entsel "\nSelect Text to Append -> "))
     (setq c (entget (car a))
           d (entget (car b))
           e (cdr (assoc 1 c))    ;get 1st text string
           f (cdr (assoc 1 d))    ;get 2nd text string
           g (strcat e "," f)
	   h (subst (cons 1 g) (assoc 1 c) c)
     )
     (entmod h)                    ;update 1st string
     
(if (eq j 1)
     (entdel (car b))
  )
     (princ)
   )
)
Message 3 of 12

gpcattaneo
Advocate
Advocate

Another...

 

(defun strC ( strlist del / newlst )
    (mapcar
        '(lambda (a)
             (setq newlst (cons del (cons a newlst)))
         )
        (reverse strlist)
    )
    (apply 'strcat (cdr newlst))    
)


(strC '("abc" "de" "f") ",")
"abc,de,f"
_$
Message 4 of 12

hmsilva
Mentor
Mentor

@sudarsann wrote:

hi,

 

Is any lisp to concatenate two or more text strings using ',' between those strings.

 

Sudarsan


Hi sudarsann,

another...

_$ (defun lst2str (lst del)
    (vl-string-left-trim del (apply 'strcat (mapcar '(lambda (x) (strcat del x)) lst)))
)
LST2STR
_$ (lst2str '( "abc" "def" "ghi") ",")
"abc,def,ghi"
_$

 

Hope this helps,
Henrique

 

EESignature

Message 5 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Something like this?

....


I'm not sure why others are suggesting things that work with lists of text strings, when Post 2 clearly indicates that your intention is to select existing Text objects on-screen.  Your approach is fine, though it can be simplified/consolidated somewhat.

 

You don't need the (graphscr), because if you're in the text screen, the (entsel) will pop you into the graphics screen.  And you don't need the (command) without arguments, because you won't be able to enter CONC as a command unless you're already at the Command: prompt, so there won't be any other command active that you need to cancel.  You have a variety of variables that are used only once, in which case you may as well skip the variable, and do what you would do to set it right where it's used.  You can include a prompt inside (entsel) rather than separately before it.  And whether to retain appended text can be just a T or nil value, rather than setting a number for it.

 

Lightly tested:

 

(defun C:CONC (/ del a b c)
  (command ".redraw")
  (initget "Yes No")
  (setq
    del (/= (getkword "\nRetain Appended Text? [Y/N] <N>: ") "Yes"); T for No or Enter
    a (entsel "\nSelect Text to Remain->")
  ); setq
  (while (setq b (entsel "\nSelect Text to Append -> "))
    (setq c (entget (car a)))
    (entmod ; update 1st string
      (subst
        (cons 1 (strcat (cdr (assoc 1 c)) "," (cdr (assoc 1 (entget (car b))))))
        (assoc 1 c)
        c
      ); subst
    ); entmod
    (if del (entdel (car b)))
  ); while
  (princ)
)

It could be made fancier, e.g. to ensure you picked something, restrict selection to Text [and/or Mtext if you like], etc.

Kent Cooper, AIA
0 Likes
Message 6 of 12

Anonymous
Not applicable

That's made it much neater Kent, I'll use that myself !

 

Particularly like the “And whether to retain appended text can be just a T or nil value, rather than setting a number”

I hadn’t thought of that

Now if I could just send you about 30 other lisps that need tidying up….

 

🙂

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant

Kent1Cooper wrote:

 

> I'm not sure why others are suggesting things that work with lists of text strings, when Post 2 clearly indicates that your intention is to select existing Text objects on-screen.  ....

 

 

 

My apologies to "others" -- I somehow glanced too quickly to notice that Post 2 is not from the same person as Post 1 [both starting with s, of similar length and similar icon color...].  Maybe a list of strings is appropriate.  I think the OP needs to clarify what the actual operation would be.

Kent Cooper, AIA
0 Likes
Message 8 of 12

F.Camargo
Advisor
Advisor

Hello,

 

Could you help me, I need concatenate only 2 text and continue the comand to the next 2 text?

I have many text that I need to join. It will save my time! 😀

 

Thanks

Fabricio

0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant

@F.Camargo wrote:

... I need concatenate only 2 text and continue the comand to the next 2 text? ....


Is the idea that you would pick Text objects one at a time, and the content of the second one would be tacked onto the end of the first one [with the same comma separator as in Message 1], then that of the fourth would be tacked onto the end of the third, etc.?

Kent Cooper, AIA
Message 10 of 12

F.Camargo
Advisor
Advisor

@Kent1Cooper  escreveu:

@F.Camargo wrote:

... I need concatenate only 2 text and continue the comand to the next 2 text? ....


Is the idea that you would pick Text objects one at a time, and the content of the second one would be tacked onto the end of the first one [with the same comma separator as in Message 1], then that of the fourth would be tacked onto the end of the third, etc.?


Hi @Kent1Cooper 

Yes! 

e.g. Text1 and Text2 = result Text1-Text2 (finally)

 

and the third could be the next concatenate text.


Thanks

 

0 Likes
Message 11 of 12

Kent1Cooper
Consultant
Consultant

Simply put, and on the assumption that you want the second one of each pair [whose content gets appended to the first one] removed, try this [minimally tested]:

(defun C:TC2 (/ ss1 ss2 d pea n) ; = Text: Concatenate 2 at a time
  (while
    (and
      (setq tsel1 (entsel "\nSelect base Text object or <exit>: "))
      (setq tdata1 (entget (car tsel1)))
      (member '(0 . "TEXT") tdata1)
    ); and
    (if
      (and
        (setq tsel2 (entsel "\nSelect Text object to append: "))
        (setq tdata2 (entget (car tsel2)))
        (member '(0 . "TEXT") tdata2)
      ); and
      (progn ; then
        (entmod
          (subst
            (cons 1 (strcat (cdr (assoc 1 tdata1)) "-" (cdr (assoc 1 tdata2))))
            (assoc 1 tdata1)
            tdata1
          ); subst
        ); entmod
        (entdel (car tsel2))
      ); progn
    ); if
  ); while
  (prin1)
)

It is built for Text only, but can easily be made to allow Mtext also, though there are potential complications there [internal formatting, splitting of the content of very long ones, etc.].

Kent Cooper, AIA
Message 12 of 12

F.Camargo
Advisor
Advisor

@Kent1Cooper  escreveu:

Simply put, and on the assumption that you want the second one of each pair [whose content gets appended to the first one] removed, try this [minimally tested]:

 

(defun C:TC2 (/ ss1 ss2 d pea n) ; = Text: Concatenate 2 at a time
  (while
    (and
      (setq tsel1 (entsel "\nSelect base Text object or <exit>: "))
      (setq tdata1 (entget (car tsel1)))
      (member '(0 . "TEXT") tdata1)
    ); and
    (if
      (and
        (setq tsel2 (entsel "\nSelect Text object to append: "))
        (setq tdata2 (entget (car tsel2)))
        (member '(0 . "TEXT") tdata2)
      ); and
      (progn ; then
        (entmod
          (subst
            (cons 1 (strcat (cdr (assoc 1 tdata1)) "-" (cdr (assoc 1 tdata2))))
            (assoc 1 tdata1)
            tdata1
          ); subst
        ); entmod
        (entdel (car tsel2))
      ); progn
    ); if
  ); while
  (prin1)
)

 

It is built for Text only, but can easily be made to allow Mtext also, though there are potential complications there [internal formatting, splitting of the content of very long ones, etc.].


Thank you very much 

It's faster now!!!

 

Regards

Fabricio

0 Likes