Count Number of Blocks which block name contains a string of text

Count Number of Blocks which block name contains a string of text

Anonymous
Not applicable
1,195 Views
10 Replies
Message 1 of 11

Count Number of Blocks which block name contains a string of text

Anonymous
Not applicable

Hello Experts,

 

I got this lisp from @pbejse  that count the number of blocks inside a polygon.

Here is the link for that thread.

 

Now I need this modified to count all blocks that contains "1" instead of just B1 and all blocks that contains "2" instead of "B2".
The goal here is to also count block "A1" "C1" and "D1" etc., then "A2" "C2" and "D2" etc.
I need it to look for the partial match instead of the equality of the block names.
Here is the code:

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

 Thank you!

0 Likes
Accepted solutions (1)
1,196 Views
10 Replies
Replies (10)
Message 2 of 11

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

Now I need this modified to count all blocks that contains "1" instead of just B1 and all blocks that contains "2" instead of "B2".
The goal here is to also count block "A1" "C1" and "D1" etc., then "A2" "C2" and "D2" etc.
I need it to look for the partial match instead of the equality of the block names.

 Here you go

 

(defun c:demo (/ col ss blk strs n el bn tgnme pts bthree)
;;;	pBe 2021 Feb	;;;
  (Setq	lst  '(("1" "B1C")
	   ("2" "B2C")
	   ("B3" "B3C")
	  )
  )
  (if
    (and
      (setq coll nil bthree 0
	    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 . "@#"))))
	    (repeat (setq n (sslength strs))
	      (setq e1 (ssname strs (setq n (1- n)))
		    bn (getpropertyvalue e1 "BlockTableRecord/Name")
	      )
	      (setq coll
		      (cond
			((eq  bn "B3")
			 	(setq bthree (1+  bthree)) coll	)
			((setq f (assoc (substr bn 2 1) coll))
			 	(subst (list (car f) (1+ (cadr f))) f coll)
			 )
			((cons (list (substr bn 2 1) 1) coll)
			 )
			)
		    )
	      )
	   )
	 )
	(and
	       (foreach	itm (cons (list "B3" bthree) coll )
		 (setq tgnme (assoc (Car itm) lst))
		 (setpropertyvalue
		   (ssname blk 0)
		   (cadr tgnme)
		   (itoa (Cadr itm))
		 )
		 itm
	       )
       		(alert "Table is Updated")
		   )
	     	)    
	   )
  (princ)
)

HTH

EDIT: Now it also updates "B3" value

0 Likes
Message 3 of 11

Anonymous
Not applicable

Wow, Amazingly perfect! @pbejse 
How can I make it work if I want to count is the block which contain "B" in it's name?

0 Likes
Message 4 of 11

pbejse
Mentor
Mentor

@Anonymous wrote:

Wow, Amazingly perfect! @pbejse 
How can I make it work if I want to count is the block which contain "B" in it's name?


 

You are welcome @Anonymous , You may have to give a more detailed explanation of what 'contain "B" in its name. I I have a feeling its more than what i just think it is .

 

 

0 Likes
Message 5 of 11

Anonymous
Not applicable

@pbejse  what if i want to count blocks with "B" in it's name and put the count in "B1C"

  (Setq	lst  '(("B" "B1C")
	   ("2" "B2C")
	   ("B3" "B3C")
	  )
  )

It does not work correctly when I changed "1" to "B".

0 Likes
Message 6 of 11

pbejse
Mentor
Mentor

@Anonymous wrote:

@pbejse  what if i want to count blocks with "B" in it's name and put the count in "B1C"

  (Setq	lst  '(("B" "B1C")
	   ("2" "B2C")
	   ("B3" "B3C")
	  )
  )

It does not work correctly when I changed "1" to "B".


Of course not. the code is specific to your previous request. Tell me ALL that i need to know to make a generic code to work on varioius conditions.

 

 

 

0 Likes
Message 7 of 11

Anonymous
Not applicable

How can we make it still count if the filter is the letters instead of the numbers.
 Count all blocks that contain A in its name it will count blocks named A1, A2, A3

 Count all blocks that contain B in its name it will count blocks named B1, B2, B3

 Count all blocks that contain C in its name it will count blocks named C1, C2, C3

Thank you for taking the time looking into this.

0 Likes
Message 8 of 11

Sea-Haven
Mentor
Mentor

Maybe need start character A end Character D, rather than code abcd etc I can just see more or less characters wanted usual problem after solution provided.

 

Maybe getting a step ahead but what about "3A" is that a block name too or only "A" as 1st character ?

 

I have A1=23 A2=12 B3=24 etc but reports individual names not common name answer goes in a table. Google Bcount.

 

Like RLX a library dcl for input. Maybe add "-" so pick 2.

 

screenshot337.png

0 Likes
Message 9 of 11

pbejse
Mentor
Mentor

@Anonymous wrote:

How can we make it still count if the filter is the letters instead of the numbers.
 Count all blocks that contain A in its name it will count blocks named A1, A2, A3

 .. B1, B2, B3

 .. C1, C2, C3

Thank you for taking the time looking into this.


 

If user choose "A" the third count is A3? and no longer B3? same goes for C? What about the previous request for numbers, will it remain as B3 for the third count? The table always shows "B3 Count".

 

 

0 Likes
Message 10 of 11

pbejse
Mentor
Mentor

@pbejse wrote:

If user choose "A" the third count is A3? and no longer B3? same goes for C? What about the previous request for numbers, will it remain as B3 for the third count? The table always shows "B3 Count".


 

Legend has it OP is still figuring out other options to add.. 

 

0 Likes
Message 11 of 11

Sea-Haven
Mentor
Mentor

Agree what are the rules, 1st character, any character, last character, range etc 

0 Likes