Modification on Given LISP

Modification on Given LISP

fkp7057
Advocate Advocate
1,371 Views
22 Replies
Message 1 of 23

Modification on Given LISP

fkp7057
Advocate
Advocate

Hello, I found below the old lisp routine in this forum.

 

this lisp counts specific blocks from the entire drawing. But want it to count only block which are in model space.
Link of the original post  : LINK 

(defun c:recount (/ block_list cnt env_name)
(setq block_list
(list
(cons '2 "1")
(cons '2 "2")
(cons '2 "3")
)
)
(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)
)

 

 

0 Likes
Accepted solutions (1)
1,372 Views
22 Replies
Replies (22)
Message 2 of 23

hak_vz
Advisor
Advisor

@fkp7057 

For a start change this line

(setq ss (ssget "X" (list '(0 . "INSERT") i)))

to

(setq ss (ssget "X" (list '(0 . "INSERT")(410 . "Model"))))

 

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 23

Sea-Haven
Mentor
Mentor

Like hak_vz if you want only in a layout use (cons 410 (getvar 'ctab)) can use in a loop finding in all layouts, but "Model" is still a valid ctab name.

0 Likes
Message 4 of 23

fkp7057
Advocate
Advocate

I have updated as per your suggestion, but still, this lisp count block which is in layout space.

0 Likes
Message 5 of 23

fkp7057
Advocate
Advocate

Tried this way too but, the block counts come 93 for all blocks, but actually, there are less than 10 countss of a particular block

0 Likes
Message 6 of 23

hak_vz
Advisor
Advisor

@fkp7057 wrote:

I have updated as per your suggestion, but still, this lisp count block which is in layout space.


Sorry It was early morning and I just copied construction from the code you have attached.

Try this

 

 

(setq ss (ssget "_X" '((0 . "INSERT")(410 . "Model"))))

 

 

Tested and it works OK.

Or you can use

 

(setq ss (ssget "_A" '((0 . "INSERT")(410 . "Model"))))

 

Similar to the "X" mode string but excludes objects on frozen layers. Selects all objects on thawed layers.

 

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 7 of 23

fkp7057
Advocate
Advocate

fkp7057_0-1636544638086.png

 

see the attached snap, it is loaded and worked as well but still, it is counting blocks that are in layout space.

might be the above error caused the problem.

 

0 Likes
Message 8 of 23

hak_vz
Advisor
Advisor

Question is is this code useful for what you want to use it.

Try to define your problem, not in a few words, but thoroughly.  Do you want to count all blocks in model space or just particular on i.e defined by its name. Since you are electrical engineer this would be like: count all blocks (inserts) named "BULB" or "FUSE".... .

Code can than be created for your specific case or better for general usage. Reusing old code isn't always best options if this code is created for different purpose.

In some case property tab can give you meaningful result without using autolisp. Check this blog post

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 9 of 23

fkp7057
Advocate
Advocate

My objective is that the quantity of the particular block needs to update automatically as per block count in model space my drawing contains more than 25-30 different blocks.

i used to show Legend/description and its quantity as per the below image.

fkp7057_0-1636546882723.png

that legend table I kept at layout/paper space.

given block work correctly but it also counts block which is in layout space. and I m having multiple sheets and going to put that table in all the sheets.

then this quantity also going to increase.

 

what I want is this code count only blocks that are in model space so in field text it shows the correct no. of the count.

as of now, I am using the method shown in the link which I have shared in an earlier msg.

here below I am pasting the code that I am going to use it.

(defun c:recount (/ block_list cnt env_name)
(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 "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" '((0 . "INSERT")(410 . "Model"))))

(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
Message 10 of 23

fkp7057
Advocate
Advocate

I am aware that there are many other and easy ways to count the block. 

some time drawing not finished in one go we need to keep updating this qty. sometimes we forgot to update qty.

so here I want to standardize/ wanted to do some automation thing for my department.

progressively I will add more and more blocks so this 1 lisp help solve the purpose.

going to set up a template kind of thing. which will be a great help for our teammates.

0 Likes
Message 11 of 23

hak_vz
Advisor
Advisor

@fkp7057 Try this . If it wont work I'll check it later today. I'm Currently at work

 

(defun c:recount (/ block_list cnt env_name)
	(setq block_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")
	)
	(foreach i block_list
		(if
			(setq ss (ssget "_X" (list (cons 0 "INSERT")(cons 410 "Model") (cons 2 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)
)

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 12 of 23

fkp7057
Advocate
Advocate

fkp7057_0-1636603542595.png

not worked 😐

 

I am using Autocad on windows10 OS.

0 Likes
Message 13 of 23

hak_vz
Advisor
Advisor

@fkp7057  Attach sample dwg with some blocks in model space and layouts, named as in your lisp so I can check how code works.

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 14 of 23

fkp7057
Advocate
Advocate

PFA attached a Sample CAD file along with the LISP file which I am using it.

In the Drawing file you can see that block name "S1" are 5 in model space but in layout space (qty. table),  its total qty shows 8 (5 in model space +3 which are in 3 layout space)

0 Likes
Message 15 of 23

hak_vz
Advisor
Advisor
Accepted solution

Tested and it works OK at my side

 

(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 "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)
)

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 16 of 23

fkp7057
Advocate
Advocate

Thank you very much.

finally, it worked... 

✌️👍

Message 17 of 23

hak_vz
Advisor
Advisor

I am glad I was able to help.

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 18 of 23

Sea-Haven
Mentor
Mentor

No need for cons 2's a ssget accepts (cons 2 "s1,s2,s3,s4,s5.....) or a foreach use ssget with 1 block name and then use ssadd to add to master selection.

 

0 Likes
Message 19 of 23

fkp7057
Advocate
Advocate

can you explain a little bit more as I am not good at lisp coding? 

I am not able to understand what you want to say.

0 Likes
Message 20 of 23

Sea-Haven
Mentor
Mentor

Like Hak_vz used a foreach for block names (setq ss "X" (list(cons 0 "INSERT") (cons 2 "s1,s2,s3,s4....")))

0 Likes