Remove all existing layers categories in workpart

Hi all!
I need journal, which removes all existing layers categories and makes all layers invisible.
Help me please.

The code below will delete the layer categories, make layer 1 the work layer, and make layers 2-256 invisible.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module2

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "turn off layers"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

'delete any layer categories from the display part
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(theSession.Parts.Display.LayerCategories.ToArray)

Dim stateArray1(255) As Layer.StateInfo
'make layer 1 the work layer
stateArray1(0) = New Layer.StateInfo(1, Layer.State.WorkLayer)
'turn off layers 2 - 256
For i As Integer = 1 To 255
stateArray1(i) = New Layer.StateInfo(i + 1, Layer.State.Hidden)
Next

'perform update to refresh layer category list
Dim id1 As Session.UndoMarkId
id1 = theSession.NewestVisibleUndoMark

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(id1)

theSession.Parts.Display.Layers.ChangeStates(stateArray1, False)

lw.Close()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Magic! It works! Many thanks! Can I do Donat bypassing the site form? =)

Thanks for the feedback. I'm glad that it works for you and I hope that it helps in your future NXOpen programs.

When you click on the "Donate" link, it will take you to a secure Paypal page. I have Paypal setup to process these transactions, you cannot bypass this page if you wish to donate.