Jump to content

Codes for deleting a specific Layer Name


Recommended Posts

Posted

Hello , :)

 

What are the codes which could delete a specific Layer Name which I may include within my routine ?

 

Many thanks.

 

Michaels

Posted

I also wrote this a while back:

 

(defun c:test nil

 (LM:DeleteLayersIf "*")

 (princ)
)

;;-----------------=={ Delete Layers If }==-------------------;;
;;                                                            ;;
;;  Deletes layers in a drawing if the layer name matches a   ;;
;;  wildcard string                                           ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  wcstr - wildcard string to identify layers to delete      ;;
;;------------------------------------------------------------;;

(defun LM:DeleteLayersIf ( wcstr / layers locked ss )
 ;; © Lee Mac 2010

 (setq layers
   (vla-get-layers
     (vla-get-ActiveDocument
       (vlax-get-acad-object)
     )
   )
 )

 (foreach x (setq locked (LM:GetLocked layers))
   (vla-put-lock x :vlax-false)
 )

 (setvar 'CLAYER "0")

 (if (setq ss (ssget "_X" (list (cons 8 wcstr))))
   (
     (lambda ( i / e )
       (while (setq e (ssname ss (setq i (1+ i))))
         (entdel e)
       )
     )
     -1
   )
 )

 (vlax-for l layers
   (if (wcmatch (vla-get-name l) wcstr)
     (LM:CatchApply vla-delete (list l))
   )
 )

 (foreach x locked
   (LM:CatchApply vla-put-lock (list x :vlax-true))
 )

 (princ)
)

;;-----------------=={ Get Locked Layers }==------------------;;
;;                                                            ;;
;;  Returns a list of VLA Layer Objects locked within the     ;;
;;  current drawing                                           ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  layers - the layers collection for the current drawing    ;;
;;------------------------------------------------------------;;
;;  Returns:  List of VLA Layer Objects, else nil             ;;
;;------------------------------------------------------------;;

(defun LM:GetLocked ( layers )
 ;; © Lee Mac 2010
 (vlax-for l layers
   (if (eq :vlax-true (vla-get-lock l))
     (setq locked (cons l locked))
   )
 )
 locked
)

;;---------------------=={ Catch Apply }==--------------------;;
;;                                                            ;;
;;  Applies a function to a list of arguments and catches     ;;
;;  an exception.                                             ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  foo  - function to be applied                             ;;
;;  args - list of arguments to be supplied to foo            ;;
;;------------------------------------------------------------;;
;;  Returns:  Result of applying foo, else nil if exception   ;;
;;------------------------------------------------------------;;

(defun LM:CatchApply ( foo args / result )
 ;; © Lee Mac 2010
 (if
   (not
     (vl-catch-all-error-p
       (setq result
         (vl-catch-all-apply (function foo) args)
       )
     )
   )
   result
 )
)

Posted

Wonderful routine Lee.

 

Thank you .:)

 

Michaels

Posted

Just a query ... what happens if there are entities within blocks on one or more of these layers?

 

And then to make the lisp as simple as possible, why not simply use the LayDel command?

Posted

Just a query ... what happens if there are entities within blocks on one or more of these layers?

 

Great question about entities which are within a Block , and that's what I wanted to check today but I did not due to being so busy.

 

And then to make the lisp as simple as possible, why not simply use the LayDel command?

 

I did not use the command call because I did not want to add a command call among the codes in my routine , so to handle it with codes that would

be more powerful and much more better with Lisp codes individually, and for me as a beginner I would like to become familiar with lisp than depending

of Cads' Commands call.

 

Thanks a lot mr. irneb

 

Michaels

Posted

No prob ... I'd just advise you to be a bit careful about messing with all the block definitions in a DWG. Stuff like those unnamed blocks generated when you modify a Dynamic Block reference can (and usually do) become unlinked from the original Dynamic Block definition. So the trick is to try and figure out which blocks are not to be touched.

 

That all said, the normal ALisp way of getting to the block definitions (i.e. using tblsearch, tblobjname, tblnext, etc.) only gives some of the block definitions inside the DWG. Another way to get ALL of them is to step through the ActiveX blocks collection. To understand some of the VLisp stuff, see Lee's helpful post here: http://www.cadtutor.net/forum/showthread.php?53374-xref-clip-boundry&p=361940&viewfull=1#post361940

 

Basically the current drawing (or rather ActiveDocument) has a property called blocks. This is a collection of block definitions, and they include the Model Space and all Paper Space blocks as well. Then each block definition is also a collection of objects inside that def. You can very quickly and easily step through a collection using the vlax-for function.

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