cancel
Showing results for 
Search instead for 
Did you mean: 

UFTabnot Searching - There has to be a better way than this

Creator
Creator

A fair amount of what I need to do requires searching for and manipulating tables on drawings. The only way that i've found to get this to work is to build an array of TabularNote tags with a loop and something like this...

 

ufs.Obj.CycleObjsInPart(workPart.Tag,

UFConstants.UF_tabular_note_type, ref tmpTabNote);

 

After compiling an array of tags for every tabular note, I search through every cell of every row looking for a specific string.

 

This seems like such a kludge and I'm hoping that someone has a better idea of how this sort of thing can be done.

1 REPLY

Re: UFTabnot Searching - There has to be a better way than this

Phenom
Phenom

If you are using NX 8 or above, you have access to a collection of all the tabular notes in a part. This is a bit easier than using the CycleObjsInPart function. However, you will still have to cycle through every cell to find the desired text. Alternatively, you could save all the tabular note cell text into a data structure such as a dictionary object, then search the dictionary each time rather than cycling through all the notes again. A small example is below (VB syntax):

 

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

Module Module1

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

    Sub Main()

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

        lw.Open()

        Dim tabNotes As New Dictionary(Of Tag, String)
        Dim cellTags As New List(Of Tag)

        CatalogTabularNotes(tabNotes)

        'specify string value to find in the tabular notes
        Dim searchString As String = "widget"

        cellTags = FindTabularNoteText(searchString, tabNotes)

        If cellTags.Count > 0 Then
            lw.WriteLine("string found " & cellTags.Count & " time(s)")
            lw.WriteLine("cell tag(s):")
            For Each temp As Tag In cellTags
                lw.WriteLine(temp.ToString)
            Next
        Else
            lw.WriteLine("string not found in any tabular note")
        End If

        lw.Close()

    End Sub


    Sub CatalogTabularNotes(ByRef theNotes As Dictionary(Of Tag, String))

        For Each tabNote As Annotations.Table In workPart.Annotations.Tables

            Dim numRows As Integer
            theUfSession.Tabnot.AskNmRows(tabNote.Tag, numRows)

            Dim numCols As Integer
            theUfSession.Tabnot.AskNmColumns(tabNote.Tag, numCols)


            For i As Integer = 0 To numRows - 1
                Dim rowTag As Tag
                theUfSession.Tabnot.AskNthRow(tabNote.Tag, i, rowTag)

                For j As Integer = 0 To numCols - 1
                    Dim colTag As Tag
                    theUfSession.Tabnot.AskNthColumn(tabNote.Tag, j, colTag)

                    Dim cellTag As Tag
                    theUfSession.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)

                    Dim cellText As String
                    theUfSession.Tabnot.AskCellText(cellTag, cellText)

                    theNotes.Add(cellTag, cellText)

                Next

            Next

        Next

    End Sub

    Function FindTabularNoteText(ByVal findString As String, ByVal notesDic As Dictionary(Of Tag, String)) As List(Of Tag)

        Dim cellTagList As New List(Of Tag)

        For Each temp As KeyValuePair(Of Tag, String) In notesDic
            If findString.ToUpper = temp.Value.ToUpper Then
                cellTagList.Add(temp.Key)
            End If
        Next

        Return cellTagList

    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