LISP Request - Block Count Inside a Closed Polyline

LISP Request - Block Count Inside a Closed Polyline

Anonymous
Not applicable
16,203 Views
81 Replies
Message 1 of 82

LISP Request - Block Count Inside a Closed Polyline

Anonymous
Not applicable

Hi CAD Friends,

 

Wonder if there is a LISP routine (already created by someone) to count all the blocks inside a closed polyline.

 

These are typically plant blocks with attributes in it. I have these sheet layout boundaries in model space, and the City now wants the count of all the plant blocks in a particular sheet, and a totals count in the end. I was using BCOUNT, but then I have to select the blocks individually along the sheet boundaries carefully. Instead of that If I can select the polylines of these sheet boundaries, then the lisp should count and provide a total count of different types of blocks contained (all the blocks with the centre point inside that selected polyline) inside the closed polyline. 

 

If some one can modify the BCOUNT lisp adding an additional feature to count blocks inside the user selected closed polyline ( get input from user selecting the closed polyline), that would be awesome.

 

Regards,

 

VA

0 Likes
16,204 Views
81 Replies
Replies (81)
Message 81 of 82

khirunath
Contributor
Contributor

This is really a great LISP. I was searching for a similar code, where I need a blocks counts for a specified layer within closed polyline.

0 Likes
Message 82 of 82

J_Spurgeon
Enthusiast
Enthusiast

@DannyNL wrote:

Ok, new code.

 

Problem was caused when PDMODE was set to 1 as that makes POINT's invisible and in that case they cannot be selected.

Removed the COMMAND for creating the POINT and changed that to ENTMAKE to speed it up a bit more. And also added a progress bar during the count as it will take some time to count the blocks on your test drawing. Now you know it is still busy.

 

(defun c:Test (/ T_OldPdmode T_Selection T_Entity T_Precision T_Position T_PointList T_Count T_BoundaryCheck T_BlockName T_BlockList)
   (setq T_OldPdmode (getvar "PDMODE"))
   (setvar "PDMODE" 0)      
   (if
      (and
         (princ "\nSelect polyline: ")
         (setq T_Selection (ssget ":S+." '((0 . "*POLYLINE"))))
         (setq T_Object (vlax-ename->vla-object (ssname T_Selection 0)))
         (vlax-Curve-isClosed T_Object)
         (vlax-Curve-isPlanar T_Object)
      )
      (progn
         (setq T_Precision 0.1)
         (setq T_Position  0.0)
         (while
            (<= T_Position (vlax-Curve-GetEndParam T_Object))
            (setq T_PointList (append T_PointList (list (vlax-Curve-GetPointAtParam T_Object T_Position))))
            (setq T_Position (+ T_Position T_Precision))
         )
         (if
            (and
               T_PointList
               (setq T_Selection (ssget "_CP" T_PointList '((0 . "INSERT"))))
            )
            (progn
               (acet-ui-progress "Counting" (sslength T_Selection))
               (setq T_Count 0)
               (foreach T_Entity (vl-remove-if '(lambda (T_Item) (listp (cadr T_Item))) (ssnamex T_Selection))
                  (acet-ui-progress (setq T_Count (1+ T_Count)))
                  (setq T_Entity (cadr T_Entity))
                  (entmake (list '(0 . "POINT") (assoc 10 (entget T_Entity))))
                  (if
                     (and
                        (setq T_BoundaryCheck (ssget "_WP" T_PointList '((0 . "POINT"))))
                        (ssmemb (entlast) T_BoundaryCheck)
                     )
                     (progn
                        (if
                           (not (assoc (setq T_BlockName (cdr (assoc 2 (entget T_Entity)))) T_BlockList))
                           (setq T_BlockList (append T_BlockList (list (list T_BlockName 1))))
                           (setq T_BlockList (subst  (list T_BlockName (1+ (cadr (assoc T_BlockName T_BlockList)))) (assoc T_BlockName T_BlockList) T_BlockList))
                        )
                     )
                     (ssdel T_Entity T_Selection)
                  )
                  (entdel (entlast))
               )
               (acet-ui-progress)
               (princ (strcat "\n ** Total number of blocks found: " (itoa (sslength T_Selection)) "\n"))
               (foreach T_Item (vl-sort T_BlockList '(lambda (T_Block1 T_Block2) (< (car T_Block1) (car T_Block2))))
                  (princ (strcat "\n" (car T_Item) ": " (itoa (cadr T_Item))))
               )
               (princ "\n")
            )
         )
      )
   )
   (setvar "PDMODE" T_OldPdmode)
   (princ)
)

This is perfect for what I am trying to build, but is there a way for me to store the count of each block type as a variable to use later?

0 Likes