TheRedGuy Posted February 26, 2009 Share Posted February 26, 2009 Hi everyone, here a simple vba code to delete layers. It previously erases all the layer's content in order to make deletion possible. Hope it helps someone. First of all, you need to create a UserForm with a ListBox control named "LBLayers", which must have the MultiSelect property set to "2-fmMultiSelectExtended". Also you´ll need a command button named "CBEraseLayer", and a quit control if you want. Add the following code to the UserForm code Option Explicit Public Sub LoadLayersList(LList As ListBox) Dim tlay As AcadLayer LList.Clear For Each tlay In ThisDrawing.Layers Call LList.AddItem(tlay.Name) Next tlay LList.ListIndex = 0 End Sub Private Sub CBEraseLayer_Click() Dim tlay As AcadLayer Dim sset As AcadSelectionSet Dim FilterType(0) As Integer Dim FilterData(0) As Variant Dim nlays As Integer, i As Integer nlays = LBLayers.ListCount If ThisDrawing.SelectionSets.Count >= 1 Then For i = ThisDrawing.SelectionSets.Count - 1 To 0 Step -1 ThisDrawing.SelectionSets.Item(i).Delete Next i End If FilterType(0) = 8 For i = 0 To nlays - 1 If LBLayers.Selected(i) Then FilterData(0) = LBLayers.List(i) Set sset = ThisDrawing.SelectionSets.Add("SSAUX01") Call sset.Select(acSelectionSetAll, , , FilterType, FilterData) Call sset.Erase Call sset.Delete ThisDrawing.Layers.Item(LBLayers.List(i)).Delete End If Next i Call LoadLayersList(LBLayers) Exit Sub End Sub Private Sub CBQuit_Click() UFEraseLayer.Hide End Sub Private Sub UserForm_Activate() Call LoadLayersList(LBLayers) End Sub Also, you will need a module with a sub in order to launch the macro from autocad, the launcher could be something like this Public Sub EraseLayers() Load UFEraseLayer UFEraseLayer.Show Unload UFEraseLayer End Sub Now, you can call the Form from the macros menu, and erase all the selected layers together with it´s contents. NOTE: Use it carefully! Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 26, 2009 Share Posted February 26, 2009 Does it account for the layer thats being deleted being layer "0" or the current layer? Quote Link to comment Share on other sites More sharing options...
TheRedGuy Posted February 26, 2009 Author Share Posted February 26, 2009 The UserForm loads all the layers names on the ListBox and let´s the user to select which layers are going to be deleted. Regarding your question, no, it doesn´t check that layer "0" cannot be deleted, so adding an if inside the layers list to avoid this situation that clearly would lead to a runtime error, would improve the code. Also, check if the layer is locked or not and ask if you want to delete it anyway. In that case, unlock the layer and proceed, otherwise don´t try to delete the objects inside the layer, this also would lead to a runtime error. Regards Alejandro Quote Link to comment Share on other sites More sharing options...
TheRedGuy Posted February 26, 2009 Author Share Posted February 26, 2009 My apologies, the text I've places few minutes ago is almost unreadable, (I'm out of fit with my english). I hope you can understand what I've tried to say, if you want I can rethink the message, it would be a nice exercise. Regards Alejandro Quote Link to comment Share on other sites More sharing options...
TheRedGuy Posted February 26, 2009 Author Share Posted February 26, 2009 The previous message is a mess, I'm sorry. If you're unable to understand what I'm trying to say, I´ll be happy to rewrite it. Regards Alejandro Quote Link to comment Share on other sites More sharing options...
CADgirl Posted February 26, 2009 Share Posted February 26, 2009 I'm sure this is a stupid question, but what is the difference between this and laydel? (I obvioulsy don't know anything about codes and all that) Quote Link to comment Share on other sites More sharing options...
TheRedGuy Posted February 27, 2009 Author Share Posted February 27, 2009 I didn't know about that command! well, at least it was a programming excercise! Regards Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 For a programming exercise in LISP, note that a layer can be deleted with a simple one-liner: (vl-catch-all-apply 'vla-Delete (list (vlax-ename->vla-object (tblobjname "LAYER" "layername")))) But there are many ways to accomplish the same thing. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 Or, with error trappings for layer 0 and current layer... kindly provided by ASMI: (defun DeleteLayer(Name / layCol dLay oVal) (vl-load-com) (if (and (/= Name "0") ; Check its not Layer 0 (/= (strcase Name)(getvar "CLAYER")) ; Check its not Current Layer ); end or (progn (setq layCol(vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))) ; Retrieve Current Layer Collection (if(vl-catch-all-error-p (setq dLay(vl-catch-all-apply 'vla-Item (list layCol(strcat Name))))) ; Retrieve Layer Object Name from Layer Collection? (princ "\nLayer does not exist! ") (if(vl-catch-all-error-p (vl-catch-all-apply 'vla-Delete (list dLay))) ; If Possible, Delete the Layer (princ "\nCan't delete layer in use! ") (setq oVal T) ); end if ); end if ); end progn (princ "\nCan't delete active layer or layer \"0\"! ") ); end if oVal ); end of DeleteLayer Quote Link to comment Share on other sites More sharing options...
TheRedGuy Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks, Lisp is a pending issue. Despite that I'm able to do some programming using other list oriented languages/programs, such as Mathematica, I´ve never been able to do something using Lisp. Probably the best time investement would be in ObjectARX or something related to .NET, what do you think about? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 Coincidentally... see here: http://www.cadtutor.net/forum/showthread.php?t=33191 Quote Link to comment Share on other sites More sharing options...
Hunter Posted February 27, 2009 Share Posted February 27, 2009 Hi,Lee~ Is this a subroutine ? Would u please tell me how to run it directly? Thanks ~ Or, with error trappings for layer 0 and current layer... kindly provided by ASMI: (defun DeleteLayer(Name / layCol dLay oVal) (vl-load-com) (if (and (/= Name "0") ; Check its not Layer 0 (/= (strcase Name)(getvar "CLAYER")) ; Check its not Current Layer ); end or (progn (setq layCol(vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))) ; Retrieve Current Layer Collection (if(vl-catch-all-error-p (setq dLay(vl-catch-all-apply 'vla-Item (list layCol(strcat Name))))) ; Retrieve Layer Object Name from Layer Collection? (princ "\nLayer does not exist! ") (if(vl-catch-all-error-p (vl-catch-all-apply 'vla-Delete (list dLay))) ; If Possible, Delete the Layer (princ "\nCan't delete layer in use! ") (setq oVal T) ); end if ); end if ); end progn (princ "\nCan't delete active layer or layer \"0\"! ") ); end if oVal ); end of DeleteLayer Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 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)) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 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)) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 Excuse my last post, doesn't allow for an empty selection set... may return error lseltp nil. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 27, 2009 Share Posted February 27, 2009 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)) Quote Link to comment Share on other sites More sharing options...
Hunter Posted March 2, 2009 Share Posted March 2, 2009 Hi, Lee: Thanks for your kindly reply~ It's really appreciated! Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 2, 2009 Share Posted March 2, 2009 No Problems Hunter, did my last code function properly? Quote Link to comment Share on other sites More sharing options...
Hunter Posted March 2, 2009 Share Posted March 2, 2009 Yeah, it works fine~ And it would be perfect if there would be a dialogue box to selecte the layers:) No Problems Hunter, did my last code function properly? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted March 2, 2009 Share Posted March 2, 2009 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.