Jump to content

LISP to add an attribute to a large number of unique blocks


Recommended Posts

Posted

Hi,

 

I am currently attempting to add an attribute to a large number of unique blocks in a drawing. My current process is to go one by one, manually going into block editor and inserting the attribute. I was wondering if there is a way to automate this process. Is there a way in LISP for me to add the same attribute to all/many blocks at once? I have other attributes in the blocks, could I set the location of the new attribute as an offset from a current one? 

 

Any help is much appreciated. 

Posted (edited)
1 hour ago, aridzv said:

see if the attached lisp helps.

add_ATT.lsp 1.68 kB · 2 downloads

Aridzv.

Thank you! This helps a lot, the hardest part I think is going to be getting the attribute in a position on the block that works. I need it to be right above another attribute I have visible. This is a very good starting point though so thanks again for sharing. 

Edited by cooldude224
  • Like 1
Posted (edited)
19 hours ago, aridzv said:

see if the attached lisp helps.

add_ATT.lsp 1.68 kB · 7 downloads

Aridzv.


Cool, I saved this one.

I just added a couple lines for settings.

 

@cooldude224 My edit should help you with the position of the attribute

 

 

(defun c:add_ATT (/ ss blk blk-lst atts-lst def AttObj obj2  text_height ip mode align)
	;; settings. Feel free to adapt to your needs.
	;; Feel free to change the settings from hard coded to user input.  See below
	(setq text_height 2.5)
	(setq ip (list 0.0 0.0))	;; insert point in the block
	(setq default_value "")
	;; MODE: 
	;;	(any combination of constants can be used by adding them together):
	;; 	acAttributeModeNormal
	;;	acAttributeModeInvisible
	;;	acAttributeModeConstant
	;;	acAttributeModeVerify
	;;	acAttributeModePreset
	(setq mode acAttributeModeNormal)
	;; Allignment: 
	;; acAlignmentLeft / acAlignmentCenter / acAlignmentRight / acAlignmentAligned / acAlignmentMiddle / acAlignmentFit / acAlignmentTopLeft / acAlignmentTopCenter / acAlignmentTopRight / acAlignmentMiddleLeft / acAlignmentMiddleCenter / acAlignmentMiddleRight / acAlignmentBottomLeft / acAlignmentBottomCenter / acAlignmentBottomRight
	(setq align acAlignmentRight)
	
;; COMMENT OUT THESE NEXT LINES IF YOU WANT THE HARD CODED SETTINGS	
	(setq text_height (getdist "\nText height: "))
	(setq ip (getpoint "\nInsert point: "))
	(setq default_value (getstring "\nDefault value: " T))

  (vl-load-com)
  (setq Tag (strcase (getstring "\nSpecify attribute tag: ")))

  (if (setq ss (ssget '((0 . "INSERT")))) ;change if to while repeats command if you keep selecting things
    (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))  ;makes a list of all entitys by ename in selection set and steps thought them one at a time
      (setq blk (cdr (assoc 2 (entget e))))	
      (if (not (vl-position blk blk-lst)) 
        (progn
          (setq blk-lst (cons blk blk-lst))
          (setq obj2 (vlax-ename->vla-object e)) 
          (setq atts-lst nil)                    ;clear list from last use
          (if (= (vla-get-hasattributes obj2) :vlax-true)
            (foreach att (vlax-invoke obj2 'getattributes)
              (setq atts-lst (cons (strcase (vla-get-tagstring att)) atts-lst))   ;make a list of all Attributs tag names to check rather then checking them all individually
            )   ;;close foreach
          )     ;;close if
          (if (not (member tag atts-lst))                                    ;checks list for "SYSTEM" could also use vl-position
            (progn
              (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
					;; VL : RetVal = (vla-AddAtribute object Height Mode Prompt InsertionPoint Tag Value)
              (setq AttObj (vla-addattribute def text_height mode "" (vlax-3D-point ip) TAG default_value))
              (vlax-put AttObj 'Alignment align)
			  (vla-move AttObj (vlax-3D-point (list 0.0 0.0)) (vlax-3D-point ip))
              (command "_.attsync" "_N" blk)
            )   ;;close progn
          )     ;;close if
        )       ;;close progn     
      )         ;;close if
    )           ;;close foreach
  )             ;;close if
  (princ)
)

 

Edited by Emmanuel Delay
  • Like 3

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...