Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 11 (616 Views)
Accepted Solution

Delete specific Block

616 Views, 10 Replies
02-13-2013 08:28 PM

Hi,

 

I need to delet a specific block from current opened drawing by LISP command.

 

for example removing XREF attached is very easy:

(COMMAND "-XREF" "D" "FRAME")

 

I can remove "FRAME" XREF from drawing.

Now, I want to delete a/all block with "TB12" name with same action!

(command "BlockDelete" "TB12")

 

so, how can I delete a/all block with specific name in current drawing?

 

Thanks,

 

*Expert Elite*
pbejse
Posts: 2,437
Registered: ‎11-24-2009
Message 2 of 11 (604 Views)

Re: Delete specific Block

02-13-2013 11:25 PM in reply to: aqdam1978

aqdam1978 wrote:

 

(command "BlockDelete" "TB12")

 

so, how can I delete a/all block with specific name in current drawing?

 

Thanks,

 


Is that a new autocad command on newer vestions? or are you wanting a code to behave the way a "BlockDelete" command if it does exists?

 

 

*Expert Elite*
hmsilva
Posts: 2,668
Registered: ‎12-17-2004
Message 3 of 11 (591 Views)

Re: Delete specific Block

02-14-2013 01:56 AM in reply to: aqdam1978

aqdam1978 wrote:
...
Now, I want to delete a/all block with "TB12" name with same action!
...

perhaps something like

 

;; usage (delblk "blkname1,blkname2,blkname3,and_so_on")

(defun delblk (blkname / ss)
(if (setq ss (ssget "x" (list (cons 0 "INSERT") (cons 2 blkname))))
(progn
(command "-layer" "_U" "*" "")
(command "erase" ss "")
(command "layerp")
(command ".purge" "b" blkname "n")
);; progn
);; if
(princ)
)

 


change the name to "BlockDelete" if you want


hope that helps

Henrique

*Expert Elite*
pbejse
Posts: 2,437
Registered: ‎11-24-2009
Message 4 of 11 (582 Views)

Re: Delete specific Block

02-14-2013 02:37 AM in reply to: hmsilva
(defun DelBlk  (lst / aDoc n)
  (setq n 0 aDoc (vla-get-activedocument (vlax-get-acad-object)))
  (vlax-for layout  (vla-get-layouts aDoc)
    (vlax-for i  (vla-get-block layout)
      (if (and
            (eq (vla-get-ObjectName i) "AcDbBlockReference")
            (member (strcase (vla-get-effectivename  i))(mapcar 'strcase lst))
            (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-invoke (list i 'Delete)))))
            	(setq n (1+ n))
            )
      )
    )
  (repeat 4
  (vla-purgeall aDoc)
 )
  (princ (strcat "\n" (itoa n)  " Objects deleted"))
  (princ)
  )

 

HTH

 

EDIT:  USAGE

 (Delblk '("block1" "block2"))

 

*Expert Elite*
hmsilva
Posts: 2,668
Registered: ‎12-17-2004
Message 5 of 11 (569 Views)

Re: Delete specific Block

02-14-2013 02:56 AM in reply to: pbejse

pbejse,
nice code, works in all layouts too, mine don't... 
And if the blocks are in locked layers?

 

Cheers
Henrique

*Expert Elite*
pbejse
Posts: 2,437
Registered: ‎11-24-2009
Message 6 of 11 (563 Views)

Re: Delete specific Block

02-14-2013 03:28 AM in reply to: hmsilva

hmsilva wrote:

pbejse,
nice code, works in all layouts too, mine don't... 



Thats always a problem when using the command alternative. it can only process the current tab. sometimes even entdel is acting up during iteration. as you well know entdel will also "restore" so i ended up with VL.

 


hmsilva wrote:

And if the blocks are in locked layers? 

Cheers
Henrique



You got me there Henrique :smileyhappy: Good point.

 

If you notice I  use vl-catch-error-p instead of unlocking layers at the get-go and restoring at the end. I had a discussion before wether to unlock a layer during runtime. The prevalent answer is "the layers are locked for a reason". From then on, i opted not to. if there's any "unlocking" to be done, i left that decision to the user. 

 

Cheers my friend

 

*Expert Elite*
hmsilva
Posts: 2,668
Registered: ‎12-17-2004
Message 7 of 11 (559 Views)

Re: Delete specific Block

02-14-2013 03:37 AM in reply to: pbejse

pbejse wrote:
...
I had a discussion before wether to unlock a layer during runtime. The prevalent answer is "the layers are locked for a reason".
...

I think that is a very valid opinion, and possibly, the correct way of looking at the situation of being in a locked layer.

 

Cheers

*Expert Elite*
Kent1Cooper
Posts: 5,272
Registered: ‎09-13-2004
Message 8 of 11 (544 Views)

Re: Delete specific Block

02-14-2013 04:44 AM in reply to: aqdam1978

aqdam1978 wrote:

.... how can I delete a/all block with specific name in current drawing?

....


Searching the Discussion Group is always a good start.  This has come up before, for instance.

Kent Cooper
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 9 of 11 (513 Views)

Re: Delete specific Block

02-15-2013 07:23 AM in reply to: pbejse

Thanks,

 

As usual, your code is the best!

 

Thanks,

Abbas

 

*Expert Elite*
pbejse
Posts: 2,437
Registered: ‎11-24-2009
Message 10 of 11 (492 Views)

Re: Delete specific Block

02-16-2013 03:10 AM in reply to: aqdam1978

aqdam1978 wrote:

Thanks,

 

As usual, your code is the best!

 

Abbas

 


 

You are welcome . Thank you for your kind words :smileyhappy: 

 

The nice thing about the routine is you can modfiy  that for other entity types. by adding conditions and additional arguments.

 

Glad i could help

 

 

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community