Programming Layer Functions NX Layers

Programming Layer Functions
NX Layers
NX has 256 layers numbered from 1 to 256.
One layer only can/must be the work layer at any given time. The work layer is selectable and
therefore visible. Work is done on the current work layer.
All other layers can have one of three statuses: selectable, visible only or invisible.
Categories names can be applied to layer(s) so that layers may be selected by layer numbers or
categories. All layers have a default category name All.
Interactive Layer functions include:
Format -> Layer Settings
Format -> Visible in View
Format-> Layer Categories
Format -> Move to Layer
Format -> Copy to Layer
NX Layers Programming
Layer Classes
The classes available in NXOpen.Layer namespace are:
Category
Represents a layer category.
CategoryCollection
Represents a collection of layer categories.
Layer Manager
Represents an object that manages layers.
StateCollection
Represents a copy of the layer states.
StateInfo
Used in several methods that gets or sets the state of a layer
Layer Status
There are two different enumerations for layer status values.
The ones for the "wrapped" user function calls are valued at 1 thru 4.
UFConstants.UF_LAYER_WORK_LAYER
1
UFConstants.UF_LAYER_ACTIVE_LAYER
2
UFConstants.UF_LAYER_REFERENCE_LAYER
3
UFConstants.UF_LAYER_INACTIVE_LAYER
4
The other set is valued at 0 thru 3. These values are used with the NXOpen.Layer.State...
methods.
NXOpen.Layer.State.WorkLayer
0
NXOpen.Layer.State.Selectable
1
NXOpen.Layer.State.Visible
2
NXOpen.Layer.State.Hidden
3
You can use either set, but they are not interchangeable.
Layer Constants
The constants for layers has two members. These are:
FirstLayer
The number of the first layer. Valid layers are numbered from
Layer.Constants.FirstLayer to Layer.Constants.LastLayer.
LastLayer
The number of the last layer. Valid layers are numbered from
Layer.Constants.FirstLayer to Layer.Constants.LastLayer
Layer State
The state has four members. These are:
WorkLayer
Work layer. All newly created objects are placed on the work layer.
Selectable
Objects on the layer are selectable.
Visible
Objects on the layer are visible but not selectable.
Hidden
Objects on the layer are not visible and not selectable.
Layer Methods
The table below lists more common layer class methods.
Class
NXOpen.Layer.Category
Represents a layer category.
Member
Description
GetMemberLayers
SetState
NXOpen.Layer.CategoryCollection Create Category
Represents a collection of layer
categories.
Description
Returns or sets the category’s
description, if one exists
Returns all the layers that belong
to the category.
Changes the state of every layer in
the category to the specified state,
except the work layer
Creates a new layer category
NXOpen.Layer.LayerManager
Represents an object that
manages layers.
ChangeStates
CopyObjects
GetAllObjectsOnLayer
GetStates
GetVisibilitiesInView
MoveDisplayableObjects
SetState
SetStates
Changes the states of the specified
layers in the part.
Copies objects to the specified
layer
Returns all objects on the specified
layer
Gets the states for all layers in the
part
Gets the visibility of all layers in a
specified view.
Movedisplayable objects to the
specified layer.
Sets the state of the specified
layer.
Set the states for all layers in the
part
We now look at a number of programs that use various layer functions.
Program 1:
This program moves all solid bodies to a specific layer. All solid bodies are selected using a collection
class (discussed in detail in “Using Collection Classes”). The layer specification is defined using a
NXInputBox to input a layer number. NXInputBox Class is discussed in Appendix 1.
Program 1 Listing: move_solid_bodies_to_new_layer.vb
Option Strict Off
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.Utilities
Module move_solid_bodies_to_new_layer
Dim s As Session = Session.GetSession()
Sub Main()
Dim workPart As Part = s.Parts.Work
Dim bodies As BodyCollection = workPart.Bodies
Dim solid_bodies(-1) As Body
Dim counter As Integer = 0
Dim newLayer As Integer = 100
Dim bodyCount As Integer = bodies.ToArray.Length
MsgBox("All Bodies in Work Part: " & bodyCount.ToString())
newLayer = NXInputBox.GetInputNumber("Destination Layer Number",
"Destination Layer number", newLayer.ToString)
If bodyCount > 0 Then
For Each thisBody As Body In bodies
If thisBody.IsSolidBody.Equals(True) Then
ReDim Preserve solid_bodies(counter)
solid_bodies(counter) = thisBody
counter += 1
End If
Next
workPart.Layers.MoveDisplayableObjects(newLayer, solid_bodies)
Dim solidBodyCount As Integer = solid_bodies.Length()
MsgBox(solidBodyCount.ToString() & "Solid Bodies moved to
layer: " & newLayer.ToString)
End If
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
End Module
This program uses the NXInputBox class (yellow highlight). It should be noted that this does not
require locking UG Access (discussed in “ “).
The solid bodies in the work part are collected into bodies using:
Dim bodies As BodyCollection = workPart.Bodies
This collects all Bodies into the collection called bodies.
Arrays are created a fixed size. If the number of elements is dynamic or unknown we should use
collections.
The above program does use an array which stores the counted bodies. To do this we used the
command: Redim Preserve
ReDim Preserve solid_bodies(counter)
ReDim is to change the size of the array solid_bodies. Preserve keeps the entries already in
solid_bodies. The next body can now be added and the counter incremented.
An alternative to this is to use a list method of the collection class using
Dim list1 As New ArrayList()
list1.Clear()
Instead of
ReDim Preserve solid_bodies(counter)
solid_bodies(counter) = thisBody
counter += 1
use
list1.Add(thisBody)
An array list cannot be used in journals. For larger lists using array list is more efficient.
Having determined the number of solid bodies in bodies using:
Dim bodyCount As Integer = bodies.ToArray.Length
We can now loop through them and set the layer number for each of the solid bodies using:
For Each thisBody As Body In bodies
If thisBody.IsSolidBody.Equals(True) Then
ReDim Preserve solid_bodies(counter)
solid_bodies(counter) = thisBody
counter += 1
End If
Next
Finally we move the solid bodies to the required layer using:
workPart.Layers.MoveDisplayableObjects(newLayer, solid_bodies)
Program 2:
It is commonly required to create objects from within a program and have them created on a
predetermined layer. To this one needs to make the work layer the layer that the programmer
wants the object to be created on. After the new object has been created it is normal to set the
work layer back to its original position. To do this we need to store the work layer then change the
work layer to create whatever is required and finally restore the original setting.
Program 2 simply creates a block on a requested layer number.
Program 2: Listing
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpenUI
Module LayerProgramming2
Sub Main()
Dim
Dim
Dim
Dim
Dim
s As Session = Session.GetSession()
ui As UI = ui.GetUI()
ufs As UFSession = UFSession.GetUFSession()
workPart As Part = s.Parts.Work
stateArray1(0) As Layer.StateInfo
' Get the current worklayer
Dim workLayer As Integer = workPart.Layers.WorkLayer
' Ask for layer to create solid block on
Dim newLayer As Integer = Nothing
newLayer = NXInputBox.GetInputNumber("Destination Layer Number",
"Destination Layer number", newLayer.ToString)
If newLayer <> workLayer Then
' Set new worklayer
stateArray1(0).Layer = newLayer
stateArray1(0).State = Layer.State.WorkLayer
workPart.Layers.ChangeStates(stateArray1, False)
End If
' Set an undo mark
Dim markId1 As Session.UndoMarkId
markId1 = s.SetUndoMark(Session.MarkVisibility.Visible, "Start")
' Create the block
Dim nullFeatures_Feature As Features.Feature = Nothing
Dim blockFeatureBuilder1 As Features.BlockFeatureBuilder
blockFeatureBuilder1 =
workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature)
blockFeatureBuilder1.BooleanOption.Type =
GeometricUtilities.BooleanOperation.BooleanType.Create
Dim originPoint1 As Point3d = New Point3d(0.0, 0.0, 0.0)
blockFeatureBuilder1.SetOriginAndLengths(originPoint1, "100",
"100", "75")
blockFeatureBuilder1.SetBooleanOperationAndTarget(Features.Feature.BooleanT
ype.Create, Nothing)
Dim feature1 As Features.Feature
feature1 = blockFeatureBuilder1.CommitFeature()
' If we have changed worklayer we may need to change it back to the
original setting
If workLayer <> workPart.Layers.WorkLayer Then
stateArray1(0).Layer = workLayer
stateArray1(0).State = Layer.State.WorkLayer
workPart.Layers.ChangeStates(stateArray1, False)
End If
End Sub
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
We first need to get the current worklayer. This is done using:
Dim workLayer As Integer = workPart.Layers.WorkLayer
Next we get the layer on which we want to create the block. This is highlighted in yellow.
We now check that we need to change the worklayer. This is highlighted in green.
Similarly we can change layer state using Hidden, Selectable, or Visible in
stateArray1(0).State = Layer.State.WorkLayer
instead of WorkLayer
We can now create the feature on the required layer. We will discuss feature creation in a separate
document. The block creation is highlighted in pink.
We have also included an undo mark. This is useful when creating any object within a program. If
undo’s are not included in the program then if a user, after executing the program, an undo will
undo not only the program creation but also one previous step prior to running the program. This is
always undesirable.
Program 3This program is used to report information about object on a layer. The reporting can
be done in a variety of ways. The program first lists all the objects on the current work layer. This
can easily to do the same for any other layer or for all layers. The other listing is to list the layer for
all solid bodies. The second list gets all features of the solid which are on the solid layer. We cannot
ask the layer number of features directly since the layer number is not directly available from the
feature rather from the solid that the feature belongs to.
Program Listing
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features
Module LayerProgramming3
Sub Main()
Dim s As Session = Session.GetSession()
Dim ui As UI = ui.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
lw.Open()
' We look only at the current work part
Dim workPart As Part = s.Parts.Work
' Get all objects on the work layer
Dim objs() As NXObject =
workPart.Layers.GetAllObjectsOnLayer(workPart.Layers.WorkLayer)
lw.WriteLine(objs.Length.ToString & " objects on layer: " &
workPart.Layers.WorkLayer.ToString)
' Now list all the objects
For Each b As NXObject In objs
lw.WriteLine(b.ToString)
Next
lw.WriteLine(vbCrLf)
' Another listing for each body in the work part we can list the
features and the body layer
For Each abody As Body In workPart.Bodies
For Each afeat As Feature In abody.GetFeatures
lw.WriteLine(afeat.GetFeatureName & " on layer : " &
abody.Layer.ToString)
Next
Next
End Sub
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
Getting all the objects on a layer is supported directly with a layers mehod. It is done in the program
using:
Dim objs() As NXObject =
workPart.Layers.GetAllObjectsOnLayer(workPart.Layers.WorkLayer)
We then loop through all the objects in objs to output these to the listing window. The relevant
code is displayed in yellow.
Next we look at all bodies and list the associated features and the body layer. We this with
obtaining a body collection in the work part using:
For Each abody As Body In workPart.Bodies
For Each afeat As Feature In abody.GetFeatures
lw.WriteLine(afeat.GetFeatureName & " on layer : " &
abody.Layer.ToString)
Next
Next
workPart.Bodies has all the bodies. abody.GetFeatures has all features for abody. Note we get the
layer number from the body and not from the feature. Again these methods can also be applied to a
display part and to components.
Appendix 1
NXInput Class
NXInputBox has the following Members:
GetInputNumber (prompt As String
) As
Double
Displays a simple text entry dialog to the user
and returns the number the user enters.
Prompt string to the user for the text entry
box.
GetInputNumber (prompt As String,
caption As String ) As Double
Displays a simple text entry dialog to the user
and returns the number the user enters.
Prompt string to the user for the text entry
box.
Caption string for the window title to the text
entry dialog.
GetInputNumber (prompt As String,
caption As String, initialText As
String ) As Double
Displays a simple text entry dialog to the user
and returns the number the user enters.
Prompt string to the user for the text entry
box.
Caption string for the window title to the text
entry dialog.
Initial text to use as default text in the text
entry box.
GetInputNumber ( prompt As String,
caption As String, initialNumber As
Double ) As Double
This initial text should form a valid double
number.
Displays a simple text entry dialog to the user
and returns the number the user enters.
Prompt string to the user for the text entry
box.
Caption string for the window title to the text
entry dialog.
Initial double to use as a default number in
the text entry box.
GetInputString and ParseInputNumber methods are also available.