Set Feature name to the selected object

Hi NX Journaling,

I want to assign the feature name to the object. I understand that to access the feature of the object, i need to do type casting or get the name of the feature by checking the associated / referenced feature of the object.

I tried doing type casting, but i get an error, "Object reference not set to an instance of an Object"

while using Ctype i get other error, "Unable to cast object of type NXOpen.Point to NXopen.Features.PointFeature"

Could you please help and modify the below code so that,
1.) i can pick the object from 3D space and get the feature name associated with the object to rename the selected object
2.) also as a next step i want to make this journal for multiple selections

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module RenameObj

Sub Main()
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
'Dim ufs As UFSession = Nothing
Dim lw As ListingWindow = theSession.ListingWindow

Dim objPoint As NXObject = SelectPoint("Report Object ID")

Do While objPoint IsNot Nothing
lw.Open()
Dim objName As String = Nothing
Dim fName As String = Nothing
'lw.WriteLine(".Name: " & fPoint.Name)
'Dim objPoint As NXOpen.TaggedObject = SelectPoint("Report Object ID")
Dim fPoint As NXOpen.Features.PointFeature = TryCast(objPoint, NXOpen.Features.PointFeature)
'Dim fPoint As NXOpen.Features.PointFeature = CType(objPoint, NXOpen.Features.PointFeature)
lw.Close()
fName = fPoint.GetFeatureName ' comment this line and uncomment the next line
'fName = fPoint.Name
lw.WriteLine(".Name: " & fName)
objPoint.SetName(fName)
'objPoint.SetName(objName & 2)
objPoint = SelectPoint("Report Object ID")
Loop
End Sub

Function SelectPoint(ByVal prompt As String) As NXObject

Dim selobj As NXObject = Nothing
Dim theUI As UI = UI.GetUI
Dim cursor As Point3d

Dim typeArray() As Selection.SelectionType =
{Selection.SelectionType.All,
Selection.SelectionType.Faces,
Selection.SelectionType.Edges}

Dim resp As Selection.Response = theUI.SelectionManager.SelectObject(
prompt, "Select an object",
Selection.SelectionScope.AnyInAssembly,
False, typeArray, selobj, cursor)

Return selobj
End Function

End Module

Thanks

Balaji

1) see my reply to your recent similar question:
http://nxjournaling.com/comment/5760#comment-5760

2) to select multiple objects see:
http://nxjournaling.com/content/using-mask-triples-selectobject-routine
This article explains the use of .SelectObject and .SelectObjects. These functions have since been deprecated in the API, but the replacements are very similar (.SelectTaggedObject and .SelectTaggedObjects). The replacement functions return Tagged objects instead of NXObjects, but otherwise, the use is identical.

Hello NX Journaling,

i found some macros for renaming the point objects with predefined prefix values. i tried making some changes to the existing macro and rename the point objects with the associated feature names. But im getting an error 'Tag' is not a member of 'System.collections.Generic.List(Of NXOpen.Point)'. Could you please help is resolving the error.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module CustomPointRename

'Const ptNamePrefix As String = "ABS"

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work

Dim ptsPoints As New List(Of Point)

If SelectPoints("Select points", ptsPoints) = Selection.Response.Cancel Then
Exit Sub
End If

Dim ptsCount As Integer = "0"

For i As Integer = 0 To ptsPoints.Count - 1
ptsCount += 1
'ptsPoints.Item(i).SetName(ptNamePrefix & ptsCount.ToString)

Dim fName As String = Nothing
Dim featureTag As Tag
theUfSession.Modl.AskObjectFeat(ptsPoints.Tag, featureTag)
Dim pointFeature As Features.Feature

'point associated to feature
pointFeature = Utilities.NXObjectManager.Get(featureTag)

fName = pointFeature.Name
ptsPoints.Item(i).SetName(fName)

Next

End Sub

Function SelectPoints(ByVal prompt As String, ByRef pointList As List(Of Point)) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select points"
Dim includeFeatures As Boolean = True
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim selObj() As TaggedObject
'Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_point_type
.Subtype = UFConstants.UF_all_subtype
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj)

If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

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

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly

End Function

End Module

Thanks, B

Balaji

theUfSession.Modl.AskObjectFeat(ptsPoints.Tag, featureTag)

The line above is the issue. You are trying to access the .Tag property of the list object, which doesn't have a .Tag property. Instead, you need to access the point object in the list.

theUfSession.Modl.AskObjectFeat(ptsPoints.Item(i).Tag, featureTag)

Hello NX Journaling,

I have made changes as your said. The journal is not throwing any error now. But it's not assigning the (feature)name to the point object.

Below is the updated code.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module CustomPointRename

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work

Dim ptsPoints As New List(Of Point)

If SelectPoints("Select points", ptsPoints) = Selection.Response.Cancel Then
Exit Sub
End If

Dim ptsCount As Integer = "0"

For i As Integer = 0 To ptsPoints.Count - 1
ptsCount += 1

Dim fName As String = Nothing
Dim featureTag As Tag
theUfSession.Modl.AskObjectFeat(ptsPoints.Item(i).Tag, featureTag)
Dim pointFeature As Features.Feature

'point associated to feature
pointFeature = Utilities.NXObjectManager.Get(featureTag)

fName = pointFeature.Name
ptsPoints.Item(i).SetName(fName)

Next

End Sub

Function SelectPoints(ByVal prompt As String, ByRef pointList As List(Of Point)) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select points"
Dim includeFeatures As Boolean = True
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim selObj() As TaggedObject
'Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_point_type
.Subtype = UFConstants.UF_all_subtype
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj)

If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

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

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly

End Function

End Module

Thanks, B

Balaji

Upon further review, there were a number of issues with the code as written.

  • the SelectPoints function was looking for the wrong responses from the .SelectTaggedObjects function, which resulted in it always returning "Cancel"
  • the selected points were never added to the list object created to hold them
  • the "includeFeatures" option of the selection function was set to TRUE, which allowed the point feature (instead of the point object) to be selected
  • the "ptsCount" variable is declared as an integer, but given a string value. I don't think this caused any issue as it was unused in the code, but I'm a bit surprised the compiler didn't complain.

I don't guarantee the following code to be error free, but it seems to do what you want.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module CustomPointRename

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

Sub Main()

lw.open
Dim workPart As Part = theSession.Parts.Work

Dim ptsPoints As New List(Of Point)

If SelectPoints("Select points", ptsPoints) = Selection.Response.Cancel Then
Exit Sub
End If

Dim ptsCount As Integer = 0

For i As Integer = 0 To ptsPoints.Count - 1
ptsCount += 1

Dim fName As String = Nothing
Dim featureTag As Tag
theUfSession.Modl.AskObjectFeat(ptsPoints.Item(i).Tag, featureTag)
Dim pointFeature As Features.Feature

'point associated to feature
pointFeature = Utilities.NXObjectManager.Get(featureTag)

fName = pointFeature.Name
ptsPoints.Item(i).SetName(fName)

Next

End Sub

Function SelectPoints(ByVal prompt As String, ByRef pointList As List(Of Point)) As Selection.Response

pointList.Clear()
Dim theUI As UI = ui.GetUI
Dim title As String = "Select points"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim selObj() As TaggedObject
'Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_point_type
.Subtype = UFConstants.UF_point_subtype
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt,
title, scope, selAction,
includeFeatures, keepHighlighted, selectionMask_array,
selObj)

If resp = Selection.Response.Ok Then
For Each ptObj As Point In selObj
pointList.Add(ptObj)
Next
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

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

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly

End Function

End Module

Hello NX Journaling, thanks a lot for the updated code. This is what i wanted the code to do. I just realized that i have set the boolean value as TRUE and the points were never added to the list. I wouldn't have figured out other issues without your support. Thanks a lot :):)

Balaji