Lisp to select all text on a layer

Lisp to select all text on a layer

Anonymous
Not applicable
4,374 Views
8 Replies
Message 1 of 9

Lisp to select all text on a layer

Anonymous
Not applicable

I'm trying to determine if a lisp command exists that will allow me to select all text on a particular layer and add a "0" in front of all of the text?

 

i have a plan with room numbers ex. 4567 and i need to add a 0 to all of the room numbers to make them five digits ex. 04567.  i know this may be impossible but thought i'd check before i took hours to change the numbers.

0 Likes
4,375 Views
8 Replies
  • Lisp
Replies (8)
Message 2 of 9

dlanorh
Advisor
Advisor
Accepted solution

Try this

 

 

(defun c:test ( / ent lyr ss cnt el nt)

  (setq ent (car (entsel "\nSelect Text on Layer to Process : "))
        lyr (cdr (assoc 8 (entget ent)))
        ss (ssget "_X" (list '(0 . "TEXT") (cons 8 lyr)))
  )

  (cond (ss
          (repeat (setq cnt (sslength ss))
            (setq el (entget (ssname ss (setq cnt (1- cnt))))
                  nt (strcat "0" (cdr (assoc 1 el)))
            )
            (entmod (subst (cons 1 nt) (assoc 1 el) el))
          )
        )
  )
  (princ)
)

 

The first prompt to select text requires a single text on the required layer. The lisp will find all text on that layer and put a "0" in front of it.

 

 

 

I am not one of the robots you're looking for

Message 3 of 9

ronjonp
Mentor
Mentor
Accepted solution

Misread the request ... let me see if I can fix this within the next 24 minutes! 🙂

 

Here we go!

 

 

 

(defun c:foo (/ e el l s)
  ;; RJP » 2020-07-14
  (if (and (setq e (car (entsel "\Pick something to get the layer: ")))
	   (setq s (ssget ":L" (list '(0 . "TEXT") (assoc 8 (entget e)))))
      )
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (setq l (cdr (assoc 1 (setq el (entget e)))))
      (entmod (append el (list (cons 1 (strcat "0" l)))))
    )
  )
  (princ)
)

 

 

 

 

 

 

Message 4 of 9

Anonymous
Not applicable
Accepted solution
(DEFUN C:TEST ()
  (setq ss (ssget '((0 . "*TEXT"))))
(vlax-for tx (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object)))
  	(if  (not (distof (vla-get-textstring tx)))
          	(ssdel  (vlax-vla-object->ename tx)  ss))
          )
  (SETQ	INDEX 0)
  (REPEAT (SSLENGTH SS)
    (SETQ ENAME	  (SSNAME SS INDEX)
	  DATA	  (ENTGET ENAME)
	  CONTENT (CDR (ASSOC 1 (ENTGET ENAME)))
	  NEWCONT (STRCAT "0" CONTENT)
	  INDEX	  (1+ INDEX)
	  NEWCONT (subst (cons 1 (STRCAT "0" CONTENT)) (assoc 1 DATA) DATA)
    )
    (entmod NEWCONT)
  )
)
Message 5 of 9

pbejse
Mentor
Mentor

@ronjonp wrote:

Misread the request ... let me see if I can fix this within the next 24 minutes! 🙂


 

Tik-tok @ronjonp  😁

 

 

Message 6 of 9

CodeDing
Mentor
Mentor
Accepted solution

@Anonymous ,

 

Welcome to the forums!

 

It appears that everybody else is assuming that ALL of your text will ONLY be 4 digits long already.

This code will instead assume that your text can vary in length and account for that:

 

EDIT:

I REALLY liked @ronjonp 's method for getting the layer, so I incorporated that.

(defun c:ADDZERO ( / ss cnt e key txt diff)
  (if (and (setq e (car (entsel "\nSelect object on Layer: ")))
(setq ss (ssget "_X" (list '(0 . "TEXT,MTEXT") (assoc 8 (entget e))))))
(progn (repeat (setq cnt (sslength ss)) (setq e (ssname ss (setq cnt (1- cnt)))) (if (eq "MTEXT" (cdr (assoc 0 (entget e)))) (setq key "Contents") (setq key "TextString") );if (setq txt (getpropertyvalue e key)) (setq diff (- 5 (strlen txt))) (if (> diff 0) (progn (repeat diff (setq txt (strcat "0" txt))) (setpropertyvalue e key txt) );progn );if );repeat (prompt "\nComplete.") );progn ;else (prompt "\nNo Text found on the designated layer.") );if (princ) );defun

Best,

~DD

Message 7 of 9

ronjonp
Mentor
Mentor

@CodeDing  Nice idea 🍺. Here's my offering that's a bit less restrictive. Still not doing MTEXT since my guess is the next question will be related to hardcoded formatting and the code not working.

(defun c:foo (/ e el l s)
  ;; RJP » 2020-07-14
  (if (and (setq e (car (entsel "\Pick something to get the layer: ")))
	   (setq s (ssget ":L" (list '(0 . "TEXT") (assoc 8 (entget e)) '(1 . "~0*"))))
      )
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (setq l (cdr (assoc 1 (setq el (entget e)))))
      (if (vl-every '(lambda (x) (<= 48 x 57)) (vl-string->list l))
	(entmod (append el (list (cons 1 (strcat "0" l)))))
      )
    )
  )
  (princ)
)

 

 

 

Message 8 of 9

pbejse
Mentor
Mentor
Accepted solution

@CodeDing wrote:

It appears that everybody else is assuming that ALL of your text will ONLY be 4 digits long already.

This code will instead assume that your text can vary in length and account for that:

 


You think thats bad. Then mine is worst, I assumed its targetting specific values 😁

 

(defun c:demo (/ ent data ss nstr i e )
  (if
    (and
      (setq ent (car (entsel "\nSelect Text ")))
      (setq ent	 (entget ent)
	    data (mapcar '(lambda (d) (assoc d ent)) '(1 8))
      )
      (setq nstr (cons 1 (strcat "0" (Cdr (car data)))))
      (setq ss (ssget "_X" (append '((0 . "TEXT")) data)))
    )
     (repeat (setq i (sslength ss))
      (Setq e (entget (ssname ss (setq i (1- i)))))
       (entmod (subst nstr (assoc 1 e) e))
    )
  )
  (princ)
)

 

 

 

Message 9 of 9

Anonymous
Not applicable

Thank you, problem solved!

0 Likes