how to count the number of blocks in several areas.

how to count the number of blocks in several areas.

merdanjorayev_tm
Explorer Explorer
1,920 Views
11 Replies
Message 1 of 12

how to count the number of blocks in several areas.

merdanjorayev_tm
Explorer
Explorer

how to count the number of blocks in several areas.

 

below is a sample task

merdanjorayev_tm_0-1727670213599.png

 

0 Likes
1,921 Views
11 Replies
Replies (11)
Message 2 of 12

Sea-Haven
Mentor
Mentor

Simplest answer is can use a (SSGET "CP" points) the points would be the vertice of say your green plines. 

 

 

(setq ss (ssget "cp" pts '((0 . "INSERT"))))

 

What do you plan to do once you have the blocks  ? Make a table, count common blocks and so forth.

0 Likes
Message 3 of 12

paullimapa
Mentor
Mentor

Let's say you want to get a count of the Computer block in two areas.

Specify the first area on the Count Palette

Then right mouse click on the Computer item and select Insert Field

Then double click on the inserted Field and copy the expression shown onto Notepad

paullimapa_0-1727677774628.png

Repeat with the second area.

Then enter FIELD command > Objects > Formula > right mouse click within the Formula window and select from the cursor menu Insert Field

paullimapa_2-1727678267244.png

On this FIELD window > Objects > Select CountInArea paste into the Expression window the contents from the copied expression from area #1 and click Evaluate to see the count in the Preview then click OK.

paullimapa_1-1727678216321.png

Back in the original FIELD window enter the + symbol and right mouse click and select Insert Field again:

paullimapa_3-1727678427645.png

On this FIELD window > Objects > Select CountInArea paste into the Expression window the contents from the copied expression from area #2 and click Evaluate to see the count in the Preview then click OK.

paullimapa_4-1727678485696.png

Click OK to insert the count total as a FIELD into your drawing.

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 12

merdanjorayev_tm
Explorer
Explorer
thank you very much for the answer.
but the problem is that there can be many such areas, to count them separately it takes a lot of time. Areas are, say, rooms, the task is that all blocks should be counted by rooms.
0 Likes
Message 5 of 12

paullimapa
Mentor
Mentor

Well you don’t want to manually select the area defined in each room to get this kind of quantity total. Sounds more like you need to invest in AutoCAD Architecture which is mentioned in this solution 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 12

Kent1Cooper
Consultant
Consultant

Are the green outlines actual objects, presumably closed Polylines?  Are they on their own Layer?

Kent Cooper, AIA
0 Likes
Message 7 of 12

merdanjorayev_tm
Explorer
Explorer

example project

0 Likes
Message 8 of 12

Sea-Haven
Mentor
Mentor

Thanks for dwg, do you want a Table answer ? The table would have label & block names with count. 

Need table example so can match.

0 Likes
Message 9 of 12

merdanjorayev_tm
Explorer
Explorer

Example Table.

 

The table must contain
the blocks name with count by rooms(regtangle, closed polylines).
and the room name contains the MNO_MET block as attributes.

 

0 Likes
Message 10 of 12

Sea-Haven
Mentor
Mentor

Try this

 

 

; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-to-count-the-number-of-blocks-in-several-areas/td-p/13053744

; By Alanh Oct 2024

(defun c:roomblocks ( / objtable numrows lay ss x ss2 co-ord obj atts rname lst lst2 lst3 ent y blk val cnt)

(defun CreateTableStyle ( / dicts dictobj key class custobj dname dictname objtable numrows lay ss x ss2 co-ord obj atts rname lst lst2 lst3 ent y blk val cnt)
(setq dicts (vla-get-Dictionaries (vla-get-ActiveDocument(vlax-get-acad-object)))) ;; Get the Dictionaries collection and the TableStyle dictionary
(setq dictObj (vla-Item dicts "acad_tablestyle"))
(setq dname "Merdan3")
(vlax-for dictname dictobj
 (if (=  (vlax-get dictname 'name) dname)
 (princ "yes")
 (progn
   (setq custObj (vla-AddObject dictObj dname "AcDbTableStyle")) ;; Create a custom table style
   (vla-put-Name custObj dname) ;; Set the name and description for the style
   (vla-put-Description custObj (strcat dname " custom table style"))
   (vla-put-BitFlags custObj 1) ;; Sets the bit flag value for the style
   (vla-put-FlowDirection custObj acTableTopToBottom) ;; Sets the direction of the table, top to bottom or bottom to top
   (vla-put-HorzCellMargin custObj 6.0) ;; Sets the horizontal margin for the table cells
   (vla-put-VertCellMargin custObj 6.0) ;; Sets the vertical margin for the table cells
   (vla-SetAlignment custObj (+ acDataRow acHeaderRow acTitleRow) acMiddleCenter) ;; Sets the vertical margin for the table cells
   (vla-SetTextHeight custObj acDataRow 12) ;; Set the text height for the Title, Header and Data rows
   (vla-SetTextHeight custObj acHeaderRow 12)
   (vla-SetTextHeight custObj acTitleRow 12)
   (vla-SetTextStyle custObj (+ acDataRow acHeaderRow acTitleRow) "Standard") ;; Set the text height and style for the Title row
 )
 )
)

(setvar 'ctablestyle dname)

(princ)
)

; By Gile
(defun my-count (a L)
  (cond
   ((null L) 0)
   ((equal a (car L)) (+ 1 (my-count a (cdr L))))
   (t (my-count a (cdr L))))
)
;**********************************************************
; By Gile
(defun remove_doubles (lst)
  (if lst
    (cons (car lst) (remove_doubles (vl-remove (car lst) lst)))
  )
)

(createtablestyle)

(command "table" 3 3 (getpoint "\nPick point for table "))
(setq objtable (vlax-ename->vla-object (entlast)))
(vla-Setcolumnwidth objtable 0 95 )
(vla-Setcolumnwidth objtable 1 750)
(vla-Setcolumnwidth objtable 2 80)
(vla-settext objtable 0 0 "ROOM DETAILS")
(vla-settext objtable 1 0 "ROOM")
(vla-settext objtable 1 1 "BLOCK NAME")
(vla-settext objtable 1 2 "COUNT")
(vla-setrowheight objtable 0 50)
(vla-setrowheight objtable 1 50)
(vla-setrowheight objtable 2 28)

(vla-put-regeneratetablesuppressed objtable :vlax-true)

(setq numrows 2)

(setq lay (cdr (assoc 8 (entget (car (entsel "\nPlease select bounding pline "))))))
(setq ss (ssget (list (cons 0 "*POLYLINE")(cons 8 lay)(cons 410 (getvar 'ctab)))))

(repeat (setq x (sslength ss))
(setq ent (ssname ss (setq x (1- x))))
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget ent))))

(setq ss2 (ssget "CP" co-ord '((0 . "INSERT")(2 . "MNO_MET"))))
(setq obj (vlax-ename->vla-object (ssname ss2 0)))
(setq atts (vlax-invoke obj 'Getattributes))
(setq rname (vlax-get (car atts) 'Textstring))

(setq lst '() lst2 '() lst3 '())

(setq ss2 (ssget "CP" co-ord (list (cons 0 "INSERT")(cons -4  "<NOT") (cons 2 "MNO_MET") (cons -4 "NOT>"))))
  
  (repeat (setq y (sslength ss2))
   (setq blk (ssname ss2 (setq y (1- y))))
  (setq lst (cons (list rname (cdr (assoc 2 (entget blk))) ) lst))
  )
  (setq lst (vl-sort lst '(lambda (x y) (< (car x)(car y)))))
  
  (setq lst2 (remove_doubles lst))
  
  (foreach val lst2
   (setq cnt (my-count val lst))
   (setq lst3 (cons (list  (nth 0 val)(nth 1 val) cnt) lst3))
  )

  (foreach val lst3
   (vla-Settext Objtable  numrows 0 (nth 0 val))
   (vla-Settext Objtable  numrows 1 (nth 1 val))
   (vla-Settext Objtable  numrows 2 (nth 2 val))
   (setq numrows (1+ numrows))
   (vla-InsertRows Objtable numrows 28 1)
  )

)

(vla-put-regeneratetablesuppressed objtable :vlax-false)
(princ)
)

(c:roomblocks)

 

Message 11 of 12

merdanjorayev_tm
Explorer
Explorer

I'm glad you're helping.

 

after creating a table with the given parameters,
after selecting the bounding line,
asks to select objects but does not respond to this.

 

merdanjorayev_tm_0-1727781897570.png

 

0 Likes
Message 12 of 12

Sea-Haven
Mentor
Mentor

Sorry some where along the way 2 lines of code ended up in wrong sequence, I have redone the code above. Please try again.

 

0 Likes