Lee Mac 发表于 2022-7-6 15:32:44

Coincidentally...
 
see here:
 
http://www.cadtutor.net/forum/showthread.php?t=33191

Hunter 发表于 2022-7-6 15:37:23

Hi,Lee~
 
Is this a subroutine ?
 
Would u please tell me how to run it directly?
 
Thanks ~
 

Lee Mac 发表于 2022-7-6 15:39:22

Hi Hunter,
 
Yes, it is a sub-function, and requires one argument - namely the layer name.
 
Something like this:
 

(defun DeleteLayer(Name / layCol dLay oVal) (vl-load-com) (if (and (/= Name "0")      (/= (strcase Name) (getvar "CLAYER")))    (progn      (setq layCol (vla-get-Layers             (vla-get-ActiveDocument         (vlax-get-acad-object))))      (if (vl-catch-all-error-p      (setq dLay    (vl-catch-all-apply 'vla-Item             (list layCol (strcat Name)))))    (princ "\nLayer does not exist! ")    (if (vl-catch-all-error-p          (vl-catch-all-apply 'vla-Delete (list dLay)))      (princ "\nCan't delete layer in use! ")      (setq oVal T))))    (princ "\nCan't delete active layer or layer \"0\"! ")) oVal)(defun c:test (/ lay) (setq lay (getstring "\nType Name of Layer...")) (DeleteLayer lay) (princ))

Lee Mac 发表于 2022-7-6 15:43:44

I suppose you could also include coding to delete everything on that layer before deleting the layer...
 

(defun DeleteLayer(Name / layCol dLay oVal) (vl-load-com) (if (and (/= Name "0")      (/= (strcase Name) (getvar "CLAYER")))    (progn      (setq layCol (vla-get-Layers             (vla-get-ActiveDocument         (vlax-get-acad-object))))      (if (vl-catch-all-error-p      (setq dLay    (vl-catch-all-apply 'vla-Item             (list layCol (strcat Name)))))    (princ "\nLayer does not exist! ")    (if (vl-catch-all-error-p          (vl-catch-all-apply 'vla-Delete (list dLay)))      (princ "\nCan't delete layer in use! ")      (setq oVal T))))    (princ "\nCan't delete active layer or layer \"0\"! ")) oVal)(defun c:test (/ lay ss) (setq lay (getstring "\nType Name of Layer...")) (setq ss (mapcar 'cadr (ssnamex (ssget "X" (list (cons 8 lay)))))) (mapcar 'entdel ss) (DeleteLayer lay) (princ))

Lee Mac 发表于 2022-7-6 15:46:10

Excuse my last post, doesn't allow for an empty selection set... may return error lseltp nil.

Lee Mac 发表于 2022-7-6 15:50:13

You could compress the code into this:
 

(defun DeleteLayer(Name / ss) (vl-load-com) (if (and (/= Name "0") (tblsearch "LAYER" Name)      (/= (strcase Name) (getvar "CLAYER")))   (progn   (if    (setq ss (ssget "X" (list (cons 8 lay))))   (mapcar 'entdel (mapcar 'cadr (ssnamex ss))))   (if    (vl-catch-all-error-p   (vl-catch-all-apply 'vla-Delete       (list (vlax-ename->vla-object (tblobjname "LAYER" Name)))))   (princ "\nError Deleting Layer")))   (princ "\nLayer not found or Current Layer or Layer \"0\"")))(defun c:test (/ lay) (setq lay (getstring "\nSpecify Layer to Delete...")) (DeleteLayer lay) (princ))

Hunter 发表于 2022-7-6 15:52:04

Hi, Lee:
Thanks for your kindly reply~
It's really appreciated!

Lee Mac 发表于 2022-7-6 15:57:34

No Problems Hunter, did my last code function properly?

Hunter 发表于 2022-7-6 15:58:59

Yeah, it works fine~
 
And it would be perfect if there would be a dialogue box to selecte the layers:)
 

Lee Mac 发表于 2022-7-6 16:03:10

Haha, you love your Dialog Boxes
 
I could create one for it, but, tbh, isn't this the same as just going into the layer manager and deleting the layer from there?
页: 1 [2]
查看完整版本: Delete Layer and it´s contents