Jump to content

Recommended Posts

Posted (edited)

Greetings,

 

I am looking for a Lisp routine to count number of objects within a block, and put the result inside of an existing text like a field. I found in Autodesk forum the next lisp routine that count the entities and their respective entity type, but from all blocks of the drawing at the same time, and I need to count each block, one by one plus the result linked to a text field (just the quantity as a number, nothing else. I am not interested in the entity type of the elements).

 

Somebody could help me with this, please... thanks a lot.

 

(defun c:cnt(/ blo doc js lst n tbl)
  (vl-load-com)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vlax-for blo (vla-get-blocks doc)
    (or (wcmatch (vla-get-name blo) "`**,*|*")
    (eq (vla-get-isxref blo) :vlax-true)
      (progn
    (setq lst nil)
    (vlax-for ent blo
      (setq tbl (cons (vla-get-objectname ent) tbl))
    )
     (while tbl    
      (setq n   (length tbl)
        js  (car tbl)
        tbl (vl-remove js tbl)
        lst (cons (cons (itoa (- n (length tbl))) js) lst)
      )
    )
    (princ (strcat "\nWe have in the block : " (vla-get-name blo)))
    (foreach n (vl-sort lst '(lambda (a b) (< (cdr a) (cdr b))))
      (princ (strcat "\n"
             (substr "     " 1 (- 5 (strlen (car n))))
             (car n)
             " "
             (substr (cdr n) 5)
         )
      )
    )
      )
    )
  )
  (princ)
)

 

Edited by SLW210
Add Code Tags
Posted

Try this:

(defun c:cnt (/ r)
  (vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
    (if	(= 0 (vlax-get b 'isxref) (vlax-get b 'islayout))
      (setq r (cons (cons (vla-get-name b) (vla-get-count b)) r))
    )
  )
  (mapcar 'print (vl-sort r '(lambda (a b) (< (car a) (car b)))))
  (princ)
)
(vl-load-com)

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...