Need help writing a VB jounal to pick a point within a component on a a drawing and returning the XYZ point and comp name

I have pieced together a journal to do what I need, but I'm unable to get the component name that I picked the point from.

Any help with this matter would be greatly appreciated. (See journal below)

Thank you,

Option Strict Off
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Threading
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Utilities

Module Comp_Name_xyzposition
Dim s As Session = Session.GetSession()
Dim ui As UI = ui.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim wp As Part = s.Parts.Work()

Sub Main()
Dim mode1() As Integer = {0, 0}
Dim pointDisplayMode As Integer = 0
Dim response1 As Integer = Nothing
Dim objectpoint(2) As Double
Dim pnt1(1) As Double
Dim text1(3) As String
Dim cursor As Point3d
Dim m1 As Session.UndoMarkId = s.SetUndoMark(Session.MarkVisibility.Visible, "M1")
wp.Preferences.ObjectPreferences.SetWidth(Preferences.PartObject.ObjectType.General, Preferences.PartObject.WidthType.ThinWidth)

Dim dwgview As Drawings.DraftingView = Nothing
select_a_drawing_member_view(dwgview)

Start0:
' Get number of decimal places to also determine xyz lablel size
Dim decimalplaces As Integer = 4
decimalplaces = NXInputBox.GetInputNumber("Enter Number of Decimal Places", "XYZ Labels", decimalplaces)

Start1:
dwgview.Expand()
ufs.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
response1 = ufs.Ui.PointSubfunction("Select Point for Label", mode1, pointDisplayMode, objectpoint)
ufs.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
wp.Views.UnexpandWork()
If response1 = 1 Then GoTo Start0
If response1 = 2 Then GoTo End1

Dim response2 As Selection.DialogResponse = SelectScreenPos(cursor)
If response2 <> Selection.DialogResponse.Pick Then
Return
End If

ufs.View.MapModelToDrawing(dwgview.Tag, objectpoint, pnt1)

' ----------------------------------------------
' Insert->Note...
' ----------------------------------------------

Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing

Dim draftingNoteBuilder1 As Annotations.DraftingNoteBuilder
draftingNoteBuilder1 = wp.Annotations.CreateDraftingNoteBuilder(nullAnnotations_SimpleDraftingAid)

text1(0) = "Component name"
text1(1) = "X " + objectpoint(0).ToString("F" & decimalplaces.ToString)
text1(2) = "Y " + objectpoint(1).ToString("F" & decimalplaces.ToString)
text1(3) = "Z " + objectpoint(2).ToString("F" & decimalplaces.ToString)

draftingNoteBuilder1.Text.TextBlock.SetText(text1)

draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)

Dim leaderData1 As Annotations.LeaderData
leaderData1 = wp.Annotations.CreateLeaderData()

draftingNoteBuilder1.Leader.Leaders.Append(leaderData1)

Dim coordinates1 As Point3d = New Point3d(pnt1(0), pnt1(1), 0.0)
Dim point1 As Point
point1 = wp.Points.CreatePoint(coordinates1)

Dim assocOrigin1 As Annotations.Annotation.AssociativeOriginData
Dim nullView As View = Nothing

leaderData1.Leader.SetValue(point1, nullview, nothing)

Dim point2 As Point3d = New Point3d(cursor.X, cursor.Y, 0.0)
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullView, point2)

Dim nXObject1 As NXObject
nXObject1 = draftingNoteBuilder1.Commit()

draftingNoteBuilder1.Destroy()

GoTo Start1
End1:
wp.Preferences.ObjectPreferences.SetWidth(Preferences.PartObject.ObjectType.General, Preferences.PartObject.WidthType.NormalWidth)
End Sub

Public Function SelectScreenPos(ByRef pos As Point3d) As Selection.DialogResponse

Dim view As NXOpen.View = Nothing
Dim letteringPrefs As LetteringPreferences = Nothing
Dim userSymPrefs As UserSymbolPreferences = Nothing

Return ui.SelectionManager.SelectScreenPosition("Select XYZ Label Position", view, pos)

End Function

Public Function GetTextParameters(ByRef textheight As Double, _
ByRef textaspectratio As Double, _
ByRef textgapfactor As Double) As Boolean
Dim mpr_array(69) As Double
Dim mpi_array(99) As Integer
Dim rad_symbol As String = ""
Dim dia_symbol As String = ""
ufs.Drf.AskPreferences(mpi_array, mpr_array, rad_symbol, dia_symbol)
textheight = mpr_array(44)
textaspectratio = mpr_array(45)
textgapfactor = mpr_array(46)
Return True
End Function

Public Function GetMaximumLength(ByVal lengthx, ByVal lengthy, ByVal lengthz) As Integer
Dim maxvalue As Integer = 0
If lengthx > lengthy Then
maxvalue = lengthx
Else
maxvalue = lengthy
End If
If maxvalue < lengthz Then
maxvalue = lengthz
End If
Return maxvalue
End Function

Public Sub CreateGroup(ByVal lns() As Line, _
ByVal notes() As Note, _
ByVal counter As Integer)
Dim nullGroup As Group = Nothing

Dim groupBuilder1 As GroupBuilder
groupBuilder1 = wp.CreateGatewayGroupBuilder(nullGroup)
groupBuilder1.ActivegroupOption = True
groupBuilder1.ActionType = 0
groupBuilder1.GroupName = "XYZ_LABEL_" & counter.ToString
Dim added1 As Boolean
For i As Integer = 0 To lns.Length - 1
added1 = groupBuilder1.ObjectsInGroup.Add(lns(i))
Next
For i As Integer = 0 To notes.Length - 1
added1 = groupBuilder1.ObjectsInGroup.Add(notes(i))
Next
Dim nXObject1 As NXObject
nXObject1 = groupBuilder1.Commit()
groupBuilder1.Destroy()
End Sub

Public Function InsertNote(ByVal note1 As String, ByVal point1 As Point3d) As Note
Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing
Dim draftingNoteBuilder1 As Annotations.DraftingNoteBuilder
draftingNoteBuilder1 = wp.Annotations.CreateDraftingNoteBuilder(nullAnnotations_SimpleDraftingAid)
draftingNoteBuilder1.Origin.Plane.PlaneMethod = Annotations.PlaneBuilder.PlaneMethodType.XyPlane
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(False)
draftingNoteBuilder1.Origin.Anchor = Annotations.OriginBuilder.AlignmentPosition.BottomLeft
Dim text1(0) As String
text1(0) = note1
draftingNoteBuilder1.Text.TextBlock.SetText(text1)
Dim nullView As View = Nothing
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullView, point1)
Dim nXObject1 As NXObject
nXObject1 = draftingNoteBuilder1.Commit()
draftingNoteBuilder1.Destroy()
Return nXObject1
End Function

Function select_a_drawing_member_view(ByRef dwgview As Drawings.DraftingView)

Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_view_type
.Subtype = UFConstants.UF_all_subtype
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
Dim vw As View = Nothing

Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Select a drawing member view", _
"Select a drawing member view", _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, vw, cursor)

If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
dwgview = CType(vw, Drawings.DraftingView)

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 immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Do you wish to pick "hard" points on the component (edge end point, edge centerpoint, etc) or "soft" points such as a point on the surface of the visible component?

I'm not sure there is a selection function that will return all of your desired information in 1 pick. Is there an equivalent command in interactive NX that you would like to mimic?

Perhaps add another selection that will return the desired component name. I realize this is a bit of a clumsy workaround...

This concept is exactly what I need. However when I use this one the leader snaps to 0,0,0 of the drawing sheet instead of the point. Anyone know what I need to do to get the leader to snap to the point?

I've been playing around with the code a bit and this version seems to fix the leader position issue.





Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpenUI
Imports NXOpen.UF

Module Comp_Name_xyzposition
Dim s As Session = Session.GetSession()
Dim ui As UI = ui.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim wp As Part = s.Parts.Work()

Sub Main()
Dim mode1() As Integer = {0, 0}
Dim pointDisplayMode As Integer = 0
Dim response1 As Integer = Nothing
Dim objectpoint(2) As Double
Dim text1(3) As String
Dim cursor As Point3d
Dim m1 As Session.UndoMarkId = s.SetUndoMark(Session.MarkVisibility.Visible, "M1")
wp.Preferences.ObjectPreferences.SetWidth(Preferences.PartObject.ObjectType.General, Preferences.PartObject.WidthType.ThinWidth)

Dim dwgview As Drawings.DraftingView = Nothing
select_a_drawing_member_view(dwgview)

Start0:
' Get number of decimal places to also determine xyz lablel size
Dim decimalplaces As Integer = 4
decimalplaces = NXInputBox.GetInputNumber("Enter Number of Decimal Places", "XYZ Labels", decimalplaces)

Start1:
dwgview.Expand()
ufs.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
response1 = ufs.Ui.PointSubfunction("Select Point for Label", mode1, pointDisplayMode, objectpoint)
ufs.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
wp.Views.UnexpandWork()
If response1 = 1 Then GoTo Start0
If response1 = 2 Then GoTo End1

Dim response2 As Selection.DialogResponse = SelectScreenPos(cursor)
If response2 <> Selection.DialogResponse.Pick Then
Return
End If

' ----------------------------------------------
' Insert->Note...
' ----------------------------------------------

Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing

Dim draftingNoteBuilder1 As Annotations.DraftingNoteBuilder
draftingNoteBuilder1 = wp.Annotations.CreateDraftingNoteBuilder(nullAnnotations_SimpleDraftingAid)

text1(0) = "Component name"
text1(1) = "X " + objectpoint(0).ToString("F" & decimalplaces.ToString)
text1(2) = "Y " + objectpoint(1).ToString("F" & decimalplaces.ToString)
text1(3) = "Z " + objectpoint(2).ToString("F" & decimalplaces.ToString)

draftingNoteBuilder1.Text.TextBlock.SetText(text1)

draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)

Dim leaderData1 As Annotations.LeaderData
leaderData1 = wp.Annotations.CreateLeaderData()

draftingNoteBuilder1.Leader.Leaders.Append(leaderData1)

Dim coordinates1 As Point3d = New Point3d(objectpoint(0), objectpoint(1), objectpoint(2))

Dim assocOrigin1 As Annotations.Annotation.AssociativeOriginData
Dim nullView As View = Nothing

leaderData1.Leader.SetValue(Nothing, dwgview, coordinates1)

Dim point2 As Point3d = New Point3d(cursor.X, cursor.Y, 0.0)
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullView, point2)

Dim nXObject1 As NXObject
nXObject1 = draftingNoteBuilder1.Commit()

draftingNoteBuilder1.Destroy()

GoTo Start1
End1:
wp.Preferences.ObjectPreferences.SetWidth(Preferences.PartObject.ObjectType.General, Preferences.PartObject.WidthType.NormalWidth)
End Sub

Public Function SelectScreenPos(ByRef pos As Point3d) As Selection.DialogResponse

Dim view As NXOpen.View = Nothing
Dim letteringPrefs As LetteringPreferences = Nothing
Dim userSymPrefs As UserSymbolPreferences = Nothing

Return ui.SelectionManager.SelectScreenPosition("Select XYZ Label Position", view, pos)

End Function

Public Function GetTextParameters(ByRef textheight As Double, _
ByRef textaspectratio As Double, _
ByRef textgapfactor As Double) As Boolean
Dim mpr_array(69) As Double
Dim mpi_array(99) As Integer
Dim rad_symbol As String = ""
Dim dia_symbol As String = ""
ufs.Drf.AskPreferences(mpi_array, mpr_array, rad_symbol, dia_symbol)
textheight = mpr_array(44)
textaspectratio = mpr_array(45)
textgapfactor = mpr_array(46)
Return True
End Function

Public Function GetMaximumLength(ByVal lengthx, ByVal lengthy, ByVal lengthz) As Integer
Dim maxvalue As Integer = 0
If lengthx > lengthy Then
maxvalue = lengthx
Else
maxvalue = lengthy
End If
If maxvalue < lengthz Then
maxvalue = lengthz
End If
Return maxvalue
End Function

Public Sub CreateGroup(ByVal lns() As Line, _
ByVal notes() As Note, _
ByVal counter As Integer)
Dim nullGroup As Group = Nothing

Dim groupBuilder1 As GroupBuilder
groupBuilder1 = wp.CreateGatewayGroupBuilder(nullGroup)
groupBuilder1.ActivegroupOption = True
groupBuilder1.ActionType = 0
groupBuilder1.GroupName = "XYZ_LABEL_" & counter.ToString
Dim added1 As Boolean
For i As Integer = 0 To lns.Length - 1
added1 = groupBuilder1.ObjectsInGroup.Add(lns(i))
Next
For i As Integer = 0 To notes.Length - 1
added1 = groupBuilder1.ObjectsInGroup.Add(notes(i))
Next
Dim nXObject1 As NXObject
nXObject1 = groupBuilder1.Commit()
groupBuilder1.Destroy()
End Sub

Public Function InsertNote(ByVal note1 As String, ByVal point1 As Point3d) As Note
Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing
Dim draftingNoteBuilder1 As Annotations.DraftingNoteBuilder
draftingNoteBuilder1 = wp.Annotations.CreateDraftingNoteBuilder(nullAnnotations_SimpleDraftingAid)
draftingNoteBuilder1.Origin.Plane.PlaneMethod = Annotations.PlaneBuilder.PlaneMethodType.XyPlane
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(False)
draftingNoteBuilder1.Origin.Anchor = Annotations.OriginBuilder.AlignmentPosition.BottomLeft
Dim text1(0) As String
text1(0) = note1
draftingNoteBuilder1.Text.TextBlock.SetText(text1)
Dim nullView As View = Nothing
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullView, point1)
Dim nXObject1 As NXObject
nXObject1 = draftingNoteBuilder1.Commit()
draftingNoteBuilder1.Destroy()
Return nXObject1
End Function

Function select_a_drawing_member_view(ByRef dwgview As Drawings.DraftingView)

Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_view_type
.Subtype = UFConstants.UF_all_subtype
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
Dim vw As View = Nothing

Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Select a drawing member view", _
"Select a drawing member view", _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, vw, cursor)

If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
dwgview = CType(vw, Drawings.DraftingView)

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 immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module





edit: leaders now point to correct point in iso/trimetric views.

YOU just made my week! Thank you so much! Works great now!
There's no way to pull the point name into that is there?

It will work on a normal view, top, side, etc. but not on ISO views. Any idea why that would be?

The Z component of the point was being zeroed out in the previous code. I have updated the code above to fix this, rather than post another long code listing.

Copy & paste the code above and try it out.

Thank you so much! It works like a charm in any view now. :)