Jump to content

Lisp for filtering certain blocks and delete them


MarcoW

Recommended Posts

I try it and it's work

 

@+

 

Hi Patrick,

 

Doesn't work for me.

Can you check attached CAD file.

 

Command: delblk

Specify Block Name : rev

Drawing Contains 36 Blocks with Name: rev

Delete [A]ll or election? :

36 objects deleted.; error: Automation Error. Invalid entity name

 

In fact, it deletes nothing.

REV_54 Blocks_DELBLK.dwg

Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    15

  • Patrick_35

    7

  • Bobzy20

    7

  • vernonlee

    7

I get the same error Patrick.

 

6 objects deleted.; error: Automation Error. Invalid entity name

 

The iterative process works fine :huh:

Link to comment
Share on other sites

Ah yes, you're right.

This is the first time that I have a problem with vla-erase.

there must be something with the block

Never mind, another solution

 

Replace

(vla-erase sel)

by

(vlax-map-collection sel 'vla-delete)

@+

Link to comment
Share on other sites

Vow, Finally this routine works perfectly, as required.

Tnx Patrick, you rocks.

 

& I must Admit that all of you guys here are doing a greeeeat job.

Impressed a lot from day one...

 

Cheers & keep it up.

 

-Ashish

Link to comment
Share on other sites

  • 2 years later...

I am first time poster and I know this is an old post. I apologize for necroing it, but I have been looking everywhere for a lisp that deletes a specified block, that I can use in a batch.

 

This one is perfect, but... the problem I am having is that if the drawing does not contain the block it continues to ask for a block name and does not end the lisp and continue the batch.

 

It grabs the next line in the batch and so on a so forth until the batch ends stuck in that one drawing.

 

Example

** Block Not Found in Drawing **

Specify Block Name : sp20a

 

** Block Not Found in Drawing **

Specify Block Name : all

 

** Block Not Found in Drawing **

Specify Block Name : (load "dpl")

 

** Invalid Block Name **

Specify Block Name : dpl

 

** Block Not Found in Drawing **

Specify Block Name : -VIEW

 

etc...etc...

 

I feel kinda guilty never contributing to this forum and then asking for a favor. So I understand if Iv aggravated someone by asking for help. :(

 

Anywho, best regards and thanks in advance for any help.

 

Mary K

Link to comment
Share on other sites

Hi Mary,

 

The following is quickly written, but should be suitable for use in a Script:

 

[color=green];; Delete Block  -  Lee Mac
;; Deletes all references of a block from a drawing, including nested references (nested to any level).
;; Proceeds to delete the block definition from the drawing, if possible.
;; Arguments:
;; block  -  block to be deleted (not case-sensitive)[/color]

([color=BLUE]defun[/color] DeleteBlock ( block [color=BLUE]/[/color] doc lst )
   ([color=BLUE]setq[/color] block ([color=BLUE]strcase[/color] block))
   
   ([color=BLUE]if[/color] ([color=BLUE]tblsearch[/color] [color=MAROON]"BLOCK"[/color] block)
       ([color=BLUE]progn[/color]
           ([color=BLUE]setq[/color] doc ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color])))

           ([color=BLUE]vlax-for[/color] lay ([color=BLUE]vla-get-layers[/color] doc)
               ([color=BLUE]if[/color] ([color=BLUE]eq[/color] [color=BLUE]:vlax-true[/color] ([color=BLUE]vla-get-lock[/color] lay))
                   ([color=BLUE]progn[/color]
                       ([color=BLUE]setq[/color] lst ([color=BLUE]cons[/color] lay lst))
                       ([color=BLUE]vla-put-lock[/color] lay [color=BLUE]:vlax-false[/color])
                   )
               )
           )
           ([color=BLUE]vlax-for[/color] def ([color=BLUE]vla-get-blocks[/color] doc)
               ([color=BLUE]vlax-for[/color] obj def
                   ([color=BLUE]if[/color]
                       ([color=BLUE]and[/color]
                           ([color=BLUE]eq[/color] [color=MAROON]"AcDbBlockReference"[/color] ([color=BLUE]vla-get-objectname[/color] obj))
                           ([color=BLUE]or[/color]
                               ([color=BLUE]and[/color]
                                   ([color=BLUE]vlax-property-available-p[/color] obj 'effectivename)
                                   ([color=BLUE]eq[/color] block ([color=BLUE]strcase[/color] ([color=BLUE]vla-get-effectivename[/color] obj)))
                               )
                               ([color=BLUE]eq[/color] block ([color=BLUE]strcase[/color] ([color=BLUE]vla-get-name[/color] obj)))
                           )
                       )
                       ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-delete[/color] ([color=BLUE]list[/color] obj))
                   )
               )
           )
           ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-delete[/color] ([color=BLUE]list[/color] ([color=BLUE]vla-item[/color] ([color=BLUE]vla-get-blocks[/color] doc) block)))
           
           ([color=BLUE]foreach[/color] lay lst
               ([color=BLUE]vla-put-lock[/color] lay [color=BLUE]:vlax-true[/color])
           )
       )
   )
   ([color=BLUE]princ[/color])
)
([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])

Call it like this:

 

(DeleteBlock [color=darkred]"Your Block"[/color])

Where "Your Block" is the name of the block to be deleted (not case-sensitive).

 

It will delete all references of the block in all layouts of the drawing, including references on locked layers, and also including all nested references, nested to any level. Finally it will delete the block definition from the drawing.

 

If you need help using it, just ask.

 

Lee

Link to comment
Share on other sites

  • 1 year later...

Is there any way to modify the code so it will search for all block names without an exact match, for example.

 

Block Names:

G_Walls_Board$0

G_Walls_Board$1

G_Walls_Board$2

G_Walls_Board$3

G_Walls_Board$4

 

So it would find all 5 blocks above containing the word: G_Walls_Board

Edited by Bobzy20
Link to comment
Share on other sites

Is there any way to modify the code so it will search for all block names without an exact match, for example.

 

Block Names:

G_Walls_Board$0

G_Walls_Board$1

G_Walls_Board$2

G_Walls_Board$3

G_Walls_Board$4

 

So it would find all 5 blocks above containing the word: G_Walls_Board

 

Try the following:

[color=GREEN];; Delete Block  -  Lee Mac[/color]
[color=GREEN];; Deletes all references of a block from a drawing, including nested references (nested to any level).[/color]
[color=GREEN];; Proceeds to delete the block definition from the drawing, if possible.[/color]
[color=GREEN];; Arguments:[/color]
[color=GREEN];; block  -  block to be deleted (not case-sensitive)[/color]

([color=BLUE]defun[/color] deleteblock ( blk [color=BLUE]/[/color] doc lst )
   ([color=BLUE]setq[/color] blk ([color=BLUE]strcase[/color] blk))
   ([color=BLUE]vlax-for[/color] lay ([color=BLUE]vla-get-layers[/color] ([color=BLUE]setq[/color] doc ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color]))))
       ([color=BLUE]if[/color] ([color=BLUE]eq[/color] [color=BLUE]:vlax-true[/color] ([color=BLUE]vla-get-lock[/color] lay))
           ([color=BLUE]progn[/color]
               ([color=BLUE]setq[/color] lst ([color=BLUE]cons[/color] lay lst))
               ([color=BLUE]vla-put-lock[/color] lay [color=BLUE]:vlax-false[/color])
           )
       )
   )
   ([color=BLUE]vlax-for[/color] def ([color=BLUE]vla-get-blocks[/color] doc)
       ([color=BLUE]vlax-for[/color] obj def
           ([color=BLUE]if[/color]
               ([color=BLUE]and[/color] ([color=BLUE]=[/color] [color=MAROON]"AcDbBlockReference"[/color] ([color=BLUE]vla-get-objectname[/color] obj))
                   ([color=BLUE]or[/color]
                       ([color=BLUE]and[/color]
                           ([color=BLUE]vlax-property-available-p[/color] obj 'effectivename)
                           ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]vla-get-effectivename[/color] obj)) blk)
                       )
                       ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]vla-get-name[/color] obj)) blk)
                   )
               )
               ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-delete[/color] ([color=BLUE]list[/color] obj))
           )
       )
   )
   ([color=BLUE]vlax-for[/color] def ([color=BLUE]vla-get-blocks[/color] doc)
       ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]vla-get-name[/color] def) blk)
           ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-delete[/color] ([color=BLUE]list[/color] def))
       )
   )
   ([color=BLUE]foreach[/color] lay lst ([color=BLUE]vla-put-lock[/color] lay [color=BLUE]:vlax-true[/color]))
   ([color=BLUE]princ[/color])
)
([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])

Call with:

(deleteblock "G_Walls_Board$#")

Anybody? Really need this for work tomorrow.

 

Lee Daddy Mac?

 

Please understand that repeatedly pestering us on the forum and via direct emails will not get your question answered any sooner, but will usually have the opposite effect.

 

Remember that our time contributing here is voluntary, we receive no remuneration for the time and effort that we donate.

Link to comment
Share on other sites

Strange, it works fine on a test drawing I created to run the test but not on the one I wanted it for!

 

Still the drawing I wanted to use it on was drawn in microGDS and exported as a DWG, this is why all the blocks are numbered $1, $2, $3 and so on. They are the same block used multiple times but when you export it for some reason it numbers them individually.

 

Anyway thanks for your help Lee.

Edited by Bobzy20
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...