Is there a way to make this LISP to count blocks inside another block?

Is there a way to make this LISP to count blocks inside another block?

Anonymous
Not applicable
8,347 Views
23 Replies
Message 1 of 24

Is there a way to make this LISP to count blocks inside another block?

Anonymous
Not applicable

I use this LISP to count how many blocks there is inside my dwg.


I attached 2 jpeg to make more understandable.


The first one, (IMG_01) there's 3 blocks (A, B and C). And the text next to them count how many of them there is on my DWG.


The second one (IMG_02) shows the same blocks, but they are inside another block (let's call it ABC). When I use the recount LISP, it shows 0.


Is there a way to make this LISP comand to count those blocks when they are inside another block?


Here's the LISP recount I use.

 

(defun c:recount (/ block_list cnt env_name)
(setq block_list
(list
(cons '2 "A")
(cons '2 "B")
(cons '2 "C")
)
)
(foreach i block_list
(if
(setq ss (ssget "X" (list '(0 . "INSERT") 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)
)

And here's DieselExpression I use on the texts next to the blocks (to count the block "A", for example)

 

$(getenv, BLOCK_COUNT_A)

I've done all this using this topic as reference, but my knowledge using LISP is very limited to help with my issue.

 

0 Likes
Accepted solutions (1)
8,348 Views
23 Replies
Replies (23)
Message 21 of 24

Moshe-A
Mentor
Mentor

yes it count also dynamic blocks

 

0 Likes
Message 22 of 24

ronjonp
Mentor
Mentor

Code removed. 

0 Likes
Message 23 of 24

Anonymous
Not applicable

I'll take this as a solution to my specific problem despite all the effort you both put to solve!

 

Thank you!

0 Likes
Message 24 of 24

ahmed_ayoub
Explorer
Explorer

hello guys can some one explain to me how to add this code into AutoCAD , or if anyone have the lisp program itself kindly loaded , I have some following blocks inside the main blocks i want something to count this  following blocks?? 

0 Likes