Jump to content

Delete everything on a layer (including what's inside a block)


KRBeckman

Recommended Posts

I think I've seen this before, but I can't seem to find it. I just need a lisp that will delete everything in the current open drawing that's on a particular layer, even if the entities are in a block.

 

Thanks a ton!!

Link to comment
Share on other sites

  • Replies 26
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    8

  • alanjt

    7

  • KRBeckman

    6

  • ksperopoulos

    2

Not sure if this would work?

 

(defun c:DeleteAll (/ ent layer i ss ent)
 (vl-load-com)
 
 (cond (  (setq ent (car (nentsel "\nSelect Object on Layer to Delete: ")))

          (setq layer (cdr (assoc 8 (entget ent))))

          (setq i -1 ss (ssget "_X" (list (cons 8 layer))))
          (while (setq ent (ssname ss (setq i (1+ i)))) (entdel ent))

          (vlax-for blks (vla-get-Blocks
                           (vla-get-ActiveDocument
                             (vlax-get-acad-object)))

            (vlax-for obj blks
              (if (eq (strcase layer) (strcase (vla-get-layer obj)))
                (vla-delete obj))))))
 (princ))

     

Link to comment
Share on other sites

Not sure if this would work?

 

(defun c:DeleteAll (/ ent layer i ss ent)
 (vl-load-com)

 (cond (  (setq ent (car (nentsel "\nSelect Object on Layer to Delete: ")))

          (setq layer (cdr (assoc 8 (entget ent))))

          (setq i -1 ss (ssget "_X" (list (cons 8 layer))))
          (while (setq ent (ssname ss (setq i (1+ i)))) (entdel ent))

          (vlax-for blks (vla-get-Blocks
                           (vla-get-ActiveDocument
                             (vlax-get-acad-object)))

            (vlax-for obj blks
              (if (eq (strcase layer) (strcase (vla-get-layer obj)))
                (vla-delete obj))))))
 (princ))

     

 

 

How would I change this to delete everything on layer "Blah"... I would rather not have to select the layer, cause it will be the same layer everytime, and the objects that I'm deleting are some of a blocks attributes and don't show up in the drawing.

Link to comment
Share on other sites

Perhaps:

 

(defun c:DeleteAll (/ *error* LAYER UFLAG)
 (vl-load-com)
 ;; Lee Mac  ~  23.02.10

 (defun *error* (msg)
   (and UFlag (vla-EndUndoMark *doc))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (setq layer "Blah")

 (setq *doc (cond (*doc) ((vla-get-ActiveDocument
                            (vlax-get-acad-object)))))

 (if (and (tblsearch "LAYER" layer)
          (eq :vlax-false
              (vla-get-lock
                (vla-item (vla-get-Layers *doc) layer))))
   (progn
     (setq uFlag (not (vla-StartUndoMark *doc)))
     
     (vlax-for lay (vla-get-Layouts *doc)

       (vlax-for obj (vla-get-Block lay)

         (if (eq (strcase layer)
                 (strcase (vla-get-layer obj)))
           
           (vl-catch-all-apply
             (function vla-delete) (list obj)))))

     (vlax-for blk (vla-get-Blocks *doc)

       (vlax-for obj blk

         (if (eq (strcase layer)
                 (strcase (vla-get-layer obj)))
           
           (vl-catch-all-apply
             (function vla-delete) (list obj)))))

     (setq uFlag (vla-EndUndomark *doc)))

   (princ "\n** Layer Locked or Not Found **"))
 
 (princ))


Link to comment
Share on other sites

Perhaps:

 

(defun c:DeleteAll (/ *error* LAYER UFLAG)
 (vl-load-com)
 ;; Lee Mac  ~  23.02.10

 (defun *error* (msg)
   (and UFlag (vla-EndUndoMark *doc))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (setq layer "Blah")

 (setq *doc (cond (*doc) ((vla-get-ActiveDocument
                            (vlax-get-acad-object)))))

 (if (and (tblsearch "LAYER" layer)
          (eq :vlax-false
              (vla-get-lock
                (vla-item (vla-get-Layers *doc) layer))))
   (progn
     (setq uFlag (not (vla-StartUndoMark *doc)))

     (vlax-for lay (vla-get-Layouts *doc)

       (vlax-for obj (vla-get-Block lay)

         (if (eq (strcase layer)
                 (strcase (vla-get-layer obj)))

           (vl-catch-all-apply
             (function vla-delete) (list obj)))))

     (vlax-for blk (vla-get-Blocks *doc)

       (vlax-for obj blk

         (if (eq (strcase layer)
                 (strcase (vla-get-layer obj)))

           (vl-catch-all-apply
             (function vla-delete) (list obj)))))

     (setq uFlag (vla-EndUndomark *doc)))

   (princ "\n** Layer Locked or Not Found **"))

 (princ))


 

Tried it and I get one problem... it automattically runs when I open an old drawing or start a new drawing... and I need it to be executed through a command.

Link to comment
Share on other sites

Tried it and I get one problem... it automattically runs when I open an old drawing or start a new drawing... and I need it to be executed through a command.

 

Doesn't for me

Link to comment
Share on other sites

If it's loading automatically, then you have something calling it. Lee's code will only run if called.

 

 

Lee, I haven't seen this before

(setq uFlag (not (vla-StartUndoMark *doc)))

 

That's an interesting idea. Yours?

Link to comment
Share on other sites

Lee, I haven't seen this before
(setq uFlag (not (vla-StartUndoMark *doc)))

That's an interesting idea. Yours?

 

Mine indeed :)

 

I use the same construct for writing to files:

 

(defun *error* (msg)
 (and ofile (close ofile))
...
 (princ))

(setq ofile (open "... "))

(setq ofile (close ofile))

Link to comment
Share on other sites

Mine indeed :)

 

I use the same construct for writing to files:

 

(defun *error* (msg)
 (and ofile (close ofile))
...
 (princ))

(setq ofile (open "... "))

(setq ofile (close ofile))

 

Yeah, that one's a given.

The undo flag is interesting though. I just have it check to make sure the activedoc is defined and then execute undo end. I like.

 

I've done the same thing for resetting a UCS to world and back. Just create a flag if it has to be changed, then it's there when the program exits.

Link to comment
Share on other sites

Yeah, that one's a given.

The undo flag is interesting though. I just have it check to make sure the activedoc is defined and then execute undo end. I like.

 

I've done the same thing for resetting a UCS to world and back. Just create a flag if it has to be changed, then it's there when the program exits.

 

Yeah, I just do it to append the Undo Loop, otherwise things go a bit skewey..:)

Link to comment
Share on other sites

Doesn't for me

 

Yep, got it. Thanks again!!

 

Mine indeed :)

 

I use the same construct for writing to files:

 

(defun *error* (msg)
 (and ofile (close ofile))
...
 (princ))

(setq ofile (open "... "))

(setq ofile (close ofile))

 

Lee, you need to start making some money doings this... Wait no, then I wouldn't get anything done.

Link to comment
Share on other sites

Lee, you need to start making some money doings this... Wait no, then I wouldn't get anything done.

 

Yeah, I wish, but not too many people would pay - as it is so accessible.

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