Author Topic: Delete Layer and all entities on it  (Read 1658 times)

0 Members and 1 Guest are viewing this topic.

PM

  • Guest
Delete Layer and all entities on it
« on: July 14, 2021, 11:35:46 AM »
Hi i have a layer with name  "_3d points " and is off all the time in my drawings.I want some timew to delete it with all entities on it without goin to properties palen and quick select this layer and delete it. Is it possible to do this with a lisp?

Thanks

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #1 on: July 14, 2021, 11:56:11 AM »
Do you want to select the objects(layer) on the screen or would you just like to delete the layer without any user interaction at all?

you can use the LAYDEL command (very low tech) but you have to be sure that the layer you want to delete isn't the current layer (you can set the current layer to ZERO to be safe).

Code - Auto/Visual Lisp: [Select]
  1. (command "._layer" "t" "0" "on" "0" "s" "0" "")
  2. (laydel "t" (ssget "x" '((8 . "_3d points"))) "yes")
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PM

  • Guest
Re: Delete Layer and all entities on it
« Reply #2 on: July 14, 2021, 12:08:31 PM »
Hi John Kaul (Se7en). I did it but gives me this message

Code - Auto/Visual Lisp: [Select]
  1. (defun c:dellay ()  
  2.   (command "._layer" "t" "0" "on" "0" "s" "0" "")
  3.     (laydel "t" (ssget "x" '((8 . "_3d points"))) "yes")
  4. )
  5.  
  6.  


Quote
Select object on layer to delete or [Name]:

I turn the layer on and i run again the code but the same result. I can not understand . We allready give the name in the code

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #3 on: July 14, 2021, 12:33:07 PM »
Oh sorry, I was interrupted while I was typing my response. We cannot use it that way. I intended to offer up two different concepts. One was using the SSGET to erase stuff on that layer and the other concept was to use LAYDEL. Sorry, my first post is incomprehensible and incomplete. I am very busy at the moment and trying to do too many things at once. But quickly...

You would use laydel something like this: (command "_laydel" "n" "_3d points" "" "yes")

You can get items on that layer to delete like this: (ssget "x" '((8 . "_3d points")))

You can get the layer entity like this:(tblobjname "layer" "_3d points")
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PM

  • Guest
Re: Delete Layer and all entities on it
« Reply #4 on: July 14, 2021, 01:08:26 PM »
with only this command

Code - Auto/Visual Lisp: [Select]
  1.  (command "_laydel" "n" "_3d points" "" "yes")
  2.  

delete the layer with all entities on it without other code

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #5 on: July 14, 2021, 01:16:39 PM »
yes, it should but you shouldn't just randomly delete layer without checking to see what the current layer is, for example (it will fail I believe). Investigate, then delete.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Delete Layer and all entities on it
« Reply #6 on: July 14, 2021, 08:35:27 PM »
My take on it   
Code: [Select]
(defun c:dellay ( / lay) 

    (setq lay (cdr (assoc 8 (entget (car (entsel "\nPick object for layer "))))))
(if (= lay (getvar 'clayer))
    (command "._layer" "t" "0" "on" "0" "s" "0" "")
)
    (command "-laydel" (ssget "x" (list (cons 8 Lay))) "" "yes")
(princ)
)

A man who never made a mistake never made anything

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #7 on: July 15, 2021, 09:34:18 AM »
Since we already know the layer we'd like to remove. I would (at the very minimum):
1. Check if layer exists
1a. if so, then check to see if it is the current layer.
1b. if so, then change to another layer.
1c. delete the layer.

Code - Auto/Visual Lisp: [Select]
  1. ( (lambda nil
  2.     (cond
  3.       ((tblsearch "layer" "_3d points")
  4.           (if (= (getvar 'clayer) "_3d points")
  5.               (command "._layer" "t" "0" "on" "0" "s" "0" "")
  6.            )
  7.           (command "-laydel" "_3d points" "" "yes")
  8.        )
  9.     )
  10.   )
  11. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PM

  • Guest
Re: Delete Layer and all entities on it
« Reply #8 on: July 15, 2021, 10:30:54 AM »
Im confiused  John Kaul (Se7en) .Can you fix the code ??

Code - Auto/Visual Lisp: [Select]
  1. (defun c:dellay3dpo ()  
  2.     (setq lay "_3d points")
  3.         (if (= lay (getvar 'clayer))
  4.             (command "._layer" "t" "0" "on" "0" "s" "0" "")
  5.         )
  6.     (command "-laydel" (ssget "x" (list (cons 8 Lay))) "" "yes")
  7. )
  8. )
  9.  
  10.  

Thanks

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #9 on: July 15, 2021, 10:54:50 AM »
You're confused about what?

That code will not work so use the code I gave you instead.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PM

  • Guest
Re: Delete Layer and all entities on it
« Reply #10 on: July 15, 2021, 11:04:36 AM »
Something like this ??


Code - Auto/Visual Lisp: [Select]
  1.  
  2.     ( defun c:dellay3dpo ()
  3. (lambda nil
  4.         (cond
  5.           ((tblsearch "layer" "_3d points")
  6.               (if (= (getvar 'clayer) "_3d points")
  7.                   (command "._layer" "t" "0" "on" "0" "s" "0" "")
  8.                )
  9.               (command "-laydel" "_3d points" "" "yes")
  10.            )
  11.         )
  12.       )
  13.     )
  14.  
  15.  

PM

  • Guest
Re: Delete Layer and all entities on it
« Reply #11 on: July 15, 2021, 11:09:17 AM »
gives me this result

Quote
#<SUBR @000001383de23368 -lambda->

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #12 on: July 15, 2021, 11:14:58 AM »
Ah. I understand what you're confused about now.

You should read:
The Basics in a Nutshell - Part 1
The Basics in a Nutshell - Part 2

You can do this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dellay3dpo ()
  2.    ((lambda nil
  3.        (cond
  4.          ((tblsearch "layer" "_3d points")
  5.              (if (= (getvar 'clayer) "_3d points")
  6.                  (command "._layer" "t" "0" "on" "0" "s" "0" "")
  7.               )
  8.              (command "-laydel" "_3d points" "" "yes")
  9.           )
  10.        )
  11.      )
  12.    )
  13. )

or this:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:dellay3dpo ()
  2.    (cond
  3.      ((tblsearch "layer" "_3d points")
  4.          (if (= (getvar 'clayer) "_3d points")
  5.              (command "._layer" "t" "0" "on" "0" "s" "0" "")
  6.           )
  7.          (command "-laydel" "_3d points" "" "yes")
  8.       )
  9.    )
  10. )

To answer your last post:
That is an expected result to your code because you told the interpreter to only create the lambda function, not execute it. That result is the location in memory where your lambda function exists.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

PM

  • Guest
Re: Delete Layer and all entities on it
« Reply #13 on: July 15, 2021, 11:22:06 AM »
why all the time  ask

Quote
Select object on layer to delete or [Name]:

This layer is all the time off and i want to delete it anyway with all entities on it

when i use

Code - Auto/Visual Lisp: [Select]
  1.  (command "_laydel" "n" "_3d points" "" "yes")
  2.  

delete evetything without questions

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Delete Layer and all entities on it
« Reply #14 on: July 15, 2021, 11:30:07 AM »
Oh?! That is because "-laydel" is a typo! Use "_laydel".
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org