Block count and link with field text

Block count and link with field text

fkp7057
Advocate Advocate
719 Views
4 Replies
Message 1 of 5

Block count and link with field text

fkp7057
Advocate
Advocate

@hak_vz   couple of modifications are required.

 

this lisp does not count blocks that are dynamic. for example, the block which is having visibility parameters,  This Lisp count block which is having default visibility, if I change block visibility then it won't count.

In another case block having action parameters like stretch/move/rotation does not count in the field it shows "0".

 

Original post link :

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/modification-on-given-lisp/m-p/10754... 

 

Code :

 

(defun c:recount (/ block_list cnt env_name ss)
(setq block_list
(list
 (cons 2 "S1")
 (cons 2 "S2")
 (cons 2 "S3")
 (cons 2 "S4")
 (cons 2 "S5")
 (cons 2 "S6")
 (cons 2 "S7")
 (cons 2 "S8")
 (cons 2 "S9")
 (cons 2 "S10")
 (cons 2 "S11")
 (cons 2 "S13")
 (cons 2 "S14")
 (cons 2 "S15")
 (cons 2 "S16")
 (cons 2 "S17")
 (cons 2 "S18")
 (cons 2 "S19")
 (cons 2 "S20")
 (cons 2 "S21")
 (cons 2 "NORMAL PUSH BUTTON")
 (cons 2 "NORMAL PUSH BUTTON (W)")
 (cons 2 "NORMAL PUSH BUTTON (FLAME PROOF)")
 (cons 2 "DB")
 (cons 2 "LDB")
 (cons 2 "ELDB")
 (cons 2 "SPDB")
 (cons 2 "ESPDB")
 (cons 2 "ESPDB")
 (cons 2 "DB")
 (cons 2 "JB")
 (cons 2 "MODULE_PLATE")
)
)
(foreach i block_list
(if
(setq ss (ssget "X" (list (cons 0 "INSERT") (cons 410 "Model") i)))

(progn
(setq
cnt (itoa (sslength ss))
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)

(progn
(setq
cnt "0"
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)

)
)
(command "_REGEN") ;_refreshes field values
(princ)
)

 

 

0 Likes
Accepted solutions (1)
720 Views
4 Replies
Replies (4)
Message 2 of 5

hak_vz
Advisor
Advisor

@fkp7057

Dynamic and static blocks are two different kind of beasts and you can not treat them the same way.

When you insert static block it retains its name  let say (2 . "S1").

With dynamic block situation is different: 

After you insert a dynamic block his name parameter is   set to (2 . "S1"), but after dynamic property change is applied like flip, scale, visibility it name changes to let say (2 . "*U34").

Try this on dynamic block

(cdr  (assoc  2  (entget(car(entsel  "\nSelect  block  >")))))

To work with blocks, static or dynamic, to avoid this behavior one have to use visual lisp.

When following code is applied on a block it shows all properties of a block (insert) that are not accessible true entget command.

(vlax-dump-object  (vlax-ename->vla-object  (car(entsel))))

In output of this command when block is selected you may notice two properties "Name" (that is key 2 in) "EffectiveName ".

I don't know how versatile you are in using it so I will create new code later today.

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

fkp7057
Advocate
Advocate

noted.

I m having zero experience in any kind of coding.

so don't know what to do with your code.

I'll be waiting for your code. 

 

👍

0 Likes
Message 4 of 5

hak_vz
Advisor
Advisor
Accepted solution

Try if this code works correct in your case. Remember block names are case sensitive.

(defun c:recount (/ block_list ss cnt i bo);
	(setq block_list
		(list 
			"S1" "S2" "S3" "S4" "S5" "S6" "S7" "S8" "S9" "S10" "S11" "S13" "S14" "S15" 
			"S16" "S17" "S18" "S19" "S20" "S21" "NORMAL PUSH BUTTON" "NORMAL PUSH BUTTON (W)" 
			"NORMAL PUSH BUTTON (FLAME PROOF)" "LDB" "ELDB" "SPDB" "ESPDB" "ESPDB" "DB" "JB"
			"MODULE_PLATE"
		)
	)
	(setq ss (ssget "X" (list (cons 0 "INSERT") (cons 410 "Model"))))
	(foreach block_name block_list
		(setq i -1 cnt 0)
		(while (< (setq i (1+ i)) (sslength ss))
			(setq bo (vlax-ename->vla-object (ssname ss i)))
			(if (= block_name (vlax-get bo 'EffectiveName)) (setq cnt (1+ cnt)))
		)
		(princ (strcat "\nBLOCK_COUNT_" block_name " " (itoa cnt)))
		(setq env_name (strcat "BLOCK_COUNT_" block_name))
		(setenv env_name (itoa cnt))
	)
	(princ "\n")
	(command "_REGEN")
	(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.
Message 5 of 5

fkp7057
Advocate
Advocate
Thanks again !! This worked awesome.
0 Likes