NX Journal - Need to know if layer has objects

Hi all, like the title suggests, I need to be able to have an argument that says "If the layer has objects then"

This is my code but I know I can't use "Null"


Dim LayerManager As Layer.LayerManager = workpart.Layers
Dim ObjTest As NXObject() = Null
Dim j As Integer = 1
Do Until (LayerArray(j - 1).State = Layer.State.Selectable And ObjTest <> Null Or j = 256)
ObjTest = LayerManager.GetAllObjectsOnLayer(j)
j = j + 1
Loop

It's just the ObjTest <> Null but I need. Some ways of getting True or False if a layer has objects

Please help!

You can use the .GetAllObjectsOnLayer method and check how many objects are returned.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()

Dim theUI As UI = UI.GetUI()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ")

lw.Open()

For i As Integer = 1 To 256
lw.WriteLine("Layer " & i.ToString & " has objects: " & DoesLayerHaveObjects(i).ToString)
Next

lw.Close()

End Sub

Function DoesLayerHaveObjects(ByVal layerNumber As Integer) As Boolean

Dim layerObjects() As NXObject = theSession.Parts.Work.Layers.GetAllObjectsOnLayer(layerNumber)

If layerObjects.Length > 0 Then
Return True
Else
Return False
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Side note: if you are using Visual Basic, it uses the "Nothing" keyword instead of "Null".

Thank you so much! The DoesLayerHaveObjects function is exactly what I needed! I hadn't thought of using the object array's length as the test. Thanks!