PDA

View Full Version : counting blocks


b_v_mc
2004-12-16, 12:26 AM
Hey all , I was wondering if you could help me out. I don't know anything about vba or visual or autolisp and i need a routine that will count the blocks i specify. I was thinking something along the lines of a command that i can make into a button. and when i use the command it will prompt me to make a selection from there i want to highlight or create a selection box around a specific area and have it count all the blocks i specify before hand and have a separate window come up with all the counts like BCOUNT only with the blocks i need. It would also have to count nested blocks.

The reason i need something specific like this instead of just using BCOUNT is that i need specific blocks, i need nested blocks and theres instances where i don't need every block in the drawing counted. I mainly will be using this to count cans on tracks of lights and when you have to count hundreds of cans its a little time consuming. the way its set up is there are several blocks of track with a certain number of cans on each I.E. 8 FOOT 5 CANS and 6 FOOT 3 CANS etc. also i have to explode one of these track blocks and modify it to fit in certain areas that need longer tracks then we have blocks for, but the cans are blocks themselves so thats where i come into a problem with BCOUNT . i need something that will count the nested can blocks and the can blocks that arnt nested and have a total, along with the other blocks that i specify, but the other blocks are simple and can be counted with bcount its just the track that gives me a hard time.

If i was unclear about anything please ask me, and i appreciate any and all help you guys can provide me.

also i was wondering besides this site what are some other sites that are good for learning VBA and Auto and visual LISP... the acad help is kind of mind tingling thanks

miff
2004-12-16, 01:40 AM
Here ya go. No error checking and it only goes 1 level deep in nesting, but it sounds like it will do what you need.

(defun c:count-blocks (/ bname doc ent name pt1 pt2 ss tmp total)
(if (and (or (setq ent (car (entsel "\nSelect a block to count: ")))
(setq name (strcase (getstring "\nBlock name to find: ")))
)
(or name
(setq name (cdr (assoc 2 (entget ent))))
)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq bname "")
(setq pt1 (getpoint "\nFirst corner of Area to look: ")
pt2 (getcorner pt1 "...other corner"))
)
(progn
;;;get all individual inserts
(if (setq ss (ssget "w" pt1 pt2 (list '(0 . "INSERT")(cons 2 name))))
(setq total (sslength ss))
(setq total 0)
)
;;;go through block defs
(vlax-for blk (vla-get-blocks doc)
(if (and (not (vl-string-search "`*" (setq bname (vla-get-name blk))))
(not (vl-string-search "|" bname))
(setq tmp 0)
)
(progn
(vlax-for obj blk
(if (and (eq (vla-get-objectname obj) "AcDbBlockReference")
(eq (vla-get-name obj) name)
)
(setq tmp (1+ tmp))
)
)
(if (and (not (zerop tmp))
(setq ss (ssget "w" pt1 pt2 (list '(0 . "INSERT")(cons 2 bname))))
)
(setq total (+ total (* (sslength ss) tmp)))
)
)
)
)
)
)
(if total
(princ (strcat "\nTotal of " (itoa total) " " name " found."))
)
(princ)
)


As for other sites, I like Cadvault.com and the Swamp.org as well as the autodesk newsgroups.

whdjr
2004-12-16, 02:22 PM
My vote goes for theswamp.org (http://theswamp.org/phpBB2/index.php?sid=764fcb893afde25dc21ac0b2f828761f)

b_v_mc
2004-12-17, 05:05 PM
Hey MIff, thanks for the time to post this code, but it didnt count the nested blocks. thanks though

miff
2004-12-17, 07:35 PM
Hey MIff, thanks for the time to post this code, but it didnt count the nested blocks. thanks though
You'e welcome. It does count nested blocks in my test drawing. Can you post a small sample drawing that has a few of your blocks along with what the expected count should be?

b_v_mc
2004-12-18, 12:25 AM
Ahh, im sorry it does work. The first time i tested it i just made 2 seperate blocks on named 8 FOOT 5 CANS and one 6FOOT 3 CANS and each of those where made with the cans as nested blocks named CAN, and when id select it and the highlight both it would only tell me it found 1 8 FOOT 5 CAN block, it didnt occur to me at the time to have an unnested CAN block to select before throwing the window over my blocks. Very cool thanks and sorry it was my bad.

This definately is a step in the right direction, id like for it to count several, specific blocks in the coarse of highlighting, say i had a Block called 8FOOT 5 CANS and 5 blockS called 2X4 FLORECENT. when i do my high lighting it will count the 5 CAN blocks nested in 8 FOOR 5 CANS and the 5 2X4's but of course this is over simplified there would be alist of about 20 or more blocks that it would need to find and various track lengths with various numbers of CAN blocks. then it would be perfect, exactly what id envisioned, but like i said this is great you certainly provided me with something i can use where as others have not, its actually funny it took you very little time to come up with this where ive been search for an answer for a few weeks and sitll no answer form the peopel who said they were trying this out. thanks again A+ stuff

b_v_mc
2004-12-22, 09:31 PM
Would it be to difficult to impliment those changes into the code?

miff
2004-12-23, 07:23 PM
Sorry, I've been really busy of late.
You want an output something like this?

Command: count-blocks

Select a block to count:
Block name to find: can
First corner of Area to look: ...other corner
3 CAN's found in 1 instances of 3cans
10 CAN's found in 2 instances of 5cans
6 CAN's found in 1 instances of 6cans
7 CAN's found in 1 instances of 7cans
Total of 26 CAN found.

b_v_mc
2004-12-23, 08:32 PM
well actually what you gave me works great first of all.. so exactly how that works only it does several blocks at a time instead of just the one you select. so if i had 5 florecense which are just regular blocks and then a run of track with 5 cans, which the cans are nested blocks, when i am asked to make my select and i draw a triangle around a large area, it will grab all accounts of blocks in that triangle, tso it will pop up a list that has the block name and the amount of those blocks reguardless of nesting, we only have one type of lighting that has anested block and those are our track blocks but theres several different track blocks butt they are only nested one level so an example i suppose would be.

2x4 florescence ..= 45
FLOOD ..............=256
SPOTS ..............=378 (floods and spots would be the only nested ones)
recessed pin light = 47
etc...
but if you dont have time or if its to difficult its cool, the first code works good. thanks a lot again and marry christmas if you celebrate it.. if not happy new year.

b_v_mc
2005-01-20, 08:17 PM
So hey miff, just checking up its been a little while...was still wondering about the last post imade.

have a good day.