Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete specific Block

24 REPLIES 24
SOLVED
Reply
Message 1 of 25
aqdam1978
9448 Views, 24 Replies

Delete specific Block

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,

 

24 REPLIES 24
Message 2 of 25
pbejse
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?

 

 

Message 3 of 25
hmsilva
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

EESignature

Message 4 of 25
pbejse
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"))

 

Message 5 of 25
hmsilva
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

EESignature

Message 6 of 25
pbejse
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 🙂 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

 

Message 7 of 25
hmsilva
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

EESignature

Message 8 of 25
Kent1Cooper
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, AIA
Message 9 of 25
aqdam1978
in reply to: pbejse

Thanks,

 

As usual, your code is the best!

 

Thanks,

Abbas

 

Message 10 of 25
pbejse
in reply to: aqdam1978


@aqdam1978 wrote:

Thanks,

 

As usual, your code is the best!

 

Abbas

 


 

You are welcome . Thank you for your kind words Smiley Happy 

 

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

 

 

 

Message 11 of 25
southie
in reply to: pbejse

pbejse

 

Lots of nice things for us to learn and experiment with in this definrd function.   Thanks!

 

v/r

Southie

Message 12 of 25
sleepy86
in reply to: southie

I hope it will be helpful. Thanks so much
Message 13 of 25
sleepy86
in reply to: sleepy86

We have to wait for "blockdelete" command from AUTOCAD 🙂
Message 14 of 25
DavidMWard
in reply to: pbejse

I am trying do something similar here.  I have some standard styles (dims, multi-leaders, etc) that I want put into each drawing.  I have the lisp to do that by inserting a dws as a block and had the code erase last as soon as it inserted it, then had the code purge the block.  it worked great until one day it didn't.  the last thing in the drawing was being erased everytime I opened the drawing, I could see things disappear.  I remmed out the lines of code erasing last and it stopped.  Now I still wanted the information in our drawings and since the block contained no visible info (just styles) it was ok.  Now the part about having layers that wouldn't purge and nothing visible was on them.  Turns out that the block with the styles was being put on the current layer everytime the button that activated some other functions also activated the insertion of the block.   So now there are often times 10, 20, 50 plus blocks on random layers.  and since the block has no visible info it can't be selected by picking (so no select similar, which would only give me the blocks on the picked layer anyway).  the only way I have found to select them is using qselect dialog and having it select all block with the given name,  then deleting them, then switch to the next layout and repeating the process.

 

bottom line I am looking for a way to remove these blocks from existing drawings and using the code to remove the block if it exists after it was placed.

 

the code in this thread was sounding very promising, and it loads fine, but when I run it I get a "error: too few arguments"  could this be a version issue.  I am using AutoCAD Architecture 2016. 

Message 15 of 25
Kent1Cooper
in reply to: DavidMWard


@DavidMWard wrote:

.... inserting a dws as a block and had the code erase last as soon as it inserted it, then had the code purge the block.  ....

.... 

the code in this thread was sounding very promising, and it loads fine, but when I run it I get a "error: too few arguments" .... 


Erase Last chooses the last thing visible on the screen.  If your drawing has nothing drawn that would serve for that, then yes, it will find whatever else is the last visible object.  You could include some drawn element in that drawing, and have it Insert it in such a way that the drawn element is guaranteed to be visible, but it's easier to use, in place of Erase Last:

(entdel (entlast))

 

which doesn't depend visibility of objects, etc.

 

For the last issue:  how exactly do you "run" it?  That is, what exactly do you type in, or have in a menu item, or whatever?  The code marked as the Solution needs to be supplied with an argument -- a list of Block names -- so if you're just giving it the function name, that would explain the message.

 

 

Kent Cooper, AIA
Message 16 of 25
DavidMWard
in reply to: Kent1Cooper

(if (findfile "filename.dws")
     (progn

          (command -s "-insert" "filename" "0,0,0" "" "" "") ;brings in the company annotative styles for dims, text and multi-leaders
          (command -s "erase" "l" "") ;erases the block filename (all styles are now imbedded in the drawing)

          (command -s "-purge" "B" "filename" "n") ;purges the block filename
     )
)

 

 

so if I understand this right, because the block has no visible entity erase won't work.  I tried entering entdel on the command line but it doesn't recognize it.  please teach me how to use entdel etc.

Message 17 of 25
Kent1Cooper
in reply to: DavidMWard


@DavidMWard wrote:

(if (findfile "filename.dws")
     (progn

          (command -s "-insert" "filename" "0,0,0" "" "" "") ;brings in the company annotative styles for dims, text and multi-leaders
          (command -s "erase" "l" "") ;erases the block filename (all styles are now imbedded in the drawing)

          (command -s "-purge" "B" "filename" "n") ;purges the block filename
     )
)

 

....  I tried entering entdel on the command line but it doesn't recognize it.  ....


Not just entdel, but with the parentheses and the argument, as in Post 15 -- it's an AutoLisp function, not a command.

 

Is that really .dws, not .dwg?

 

You should be able to cancel the Insertion part after it has brought in the definition, but before it gets an Insertion point, which will eliminate the need to either Erase or (entdel) it.

 

The (command-s) function [without the space, right?] is new and I'm not so comfortable with it yet, but I believe regular (command) will do except within an error handler, where it can't be used except after some other shenanigans.

 

More than one command can be included in one (command) function, eliminating the need for the (progn) function.

 

I suspect you should be able to do just:

 

(if (findfile "filename.dws"); or .dwg if that's what you mean

  (command

    "-insert" "filename" nil ;brings in the company annotative styles for dims, text and multi-leaders ; nil cancels Insertion
    "-purge" "B" "filename" "n" ;purges the block filename
  )
)

 

Kent Cooper, AIA
Message 18 of 25
DavidMWard
in reply to: Kent1Cooper

I believe that your code fix will work.  now it is up to me to find (again) where it is actually intitiating the command.  at one point it was when you clicked on a button/ribbon and a macro would initiate the insertion and erase last sequence.  at another point it was in the "company" lisp.  it has been some time since I initially started this.  I'll let you know once I sort it our.  Thanks again for the insight about erase last skipping objects that contain no entities.

Message 19 of 25
DavidMWard
in reply to: DavidMWard

As to why it is DWS,  that stems from my misunderstanding exactly what DWS's were used for.  I interpreted as a STANDARDs file.  Upon further investigation what It should be is a DWT perhaps.  I understand now that DWS's are used in back checking that drawing standards are being maintained.

Message 20 of 25
DavidMWard
in reply to: DavidMWard

Any insight as to why erase/delete doesn't work on objects selected by a lisp.  For example, I have the lisp select all blocks of the given name regaurdless of layer, and then have it erase them.

 

(defun c:delblk ()
(if (tblsearch "BLOCK" "blockname")
(sssetfirst nil (ssget "_x" '((0 . "INSERT")(2 . "blockname"))))
(command "_.erase")
)
(princ)
)

 

results in all the blocks in the current tab being selected, (quick properties confirms this selection) and the command to erase is ignored.  if I hit the delete button or key in erase or delete it does just that.  It doesn't matter whether I use "erase", "-erase", ".erase" (or the same thing for delete)  it simply ignores it.  I even considered having it explode the block after selection (the block has no entities in it so theoretically there would be nothing left behind, just smoke), but as with erase and delete it ignores the command.  I recall reading in one of the threads I ran across about this subject that exploding multiple blocks from a lisp is not easy if possible at all.

 

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

Post to forums  

”Boost