Select Dynamic Blocks By Name Without Pick First

Select Dynamic Blocks By Name Without Pick First

Anonymous
Not applicable
1,770 Views
5 Replies
Message 1 of 6

Select Dynamic Blocks By Name Without Pick First

Anonymous
Not applicable

Hi,

I'm after a method of selecting all dynamic blocks in the current space so I can delete them programmatically - obviously the name of the block will need to be added to the lisp.

I've managed to find this code from CADTutor but it requires the selection of one instance first.

Would anyone know either what can be changed in this code or maybe have an alternative that selects all versions without a pick first?

Many thanks in advance.

 

(defun c:sb    (/ e name n out ss x rjp-getblockname)
(vl-load-com)
(prompt "\n   Pick BLOCK to acquire its instances in the drawing...")
  (defun rjp-getblockname (obj)
    (if    (vlax-property-available-p obj 'effectivename)
      (vla-get-effectivename obj)
      (vla-get-name obj)
    )
  )
  (if (setq x     (ssget '((0 . "INSERT")))
        x     (ssname x 0)
        name (rjp-getblockname (vlax-ename->vla-object x))
        ss     (ssget "_X" '((0 . "INSERT")))
        n     -1
        out     (ssadd)
      )
    (while (setq e (ssname ss (setq n (1+ n))))
      (if (= (rjp-getblockname (vlax-ename->vla-object e)) name)
    (ssadd e out)
      )
    )
  )
  (sssetfirst nil out)
  (princ)
)

0 Likes
Accepted solutions (2)
1,771 Views
5 Replies
Replies (5)
Message 2 of 6

ВeekeeCZ
Consultant
Consultant
Accepted solution

Actually it's easier let the user type a name...

 

(vl-load-com)

(defun c:sb    (/ e name n out ss x rjp-getblockname)
  
  (defun rjp-getblockname (obj)
    (if    (vlax-property-available-p obj 'effectivename)
      (vla-get-effectivename obj)
      (vla-get-name obj)))

  (if (setq name   (strcase (getstring T "\nBlock name: "))
            ss     (ssget "_X" '((0 . "INSERT")))
            n     -1
            out     (ssadd))
    (while (setq e (ssname ss (setq n (1+ n))))
      (if (= (strcase (rjp-getblockname (vlax-ename->vla-object e))) name)
        (ssadd e out))))
    
  (sssetfirst nil out)
  (princ)
  )
Message 3 of 6

Anonymous
Not applicable

That works great, thanks very much - really appreciated.

0 Likes
Message 4 of 6

Anonymous
Not applicable

Sorry in advance but I'm going to be a complete nuisance now - is there a way to hard code in a specific block name for selection instead of the user inserting in a block name?

Many thanks.

0 Likes
Message 5 of 6

ВeekeeCZ
Consultant
Consultant
Accepted solution

Sure. See the examples of commands. As you can see as the last example, now you can specify more block names delimited by a comma.

 

 

(defun c:selBlock1 () 		(selblkbyname "Block1"))
(defun c:selBlock2 () 		(selblkbyname "Block2"))
(defun c:selBlock1and2 () 	(selblkbyname "Block1,Block2"))


(defun selblkbyname (name / e n out ss x rjp-getblockname)
  
  (defun rjp-getblockname (obj)
    (if    (vlax-property-available-p obj 'effectivename)
      (vla-get-effectivename obj)
      (vla-get-name obj)))

  (if (setq ss     (ssget "_X" '((0 . "INSERT")))
            n     -1
            out     (ssadd))
    (while (setq e (ssname ss (setq n (1+ n))))
      (if (wcmatch (rjp-getblockname (vlax-ename->vla-object e)) name)
        (ssadd e out))))
    
  (sssetfirst nil out)
  (princ)
)

(vl-load-com)

 

But you don't have to make hard-coded command, you can call the main function with a block name as a parameter:

 

(selblkbyname "MyBlock")

 

Have fun!

Message 6 of 6

Anonymous
Not applicable

That sounds great, just what I needed - many thanks again!

0 Likes