Coordinates of regular expressions in annotatnions

Hello.

I need to create a journal for Drafting application.

I`d like it to search all annotatnions in a drawing for a specific combination of symbols (left parenthesis, 1 letter, 1 number, right parenthesis). Then I`d need it to extract coordinates of a point laying in the center of this combination.

I have yet to create my first journal, so before I begin, I`d like to ask if this is even possible to do what I described. Thank you very much for advice.

I'm pretty sure this will be possible. Bear in mind that if the note references an expression or attribute, you may need to "decode" the text to get at the underlying value. Can you give a few examples of the text you are looking for and what you need to extract from it?

What version of NX?

I`m working on NX 9.0.3.4

One of examples is an annotation like this:
"Section A-A (C4)"
or
"See note 4 (B1)"

I need the numerical coordinates pointing to the center of "(C4)" or "(B1)" part of the annotations. Usually these texts are blunt text, so most of the time no decoding is necessary.

Initially I thought you were looking for either the origin coordinates of the note that contains the given string or the position of the target text within the string. It appears that you want a coordinate in the center of the target text. This should also be possible (or at least possible to get a good approximation), but will be a bit more difficult.

Are you using an NX font or a true type font for your notes?

Yes, that is what I`m looking for.

I had to check with my corporate masters, but seems I`m free to use either font types.

I asked about the font type because I thought we'd have to use the font metrics to help calculate a position.

However, I happened upon a much simpler solution. If the search pattern is found in the note, split the note between the letter and number, create a new NX note that ends with the letter, measure the distance between the note's mid-left and mid-right position, add that length to the start point of the original note - this is the mid point of the matched text (or very close). It works if you are using NX fonts or true type fonts.

'NXJournaling.com
'January 8, 2015
'
'Find the coordinates of a regular expression match in a note object.
'This version only marks the match location with a temporary point and temporary text.
'Limitations:
' this version only looks at single line notes (or rather, the first line of every note).
' this version returns the first match found only, if the note has multiple matches, the rest are ignored

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

Module Module2

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

Sub Main()

If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "mark notes"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim noteList As New List(Of Annotations.Note)

Dim searchPattern As String = "\([a-z,A-Z]\d\)"

For Each tempNote As Annotations.Note In workPart.Notes

Dim noteText() As String = tempNote.GetText
Dim text1 As String = noteText(0)

'does note contain search string?
'If text1.Contains(searchString) Then
' noteList.Add(tempNote)
'End If

'does note contain the search pattern?
If Regex.IsMatch(text1, searchPattern) Then
noteList.Add(tempNote)
End If

Next

'process the notes that were found
For Each foundNote As Annotations.Note In noteList
Dim noteText() As String = foundNote.GetText
Dim text1 As String = noteText(0)
lw.WriteLine("note text (line 1): " & text1)

'trim note in middle of search string
'note mid-right position, this is coordinates of middle of search string
Dim text2(0) As String

Dim theMatch As Match = Regex.Match(text1, searchPattern)
lw.WriteLine("match: " & theMatch.Groups(0).Value)
'lw.WriteLine("found at position: " & theMatch.Groups(0).Index)

text2(0) = text1.Substring(0, theMatch.Groups(0).Index + theMatch.Groups(0).Value.Length / 2)
'lw.WriteLine("text2(0): " & text2(0))

Dim tempNote As Annotations.Note = CreateNoteInheritSettings(text2(0), foundNote)
Dim tempNoteLength As Double = MeasureNoteLength(tempNote)
'lw.WriteLine("newNote length: " & tempNoteLength.ToString)

Dim patternCoordinates As Point3d
patternCoordinates = DisplayTempPointNote(foundNote, tempNoteLength)
lw.WriteLine(patternCoordinates.ToString)

'delete the temp note
DeleteObject(tempNote)

Next

lw.Close()

End Sub

Function CreateNoteInheritSettings(ByVal noteText As String, ByVal templateNote As Annotations.Note) As Annotations.Note

'font size
Dim templateNoteSize As Double
templateNoteSize = templateNote.GetLetteringPreferences.GetGeneralText.Size

'character space factor
Dim templateNoteCharSpaceFactor As Double
templateNoteCharSpaceFactor = templateNote.GetLetteringPreferences.GetGeneralText.CharacterSpaceFactor

'aspect ratio
Dim templateNoteAspectRatio As Double
templateNoteAspectRatio = templateNote.GetLetteringPreferences.GetGeneralText.AspectRatio

'italic font?
Dim templateNoteIsItalic As Boolean
templateNoteIsItalic = templateNote.GetLetteringPreferences.GetGeneralText.Italic

Dim nullAnnotations_SimpleDraftingAid As Annotations.SimpleDraftingAid = Nothing

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

draftingNoteBuilder1.Origin.Plane.PlaneMethod = Annotations.PlaneBuilder.PlaneMethodType.XyPlane
draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(False)
draftingNoteBuilder1.Origin.Anchor = Annotations.OriginBuilder.AlignmentPosition.MidLeft

Dim text1(0) As String
text1(0) = noteText
draftingNoteBuilder1.Text.TextBlock.SetText(text1)

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

draftingNoteBuilder1.Style.LetteringStyle.Angle = 0.0
draftingNoteBuilder1.Style.LetteringStyle.GeneralTextSize = templateNoteSize
draftingNoteBuilder1.Style.LetteringStyle.GeneralTextCharSpaceFactor = templateNoteCharSpaceFactor
draftingNoteBuilder1.Style.LetteringStyle.GeneralTextAspectRatio = templateNoteAspectRatio

Dim nullView As NXOpen.View = Nothing

Dim point1 As Point3d = New Point3d(0.0, 0.0, 0.0)
draftingNoteBuilder1.Origin.Origin.SetValue(Nothing, nullView, point1)

Dim newNote As Annotations.Note
newNote = draftingNoteBuilder1.Commit()

draftingNoteBuilder1.Destroy()

Return newNote

End Function

Function MeasureNoteLength(ByVal theNote As Annotations.Note) As Double

Dim tempLetteringPref As Annotations.LetteringPreferences
tempLetteringPref = theNote.GetLetteringPreferences
tempLetteringPref.AlignmentPosition = Annotations.AlignmentPosition.MidLeft
theNote.SetLetteringPreferences(tempLetteringPref)

Dim noteStart As Point3d = theNote.AnnotationOrigin

tempLetteringPref.AlignmentPosition = Annotations.AlignmentPosition.MidRight
theNote.SetLetteringPreferences(tempLetteringPref)

Dim noteEnd As Point3d = theNote.AnnotationOrigin

theNote.SetLetteringPreferences(tempLetteringPref)

Return noteEnd.X - noteStart.X

End Function

Function DisplayTempPointNote(ByVal theNote As Annotations.Note, ByVal XOffset As Double) As Point3d

'get mid-left start point of note
Dim alignmentPos As Annotations.AlignmentPosition
alignmentPos = theNote.GetLetteringPreferences.AlignmentPosition

Dim oldPos As Annotations.AlignmentPosition
oldPos = alignmentPos

Dim tempLetteringPref As Annotations.LetteringPreferences
tempLetteringPref = theNote.GetLetteringPreferences
tempLetteringPref.AlignmentPosition = Annotations.AlignmentPosition.MidLeft
theNote.SetLetteringPreferences(tempLetteringPref)

Dim noteStart As Point3d = theNote.AnnotationOrigin

tempLetteringPref.AlignmentPosition = oldPos
theNote.SetLetteringPreferences(tempLetteringPref)

Dim markerPos(2) As Double
markerPos(0) = noteStart.X + XOffset
markerPos(1) = noteStart.Y
markerPos(2) = 0

theUfSession.Disp.DisplayTemporaryPoint(Tag.Null, UFDisp.ViewType.UseWorkView, markerPos, Nothing, UFDisp.PolyMarker.Asterisk)
theUfSession.Disp.DisplayTemporaryText(Tag.Null, UFDisp.ViewType.UseWorkView, "(" & markerPos(0) & ", " & markerPos(1) & ")", markerPos, UFDisp.TextRef.Bottomcenter, Nothing, 0, 0)

Return New Point3d(markerPos(0), markerPos(1), markerPos(2))

End Function

Sub DeleteObject(ByVal tempObj As NXObject)

theSession.UpdateManager.ClearErrorList()

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

Dim objects1(0) As NXObject

objects1(0) = tempObj
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(objects1)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId1)

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

'----Other unload options-------
'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