Auto populate of block attributes with incremental series number of existing blocks

Auto populate of block attributes with incremental series number of existing blocks

vinceaedrian86GN6
Explorer Explorer
1,530 Views
7 Replies
Message 1 of 8

Auto populate of block attributes with incremental series number of existing blocks

vinceaedrian86GN6
Explorer
Explorer

I am trying to make a routine where I select the blocks that I need to auto-populate their attributes with incremental series number at the end. For example: "Motor-1", "Motor-2", "Motor-3"... "Motor-100". The thing is, this series number changes over numerous revisions and the series numbers are assigned based on the engineer's wiring diagram so Lee Mac's auto numbering LISP is not applicable. I would be needing something more simple and flexible where if there are 100 blocks, I can select them 1 by 1 and the LISP will cycle through my selection 1 by 1 and populate the attribute tag that increments in order of my selection. If there are future revision, I can just select the blocks 1 by 1 again but in a different order, where required. 

 

From Lee Mac's LISP codes, I have compiled some syntaxes and derived with these but no matter how much I search and edit, I cannot make it work. 

 

(defun c:autotag (/ ssBlock intIncrement startValue prefixValue objBlock)
     (setq ssBlock (ssget "_I"))
     (setq intIncrement 0)
     (setq startValue (getint "\nSpecify Start Value: ")
               prefixValue (strcase (getstring t "\nSpecify prefix: ")))

     (repeat (sslength ssBlock)
          (setq objBlock (vlax-ename->vla-object (CDR (CAR (entget (ssname ssBlock intIncrement))))))
          (setpropertyvalue obj "EQP_TAG" (strcat prefixValue (rtos startValue 2 0)))

          (setq startValue (1+ startValue))
          (setq intIncrement (1+ intIncrement))
     )
)

 

 

I know this community would be willing to help. Thank you all! 

0 Likes
Accepted solutions (1)
1,531 Views
7 Replies
Replies (7)
Message 2 of 8

hak_vz
Advisor
Advisor
Accepted solution

 

@vinceaedrian86GN6 

Welcome to Autolisp forum.

I hope this is what you looking for.

(defun c:autotag (/ LM:vl-setattributevalue ssBlock intIncrement startValue prefixValue objBlock)
	(defun LM:vl-setattributevalue ( blk tag val )
		(setq tag (strcase tag))
		(vl-some
		   '(lambda ( att )
				(if (= tag (strcase (vla-get-tagstring att)))
					(progn (vla-put-textstring att val) val)
				)
			)
			(vlax-invoke blk 'getattributes)
		)
	)
	(setq ssBlock (ssget))
	(setq intIncrement 0)
	(setq startValue (getint "\nSpecify Start Value: ")
		   prefixValue (strcase (getstring t "\nSpecify prefix: ")))

	(repeat (sslength ssBlock)
	  (setq objBlock (vlax-ename->vla-object (ssname ssBlock intIncrement)))
	  (LM:vl-setattributevalue objBlock "EQP_TAG" (strcat prefixValue (rtos startValue 2 0)))
	  (setq startValue (1+ startValue))
	  (setq intIncrement (1+ intIncrement))
	)
	(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.
0 Likes
Message 3 of 8

vinceaedrian86GN6
Explorer
Explorer

This works like magic thank you so much!!! Can you briefly explain the lines that you added? I understand that the LM functions are from Lee Mac? I am now curious how my initial set of syntaxes didn't work. Was there a specific item in LM functions that are really needed to execute? 

0 Likes
Message 4 of 8

hak_vz
Advisor
Advisor

I'm currently at work so just briefly.

Setpropertyvalue function works with individual entity, not compound entity i.e block with attributes.

That's why we have LM function (it checks if block have attribute with that name and it sets its value.

Nothing special but that's it.

 

 

 

 

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 5 of 8

vinceaedrian86GN6
Explorer
Explorer

That is so helpful. Thank you so much for the help!!!

0 Likes
Message 6 of 8

ВeekeeCZ
Consultant
Consultant

I would encourage you to fix your code.

 

The obvious issue is that you supply the getpropertyvalue with VLA-OBJECT while it needs just ENAME.

The second... explain me this... (CDR (CAR (entget (ssname what you expect to get?

 

Also, ever heard of the ITOA func?

 

 

Spoiler. 

(defun c:Autotag ( / s p n)

  (or *at-i* (setq *at-i* 1))  		; global variable
  
  (if (and (setq s (ssget "_:L" '((0 . "INSERT") (66 . 1))))
		(setq *at-i* (cond ((getint (strcat "\nSpecify Start Value <" (itoa *at-i*) ">: ")))
				   (*at-i*)))
		(setq p (strcase (getstring t "\nSpecify Prefix: ")))
		)
    (repeat (setq n (sslength s))
      (setpropertyvalue (ssname s (setq n (1- n))) "UV" (strcat p (itoa *at-i*))) ;EQP_TAG
      (setq *at-i* (1+ *at-i*))))
  (princ)
  )

 

0 Likes
Message 7 of 8

Village_Idiot
Contributor
Contributor

I know I'm late to the party but for anyone else stumbling on this...

Lee Mac's Incremental Numbering Suite (numinc) will do want you want.  Be sure to see the "About" button at the bottom for additional options.

 

Village_Idiot_0-1698343147069.png

 

Message 8 of 8

Sea-Haven
Mentor
Mentor

Just another suggestion rather than hard code an Attribte tag name can pick the name from a block. That way can use to change others. 

 

 

(setq EQP_TAG (cdr (assoc 2 (entget (car (nentsel "\Pick the attribute to change in block "))))))

 

Maybe also

 

(setq ssBlock (ssget '((0 . "INSERT"))))

 

0 Likes