Reference specific extrude

Hi All

Is it possible to assign a name to a feature, such a a simple extrude, in NX rather than referring to the object by it's arbitrary name given when creating the part? Below is some simple code used to change the colour of an extrude. I want to reference a specific name when finding the object rather than just "EXTRUDE(13)". What would be the best way of achieving this? Thanks in advance.

Private Sub cmdChangeColour_Click(sender As System.Object, e As System.EventArgs) Handles cmdChangeColour.Click
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Edit->Object Display...
' ----------------------------------------------
' ----------------------------------------------
' Dialog Begin Color
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = False
displayModification1.ApplyToOwningParts = False
displayModification1.NewColor = 136
displayModification1.NewWidth = DisplayableObject.ObjectWidth.One

Dim objects1(0) As DisplayableObject
Dim body1 As Body = CType(workPart.Bodies.FindObject("EXTRUDE(13)"), Body)

objects1(0) = body1
displayModification1.Apply(objects1)
displayModification1.Dispose()
' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub

I know of at least two ways to find an object by name.

1) Loop through the collection the object belongs to, checking the name of each against the desired name.
2) Use one of the following UF functions: UF_OBJ_cycle_by_name, UF_OBJ_cycle_by_name_and_type, or UF_OBJ_cycle_by_name_and_type_extended

If you go to the GTAC symptom/solution database and search for "delete all objects with a specified name" or "nx_api4920", you will find an example of using UF_OBJ_cycle_by_name. A quick way to get to the GTAC database is through the NX Help menu: Help -> online technical support -> PLM solutions symptom/solution database.

Could you please give an example of the code of this in use? I can't access the GTAC database as I am using a University license.

Thank you

The code below will look for a "feature" named "test". It shows two methods for finding the feature: iterating through the feature collection, and using the "cycle by object and type" function. Before running the code, right click on a feature in the work part and choose "rename", enter "test" as the new name. In my case, I chose to rename "Extrude(5)"; what you see in the part navigator after renaming a feature will depend on your NX settings. The default is to show both the feature name and any custom name given; in my case it looks like: Extrude(5) "test".

Option Strict Off
Imports System
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UF.UFSession = UF.UFSession.GetUFSession
If IsNothing(theSession.Parts.Work) 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 = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Const nameToFind As String = "test"

Dim test1 As Features.Feature
Dim test2 As Features.Feature

'method 1: iterate through the features collection
lw.WriteLine("find by name: iterate through collection")
Dim found1 As Boolean = False
For Each temp As Features.Feature In workPart.Features
If temp.Name = nameToFind Then
test1 = temp
found1 = True
Exit For
End If
Next

If found1 Then
lw.WriteLine("name: " & test1.Name)
lw.WriteLine("feature name: " & test1.GetFeatureName)
lw.WriteLine("feature type: " & test1.FeatureType)
lw.WriteLine("")

Else
lw.WriteLine("no feature named: " & nameToFind & " was found in the work part")

End If
lw.WriteLine("")

'method 2: use cycle object by name and type
lw.WriteLine("find by name: cycle by object and type")
Dim theTag As Tag = Tag.Null
Dim found2 As Boolean = False
Do
theUfSession.Obj.CycleByNameAndType(workPart.Tag, nameToFind, UF.UFConstants.UF_feature_type, False, theTag)
If Not theTag = Tag.Null Then
test2 = DirectCast(Utilities.NXObjectManager.Get(theTag), Features.Feature)
found2 = True
Exit Do
End If
Loop Until theTag = Tag.Null

If found2 Then
lw.WriteLine("name: " & test2.Name)
lw.WriteLine("feature name: " & test2.GetFeatureName)
lw.WriteLine("feature type: " & test2.FeatureType)
lw.WriteLine("")

Else
lw.WriteLine("no feature named: " & nameToFind & " was found in the work part")

End If

lw.Close()

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