Auto label creation for Drafting Points

Hello All,

Below journal creates label for the visible drafting points as the same name as that of the point objects.

Label positioning is based on the cursor position selection.
Could someone help with the positioning of the point labels based on the WCS.

' NX 12.0.1.7
' Journal created by Balaji_Sampath03 on Mon Jun 29 20:50:40 2020 India Standard Time

'**************************************************************************************
'This Macro creates the Point Label for all visible drafting points in the view
'All the points are labeled based on the 3D point object names
'This macro does not have any exception to catch any errors and is not tested extensively
'Location of the point labels are selected by the cursor picking a position on the screen
' Updated the code with the option to remove the numbers from the Point names
'***************************************************************************************

Option Strict Off

Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Annotations
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Text.RegularExpressions

Module ViewDepEdit

Sub Main (ByVal args() As String)

Dim theUI As UI = UI.GetUI()
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim theDraftView As Drawings.DraftingView
Dim ptsPoints As New List(Of Point)
Dim ptsNames As New List(Of String)
Dim ptsCount As Integer = "0"
Dim tempName As String = Nothing

Dim lw As ListingWindow = theSession.ListingWindow

Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Auto Label Points")

Do Until SelDraftingView("select drafting view", theDraftView) = Selection.Response.Cancel

'''''''''''''''''''''''''''''''''
'Get a location for Point label
'''''''''''''''''''''''''''''''''
Dim cursor As Point3d
Dim response As Selection.DialogResponse = SelectScreenPos(cursor)
If response <> Selection.DialogResponse.Pick Then
Return
End If

For Each tempObj As DisplayableObject In theDraftView.AskVisibleObjects
If TypeOf tempObj Is Point Then
ptsPoints.Add(tempObj)
tempName = tempObj.Name
ptsNames.Add(tempName)
tempObj.Highlight
End If
Next

' Ask to remove numbers from String
'----------------------------------

Dim answer As Integer = theUI.NXMessageBox.Show("Keep / Remove Numbers",
NXOpen.NXMessageBox.DialogType.Question, "Remove numbers from Point Names?")

If answer = 1 Then
'ptsNames.Clear()
For i As Integer = 0 to ptsPoints.Count -1
ptsCount += 1
ptsNames.Item(i) = Regex.Replace(ptsNames.Item(i), "[\d-]", String.Empty)
ptsNames.Add(ptsNames.Item(i))
Next
End If

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

Dim nullNXOpen_Annotations_SimpleDraftingAid As NXOpen.Annotations.SimpleDraftingAid = Nothing
Dim draftingNoteBuilder1 As NXOpen.Annotations.DraftingNoteBuilder = Nothing
Dim leaderData1 As NXOpen.Annotations.LeaderData = Nothing
Dim symbolscale1 As Double = Nothing
Dim symbolaspectratio1 As Double = Nothing

Dim text1(0) As String
text1(0) = ptsNames.Item(i)

draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(nullNXOpen_Annotations_SimpleDraftingAid)
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)
draftingNoteBuilder1.Origin.Anchor = NXOpen.Annotations.OriginBuilder.AlignmentPosition.MidCenter
draftingNoteBuilder1.Text.TextBlock.SetText(text1)
draftingNoteBuilder1.TextAlignment = NXOpen.Annotations.DraftingNoteBuilder.TextAlign.Belowbottom
draftingNoteBuilder1.Origin.Plane.PlaneMethod = NXOpen.Annotations.PlaneBuilder.PlaneMethodType.XyPlane
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)

leaderData1 = workPart.Annotations.CreateLeaderData()
leaderData1.StubSize = 5.0
leaderData1.Arrowhead = NXOpen.Annotations.LeaderData.ArrowheadType.FilledArrow
leaderData1.VerticalAttachment = NXOpen.Annotations.LeaderVerticalAttachment.Center

draftingNoteBuilder1.Leader.Leaders.Append(leaderData1)

leaderData1.StubSide = NXOpen.Annotations.LeaderSide.Inferred
leaderData1.StubSize = 2

symbolscale1 = draftingNoteBuilder1.Text.TextBlock.SymbolScale
symbolaspectratio1 = draftingNoteBuilder1.Text.TextBlock.SymbolAspectRatio

Dim baseView1 As NXOpen.Drawings.DraftingView = theDraftView
Dim draftingPoint1 As NXOpen.Drawings.DraftingPoint = ptsPoints.Item(i)

Dim point1_1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
point1_1 = ptsPoints(i).Coordinates
Dim point2_1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
point2_1 = ptsPoints(i).Coordinates

Dim nullNXOpen_View As NXOpen.View = Nothing
leaderData1.Leader.SetValue(NXOpen.InferSnapType.SnapType.Exist, draftingPoint1, baseView1, point1_1, Nothing, nullNXOpen_View, point2_1)

'Jog position relocated or jog removed
leaderData1.Jogs.Clear()

Dim assocOrigin1 As NXOpen.Annotations.Annotation.AssociativeOriginData = Nothing
assocOrigin1.OriginType = NXOpen.Annotations.AssociativeOriginType.Drag
assocOrigin1.View = nullNXOpen_View
assocOrigin1.ViewOfGeometry = nullNXOpen_View
Dim nullNXOpen_Point As NXOpen.Point = Nothing

assocOrigin1.PointOnGeometry = nullNXOpen_Point
Dim nullNXOpen_Annotations_Annotation As NXOpen.Annotations.Annotation = Nothing

assocOrigin1.VertAnnotation = nullNXOpen_Annotations_Annotation
assocOrigin1.HorizAnnotation = nullNXOpen_Annotations_Annotation
assocOrigin1.AlignedAnnotation = nullNXOpen_Annotations_Annotation
assocOrigin1.VertAlignmentPosition = NXOpen.Annotations.AlignmentPosition.TopLeft
assocOrigin1.HorizAlignmentPosition = NXOpen.Annotations.AlignmentPosition.TopLeft
assocOrigin1.DimensionLine = 0
assocOrigin1.AssociatedView = nullNXOpen_View
assocOrigin1.AssociatedPoint = ptsPoints(i)
assocOrigin1.OffsetAnnotation = nullNXOpen_Annotations_Annotation
assocOrigin1.OffsetAlignmentPosition = NXOpen.Annotations.AlignmentPosition.TopLeft
assocOrigin1.XOffsetFactor = 0.0
assocOrigin1.YOffsetFactor = 0.0
assocOrigin1.StackAlignmentPosition = NXOpen.Annotations.StackAlignmentPosition.Above

Dim label_ABS_Loc As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
label_ABS_Loc = ptsPoints(i).Coordinates

'*******************
'Convert ABS to WCS
'*******************

'Dim label_WCS_Loc As NXOpen.Point3d = Nothing
'Dim pt1(2), pt2(2) As Double

'pt1(0) = ptsPoints(i).Coordinates.X
'pt1(1) = ptsPoints(i).Coordinates.Y
'pt1(2) = ptsPoints(i).Coordinates.Z

'theUfSession.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_COORDS, pt1, _
' UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt2)

'label_WCS_Loc.X = pt2(0)
'label_WCS_Loc.Y = pt2(1)
'label_WCS_Loc.Z = pt2(2)

'*****************************************
'Select Label position upon Cursor picking
'*****************************************

draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullNXOpen_View, cursor)
'draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullNXOpen_View, label_ABS_Loc)

draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)
leaderData1.StubSide = NXOpen.Annotations.LeaderSide.Right

Dim nXObject1 As NXOpen.NXObject = Nothing

nXObject1 = draftingNoteBuilder1.Commit()
draftingNoteBuilder1.Destroy()

'theSession.DeleteUndoMark(markId1, "Auto Label Points")

Next

ptsPoints.Clear()
ptsNames.Clear()

Loop

'********************
'Clear Selection List
'********************
Dim partCleanup As NXOpen.PartCleanup = Nothing
partCleanup = theSession.NewPartCleanup()
partCleanup.TurnOffHighlighting = True
partCleanup.DoCleanup()
partCleanup.Dispose()

End Sub

Function SelDraftingView(ByVal prompt As String, ByRef selView As Drawings.DraftingView) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a drafting view"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple
Dim selObj As TaggedObject

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

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selObj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
selView = CType(selObj, Drawings.DraftingView)
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Public Function SelectScreenPos(ByRef pos As Point3d) As Selection.DialogResponse
Dim theUI As UI = UI.GetUI()
Dim view As NXOpen.View = Nothing
Dim letteringPrefs As LetteringPreferences = Nothing
Dim userSymPrefs As UserSymbolPreferences = Nothing
Return theUI.SelectionManager.SelectScreenPosition("Select location for tabnote", view, pos)
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

I don't have time to step through all your code right now, but it looks very similar to some code that I borrowed (thanks, Frank Swinks) and modified a while back. The code below allows you to select points from a drafting view and labels the coordinate position with respect to the absolute coordinate system. This code could be modified to report the WCS coordinates (looks like you have code commented out that might work). Your code uses the drafting point object directly; I'm not sure there is a way to get WCS coordinates from the drafting points, because they only exist in the drafting view...

'NXJournaling.com
'March 23, 2017

'Create a label with coordinates to a point object selected in a drafting view.
'https://community.plm.automation.siemens.com/t5/NX-Programming-Customization/Scaling-of-leader-text-in-drawing/m-p/398810#M8018

'based, in part, on code by Frank Swinks
'http://www.eng-tips.com/viewthread.cfm?qid=350228

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpenUI
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, "NXJ")

lw.Open()

Dim workPart As Part = theSession.Parts.Work

Dim pointTag As NXOpen.Tag
Dim myPoint As Point
Dim cursor As Point3d

Dim cursorView As Integer
theUfSession.Ui.AskCursorView(cursorView)
theUfSession.Ui.SetCursorView(0)

Dim dwgview As Drawings.DraftingView = Nothing
Dim dwgViewTag As Tag = Tag.Null

If select_a_point(pointTag, dwgViewTag) = Selection.Response.Cancel Then
Return
End If

SelectScreenPos(cursor)

dwgview = Utilities.NXObjectManager.Get(dwgViewTag)

theUfSession.Disp.SetHighlight(pointTag, 0)
myPoint = Utilities.NXObjectManager.Get(pointTag)
'lw.WriteLine(myPoint.Coordinates.X & "," & myPoint.Coordinates.Y & "," & myPoint.Coordinates.Z)

theUfSession.Ui.SetCursorView(cursorView)

Dim text1(2) As String

Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing

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

text1(0) = "X " + myPoint.Coordinates.X.ToString("F3")
text1(1) = "Y " + myPoint.Coordinates.Y.ToString("F3")
text1(2) = "Z " + myPoint.Coordinates.Z.ToString("F3")

draftingNoteBuilder1.Text.TextBlock.SetText(text1)

draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)

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

draftingNoteBuilder1.Leader.Leaders.Append(leaderData1)

Dim nullView As View = Nothing

leaderData1.Leader.SetValue(myPoint, dwgview, myPoint.Coordinates)

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()

lw.Close()

End Sub

Function select_a_point(ByRef point As NXOpen.Tag, ByRef viewTag As NXOpen.Tag) As Selection.Response

Dim message As String = "Point:"
Dim title As String = "Select a Point"
Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
Dim response As Integer
'Dim viewTag As NXOpen.Tag
Dim cursor(2) As Double
Dim sel_mask As UFUi.SelInitFnT = AddressOf mask_for_points

theUfSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

Try
theUfSession.Ui.SelectWithSingleDialog(message, title, scope, sel_mask,
Nothing, response, point, cursor, viewTag)
Finally
theUfSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
End Try

If response <> UFConstants.UF_UI_OBJECT_SELECTED And
response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
Return Selection.Response.Cancel
Else
Return Selection.Response.Ok
End If

End Function

Function mask_for_points(ByVal select_ As IntPtr,
ByVal userdata As IntPtr) As Integer

Dim num_triples As Integer = 1
Dim mask_triples(0) As UFUi.Mask

mask_triples(0).object_type = UFConstants.UF_point_type
mask_triples(0).object_subtype = 0
mask_triples(0).solid_type = 0

theUfSession.Ui.SetSelMask(select_,
UFUi.SelMaskAction.SelMaskClearAndEnableSpecific,
num_triples, mask_triples)
Return UFConstants.UF_UI_SEL_SUCCESS

End Function

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

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

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

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

Hi NXJournaling, To position the drafting labels w.r.t the drafting points i tried using Label Editing -> Origin tool -> Point constructor option and picked the point attached to the label leader. I'am getting the point3D coordinates for the selected point which i'am not sure about the reference axis for this point. I have posted the edited code below. Could you please help in finding out the reference axis system.

' NX 12.0.1.7
' Journal created by Balaji_Sampath03 on Wed Jul 22 17:35:14 2020 India Standard Time
'
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

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 markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Adjust Label Position")

Dim label1 As NXOpen.Annotations.Label = CType(workPart.FindObject("ENTITY 25 1 1"), NXOpen.Annotations.Label)

Dim draftingNoteBuilder1 As NXOpen.Annotations.DraftingNoteBuilder = Nothing
draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(label1)

Dim assocOrigin1 As NXOpen.Annotations.Annotation.AssociativeOriginData = Nothing
draftingNoteBuilder1.Origin.SetAssociativeOrigin(assocOrigin1)

Dim labelLoc As NXOpen.Point3d = New NXOpen.Point3d(369.6027491078122, 234.55430128547872, 0.0)
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, assocOrigin1.ViewOfGeometry, labelLoc)
'draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = draftingNoteBuilder1.Commit()

draftingNoteBuilder1.Destroy()

End Sub
End Module

Balaji

When working in the drafting application, you can place drafting objects (views, notes, dimensions, etc); the coordinates of these objects are saved w.r.t. the sheet coordinate system. The sheet coordinate system origin is located at the bottom left corner of the sheet and runs the length and height of the sheet (all coordinates are positive). The coordinates you see will be sheet coordinates unless you expanded the drafting view or otherwise set the selection function option to select within the view itself.

hi, Thanks for your quick answer. How do i find out the point location from the drafting coordinate system. I intend to position all the labels in a view w.r.t the points. Do we have a method of finding it ..?? Thanks, B

Balaji

There is a method: "MapModelToDrawing" in the API that will convert model coordinates to paper coordinates given a position in the model space and a drafting view on the sheet.

Hi, thanks a lot. By the way do you have any sample code that uses this method. Where can I study all this methods in the API. I tried searching a lot but I couldn’t find a proper material that describes all the functions used in the API.

Balaji

The code below makes use of the MapModelToDrawing function. The eng-tips link gives more context of what the overall journal is designed to do.


'NXJournaling.com
'November 13, 2019

'Add labels (note with a leader) to selected component.
'The label includes the "TOOL_ID" attribute for the selected component.
'The label includes the "DB_PART_NAME" if the component's "TOOL_CLASS" attribute equals "COMM".

'https://www.eng-tips.com/viewthread.cfm?qid=457883

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

Module Module2

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
Dim theUI As UI = UI.GetUI()

Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim lg As LogFile = theSession.LogFile

Dim noteLength As Double
Dim noteHeight As Double
Dim alignPos As Annotations.AlignmentPosition = Nothing
Dim curStubLength As Double
Dim curPickEnd(1) As Double

Sub Main()

lg.WriteLine("~~ NXJ label component ~~")
lg.WriteLine("800c0a41-139c-4682-b37f-cfd398de402c")
lg.WriteLine(" timestamp: " & Now)

If IsNothing(theSession.Parts.Work) Then
'active part required
lg.WriteLine(" no active part, exiting journal")
Return
End If

lw.Open()

Const undoMarkName As String = "create component labels"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim myDraftingCurve As Drawings.DraftingCurve = Nothing
Dim myView As View = Nothing
Dim myPickPt As Point3d

While SelectDraftingCurve(myDraftingCurve, myPickPt, myView) <> Selection.Response.Cancel

'lw.WriteLine("curve tag: " & myDraftingCurve.Tag.ToString)

Dim obj_coords As Point3d = Nothing
obj_coords = GetPositionOnObject(myDraftingCurve, myView, myPickPt)

Dim obj_coords_dbl(2) As Double
obj_coords_dbl(0) = obj_coords.X
obj_coords_dbl(1) = obj_coords.Y
obj_coords_dbl(2) = obj_coords.Z
theUfSession.View.MapModelToDrawing(myView.Tag, obj_coords_dbl, curPickEnd)

Dim parentCount As Integer
Dim parents() As Tag = Nothing
theUfSession.Draw.AskDraftingCurveParents(myDraftingCurve.Tag, parentCount, parents)
Dim someComp As Assemblies.Component = Nothing

'lw.WriteLine("parent count: " & parentCount.ToString)
For Each temp As Tag In parents
Dim tempObj As NXObject = Utilities.NXObjectManager.Get(temp)
'lw.WriteLine("parent type: " & tempObj.GetType.ToString)
'lw.WriteLine("is occurrence: " & tempObj.IsOccurrence.ToString)
If tempObj.IsOccurrence Then
'lw.WriteLine("owning component: " & tempObj.OwningComponent.DisplayName)
someComp = tempObj.OwningComponent
Else
'lw.WriteLine("owning part: " & tempObj.OwningPart.Leaf)
End If
Next

If IsNothing(someComp) Then
lw.WriteLine("no component selected")
Return
End If

'lw.WriteLine("comp name: " & someComp.Name)
'lw.WriteLine(myPickPt.ToString)
'lw.WriteLine("selected from view: " & myView.Name)

'Dim compToolID As String = GetComponentAttribute(someComp, "TOOL_ID")
'Dim compPartName As String = GetComponentAttribute(someComp, "DB_PART_NAME")
'Dim compClassification As String = GetComponentAttribute(someComp, "TOOL_CLASS")

'lw.WriteLine("compToolID: " & compToolID)
'lw.WriteLine("compPartName: " & compPartName)
'lw.WriteLine("compClassification: " & compClassification)

Dim curLetterPrefs As Annotations.LetteringPreferences = theSession.Parts.Work.Annotations.Preferences.GetLetteringPreferences
alignPos = curLetterPrefs.AlignmentPosition
Dim userSymbolPreferences1 As Annotations.UserSymbolPreferences
userSymbolPreferences1 = theSession.Parts.Work.Annotations.NewUserSymbolPreferences(Annotations.UserSymbolPreferences.SizeType.ScaleAspectRatio, 1, 1)
Dim curDimPrefs As Annotations.LineAndArrowPreferences = theSession.Parts.Work.Annotations.Preferences.GetLineAndArrowPreferences
curStubLength = curDimPrefs.StubLength

MeasureNoteLength(MakeLabelText(someComp))

'select point for label
Dim labelPosition As Point3d = SelectScreenPoint()
'lw.WriteLine("label position: " & labelPosition.ToString)

'UF_DRF_create_label

AddComponentLabel(someComp, myDraftingCurve, myView, obj_coords, labelPosition, MakeLabelText(someComp))

End While

lw.Close()

End Sub

Function SelectDraftingCurve(ByRef theDraftingCurve As Drawings.DraftingCurve,
ByRef pickPt As Point3d,
ByRef selView As View) As Selection.Response

Dim message As String = "Drafting Curve:"
Dim title As String = "Select a drafting curve"
Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
Dim response As Integer
Dim draftingCurveTag As Tag = Tag.Null
Dim view As NXOpen.Tag
Dim cursor(2) As Double
Dim ip As UFUi.SelInitFnT = AddressOf mask_for_curves

Dim cursorView As Integer
theUfSession.Ui.AskCursorView(cursorView)

theUfSession.Ui.SetCursorView(0)

theUfSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

Try
theUfSession.Ui.SelectWithSingleDialog(message, title, scope, ip,
Nothing, response, draftingCurveTag, cursor, view)
theDraftingCurve = Utilities.NXObjectManager.Get(draftingCurveTag)
selView = Utilities.NXObjectManager.Get(view)
pickPt.X = cursor(0)
pickPt.Y = cursor(1)
pickPt.Z = cursor(2)
If draftingCurveTag <> Tag.Null Then
theUfSession.Disp.SetHighlight(draftingCurveTag, 0)
End If
Finally
theUfSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
theUfSession.Ui.SetCursorView(cursorView)
End Try

If response <> UFConstants.UF_UI_OBJECT_SELECTED And
response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
Return Selection.Response.Cancel
Else
Return Selection.Response.Ok
End If

End Function

Function mask_for_curves(ByVal select_ As IntPtr, ByVal userdata As IntPtr) As Integer

Dim num_triples As Integer = 4
Dim mask_triples(3) As UFUi.Mask

mask_triples(0).object_type = UFConstants.UF_line_type
mask_triples(0).object_subtype = 0
mask_triples(0).solid_type = 0

mask_triples(1).object_type = UFConstants.UF_circle_type
mask_triples(1).object_subtype = 0
mask_triples(1).solid_type = 0

mask_triples(2).object_type = UFConstants.UF_conic_type
mask_triples(2).object_subtype = 0
mask_triples(2).solid_type = 0

mask_triples(3).object_type = UFConstants.UF_spline_type
mask_triples(3).object_subtype = 0
mask_triples(3).solid_type = 0

theUfSession.Ui.SetSelMask(select_,
UFUi.SelMaskAction.SelMaskClearAndEnableSpecific,
num_triples, mask_triples)

Return UFConstants.UF_UI_SEL_SUCCESS

End Function

Function GetComponentAttribute(ByVal theComp As Assemblies.Component, ByVal attributeTitle As String) As String

If theComp.HasUserAttribute(attributeTitle, NXObject.AttributeType.String, -1) Then
Return theComp.GetUserAttributeAsString(attributeTitle, NXObject.AttributeType.String, -1)
Else
Return Nothing
End If

End Function

Function SelectScreenPoint() As Point3d

'Allow user to interactively pick the point where the annotation
'will be placed.

'This Function needs Sub MotionCallback() to work properly.

Dim myScreenPos(2) As Double
Dim theViewTag As Tag = theSession.Parts.Display.Views.WorkView.Tag
Dim theResponse As Integer

theUfSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

theUfSession.Ui.SpecifyScreenPosition("pick a point", AddressOf MotionCallback, Nothing, myScreenPos, theViewTag, theResponse)

theUfSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

If theResponse = UFConstants.UF_UI_PICK_RESPONSE Then
Return New Point3d(myScreenPos(0), myScreenPos(1), myScreenPos(2))
Else
Return Nothing
End If

End Function

Function GetPositionOnObject(ByVal obj As DisplayableObject, ByVal aView As View, ByRef coords As Point3d)

Dim guess1 As Double() = New Double(2) {}
Dim pt1 As Double() = New Double(2) {}
Dim pt2 As Double() = New Double(2) {}
Dim min_dist As Double

Dim loc_view As Point3d = MapAbs2View(aView, coords)

Dim sp_view As New Point3d(loc_view.X, loc_view.Y, loc_view.Z + 1000)
Dim ep_view As New Point3d(loc_view.X, loc_view.Y, loc_view.Z - 1000)
Dim sp_abs As Point3d = MapView2Abs(aView, sp_view)
Dim ep_abs As Point3d = MapView2Abs(aView, ep_view)

Dim line1 As Line = theSession.Parts.Work.Curves.CreateLine(sp_abs, ep_abs)
line1.SetVisibility(NXOpen.SmartObject.VisibilityOption.Visible)
line1.RedisplayObject()

theUfSession.Modl.AskMinimumDist(obj.Tag, line1.Tag, 0, guess1, 0, guess1, min_dist, pt1, pt2)

Dim markId2 As Session.UndoMarkId = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Delete")
theSession.UpdateManager.AddToDeleteList(line1)
theSession.UpdateManager.DoUpdate(markId2)

Return New Point3d(pt1(0), pt1(1), pt1(2))
End Function

Function MapView2Abs(ByVal aView As View, ByVal coords As Point3d)
Dim vmx As Matrix3x3 = aView.Matrix
Dim vw() As Double = {0, 0, 0, vmx.Xx, vmx.Xy, vmx.Xz, vmx.Yx, vmx.Yy, vmx.Yz}
Dim abs() As Double = {0, 0, 0, 1, 0, 0, 0, 1, 0}
Dim mx(11) As Double
Dim irc As Integer
Dim c() As Double = {coords.X, coords.Y, coords.Z}

theUfSession.Trns.CreateCsysMappingMatrix(vw, abs, mx, irc)
theUfSession.Trns.MapPosition(c, mx)

MapView2Abs = New Point3d(c(0), c(1), c(2))
End Function

Function MapAbs2View(ByVal aView As View, ByVal coords As Point3d)
Dim vmx As Matrix3x3 = aView.Matrix
Dim vw() As Double = {0, 0, 0, vmx.Xx, vmx.Xy, vmx.Xz, vmx.Yx, vmx.Yy, vmx.Yz}
Dim abs() As Double = {0, 0, 0, 1, 0, 0, 0, 1, 0}
Dim mx(11) As Double
Dim irc As Integer
Dim c() As Double = {coords.X, coords.Y, coords.Z}

theUfSession.Trns.CreateCsysMappingMatrix(abs, vw, mx, irc)
theUfSession.Trns.MapPosition(c, mx)

MapAbs2View = New Point3d(c(0), c(1), c(2))
End Function

Sub MotionCallback(ByVal pos() As Double,
ByRef motion_cb_data As UFUi.MotionCbData,
ByVal client_data As System.IntPtr)

'This sub will be called every time a cursor move is detected during the
'SpecifyScreenPosition function.

'The parameters motion_cb_data and client_data are not used in this implementation,
'but the Sub must have the same signature as UFUI.MotionFnT to work properly.

'disable (for now)
'Return

Dim wView As NXOpen.View = theSession.Parts.Display.Views.WorkView
Dim topLeft(2) As Double
Dim topRight(2) As Double
Dim botLeft(2) As Double
Dim botRight(2) As Double

'Dim labelWidth As Double = 20
'Dim labelHeight As Double = 5

'draw rectangle(s) to represent tabular note boundary
'Select Case alignPos
' Case Is = Annotations.AlignmentPosition.TopLeft
' 'draw rectangle to the right and down
' 'pos = top left corner
' topLeft(0) = pos(0)
' topLeft(1) = pos(1)
' topRight(0) = pos(0) + noteLength
' topRight(1) = pos(1)
' botLeft(0) = pos(0)
' botLeft(1) = pos(1) - noteHeight
' botRight(0) = topRight(0)
' botRight(1) = botLeft(1)
' Case Is = Annotations.AlignmentPosition.TopRight
' 'draw rectangle to the left and down
' 'pos = top right corner
' topRight(0) = pos(0)
' topRight(1) = pos(1)
' topLeft(0) = topRight(0) - noteLength
' topLeft(1) = topRight(1)
' botLeft(0) = topLeft(0)
' botLeft(1) = topLeft(1) - noteHeight
' botRight(0) = topRight(0)
' botRight(1) = botLeft(1)
' Case Else 'shouldn't happen

'End Select

'assume mid center alignment position for now
topLeft(0) = pos(0) - noteLength / 2
topLeft(1) = pos(1) + noteHeight / 2
topRight(0) = topLeft(0) + noteLength
topRight(1) = topLeft(1)
botLeft(0) = topLeft(0)
botLeft(1) = topLeft(1) - noteHeight
botRight(0) = topRight(0)
botRight(1) = topRight(1) - noteHeight

Dim stubStart(1) As Double
Dim stubEnd(1) As Double
stubStart(0) = pos(0) - noteLength / 2
stubStart(1) = pos(1)
stubEnd(0) = stubStart(0) - curStubLength
stubEnd(1) = stubStart(1)

'for other temp graphics commands, see:
' DisplayOgpArc
' DisplayOgpCircle
' DisplayOgpLine
' DisplayOgpPolyline

theUfSession.Disp.DisplayOgpLine(wView.Tag, topLeft, topRight)
theUfSession.Disp.DisplayOgpLine(wView.Tag, topLeft, botLeft)
theUfSession.Disp.DisplayOgpLine(wView.Tag, botLeft, botRight)
theUfSession.Disp.DisplayOgpLine(wView.Tag, topRight, botRight)
theUfSession.Disp.DisplayOgpLine(wView.Tag, stubStart, stubEnd)
theUfSession.Disp.DisplayOgpLine(wView.Tag, stubEnd, curPickEnd)

End Sub

Sub AddComponentLabel(ByVal theComponent As Assemblies.Component,
ByVal theDraftingCurve As Drawings.DraftingCurve,
ByVal theView As View,
ByVal leaderPt As Point3d,
ByVal originPt As Point3d,
ByVal theText() As String)

Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Note")

Dim nullNXOpen_Annotations_SimpleDraftingAid As NXOpen.Annotations.SimpleDraftingAid = Nothing

Dim draftingNoteBuilder1 As NXOpen.Annotations.DraftingNoteBuilder = Nothing
draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(nullNXOpen_Annotations_SimpleDraftingAid)

draftingNoteBuilder1.Origin.Anchor = NXOpen.Annotations.OriginBuilder.AlignmentPosition.MidCenter

draftingNoteBuilder1.Origin.Plane.PlaneMethod = NXOpen.Annotations.PlaneBuilder.PlaneMethodType.XyPlane

Dim leaderData1 As NXOpen.Annotations.LeaderData = Nothing
leaderData1 = workPart.Annotations.CreateLeaderData()
leaderData1.StubSize = theSession.Parts.Work.Annotations.Preferences.GetLineAndArrowPreferences.StubLength
leaderData1.Arrowhead = theSession.Parts.Work.Annotations.Preferences.GetLineAndArrowPreferences.FirstArrowType
'lw.WriteLine("first arrow type: " & theSession.Parts.Work.Annotations.Preferences.GetLineAndArrowPreferences.FirstArrowType.ToString)
leaderData1.VerticalAttachment = NXOpen.Annotations.LeaderVerticalAttachment.Center
leaderData1.StubSide = NXOpen.Annotations.LeaderSide.Inferred

draftingNoteBuilder1.Leader.Leaders.Append(leaderData1)

draftingNoteBuilder1.Text.TextBlock.SetText(theText)

' Jog position relocated or jog removed
leaderData1.Jogs.Clear()

Dim assocOrigin1 As NXOpen.Annotations.Annotation.AssociativeOriginData = Nothing
assocOrigin1.OriginType = NXOpen.Annotations.AssociativeOriginType.Drag
Dim nullNXOpen_View As NXOpen.View = Nothing

draftingNoteBuilder1.Origin.SetAssociativeOrigin(assocOrigin1)

draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullNXOpen_View, originPt)

draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(False)

leaderData1.Leader.SetValue(theDraftingCurve, theView, leaderPt)

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = draftingNoteBuilder1.Commit()

draftingNoteBuilder1.Destroy()

End Sub

Function MakeLabelText(ByVal theComponent As Assemblies.Component) As String()

Dim labelText As New List(Of String)

If theComponent.HasUserAttribute("TOOL_ID", NXObject.AttributeType.String, -1) Then
Dim associativeText1 As Annotations.AssociativeText
associativeText1 = workPart.Annotations.CreateAssociativeText()

labelText.Add(associativeText1.GetObjectAttributeText(theComponent, "TOOL_ID"))

associativeText1.Dispose()
End If

If GetComponentAttribute(theComponent, "TOOL_CLASS").ToUpper = "COMM" Then
Dim associativeText1 As Annotations.AssociativeText
associativeText1 = workPart.Annotations.CreateAssociativeText()

labelText.Add(associativeText1.GetObjectAttributeText(theComponent, "DB_PART_NAME"))

associativeText1.Dispose()

End If

Return labelText.ToArray

End Function

Sub MeasureNoteLength(ByVal noteText() As String)

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "measure note")

Dim noteOrigin(2) As Double
noteOrigin(0) = 0
noteOrigin(1) = 0
noteOrigin(2) = 0
Dim noteTag As Tag = Tag.Null

theUfSession.Drf.CreateNote(noteText.Length, noteText, noteOrigin, 0, noteTag)

Dim upLeft(2) As Double
theUfSession.Drf.AskAnnotationTextBox(noteTag, upLeft, noteLength, noteHeight)
'lw.WriteLine("note length: " & noteLength.ToString)
'lw.WriteLine("note height: " & noteHeight.ToString)

theSession.UndoToMark(markId1, "measure note")

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

End Function

End Module

Hi NXJournaling, thanks a lot for the code and the link. I'll go through the code and try to complete my journal with this info. I'll post the code once it's done. Thanks again :) :) , B

Balaji

hi, thanks for your quick response. actually i want to position my point labels near to the points in the screen. At the moment i have used pick any position by cursor and all the point labels are created one over the other. after execution i have to drag the label to the near by points. I tried converting from ABS to WCS in the code which i have commented and it doesn't seems to work. If i take the ABS all my labels are going outside my (clipped) view. is there any other way like... i can associate the label text with the leader head and position it somewhere near by the leader... (by controlling the stub length or something else...)
i have not used the function to convert the coordinates from ABS to WCS in a right way... is it possible that you can fix that in my code..?? many thanks in advance :)

Balaji

Hi, I understand that if I use the drafting points directly I cannot get the WCS... is there any other way that I could go to the modeling mode and measure the coordinates between the ABS and the WCS and later use that value to convert the ABS to WCS ... by subtracting the value from the ABS coordinates..

Balaji