Author Topic: Delete Unlocked Layers  (Read 1935 times)

0 Members and 1 Guest are viewing this topic.

rodolfo_palma

  • Mosquito
  • Posts: 5
Delete Unlocked Layers
« on: August 04, 2020, 06:56:32 AM »
Hello everyone!

I need help with an autolisp code that delete all unlocked layers presents in the drawing.

With normal commands I probably could do this with LAYDEL, but i need include this inside another code, so this need to be done in autolisp.

Any ideas?

Thanks!
R.Palma

JohnK

  • Administrator
  • Seagull
  • Posts: 10636
Re: Delete Unlocked Layers
« Reply #1 on: August 04, 2020, 12:36:21 PM »
My VisualLisp contribution.

Code - Auto/Visual Lisp: [Select]
  1. (defun My-Unlocked-Layer-Delete ( / *acadobject* *activedocument* )
  2.   (setq *acadobject*
  3.          (cond
  4.            (*acadobject*)
  5.            ((vlax-get-acad-object)))
  6.       *activedocument*
  7.          (cond
  8.            (*activedocument*)
  9.            ((vla-get-activedocument *acadobject*))) )
  10.   (vlax-for item (vla-get-layers *activedocument*)
  11.     ;; If layer is not locked, and not layer 0.
  12.     (if (and (eq (vla-get-lock item) :vlax-false)
  13.              (not (eq (vla-get-name item) "0")))
  14.       (vlax-invoke item 'Delete))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

rodolfo_palma

  • Mosquito
  • Posts: 5
Re: Delete Unlocked Layers
« Reply #2 on: August 04, 2020, 08:09:24 PM »
Thanks for the help John Kaul.

But when I'm trying to run it, this error appears:

; error: AutoCAD.Application: Object is referenced by other object(s)

JohnK

  • Administrator
  • Seagull
  • Posts: 10636
Re: Delete Unlocked Layers
« Reply #3 on: August 04, 2020, 09:36:26 PM »
Thanks for the help John Kaul.

But when I'm trying to run it, this error appears:

; error: AutoCAD.Application: Object is referenced by other object(s)

That would be caused because the current layer is unlocked (and thus being deleted). The following adds the current layer check as part of the conditional check (and doesn't try to delete it, even if it is unlocked). You should be checking and changing the current layer in your code -i.e. having that check part of a utilitiy routine like this is not the best practice.

The code is still strictly VisualLisp.
Code - Auto/Visual Lisp: [Select]
  1. (defun My-Unlocked-Layer-Delete ( / *acadobject* *activedocument* )
  2.   (setq *acadobject*
  3.          (cond
  4.            (*acadobject*)
  5.            ((vlax-get-acad-object)))
  6.       *activedocument*
  7.          (cond
  8.            (*activedocument*)
  9.            ((vla-get-activedocument *acadobject*))) )
  10.   (vlax-for item (vla-get-layers *activedocument*)
  11.     ;; If layer is not locked, current, and not layer 0.
  12.     (if (and (eq (vla-get-lock item) :vlax-false)
  13.              (not (eq (vla-get-name item) "0"))
  14.              (not
  15.                 (eq
  16.                     (vla-get-name (vlax-get-property *activedocument* 'ActiveLayer))
  17.                     (vla-get-name item))))
  18.         (vlax-invoke item 'Delete))) )
  19.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Delete Unlocked Layers
« Reply #4 on: August 05, 2020, 02:02:01 PM »
A layer may only be deleted if it is not referenced by any entities (graphical or non-graphical), as such, you'll need to iterate over the drawing database (including entities nested within all block definitions and also AcDbBlockBegin entities), and either reassign or delete those entities which reference the layer, before deleting the layer definition itself.

JohnK

  • Administrator
  • Seagull
  • Posts: 10636
Re: Delete Unlocked Layers
« Reply #5 on: August 05, 2020, 02:17:23 PM »
We in the United States of America invented the English language; you guys from across the pond should think about adopting it as well because after reading your post I wish you were using English when you constructed your reply.

...that sounds like a lot of work! The reassigning would be a task of another sub routine--I hope, because I wouldn't know how to do that (I don't even *open* AutoCAD much these days).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete Unlocked Layers
« Reply #6 on: August 05, 2020, 03:55:43 PM »
Here's a quick one ( slow code :) ) that tries to delete everything then purges the drawing to remove the layers.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ d er i n)
  2.   ;; RJP » 2020-08-05
  3.   (mapcar 'set '(er n) '(0 0))
  4.     ;; If it's not an xref
  5.     (if (= 0 (vlax-get b1 'isxref))
  6.       (vlax-for b2 b1
  7.         (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-delete (list b2)))
  8.           (setq er (1+ er))
  9.           (setq n (1+ n))
  10.         )
  11.       )
  12.     )
  13.   )
  14.   (repeat 3 (vla-purgeall d))
  15.   (alert (strcat (itoa er)
  16.                  " errors deleting objects...\n"
  17.                  (itoa n)
  18.                  " objects were deleted...\n"
  19.                  (itoa (- i (vla-get-count (vla-get-layers d))))
  20.                  " layers purged..."
  21.          )
  22.   )
  23.   (princ)
  24. )
« Last Edit: August 05, 2020, 03:58:53 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

rodolfo_palma

  • Mosquito
  • Posts: 5
Re: Delete Unlocked Layers
« Reply #7 on: August 05, 2020, 07:38:53 PM »
I apologize for english mistakes.. I'm trying my best here, hehe.

Thanks for all the replies, I could learn a lot from them.

I discover my mistakes... I was trying to run this code in a Civil3d template. The layers are impossible to delete, since they are referenced by non-graphical entities (Civil3d styles).
Lee Mac was absolutely right! hehe.

My intention with this code is to keep in the drawing only the layers that are in use by graphical entities.
My knowledge is very limited, but what I got so far is this:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:lapurge ( / oldecho obj i la la_lst e acadobj activedoc lay la_name)
  2.  
  3.         (vl-load-com)
  4.         (setvar "clayer" "0")
  5.         (setq oldecho (getvar "cmdecho"))
  6.         (setvar "cmdecho" 0)
  7.         (setvar "nomutt" 1)
  8.         (command "._UNDO" "_Begin")
  9.  
  10. ;******************************************************************************
  11. ; Lock Layers in use
  12.  
  13.         (setq obj (ssget "X"))
  14.         (repeat (setq i (sslength obj))
  15.                 (if (not (member (setq la (cdr (assoc 8 (entget (ssname obj (setq i (1- i))))))) la_lst))
  16.                         (progn
  17.                                 (setq   e (entget (tblobjname "LAYER" la))
  18.                                                 la_lst (cons la la_lst))
  19.                                 (entmod (subst (cons 70 4) (assoc 70 e) e))
  20.                         )
  21.                 )
  22.         )
  23.  
  24. ;******************************************************************************
  25. ; Delete layers not used
  26.  
  27.                 (if     (and (eq (vla-get-lock lay) :vlax-false)
  28.             (not (eq (vla-get-name lay) "0")))
  29.                                 (progn
  30.                                         (setq la_name (vlax-get-property lay 'Name))
  31.                                         (command "._laydel" "N" la_name "" "Y")
  32.                                 )
  33.                 )
  34.         )
  35.                        
  36. ;******************************************************************************
  37. ; Unlock all layers and restore variables
  38.  
  39.         (command "._layer" "U" "*" "")
  40.         (command "._UNDO" "_End")
  41.         (setvar "cmdecho" oldecho)
  42.         (setvar "nomutt" 0)
  43.         (princ)
  44.        
  45. )
  46.