LISP routine to edit attributes of a block reference within a drawing

LISP routine to edit attributes of a block reference within a drawing

seary16
Explorer Explorer
357 Views
3 Replies
Message 1 of 4

LISP routine to edit attributes of a block reference within a drawing

seary16
Explorer
Explorer

I am a complete beginner when it comes to LISP and I'm trying to create this routine as practice but also due to its practicality however, I can't seem to get the hand of it.

 

I have a block reference called "A1_N"

I have a few attributes I would like to edit:

A
DATE.A
NAME.AD
NAME.AC
NO.A
NAME.AA

 

Any advice or guidance would be greatly appreciated!

 

Hopefully I will be able to develop this and run it across multiple drawings with the same block reference.

0 Likes
Accepted solutions (1)
358 Views
3 Replies
Replies (3)
Message 2 of 4

Moshe-A
Mentor
Mentor
Accepted solution

@seary16 hi,

 

Here is a simple one for you, if it does not work, post your block.

 

Moshe

 

 

(defun c:modatt (/ data^ ss l ename item) ; declare local variables

 ; set data list
 ;             (attag . attvalue) ; this known as dotted pair in autolisp
 (setq data^ '(("a"       . "value of a")       ("date.a" . "value of date.a") ("name.ad"   . "value of name.ad")
	       ("name.ac" . "value of name.ac") ("no.a"   . "value of no.a")     ("name.aa" . "value of name.aa")))
  
 (if (setq ss (ssget '((0 . "insert") (2 . "a1_n") (66 . 1)))) ; pick all blocks by name "a1_n" contain attributes
  ; then
  (repeat (setq l (sslength ss))  ; loop on all selected blocks
   (setq ename (ssname ss (setq l (1- l)))) ; get ename 

   ; loop through data list
   (foreach item data^
    (if (getpropertyvalue ename (car item))         ; check if tag exist?
     (setpropertyvalue ename (car item) (cdr item)) ; set attribute value
    )
   ); foreach
  ); repeat
 ); if

 (princ)
)

 

0 Likes
Message 3 of 4

pbejse
Mentor
Mentor
(defun c:Demo ( / attb_data  ss cntr entx tagValue )
(setq attb_data '(("A" . "The A")
	 	("DATE.A" . "Your Value for DATE.A")
		("NAME.AA" . "Your Value for NAME.AA")
	 	("NAME.AD" . "Your Value for NAME.AD")
	 	("NAME.AC" . "Your Value for NAME.AC")
	 	("NO.A" . "Your Value for DNO.A")
	)
)  
	(if
	  (setq ss (ssget '((0 . "INSERT")(66 . 1))));<-- Selection with filter
		  (repeat (setq i (sslength ss))
			(setq blk (ssname ss (setq i (1- i))))
		    
		    (while (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enxt (entget blk))))))
		        (if
		            (and
			      	(setq tagValue (assoc (cdr (assoc 2 enxt)) attb_data))
		                (entmod (subst (cons 1 (cdr tagValue)) (assoc 1 (reverse enxt)) enxt))
		            )
		            (entupd blk)
		        )
		    )
		    nil
		)
	  )
  )
0 Likes
Message 4 of 4

Sea-Haven
Mentor
Mentor
0 Likes