Block count to field and dynamic blocks

Block count to field and dynamic blocks

Anonymous
Not applicable
1,418 Views
4 Replies
Message 1 of 5

Block count to field and dynamic blocks

Anonymous
Not applicable

Hi everyone,

 

I'm using this code (not written by me) to count specific blocks in a drawing and link this count to a a field:

 

 

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

 

 

Normally, this is working nicely. However, when I use it for dynamic blocks, the modified ones won't be counted, because of *U anonymous block names. I know the way to solve this would be getting the effective name of the blocks with some code like this one by Lee Mac:

 

;; Effective Block Name  -  Lee Mac
;; obj - [vla] VLA Block Reference object

(defun LM:effectivename ( obj )
    (vlax-get-property obj
        (if (vlax-property-available-p obj 'effectivename)
            'effectivename
            'name
        )
    )
)

But I couldn't manage to put it all together. Could anyone give me a hand on this?

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

ВeekeeCZ
Consultant
Consultant

Try this... untested.

 

(defun c:recount (/ block_list cnt env_name LM:effectivename :ssfltname ss sx)
  
  (defun LM:effectivename ( obj )
    (vlax-get-property obj
      (if (vlax-property-available-p obj 'effectivename)
        'effectivename
        'name)))
  
  (defun :ssfltname (ss name / i en)
    (repeat (setq i (sslength ss))
      (if (/= name (LM:effectivename (vlax-ename->vla-object (setq en (ssname ss (setq i (1- i)))))))
        (ssdel en ss))))
  
  (if (and (setq block_list (list "1" "2" "3")
                 ss (ssget "X" (list '(0 . "INSERT")))))
    
    (foreach i block_list
      (if
        (setq sx (:ssfltname ss i))
        (progn
          (setq
            cnt (itoa (sslength sx))
            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 3 of 5

SeeMSixty7
Advisor
Advisor
Accepted solution

Try this.

 

Good Luck,

 


(defun c:recount (/ block_list cnt env_name) (setq block_list (list (list "1" 0) (list "2" 0) (list "3" 0))) ; Block List with Initial values of 0 count (if (setq ss (ssget "X" '((0 . "INSERT")))) (progn (setq cnt 0 len (sslength ss) ) (while (< cnt len) (setq blkent (ssname ss cnt) ; retrieve the insert definition based on the index position of the counter blkdata (entget blkent) ; get the DXF data of the insert entity blkname (cdr (assoc 2 blkdata)) ; get the traditional block name blkobj (vlax-ename->vla-object (cdr (assoc -1 blkdata))) ; get the entity object blktruename (vla-get-EffectiveName blkobj); get the effective block name (dynamic block store actual name here) cnt (1+ cnt); bump the counter ) (if (setq blkcounter (assoc blktruename block_list)) ; if block is in our list (setq block_list (subst (list blktruename (1+ (cadr blkcounter))) blkcounter block_list)) ) ) ) ) (foreach blk block_list ; Step Through and set env vars (setq cnt (itoa (cadr blk)) env_name (strcat "BLOCK_COUNT_" (car blk)) ) (princ (strcat "\nSet " env_name " to " cnt)) (setenv env_name cnt) ) (command "_REGEN") ;_refreshes field values (princ) )
Message 4 of 5

Anonymous
Not applicable

Thank you so much, problem solved!

Message 5 of 5

Anonymous
Not applicable

Could you please provide a bit more info ... I use the originally referenced lsp and diesel to count blocks and return in a field/table, but have the same problem when trying to use with a dynamic block that has visibility states of those original blocks, not being counted. I've tried replacing that original lsp with the one you've provided (with relevant block names subbed of course), but with no luck. I'm a copy/paster, I do not have the knowledge or skill to write my own lsp or understand the processes/code within it. So the more idiot proof you can make it the better and more useful to me, and others I'm sure.  Regardless, thank you in advance for being so helpful. 

 

0 Likes