TheRedGuy 发表于 2022-7-6 15:00:38

Delete Layer and it´s contents

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 ExplicitPublic 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 SubPrivate 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 SubEnd SubPrivate Sub CBQuit_Click()   UFEraseLayer.HideEnd SubPrivate 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 UFEraseLayerEnd 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!

Lee Mac 发表于 2022-7-6 15:04:23

Does it account for the layer thats being deleted being layer "0" or the current layer?

TheRedGuy 发表于 2022-7-6 15:08:36

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

TheRedGuy 发表于 2022-7-6 15:11:24

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

TheRedGuy 发表于 2022-7-6 15:14:55

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

CADgirl 发表于 2022-7-6 15:17:46

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)

TheRedGuy 发表于 2022-7-6 15:22:32

I didn't know about that command! well, at least it was a programming excercise!
 
Regards

Lee Mac 发表于 2022-7-6 15:24:52

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.

Lee Mac 发表于 2022-7-6 15:28:08

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

TheRedGuy 发表于 2022-7-6 15:31:21

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?
页: [1] 2
查看完整版本: Delete Layer and it´s contents