I'm looking to automate the naming of my layers in solidworks.
In my example, we have a number of layers that we don't use anymore but exist in old planes.
When I want to retouch the old plans, I would like to delete all the layers except some layers (dimensions, annotations, drawing, IndexA, Index B Index....)
I know how to delete all layers but not exclude some.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swLayerMgr As SldWorks.LayerMgr
Dim swLayer As SldWorks.Layer
Dim vLayerArr As Variant
Dim vLayer As Variant
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
swApp.SendMsgToUser2 "pas de documents ouvert", swMbWarning, swMbOk
Exit Sub
End If
If swModel.GetType <> 3 Then
swApp.SendMsgToUser2 "Ouvrir une mise en plan", swMbWarning, swMbOk
Exit Sub
End If
swModel.ViewZoomtofit2
Set swLayerMgr = swModel.GetLayerManager
vLayerArr = swLayerMgr.GetLayerList
Dim LayerList As Object
Set LayerList = CreateObject("Scripting.Dictionary")
LayerList.Add "cotations", 0
LayerList.Add "Annotations", 0
LayerList.Add "dessin", 0
LayerList.Add "IndiceA", 0
LayerList.Add "IndiceB", 0
For Each vLayer In vLayerArr
Set swLayer = swLayerMgr.GetLayer(vLayer)
If Not LayerList.Exists(swLayer.Name) Then
Debug.Print "Supprime " & swLayer.Name
swLayerMgr.DeleteLayer swLayer.Name
ElseIf swLayer.Name = "cotations" Then
swLayer.Color = 0
ElseIf InStr(swLayer.Name, "Indice") > 0 Then
swLayer.Color = 255
End If
Next
End Sub