Selecting and Deleting a Unamed block with Lisp

azmand01!e
Enthusiast

Selecting and Deleting a Unamed block with Lisp

azmand01!e
Enthusiast
Enthusiast

Hi folks,

I am trying to select a specific block and delete it with Lisp.
I can not pick the block using any selection mode. Reason being is I want to run a batch script on several files. 
For instance, I have this code which will work, however, if If place the code in a loop to delete all REVISION_SYMBOL_I_22x34 blocks in a drawing, it will actually delete all unamed blocks and not just the one I need deleted. It seems that my code just selectes any unamed block after it deletes the first one I want when I place in a loop (loop no in code below). Any help appreciated. Thanks 

 

Moderator edit: Put code in code window, use </> button.

(defun c:DeleteBlocks ()
(vl-load-com)
(setq blockName "REVISION_SYMBOL_I_22x34")
(setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," blockName)))))
(setq blkRef (ssname ss 0))
(vla-delete (vlax-ename->vla-object blkRef))
(ssdel blkRef ss)
(princ)
)
Tags 
0 Likes
Reply
Accepted solutions (2)
584 Views
7 Replies
Replies (7)

paullimapa
Mentor
Mentor

What if you replaced this line:

(strcat "`*U*," blockName)

with this:

blockName


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

azmand01!e
Enthusiast
Enthusiast

Hi,

 

That would give a malformed function error.

0 Likes

paullimapa
Mentor
Mentor

Maybe you missed something 

That line of code should now look like this

(setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2  blockName))))


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

azmand01!e
Enthusiast
Enthusiast

I had an extra parentheses..

Correct to this.. but this won't find the block or any other block.

Moderator edit: please post code to code window using </> button.

(defun c:DelRevBlk ()
(vl-load-com)
(setq blockName "REVISION_SYMBOL_I_22x34")
(setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 blockName))))
(setq blkRef (ssname ss 0))
(vla-delete (vlax-ename->vla-object blkRef))

 

So far only this version finds the block. But it finds any block that has a uname, not specifically the one I want deleted.

(setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," blockName)))))
(ssdel blkRef ss)
(princ)
)

 

0 Likes

paullimapa
Mentor
Mentor
Accepted solution

Looks like the block you’re searching for is then a dynamic block placed in the dwg. Then use this 

 

(setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 (strcat "`*U*," blockName)))))
(setq blkRef (ssname ss 0))
(setq obj (vlax-ename->vla-object blkRef))
(if (eq (vla-get-effectivename obj) blockName) (ssdel blkRef ss))

 

But this only checks the first of the selection set. You should loop through the entire set and if it finds the matching effective block name then it’ll delete the block from the set 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

azmand01!e
Enthusiast
Enthusiast
Accepted solution

I needed to change this line to:

(if (and (eq (vla-get-effectivename obj) blockName)
(entdel blkRef))

 

and then it works .

Thanks for help. 

0 Likes

paullimapa
Mentor
Mentor

Glad that worked out for you…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes