Author Topic: Counting Blocks  (Read 19704 times)

0 Members and 1 Guest are viewing this topic.

Luke

  • Guest
Counting Blocks
« on: April 09, 2007, 05:39:49 PM »
I've tried this before and was having a difficult time getting the results I was hoping for.

I am using Vanilla AutoCAD 2008 Full Version.

I am doing some drawings that require the use of several hundres "icons".  These "icons" are blocks that represent a fixture to be used on the floor plan.  I want AutoCAD to count all of the blocks for me.  Now I know I can export the information to different types of external files but I do not want to do that.  I want my drawing and the table to all be contained on the same paper so I can send 1 pdf to my installers.  What I would like to do is put the information, the table right there on my drawing, with a picture of the icon or block and a description and a count.  That way it will work as a legend and as a very basic Bill of Material.  I have figured out how to get the "data Extraction" to initially count my blocks but I am having trouble getting the qty to update with any changes.  For example if I insert 3 of block "Widget-B" and create the table and then add 2 more of "widget-b" tomorrow I thought there is a way to get the table to update rather than completely regenerating it.  Problem is I'm not having much luck getting it to happen. 

Any help or suggestions directly relating to this topic would be greatly appreciated.

Thanks in advance

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Counting Blocks
« Reply #1 on: April 09, 2007, 06:11:40 PM »
Just  a guess ... is there a 'field' you can use that keeps a count of blocks in the dwg?
TheSwamp.org  (serving the CAD community since 2003)

Luke

  • Guest
Re: Counting Blocks
« Reply #2 on: April 10, 2007, 12:36:10 PM »
OK sure that is what I was hoping.  Can you tell me what the field is?

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Counting Blocks
« Reply #3 on: April 10, 2007, 02:40:40 PM »
I didn't find a specific field but I do have something that will work, of course it involves lisp.

1. Insert field of DIESEL expression $(getenv, blk_count)
2. Load the following AutoLISP app and run it every time you want to update your block count.

Code: [Select]
(defun c:recount ( / cnt )
  (if
    (setq cnt (itoa (sslength (ssget "x" '((0 . "INSERT")(2 . "<block name>"))))))
    (setenv "blk_count" cnt)
    )
  )

3. Run 'updatefield' and select your table.
TheSwamp.org  (serving the CAD community since 2003)

Luke

  • Guest
Re: Counting Blocks
« Reply #4 on: April 10, 2007, 02:43:11 PM »
I've never used Diesel at all but do enough lisping to be dangerous.  I'll give this a try, hopefully this evening.  Thanks for you help.

deegeecees

  • Guest
Re: Counting Blocks
« Reply #5 on: April 10, 2007, 03:24:58 PM »
If you have Express Tools (I may be getting off the wrong train here) you could use "bcount". Try typing it in the command line.

Luke

  • Guest
Re: Counting Blocks
« Reply #6 on: April 10, 2007, 03:38:23 PM »
please explain how to get that information yielded by 'bcount" into a table or list that will show up on my drawing and can be easily updated.

I think Mark is on the right track I just need to get a free minute to try his suggestion.

deegeecees

  • Guest
Re: Counting Blocks
« Reply #7 on: April 10, 2007, 03:48:52 PM »
All I know is the "old school" method using attributes and Lisp, dont wanna gum up the issue at hand...

Luke

  • Guest
Re: Counting Blocks
« Reply #8 on: April 10, 2007, 04:03:59 PM »
Mark,
In the code you supplied, do I need to identify the name(s) of each of my blocks somewhere?

Luke

  • Guest
Re: Counting Blocks
« Reply #9 on: April 10, 2007, 04:06:47 PM »
Sorry, I see it now.

Luke

  • Guest
Re: Counting Blocks
« Reply #10 on: April 10, 2007, 04:17:46 PM »
OK Mark. I've got it partially working.  What would my code look like for 4 or 5 different blocks?  I've got it working for a single block but do not know how to change the code properly to work for multiples. 

Or would I have to have a different diesel expression and different lisp for each block?  Sure hope not but will do if necessary.

Thanks,
Luke

Crank

  • Water Moccasin
  • Posts: 1503
Re: Counting Blocks
« Reply #11 on: April 11, 2007, 02:24:50 AM »
Mark, that is fantastic!

Never noticed before that diesel also can be used in fields.
Vault Professional 2023     +     AEC Collection

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Counting Blocks
« Reply #12 on: April 11, 2007, 10:11:53 AM »
Or would I have to have a different diesel expression and different lisp for each block?

You'll have to have a different diesel expression for each block, I don't see anyway around that.

Here's the code that handles multiple blocks.

Code: [Select]
(defun c:recount (/ block_list cnt env_name)

  ;; The intent of this app is to count the number of blocks inserted into
  ;; a given dwg and set environment variables based on the block name so
  ;; one can use the diesel expression '$(getenv, <name>)' in a table cell
  ;; to track the number of block insertitions therein
  ;;
  ;; Typical output
  ;;
  ;; Set BLOCK_COUNT_ELM to 6
  ;; Set BLOCK_COUNT_CITRUS to 5
  ;; Set BLOCK_COUNT_OAK to 11
  ;; Set BLOCK_COUNT_PALM to 5
  ;;
  ;; Given the above output you would use '$(getenv, BLOCK_COUNT_OAK)' in
  ;; your table cell
  ;;
  ;; version 1.0
  ;; Wed Apr 11, 2007
  ;; Mark S. Thomas ( mark@theswamp.org )

  ;
  ; list of blocks you wish to count
  ;
  (setq block_list
        (list
          ;
          ; add a line for each block you wish to count
          ; the following are just samples
          ;
          (cons '2 "ELM")
          (cons '2 "CITRUS")
          (cons '2 "OAK")
          (cons '2 "PALM")
          )
        )


  (foreach i block_list
           (if
             (setq ss (ssget "X" (list '(0 . "INSERT") i)))
             (progn
               (setq
                 ;
                 ; count the number of blocks inserted
                 ; create the environment name, i.e. "BLOCK_COUNT_ELM"
                 ;
                 cnt (itoa (sslength ss))
                 env_name (strcat "BLOCK_COUNT_" (cdr i))
                 )

               ; finally we set the environment variable
               (setenv env_name cnt)

               ; show the user what was set and how many blocks were counted
               (princ (strcat "\nSet " env_name " to " cnt))
               )
             )
           )
  (princ)
  )

This is the block I used for testing.

TheSwamp.org  (serving the CAD community since 2003)

Luke

  • Guest
Re: Counting Blocks
« Reply #13 on: April 11, 2007, 10:47:34 AM »
Mark,
Perfect! This is doing exactaly what I want.  And your comments in the code help me understand what is going on so next time I can give it a shot on my own.

Thanks so much!

So one final question.  Right now doing this in a table it does not count the block in the table.  Great, just what I want, but If I wanted to do this as just a list (so no gird or headaches of a table show up) it would count the block in the list resulting in 1 too many.  Is there a way to tell the lisp to subtract 1 from the total?

OK now I'm done bothering you.  Thanks a ton for your help!

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Counting Blocks
« Reply #14 on: April 11, 2007, 11:02:53 AM »
Is there a way to tell the lisp to subtract 1 from the total?

Try changing the line ...

Code: [Select]
cnt (itoa (sslength ss))
to ...

Code: [Select]
cnt (1- (itoa (sslength ss)))
TheSwamp.org  (serving the CAD community since 2003)