Jump to content

counting blocks


tricon77

Recommended Posts

I am looking for a script / function to count the number of blocks with the following criteria as input:

 

block effective name

pattern match for the attribute

attribute name

in the current 'ctab

 

the output can be an integer.

 

Thank you for your input.

Link to comment
Share on other sites

29 minutes ago, tricon77 said:

I am looking for a script / function to count the number of blocks with the following criteria as input:

 

block effective name

pattern match for the attribute

attribute name

in the current 'ctab

 

the output can be an integer.

 

Thank you for your input.

@tricon77, please upload your sample.dwg

Link to comment
Share on other sites

You can work backwards select an attribute via NENTSEL this returns attribute name then in turn can reselect the block using attribute point, then select all in current space. Maybe something like this.

 

; https://www.cadtutor.net/forum/topic/86912-counting-blocks/
; crude block search in a layout
; By AlanH June 2024

(defun c:blkmatch ( / ent pt entg str tagstr ss bname obj atts att)
(setq ent (nentsel "\nPick attribute to look at with correct value "))
(setq pt (cadr ent))
(setq entg (entget (car ent)))
(setq str (cdr (assoc 1 entg)))
(setq tagstr (cdr (assoc 2 entg)))

(setq ss (ssget pt '((0 . "INSERT"))))
(setq bname (cdr (assoc 2 (entget (ssname ss 0)))))

(setq ss (ssget "X" (list (cons 0 "INSERT")(cons 2 bname)(cons 410 (getvar 'ctab)))))

(setq cnt 0)

(repeat (setq x (sslength ss))
  (setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
  (setq atts (vlax-invoke obj 'Getattributes))
  (foreach att atts
    (if (and (= (vlax-get att 'tagstring) tagstr)(= (vlax-get att 'textstring) str))
    (setq cnt (1+ cnt))
    )
  )
)
(alert (strcat "There are " (rtos cnt 2 0) " objects that match"))

(princ)
)
(c:blkmatch)

 

Link to comment
Share on other sites

(defun C:spool_labels ( / prefix cnt oldclayer oldattdia oldattreq pt)
  (setq prefix (getstring t "\nENTER PREFIX: ")
        cnt (getint "\nENTER INTEGER TO START: ") ;; want the program to figure this out
				oldclayer (getvar 'clayer)
        oldattdia (getvar 'attdia)
        oldattreq (getvar 'attreq)
  )
  ;; Turn dialog off for the attributes
  (setvar 'attdia 0)
  (setvar 'attreq 1)

  ;; Create layer for the blocks to be inserted on
	(command "-layer" "make" "Blocks-Spool Label" "color" "magenta" "" "")

  (while (setq pt (getpoint "\nPICK INSERTION POINT: ")) ;; get insertion point for block
    (command
      ".-insert"  ;; command to insert a dynamic block
      "_MDP-Label-Spool" ;; name of block to insert
      "_non"
      pt ;; insertion point for block
      "1" ;; x scale value for the block
      "0" ;; rotation value for the block
      ;; concatenate the prefix to the counter for the name
      (strcase (strcat prefix "#" (itoa cnt)))
    )
    ;; increment the counter
    (setq cnt (1+ cnt))
  )
  ;; restore change system values
	(setvar 'clayer oldclayer)
	(setvar 'attdia oldattdia)
	(setvar 'attreq oldattreq)
  (princ)
)

 

The above code is what I am working on.

 

@devitg After looking a my question again.  Notice I was very vague. Sorry about that.

@BIGAL I like that train of thought.

 

The end goal is, I want to be able to select an attribute in a block or type in to get the prefix value. Then repeat inserting blocks with new values.

 

If the prefix was typed and not found on the current tab

   start inserting block with <prefix> # 1

 

If the prefix was typed and found on the current tab

   start inserting block with <prefix> # <next number>

 

If attribute is selected

   start inserting block with <prefix in attribute> # <next number>

 

 

I think I need the logic of setting the cnt variable before entering the while loop.

 

Thank you for your time and knowledge. 

demo.dwg

  • Like 1
Link to comment
Share on other sites

Are you limited to A-Z just need to know as it is easy to look at 1st character. And get all values A=3 B=2 and so on.

Link to comment
Share on other sites

You are on the right track, but the prefix can be any character but a #.  Looks like I messed up on the last c blocks in the previous attach file because there is no # in the name.  I have attached more realistic example.

 

Thank you for your help,

Michael

demo.dwg

Link to comment
Share on other sites

That is ok I have asked also for some help elsewhere and have a response.

 

Thanks to Lee-mac for the list solution find higehst value.

Multi radio buttons.lsp(("C" 3) ("C" 2) ("C" 1) ("B" 2) ("B" 1) ("A" 3) ("A" 2) ("A" 1)) 

desired result (("C" 3)("B" 2)("A" 3) )

 

Try this matches your sample dwg # or not.

 

; https://www.cadtutor.net/forum/topic/86912-counting-blocks/
; demo.dwg
; Counts number in attribute

; thanks to Lee-mac for this defun
(defun LM:countlst ( lst / key itm mxv rtn )
    (while (setq itm (car lst))
        (setq key (car  itm)
              mxv (cadr itm)
              lst (vl-remove-if '(lambda ( x ) (if (= (car x) key) (if (< mxv (cadr x)) (setq mxv (cadr x)) t))) (cdr lst))
              rtn (cons (list key mxv) rtn)
        )
    )
    (reverse rtn)
)


(defun donextchar ( / chrx val)
(setq chrval 0)
(foreach val lst2
(setq chrx (ascii (car val)))
(if (> chrx chrval)(setq chrval chrx num 1))
)
(setq chrval (+ chrval 1))
(command "-insert" "_MDP-Label-Spool" "s" 1.0 (getpoint "\nPick point for block ") 0.0 (strcat (chr chrval) "#" (rtos num 2 0)))
(princ)
)

(defun c:nextchar ( / )

(if (not AH:butts)(load "Multi radio buttons.lsp"))
(setvar 'attreq 1)

(setq ss (ssget '((0 . "INSERT"))))
(setq lst '())
(repeat (setq x (sslength ss))
  (setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
  (if (= (vlax-get obj 'Effectivename) "_MDP-Label-Spool")
  (progn
(setq atts (vlax-invoke obj 'Getattributes))
(setq att (vlax-get (car atts) 'textstring))
(setq len (strlen att))
(setq chr1 (substr att 1 1))
(if (= (vl-string-search "#" att) nil)
(setq chr2 (atoi (substr att 2 )))
(setq chr2 (atoi (substr att 3 )))
)
(setq lst (cons (list chr1 chr2 ) lst))
)
)
)

(setq lst2 (LM:countlst lst))

(setq lst3 '())
(setq x 0)
(repeat (length lst2)
(setq lst3 (cons (strcat (car (nth x lst2)) " " (rtos (+ (cadr (nth x lst2)) 1) 2 0)) lst3))
(setq x (1+ x))
)
(setq lst3 (cons "Add next character" lst3))
(setq lst3 (cons "Please choose" lst3))

(setq ans (AH:butts 1 "V" lst3))

(if (= but 1)(donextchar))
(setq chrval (ascii (car (nth but lst2))) num (cadr (nth but lst2)))

(setq ans2 "Yes")
(while (= ans2 "Yes")
  (setq ans2 (AH:butts 1 "H" (list "Insert more yes no" "Yes" "No")))
  (if (= ans2 "Yes")
    (progn
     (setq num (1+ num))
     (command "-insert" "_MDP-Label-Spool" "s" 1.0 (getpoint "\nPick point for block ") 0.0 (strcat (chr chrval) "#" (rtos num 2 0)))
    )
	(setq ans2 "No")
  )
)

(princ)
)
(c:nextchar)

 

Save multi radio buttons to a support path or change the (load to include the full path of where the file is located.

 

Edited by BIGAL
Link to comment
Share on other sites

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...