Layer settings


i have to create new layers in many existing parts

someone could help me to create a journal to make this ?

i have the list of the layers, but i don't know how to import in my existig part


Are you talking about creating layer categories, changing the layer status (work layer, visible only, etc) or both?

You say that you "have a list of the layers" - in what form do you have this list (a text file, a spreadsheet, an existing part file, etc)?

Creating a layer category is fairly easy, see the following thread for a simple example:

I'm talking about creating layer categories.

I have a text file but also an existing part file with the correct layers

Can you post an example entry of your text file? If it has a well-defined structure (such as a CSV file), we can read the text file, parse out the values and create the layer categories in the NX part file.

Alternately, we could read the example file and re-create the layer categories in the target file.

Do you have a preference?

This is my text file, i think it's not correct as a CSV file, but is simple to create from this ..

1 Axis
2-24 Mold inserts
25-49 Mold frames
50-59 Vents
60-69 Cooling inserts
70-79 Cooling lines
80-89 Ejector Plate and pins
100-119 Cores
120-139 Core box inserts
140-149 Core box frames
150-159 Blow tubes
160-169 Hydraulic or mechanic movers
170-179 NK’s
200-209 Ejection pins
210-219 Screws, washers and bolts
220-250 Sketches
231-250 Construction geometry
251-256 WNC models and transition surfaces.

I think it's better from a file, so I can simply change the file as needed

Anyway the better way is the simplest way !

The code below will prompt for a text file and attempt to parse the information and create layer categories in the current work part. The input file needs to have the following formatting:
{layer|layer range}:{category name}
So the input would be very similar to what is shown in your post above (note that there needs to be a colon (:) between the layer(s) and category name.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
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, "layer categories from file")
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim line As String
            Dim delim As Char() = {":"c}
            Dim layerList As New List(Of Integer)
            Using sr As IO.StreamReader = New IO.StreamReader(openFileDialog1.FileName)
                'line = sr.ReadLine()
                'Do While Not line Is Nothing
                    line = sr.ReadLine()
                    If IsNothing(line) Then
                        Exit Do
                    End If
                    If Not line.Contains(delim) Then
                        Continue Do
                    End If
                    Dim strings As String() = line.Split(delim)
                    'strings(0) = category layer range
                    'strings(1) = category name
                    Dim layers() As String = Nothing
                    Dim layerRangeStart As Integer = Nothing
                    Dim layerRangeEnd As Integer = Nothing
                    If strings(0).Contains("-") Then
                        'string represents a range of layers
                        'e.g. "220-250: Sketches"
                        layers = strings(0).Split("-"c)
                        Dim rangeParsed As Boolean = False
                        If Not Integer.TryParse(layers(0), layerRangeStart) Then
                            'could not parse string to integer, skip this entry
                            Continue Do
                        End If
                        If Not Integer.TryParse(layers(1), layerRangeEnd) Then
                            'could not parse string to integer, skip this entry
                            Continue Do
                        End If
                        For i As Integer = layerRangeStart To layerRangeEnd
                        'string represents a single layer
                        'e.g. "1: Solid"
                        If Not Integer.TryParse(strings(0), layerRangeStart) Then
                            'could not parse string to integer
                            Continue Do
                        End If
                    End If
                    'strip potentially problematic characters from the layer category name
                    Dim layerCatName As String = strings(1).Trim
                    layerCatName = Regex.Replace(layerCatName, " ", "_")
                    layerCatName = Regex.Replace(layerCatName, "\W", "")
                    If String.IsNullOrWhiteSpace(layerCatName) Then
                        'layer name cannot be an empty string
                        Continue Do
                    End If
                    'create the layer category
                    Dim category1 As Layer.Category
                    category1 = theSession.Parts.Work.LayerCategories.CreateCategory(layerCatName, "", layerList.ToArray)
                    'line = sr.ReadLine()
                Loop Until line Is Nothing
            End Using
        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


I need to know what is the WorkLayer in a part.

I use this code from GTAC (nx_api3102) but he dosn't work.

You can help me ?


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module demo_different_enum_values_for_layer_status

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

Sub Main()

Echo("UF Session Values: ")


Echo(" ")

Echo("Session Values: ")

Echo("NXOpen.Layer.State.WorkLayer: " & _

End Sub

Sub Echo(ByVal output As String)


End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

Return Session.LibraryUnloadOption.Immediately

End Function

End Module


The GTAC code is simply printing out the value of previously defined constants; it is not looking at the current work layer. It is working correctly, it is simply not designed to do what you are looking for.

The code below will display the work part and work layer information to the listing window.

Option Strict Off
Imports System
Imports NXOpen
Module Module73
    Dim theSession As Session = Session.GetSession()
    Dim lw As ListingWindow = theSession.ListingWindow
    Sub Main()
        lw.WriteLine("work part: " & theSession.Parts.Work.FullPath)
        lw.WriteLine("work layer: " & theSession.Parts.Work.Layers.WorkLayer.ToString)
    End Sub
End Module