Delete all objects on a frozen layer

Delete all objects on a frozen layer

angiegauthier
Enthusiast Enthusiast
7,571 Views
31 Replies
Message 1 of 32

Delete all objects on a frozen layer

angiegauthier
Enthusiast
Enthusiast

Hi Everyone!

 

Can someone tell me how (or if it's possible) with Lisp to delete anything that is on a frozen layer?

 

Thanks 

Angie

0 Likes
Accepted solutions (1)
7,572 Views
31 Replies
Replies (31)
Message 2 of 32

hmsilva
Mentor
Mentor

Hi Angie,

 

in a frozen layer, the erase command will delete all objects in that layer...

As a 'demo'

 

(defun c:demo ( / ss)
  (if (setq ss (ssget "_X" '((8 . "The Frozen Layer"))))
    (command "_erase" ss "")
    )
  (princ)
  )

 

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 3 of 32

angiegauthier
Enthusiast
Enthusiast

Hi Henrique,

 

yes, that works to delete all objects on the layer, but is there a way to detect which layers are frozen and delete only objects on those layers? I should have specified that I don't know what layers are frozen.

 

Thanks

Angie

0 Likes
Message 4 of 32

hmsilva
Mentor
Mentor
Accepted solution

Hi Angie,

 

try the following code:

 

(vl-load-com)
(defun c:demo (/ adoc layers freezelst)
  (setq adoc   (vla-get-activedocument (vlax-get-acad-object))
        layers (vla-Get-Layers adoc)
  )
  (vlax-for lay layers
    (if (and (= (vla-get-freeze lay) :vlax-true)
             (= (vla-get-lock lay) :vlax-false)
        )
      (setq freezelst (cons (vla-get-name lay) freezelst))
    )
  )
  (if freezelst
    (vlax-for layt (vla-get-layouts adoc)
      (vlax-for blk (vla-get-block layt)
        (if (vl-position (vla-get-layer blk) freezelst)
          (vla-delete blk)
        )
      )
    )
  )
  (princ)
)

 

 

Hope this helps,
Henrique

EESignature

Message 5 of 32

angiegauthier
Enthusiast
Enthusiast

That is exactly what I need. Thank so much!!

Ang

0 Likes
Message 6 of 32

hmsilva
Mentor
Mentor

You're welcome, Angie!
Glad I could help

Henrique

EESignature

0 Likes
Message 7 of 32

Kent1Cooper
Consultant
Consultant

@angiegauthier wrote:

... is there a way to detect which layers are frozen and delete only objects on those layers? ....


[hmsilva posted a suggestion while I had this underway, but here's another way.]

 

(defun C:DFL (/ laynames lay lay70 ss n); = Delete all objects on Frozen/unLocked Layer(s)
  (setq laynames ""); initially-empty string
  (while (setq lay (cdadr (tblnext "layer" (not lay))))
    (setq lay70 (cdr (assoc 70 (tblsearch "layer" lay)))
    (if
      (and
        (= (logand 1 lay70) 1); it's frozen
        (= (logand 4 lay70) 0); it's not locked
      ); and       
      (setq laynames (strcat laynames lay ",")); comma-delimited names of frozen/unlocked Layers
    ); if
  ); while
  (if (> (strlen laynames) 0); found at least one frozen/unlocked Layer

    (progn ; then

      (setq ss (ssget "_X" (list (cons 8 laynames))))
      (repeat (setq n (sslength ss))
        (entdel (ssname ss (setq n (1- n))))
      ); repeat

    ); progn
  ); if
); defun

 

The earlier suggestion to use Erase won't do what you want if there might be things in both Model and Paper Space and/or in different Paper Space Layouts, because any command with object selection will "see" only things in the current space.

 

I just hope you're confident enough about what may be on those Layers.  I'd be a little reluctant to do this in a brute-force way -- who knows what may be there to be removed that won't be apparent?

Kent Cooper, AIA
0 Likes
Message 8 of 32

FCort
Collaborator
Collaborator

Hi,

 

Please indicate how to use this code. I need to erase all layers that are frozen and/or off (with all the objects that have these layers).  It could be better if it allow to select which frozen layers will be eliminated.

 

Laydel is good, but it doesn’t allow to select many at once.

 

I copied the code to note pad, and saved as name.LSP, then type command appload, the select the lisp file, but it doesn’t work.

 

Thanks in advance.

0 Likes
Message 9 of 32

Kent1Cooper
Consultant
Consultant

@FCort wrote:

.... 

Please indicate how to use this code. ....

 

I copied the code to note pad, and saved as name.LSP, then type command appload, the select the lisp file, but it doesn’t work.

....


Did you pick the Load button after selecting the file?  And did you type the DFL command name?  If so, what does "doesn't work" mean?  Loading fails somehow?  Loads but command name isn't recognized?  Command name goes in but nothing happens?  Command name goes in but it does something differently from what you expect?  Error message(s) of any kind?  Etc.

 

As to the Layer selection, there are some things that might be a starting point on another thread.  This Post of mine has LayerThawOnSelect.lsp, which temporarily thaws, and turns on, and highlights the contents of, all Layers that are Frozen or Off, so you can select an object on any such Layer you want, and the Layer name will go into a list.  What it does with the list can be changed to getting rid of everything on those Layers, instead of Thawing and turning them On.  There are other Posts on that same thread that generate dialog box lists you can pick from, if you prefer that approach, and again could be modified easily enough for what you want to do.

 

On a side note, I notice something that could be consolidated in my routine above.  This part:

...

    (if
      (and
        (= (logand 1 lay70) 1); it's frozen
        (= (logand 4 lay70) 0); it's not locked
      ); and       
      (setq laynames ....

 

could be just this:

....

    (if (= (logand 5 lay70) 1); it's frozen and not locked
      (setq laynames ....

Kent Cooper, AIA
0 Likes
Message 10 of 32

alanjt_
Collaborator
Collaborator

Kent, if memory serves, entdel will only work in the active layout. If you want to iterate through a selectionset and delete an item, regardless of active layout, you'll need to use vla-delete.

0 Likes
Message 11 of 32

Kent1Cooper
Consultant
Consultant

@alanjt_ wrote:

Kent, if memory serves, entdel will only work in the active layout. If you want to iterate through a selectionset and delete an item, regardless of active layout, you'll need to use vla-delete.


Hmmm....  It seems to be a mixed bag.  If I do:

(setq test (car (entsel)))

 

to put some entity name into the 'test' variable, and then to go to some different space and do:

 

(entdel test)

 

it sometimes works and sometimes doesn't.

 

If 'test' is in Paper Space, and I go to delete it when in Model Space, it succeeds.  Likewise the reverse -- 'test' in Model Space, delete when in a Paper Space layout.  The situation in which it doesn't work is when 'test' is in one Paper Space layout and I try to delete it when in another Paper Space layout.

 

So it would seem [though it may be worth more extensive testing] that if one does such a thing from Model Space, it should work.  But if (vla-delete) works in any case, by all means go with that.  It would have the added requirement to convert the entities to VLA objects.

Kent Cooper, AIA
0 Likes
Message 12 of 32

alanjt_
Collaborator
Collaborator

From model, entdel will only erase objects in the first layout. If you have multiple layouts, any after ther first are ignored.

 

0 Likes
Message 13 of 32

FCort
Collaborator
Collaborator

Thank you, but after load and running DFL a message comes out:

 

 ; error: malformed list on input

 

What I need is erase all frozen off (no visible) layers at once.

0 Likes
Message 14 of 32

Kent1Cooper
Consultant
Consultant

@alanjt_ wrote:

From model, entdel will only erase objects in the first layout. If you have multiple layouts, any after ther first are ignored.

 


That's not how it works here [it can delete something in any layout from Model Space], but I'm currently at my ol' Acad2004 location -- maybe it's changed in newer versions.

Kent Cooper, AIA
0 Likes
Message 15 of 32

Kent1Cooper
Consultant
Consultant

@FCort wrote:

Thank you, but after load and running DFL a message comes out:

 

 ; error: malformed list on input

 

What I need is erase all frozen off (no visible) layers at once.


I think that must have come after only loading it, because there was in fact a parenthesis missing -- I guess I must not have actually tested it back then.

 

But in any case, that was for only Frozen Layers, not also Layers that are Off.  So fixing the parenthesis thing, and adding the Off-Layers thing, and [just in case -- I'm not sure this is needed] changing to (vla-delete) instead of (entdel), give this a try [lightly tested]:

(defun C:DFOL (/ laynames lay lay70 ss n); = Delete all objects on unlocked Frozen or Off Layer(s)
  (setq laynames ""); initially-empty string
  (while (setq lay (cdadr (tblnext "layer" (not lay))))
    (setq lay70 (cdr (assoc 70 (tblsearch "layer" lay))))
    (if
      (and
        (or
          (= (logand 1 lay70) 1); it's frozen
          (minusp (cdr (assoc 62 (tblsearch "layer" lay)))); it's off
        ); or
        (= (logand 4 lay70) 0); it's not locked
      ); and
      (setq laynames (strcat laynames lay ","))
; comma-delimited names of unlocked frozen-or-off Layers ); if ); while (if (and (> (strlen laynames) 0); found at least one such Layer (setq ss (ssget "_X" (list (cons 8 laynames)))); something drawn there ); and (repeat (setq n (sslength ss)) (vla-delete (vlax-ename->vla-object (ssname ss (setq n (1- n))))) ); repeat ); if ); defun (vl-load-com)

But I still have my reservation about this, expressed at the end of Post 7.

Kent Cooper, AIA
Message 16 of 32

FCort
Collaborator
Collaborator
Thank you again, but it doesn’t delete any frozen layer. I would like something similar like LAYDEL, but that allows select many frozen-off layers at once (selecting the frozen-off layers from the layer properties manager will be better). To use LAYDEL the layer has to be active on, but in my case I want to eliminate the ones that are not active/off. ( I have a lot of layers in a drawing, so, if I turn on all the layers, it is going to be hard to find the ones that I want to eliminate.)
0 Likes
Message 17 of 32

alanjt_
Collaborator
Collaborator
(defun c:DelFrz (/ names layers)

  (vlax-for l (vla-get-layers
                (cond (*AcadDoc*)
                      ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
                )
              )
    (if (eq (vla-get-freeze l) :vlax-true)
      (progn
        (vla-put-lock l :vlax-false)
        (setq names  (cons (vla-get-name l) names)
              layers (cons l layers)
        )
      )
    )
  )

  (if names
    (progn
      (vlax-for layout (vla-get-layouts *AcadDoc*)
        (vlax-for block (vla-get-block layout)
          (if (member (vla-get-layer block) names)
            (vl-catch-all-apply 'vla-delete (list block))
          )
        )
      )

      (foreach layer layers (vl-catch-all-apply 'vla-delete (list layer)))
    )
  )

  (princ)
)
(vl-load-com)
0 Likes
Message 18 of 32

FCort
Collaborator
Collaborator

Thank you alanjt,

 

DelFrz doesn't delete any frozen layer and frozen objects (see attached pic. result after running DelFrz ).

 

frozen layers.JPG

0 Likes
Message 19 of 32

Kent1Cooper
Consultant
Consultant

@FCort wrote:
Thank you again, but it doesn’t delete any frozen layer. ....

As a continuation of the Subject of this thread, it removes the objects from such Layers, but doesn't Purge them.  That could be added easily enough, but I can't take the time to do it right now -- maybe in the next couple of days, if you can't figure out how to add that yourself.

Kent Cooper, AIA
0 Likes
Message 20 of 32

alanjt_
Collaborator
Collaborator

Odd. Works fine on my end.

What version are you running?

0 Likes