Add Text

Add Text

k005
Advisor Advisor
2,126 Views
10 Replies
Message 1 of 11

Add Text

k005
Advisor
Advisor

Hi All,

 

 

How can I add these options to the beginning of an existing text object?

Temel
1.kat
2.kat
3.kat

Çatı kat

...

 

0 Likes
Accepted solutions (3)
2,127 Views
10 Replies
Replies (10)
Message 2 of 11

hak_vz
Advisor
Advisor
Accepted solution

@k005Here you have simplest form of requested code. You enter text to add at front of existing text entity, and in loop pick texts to modify. If you want to extract text to add from some existing text in a drawing, this is also simple task, that I leave you to practice autolisp. 

 

(defun c:AddTextAtFront (/ str e ent newtext)
	(setq str (getstring "\nString to add at front >"))
	(while (setq e (car (entsel "Select text to modify")))
		(cond 
			((and e)
				(cond 
					((= "TEXT" (cdr (assoc 0 (setq ent (entget e)))))
						(setq newtext (strcat str " " (cdr(assoc 1 ent))))
						(setq ent (subst (cons 1 newtext) (assoc 1 ent) ent))
						(entmod ent)				
					)
				)
			)
		)
	)
(princ)	
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 11

CADaSchtroumpf
Advisor
Advisor

My way:

(defun Entsel_Getstring ( / ent key kstr)
  (setq kstr nil ent "")
  (princ "\nSelect an text objet / Enter string: ")
  (while (and (not (equal (setq key (grread T 4 2)) '(2 13))) (/= (car key) 3))
    (if (eq (car key) 2)
      (progn
        (setq kstr (cons (cadr key) kstr))
        (princ (chr (cadr key)))
      )
      (setq kstr (cons 13 kstr))
    )
  )
  (if (eq (car key) 3)
    (if (setq ent (nentselp (cadr key)))
      (progn
        (setq ent (entget (car ent)))
        (if (assoc 1 ent) (cdr (assoc 1 ent)) (setq ent nil))
      )
      ""
    )
    (foreach n kstr (setq ent (strcat (chr n) ent)))
  )
)
(defun c:foo ( / js prefix n dxf_ent)
  (princ "\nSelect texts to add prefix")
  (setq js (ssget '((0 . "*TEXT"))))
  (cond
    (js
      (setq prefix (Entsel_Getstring))
      (repeat (setq n (sslength js))
        (setq dxf_ent (entget (ssname js (setq n (1- n)))))
        (entmod
          (subst
            (cons 1 (strcat prefix " " (cdr (assoc 1 dxf_ent))))
            (assoc 1 dxf_ent)
            dxf_ent
          )
        )
      )
    )
    (T (princ "\nNo Text selected!"))
  )
  (prin1)
)
Message 4 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

@k005 wrote:

How can I add these options to the beginning of an existing text object?

Temel
1.kat
2.kat
3.kat

Çatı kat

...


It seems to me this will do, in simplest terms:

 

(defun C:TEST (/ pre)
  (initget "Temel 1.kat 2.kat 3.kat")
  (setq pre (getkword "\nPrefix [Temel/1.kat/2.kat/3.kat]: "))
  (setpropertyvalue (setq txt (car (entsel))) "TextString"
    (strcat pre " " (getpropertyvalue txt "TextString"))
  )
)

 

It could be made to verify that you selected a Text object, not on a locked Layer, and/or to let you select more than one, either one at a time or collectively.

[I didn't include the last prefix choice, not because it isn't in the sample drawing, but because it has a space in it.  The (initget) function uses spaces as separators between choices, so your last choice would become two separate choices.  There may be a way around that, but I think it would involve some kind of stand-in value.]

Kent Cooper, AIA
Message 5 of 11

k005
Advisor
Advisor

@Kent1Cooper 

 

Thank you very much. Yes, that's exactly it. 🤗

0 Likes
Message 6 of 11

k005
Advisor
Advisor

@hak_vz  @CADaSchtroumpf 

 

I save as an alternative to my archive. friends. Thank you so much.

0 Likes
Message 7 of 11

pbejse
Mentor
Mentor
Accepted solution

@k005 wrote:

How can I add these options to the beginning of an existing text object?

Temel
1.kat
2.kat
3.kat

Çatı kat

...


 

(defun c:AddStr ( / _list _strcat options prf ss i ent)
      
;;	You can add string for prefix here		;;
(setq prefix '("Temel" "Çati kat" "1.kat" "2.kat" "3.kat"))

(setq _strcat (lambda (l)(apply 'strcat l)))      
(defun _list (ls n )
(if ls (Cons (list (strcat (itoa n) " ")
                   (strcat (itoa n) " ." (Car ls) "/"))
                (_list (cdr ls) (1+ n)))))
 
(setq options  (_list prefix 1))     
(initget 1 (_strcat (mapcar 'car  options)))      
(setq prf (getkword (strcat "\nPrefix ["
		(_strcat  (mapcar 'cadr  options))
                          "]: "))
      )

(if
      (and
            (setq prf (nth (1- (atoi prf)) prefix))
            (setq ss (ssget "_:L" '((0 . "TEXT"))))
            )
    (repeat (setq i (sslength ss))
          (setq ent (entget (ssname ss (Setq i (1- i)))))
          (entmod (subst (Cons 1 
                               (strcat prf " " (cdr (assoc 1 ent))))
                               (assoc 1 ent) ent))
          )
      )
      (princ)
)

 

Command: AddStr

options.png

Select objects: Specify opposite corner: 3 found

 

HTH

 

 

 

Message 8 of 11

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

....

[I didn't include the last prefix choice, not because it isn't in the sample drawing, but because it has a space in it.  The (initget) function uses spaces as separators between choices, so your last choice would become two separate choices.  There may be a way around that, but I think it would involve some kind of stand-in value.]


There was a relatively simple way around the space problem.  Though the option offering has a hyphen in its place, in result it becomes a space again.  But the non-English characters present a problem.  While it can work with the Ç [C with cedilla], it can't handle the ı [i with no dot], not just in the prompt option but even in a substitution in the result.  Note that it appears correctly in the code, but not in use in AutoCAD.

(defun C:TEST (/ pre)
  (initget "Temel 1.kat 2.kat 3.kat Çatı-kat")
  (setq pre (getkword "\nPrefix [Temel/1.kat/2.kat/3.kat/Çatı-kat]: "))
  (setpropertyvalue (setq txt (car (entsel))) "TextString"
    (strcat (vl-string-subst " " "-" pre) " " (getpropertyvalue txt "TextString"))
  )
)

 

Kent Cooper, AIA
Message 9 of 11

k005
Advisor
Advisor

@pbejse 

 

Again a very good solution.

Message 10 of 11

k005
Advisor
Advisor

@Kent1Cooper 

 

Understood. Thanks.

0 Likes
Message 11 of 11

Sea-Haven
Mentor
Mentor

My $0.05  Just the choice bit, you can use the library function Multi radio buttons.lsp in any code.

 

(if (not AH:Butts)(load "Multi radio buttons.lsp")) ; loads the program if not loaded already
(if (= but nil)(setq but 1)) 	; this is needed to set default button
; you can reset default button to user pick
(setq ans (ah:butts but "V"   '("Choose" "Temel" "Çati kat" "1.kat" "2.kat" "3.kat")))

 

SeaHaven_0-1631333779466.png

 

 

0 Likes