LISP to Select Blocks and Add Attributes

LISP to Select Blocks and Add Attributes

Kyle.para
Advocate Advocate
17,043 Views
53 Replies
Message 1 of 54

LISP to Select Blocks and Add Attributes

Kyle.para
Advocate
Advocate

Hi All,

 

We have to add a ton of attributes to all of our blocks.

 

I found code that was originally written by Lee Mac, but it was designed to type in the block name that you wanted.

 

I just want to be able to select all the blocks that I want and then have it insert the tags into all of the blocks that are selected.

 

I tried to change the "name enter box" to a select command but the program comes back with no blocks found.

 

Sorry I am new to this and may as well be trying to learn another language.

 

(defun c:addattribs ( / blk def )
  ; Get Entities
    (while (not blk)
            (princ "\nSelect Blocks to Update:")
               (setq blk (ssget '((-4 . "<NOT") (0 . "INSERT,POLYLINE,VIEWPORT") (-4 . "NOT>"))))
    ) ;_  end while
        )
        (if (/= "" blk)
        (progn
            (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
            (vla-addattribute def
                (getvar 'textsize)
                acattributemodelockposition
                "New Attribute 1"
                (vlax-3D-point 0 0)
                "NEW_TAG1"
                "New Value 1"
            )
            (vla-addattribute def
                (getvar 'textsize)
                acattributemodelockposition
                "New Attribute 2"
                (vlax-3D-point 0 (- (* 1.5 (getvar 'textsize))))
                "NEW_TAG2"
                "New Value 2"
            )
            (command "_.attsync" "_N" blk)
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

Thanks.

0 Likes
17,044 Views
53 Replies
Replies (53)
Message 41 of 54

Kyle.para
Advocate
Advocate

Daniel, 

 

We still use this code till this day. What are you trying to do exactly?

0 Likes
Message 42 of 54

Dany.jee
Contributor
Contributor

Hello Kyle,

I have a library with a lot of symbol/ block, without attributes, I i would like to add them with a lisp quickly, All the same attributes for all block.

 

When I run the first code, the attribute created is on the center of the block, so it appaears in the middle of the view.

I am looking to move it down to let the symbol view free.

I don't know how to define (vlax-3D-point 72 84)  to move it.

And I need part of them to be invisible too.

 

I attech the dwg  a part of the library. In the first  red Rectangle there is a "Native" block , then explode view with attributes I need in, and the last is the "FINAL block".

 

I hope I was enought clear. 

If I know how to set the location of the attibutes and the invisibility I will repeat the code for all attributes.

Waiting for you enlightment.

Cheers

D.

 

 

 

 

 

 

0 Likes
Message 43 of 54

Kyle.para
Advocate
Advocate

Ah I see now what you mean. I thought this was a different code at first. If I understand your question correctly you want to change the position where they land in the block? You need to adjust the values 72 and 84 those are your x y values in the block. You can also make them negative depending on the 0 point. Hope that helps. 

 

As for the invisibility I thought it was already in the code or are they being generated visible currently?

0 Likes
Message 44 of 54

dlanorh
Advisor
Advisor

(vlax-3d-point) requires a list

 

(vlax-3d-point (list 72. 84. 0.))

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

0 Likes
Message 45 of 54

Dany.jee
Contributor
Contributor

Yes that's it, But changing the values of x and y didn't do nothing..

 

I solved to do it, but i really don't know how I did.. haha.

 

Now I have a probleme with: 
(vlax-put def 'Alignment 4)
(vlax-put def acAlignmentmiddle) 

I don't know How to set the attribut Alignementinmiddle .. not the good syntax I think..

 

here is the new code:

(defun c:addatribs ( / ss i blk blks def )
    (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
	(vla-addattribute def
;------------ATTRIBUT 01 ---------------------------------------------------    
		3
             acattributemodelockposition
             "DENOMBREMENT"
             (vlax-3D-point 0 +9)
		;(vlax-put def 'Alignment 4)
		;(vlax-put def acAlignmentmiddle) 
	
             "DENOMBREMENT"
             "xxxx"
         )
;------------ATTRIBUT 02 ---------------------------------------------------  
         (vla-addattribute def
             
		3
             acattributemodelockposition
             "TYPE/CLASSE"
             (vlax-3D-point 0 +5)
             "TYPE/CLASSE"
             "xxxx"
         )
;------------ATTRIBUT 03 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#2"
             (vlax-3D-point 0 -7)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#2"
             "xxxx"
         )
;------------ATTRIBUT 04 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#3"
             (vlax-3D-point 0 -11)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#3"
             "xxxx"
         )

;------------ATTRIBUT 05 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#4"
             (vlax-3D-point 0 -15)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#4"
             "xxxx"
         )

;------------ATTRIBUT 06 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#5"
             (vlax-3D-point 0 -19)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#5"
             "xxxx"
         )

;------------ATTRIBUT 07 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "DESIGNATION_ZONE"
             (vlax-3D-point 0 -23)
             "DESIGNATION_ZONE"
             "xxxx"
         )

	;(vlax-put AttObj 'Alignment acAlignmentmiddle) ;; 4
         (command "_.attsync" "_N" blk)
     )
    (princ)
)
(vl-load-com) (princ)

And I am looking a way to delete the old attribut an block could have before applying this lisp.

 

Thank U in advance for your reply 🙂

0 Likes
Message 46 of 54

devitg
Advisor
Advisor

I got it from your previous post 

 

0 Likes
Message 47 of 54

Dany.jee
Contributor
Contributor

I don't understand  what you want to say?🤔

0 Likes
Message 48 of 54

devitg
Advisor
Advisor

Hi@Dany.jee , I erased my post , as I ask for a sample , and I saw you had uploaded in a previous post . 

 

I put my finger in movement, before  I start my brain neurons.   

0 Likes
Message 49 of 54

Dany.jee
Contributor
Contributor

Hi @devitg, do you have some clue about the alignment method in my lisp? 

I am sur it's very simple but I am not a coder.. yet 

 

(defun c:addatribs ( / ss i blk blks def )
    (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
	(vla-addattribute def
;------------ATTRIBUT 01 ---------------------------------------------------    
		3
             acattributemodelockposition
             "DENOMBREMENT"
             (vlax-3D-point 0 +9)
		;(vlax-put def 'Alignment 4)
		;(vlax-put def acAlignmentmiddle) 
	
             "DENOMBREMENT"
             "xxxx"
         )
;------------ATTRIBUT 02 ---------------------------------------------------  
         (vla-addattribute def
             
		3
             acattributemodelockposition
             "TYPE/CLASSE"
             (vlax-3D-point 0 +5)
             "TYPE/CLASSE"
             "xxxx"
         )
;------------ATTRIBUT 03 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#2"
             (vlax-3D-point 0 -7)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#2"
             "xxxx"
         )
;------------ATTRIBUT 04 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#3"
             (vlax-3D-point 0 -11)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#3"
             "xxxx"
         )

;------------ATTRIBUT 05 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#4"
             (vlax-3D-point 0 -15)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#4"
             "xxxx"
         )

;------------ATTRIBUT 06 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodelockposition
             "CIRCUIT_ELEC/ADR_KNX/DALI_#5"
             (vlax-3D-point 0 -19)
             "CIRCUIT_ELEC/ADR_KNX/DALI_#5"
             "xxxx"
         )

;------------ATTRIBUT 07 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "DESIGNATION_ZONE"
             (vlax-3D-point 0 -23)
             "DESIGNATION_ZONE"
             "xxxx"
         )


;------------ATTRIBUT 08 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "COSWIN"
             (vlax-3D-point 0 -27)
             "COSWIN"
             "xxxx"
         )
;------------ATTRIBUT 09 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "NUM_LOCAL"
             (vlax-3D-point 0 -31)
             "NUM_LOCAL"
             "xxxx"
         )
;------------ATTRIBUT 10 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "POSITION_BORDEREAU"
             (vlax-3D-point 0 -31)
             "POSITION_BORDEREAU"
             "xxxx"
         )

;------------ATTRIBUT 11 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "DENOMINATION"
             (vlax-3D-point 0 -35)
             "DENOMINATION"
             "xxxx"
         )

;------------ATTRIBUT 12 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "FOURNISSEUR"
             (vlax-3D-point 0 -39)
             "FOURNISSEUR"
             "xxxx"
         )
;------------ATTRIBUT 13 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "MARQUE"
             (vlax-3D-point 0 -43)
             "MARQUE"
             "xxxx"
         )
;------------ATTRIBUT 14 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "N°_DE_SERIE/REF"
             (vlax-3D-point 0 -47)
             "N°_DE_SERIE/REF"
             "xxxx"
         )
;------------ATTRIBUT 15 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "ANNEE_DE_FABRICATION"
             (vlax-3D-point 0 -51)
             "ANNEE_DE_FABRICATION"
             "xxxx"
         )
;------------ATTRIBUT 16 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "DEBUT_DE_GARANTIE"
             (vlax-3D-point 0 -51)
             "DEBUT_DE_GARANTIE"
             "xxxx"
         )
;------------ATTRIBUT 17 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "DUREE_DE_GARANTIE"
             (vlax-3D-point 0 -55)
             "DUREE_DE_GARANTIE"
             "xxxx"
         )
;------------ATTRIBUT 18 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "REF_FICHE_TECHNIQUE"
             (vlax-3D-point 0 -59)
             "REF_FICHE_TECHNIQUE"
             "xxxx"
         )
;------------ATTRIBUT 19 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "ATTRIBUT_1"
             (vlax-3D-point 0 -63)
             "ATTRIBUT_1"
             "xxxx"
         )
;------------ATTRIBUT 20 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "ATTRIBUT_2"
             (vlax-3D-point 0 -67)
             "ATTRIBUT_2"
             "xxxx"
         )
;------------ATTRIBUT 21 ---------------------------------------------------  
		(vla-addattribute def
             
		3
             acattributemodeinvisible
             "ATTRIBUT_3"
             (vlax-3D-point 0 -71)
             "ATTRIBUT_3"
             "xxxx"
         )


	;(vlax-put 'Alignment acAlignmentmiddle) ;; 4
         (command "_.attsync" "_N" blk)
     )
    (princ)
)
(vl-load-com) (princ)

 And I found another lisp to delette all attributes

This one:  And I'm trying to combine them into one but, just the first one is executed

(defun tl_fld (v1 v2) (cdr (assoc v1 v2)))

(defun c:attdel2 (/	     ss		cnt	   blk
		  blkdat     blknam	blktblnam  blknewdat 
		  blktbldat  newblkdat	atk_dbug   nextent
		  nextentdat nextenttyp	endblklst
		 )
  (command "undo" "group")
  (setq atk_dbug nil)
  ;;  (setq atk_dbug T)
  (if (setq ss (ssget (list (cons 0 "insert") (cons 66 1))))
    (progn
      (setq cnt -1)
      (repeat (sslength ss)
	(setq blk	(ssname ss (setq cnt (1+ cnt)))
	      blkdat	(entget blk)
	      blknam	(tl_fld 2 blkdat)
	      newblknam	(strcat blknam "_na")
	      blknewdat	(subst (cons 2 newblknam)
			       (assoc 2 blkdat)
			       blkdat
			)
	      blknewdat	(subst (cons 66 0)
			       (assoc 66 blknewdat)
			       blknewdat
			)
	)
	(if (not (tblsearch "block" newblknam))
	  (progn
	    (setq blktblnam (tblobjname "block" blknam)
		  blktbldat (entget blktblnam)
		  newblkdat (subst (cons 2 newblknam)
				   (assoc 2 blktbldat)
				   blktbldat
			    )
		  newblkdat (subst (cons 70 (- (tl_fld 70 blktbldat) 2))
				   (assoc 70 blktbldat)
				   newblkdat
			    )
	    )
	    (if	(entmake newblkdat)
	      (progn
		(if atk_dbug
		  (princ "\nStarting new block definition . . . ")
		)
		(setq nextent	 (entnext blktblnam)
		      nextentdat (entget nextent)
		      nextenttyp (tl_fld 0 nextentdat)
		)
		(while (and nextenttyp (/= nextenttyp "ENDBLK"))
		  (if (/= nextenttyp "ATTDEF")
		    (if	(not (entmake nextentdat))
		      (princ "\nCan't make subentity.")
		      (if atk_dbug
			(princ (strcat "\nAdding "
				       nextenttyp
				       " as subentity "
				       (itoa xcnt)
			       )
			)
		      )
		    )
		    (if	atk_dbug
		      (princ "\nSkipping attribute definition.")
		    )
		  )
		  (setq	nextentdat
				   nil
			nextenttyp nil
		  )
		  (if (setq nextent (entnext nextent))
		    (setq
		      nextentdat (entget nextent)
		      nextenttyp (tl_fld 0 nextentdat)
		    )
		  )
		)
		(setq endblklst	(list (cons 0 "endblk")
				      (cons 100 "AcDbBlockEnd")
				)
		)
		(if (not (entmake endblklst))
		  (princ "\nCan't terminate block definition")
		)
	      )
	      (princ "\nCan't start new block definition.")
	    )
	  )
	)
	(if (tblsearch "block" newblknam)
	  (progn
	    (entdel blk)
	    (entmake blknewdat)
	  )
	)

      )
      (setq ss nil)
    )
  )
  (command "undo" "end")
  (tl_$tlp)
)

 

hanks for your help 🙂

0 Likes
Message 50 of 54

Dany.jee
Contributor
Contributor

Hello,

I still didn't fins the solution, anyone have a clue to center the attributes?

I am reaaly stuck with that.

 

Dany

0 Likes
Message 51 of 54

Anonymous
Not applicable

Hey Daniel did you try:

 

;(vlax-put def acAlignmentcenter)
0 Likes
Message 52 of 54

hunyar.adam.mate.provill
Explorer
Explorer

Hello everyone!

 

I'm working with a lot of blocks and I need to add some extra attributes to each of them. Existing blocks work with parameters and dynamic attributes. When I use these lisp's they work perfectly as long as I use the ATTSYNC command. In this case, the new attributes are removed from the blocks. I noticed that if I delete all the parameters from my existing block, new parameters can remain in my old blocks, but I have to re-parameterize them all.

 

Is there any solution?

 

Thanks

0 Likes
Message 53 of 54

Kyle.para
Advocate
Advocate

Hey Hunyar, you can try to open up the  block editor and then open up the parameter manager in there. I believe you have to reassign the new attribute to the old parameter. 

 

Cheers, KP

0 Likes
Message 54 of 54

hunyar.adam.mate.provill
Explorer
Explorer
Yes but it is very time consuming and this is the reason why i am lookig for better solution.
The new attributes do not have to work with parameters. Sometimes i need to determine elements location, foor exampler name of room or room numbers. I want to prevent that use ATTSYNC command and lose everything what I did before.
0 Likes