乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 60|回复: 19

[编程交流] Delete Layer and it´s contents

[复制链接]

2

主题

8

帖子

6

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:00:38 | 显示全部楼层 |阅读模式
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
 
  1. 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
 
  1. 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!
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:04:23 | 显示全部楼层
Does it account for the layer thats being deleted being layer "0" or the current layer?
回复

使用道具 举报

2

主题

8

帖子

6

银币

初来乍到

Rank: 1

铜币
10
发表于 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
回复

使用道具 举报

2

主题

8

帖子

6

银币

初来乍到

Rank: 1

铜币
10
发表于 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
回复

使用道具 举报

2

主题

8

帖子

6

银币

初来乍到

Rank: 1

铜币
10
发表于 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
回复

使用道具 举报

6

主题

41

帖子

35

银币

初来乍到

Rank: 1

铜币
30
发表于 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)
回复

使用道具 举报

2

主题

8

帖子

6

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:22:32 | 显示全部楼层
I didn't know about that command! well, at least it was a programming excercise!  
 
Regards
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:24:52 | 显示全部楼层
For a programming exercise in LISP, note that a layer can be deleted with a simple one-liner:
 
  1. (vl-catch-all-apply 'vla-Delete (list (vlax-ename->vla-object (tblobjname "LAYER" "layername"))))
 
But there are many ways to accomplish the same thing.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 15:28:08 | 显示全部楼层
Or, with error trappings for layer 0 and current layer... kindly provided by ASMI:
 
  1. (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
回复

使用道具 举报

2

主题

8

帖子

6

银币

初来乍到

Rank: 1

铜币
10
发表于 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?
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 21:24 , Processed in 0.446680 second(s), 83 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表