Count Number of Block Inside a Polygon then Update the Table Using LISP

Count Number of Block Inside a Polygon then Update the Table Using LISP

Anonymous
Not applicable
1,294 Views
6 Replies
Message 1 of 7

Count Number of Block Inside a Polygon then Update the Table Using LISP

Anonymous
Not applicable

Hello, Experts!

 

Do you have a lisp that can count the number of blocks listed inside a polygon then updates the table with the correct count?

 

I attached the dwg file for testing.

 

LISP should work like this:

User: Run the command.

User: Select Polygon.

Program: Count the number of B1 & B2 inside the Polygon but disregard B3.

Program: Pop up message "Table is Updated"

 

Regards,

 

 

0 Likes
Accepted solutions (1)
1,295 Views
6 Replies
Replies (6)
Message 2 of 7

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Hello, Experts!

 

Do you have a lisp that can count the number of blocks listed inside a polygon then updates the table with the correct count?

 

I attached the dwg file for testing.

 

LISP should work like this:

User: Run the command.

User: Select Polygon.

Program: Count the number of B1 & B2 inside the Polygon but disregard B3.

Program: Pop up message "Table is Updated"

 

Regards,


 

(defun c:demo (/ col ss blk strs n el bn tgnme pts)
  (Setq	lst
	 '(("B1" "B1C")
	   ("B2" "B2C")
	  )
  )
  (if
    (and
      (setq coll nil
	    ss	 (ssget '((0 . "LWPOLYLINE")))
      )
      (setq blk (ssget "_X" '((0 . "INSERT") (2 . "BTable"))))
    )
     (progn
       (repeat (setq i (sslength ss))
	 (setq e   (ssname ss (setq i (1- i)))
	       pts (mapcar 'cdr
			   (vl-remove-if-not
			     '(lambda (x) (= (car x) 10))
			     (entget e)
			   )
		   )
	 )
	 (if
	   (setq strs (ssget "_CP" pts '((0 . "INSERT") (2 . "B1,B2"))))
	    (repeat (setq n (sslength strs))
	      (setq e1 (ssname strs (setq n (1- n)))
		    bn (getpropertyvalue e1 "BlockTableRecord/Name")
	      )
	      (if (Setq f (assoc bn coll))
		(setq coll (subst (list bn (1+ (cadr f))) f coll))
		(setq coll (cons (list bn 0) coll))
		      )
		    )
		 )
	       )
	(and
	       (foreach	itm coll
		 (setq tgnme (assoc (Car itm) lst))
		 (setpropertyvalue
		   (ssname blk 0)
		   (cadr tgnme)
		   (itoa (Cadr itm))
		 )
		 itm
	       )
       		(alert "Table is Updated")
		   )
	     	)    
	   )
  (princ)
)

 

HTH

 

0 Likes
Message 3 of 7

Anonymous
Not applicable

@pbejse 

 

Almost there the only problem is the count is always short by 1 unit

0 Likes
Message 4 of 7

pbejse
Mentor
Mentor

@Anonymous wrote:

@pbejse 

 

Almost there the only problem is the count is always short by 1 unit


oops, my bad..

change this..

(list bn 0) 

to

(list bn 1) 

HTH

 

 

Message 5 of 7

Anonymous
Not applicable

@pbejse 

Exactly what i need!

Thank you!

0 Likes
Message 6 of 7

pbejse
Mentor
Mentor

@Anonymous wrote:

@pbejse 

Exactly what i need!

Thank you!


You are welcome, glad it works for you

 

Cheers

 

Message 7 of 7

Anonymous
Not applicable

very good and simple tool

0 Likes