Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Selecting and Deleting a Unamed block with Lisp

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
azmand01!e
407 Views, 7 Replies

Selecting and Deleting a Unamed block with Lisp

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 
7 REPLIES 7
Message 2 of 8
paullimapa
in reply to: azmand01!e

What if you replaced this line:

(strcat "`*U*," blockName)

with this:

blockName


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 8
azmand01!e
in reply to: paullimapa

Hi,

 

That would give a malformed function error.

Message 4 of 8
paullimapa
in reply to: azmand01!e

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
Message 5 of 8
azmand01!e
in reply to: paullimapa

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

 

Message 6 of 8
paullimapa
in reply to: azmand01!e

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
Message 7 of 8
azmand01!e
in reply to: paullimapa

I needed to change this line to:

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

 

and then it works .

Thanks for help. 

Message 8 of 8
paullimapa
in reply to: azmand01!e

Glad that worked out for you…cheers!!!


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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta