Replacing Text In A Table Cell

Hi All,

I am new to the NX journal and I recorded a journal that replaces text in one of the table cell in the title blocks on my drawing.

I figured that because of the selection stickiness, I can't use it on other drawings.

My initial thought was to specify the location of the cell with other method to replace the "FindObject" but I couldn't find instructions on how to do it.

Can someone help me with removing the selection stickiness or point me to the correct direction?

I am working with NX 12. Here is the recorded code. Thanks!


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, "Edit Cell")

Dim table1 As NXOpen.Annotations.Table = CType(workPart.Annotations.Tables.FindObject("HANDLE R-9610"), NXOpen.Annotations.Table)

Dim displayableObject1 As NXOpen.DisplayableObject = CType(workPart.FindObject("HANDLE R-9508"), NXOpen.DisplayableObject)

table1.EditCellText(displayableObject1, "NA")

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

When you want to find a specific entity when running a journal, the usual advice is to give the target entity a unique name or attribute. When the journal runs, it can find the entity based on the name or attribute and perform some action on it. However, tabular notes in NX are an exception to this rule. I've found no way to apply a name or attribute to a specific cell in a tabular note.

Working with tabular notes in interactive NX is fairly straightforward; but the object model and API calls to manipulate them seem overly convoluted (IMHO). While I've not yet found a way to name a specific cell, you can give a name to a tabular note section (which is not the same as the tabular note itself, btw).

The code below is a bit of a work-around. You will need to know the row and column number of the cell you want to change. To see the code in action, do the following:

  • open an NX file and create a new tabular note
  • right click on the tabular note -> properties -> general tab => enter "TABLETEST" for the object name.
  • run the journal. The text "testing" should show up in the cell at the first row, first column (top left corner).
  • edit the constants as desired at the beginning of Sub Main (or edit the code to provide inputs for the user).

'NXJournaling.com
'January 16, 2023

'Find a tabular note with the specified name and change the text of the specified cell.
'Change the constants at the beginning of Sub Main before running the journal.

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 workPart As Part = theSession.Parts.Work

Sub Main()

'**************************************************
'* constants to find tabular note section and cell
'* change these as necessary before running journal
'*
'* note that computers start counting with zero
'* a tabular note with 3 columns and 5 rows
'* will have columns 0, 1, 2
'* and rows 0, 1, 2, 3, 4
'* counting starts at top left cell
'* this is accounted for later in the code
Const tableName As String = "TABLETEST"
Const rowNumber As Integer = 1
Const columnNumber As Integer = 1
Const newText As String = "testing"
'**************************************************

Dim myTableSection As Annotations.TableSection = Nothing
Dim myTableTag As Tag = Tag.Null

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

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

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

'lw.WriteLine("table sections: " & workPart.Annotations.TableSections.ToArray.Length.ToString)
For Each ts As Annotations.TableSection In workPart.Annotations.TableSections
'lw.WriteLine("name: " & ts.Name)
If ts.Name.ToUpper = tableName.ToUpper Then
myTableSection = ts
theUfSession.Tabnot.AskTabularNoteOfSection(ts.Tag, myTableTag)
End If
Next

If IsNothing(myTableSection) Then
lw.WriteLine("tabular note not found with name: " & tableName)
Return
End If

Dim numCols As Integer
theUfSession.Tabnot.AskNmColumns(myTableTag, numCols)
'lw.WriteLine(tableName & " columns: " & numCols.ToString)
If numCols < columnNumber Then
lw.WriteLine("error: tabular note '" & tableName & "' only has " & numCols.ToString & " columns")
lw.WriteLine(" columnNumber = " & columnNumber.ToString)
Return
End If

Dim numRows As Integer
theUfSession.Tabnot.AskNmRows(myTableTag, numRows)
'lw.WriteLine(tableName & " rows: " & numRows.ToString)
If numRows < rowNumber Then
lw.WriteLine("error: tabular note '" & tableName & "' only has " & numRows.ToString & " rows")
lw.WriteLine(" rowNumber = " & rowNumber.ToString)
Return
End If

Dim rowTag As Tag = Tag.Null
'use rowNumber - 1 so that the returned row matches the expected input
'row numbering starts at 0, not 1
theUfSession.Tabnot.AskNthRow(myTableTag, rowNumber - 1, rowTag)
If rowTag = Tag.Null Then
lw.WriteLine("error: row not found")
Return
End If

Dim colTag As Tag = Tag.Null
'use columnNumber - 1 so that the returned column matches the expected input
'column numbering starts at 0, not 1
theUfSession.Tabnot.AskNthColumn(myTableTag, columnNumber - 1, colTag)
If colTag = Tag.Null Then
lw.WriteLine("error: column not found")
Return
End If

Dim myCellTag As Tag = Tag.Null
theUfSession.Tabnot.AskCellAtRowCol(rowTag, colTag, myCellTag)
theUfSession.Tabnot.SetCellText(myCellTag, newText)

lw.Close()

End Sub

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

Thanks for the help!
I was able to update the tabular note by using the code!

However, the way that I want to use this journal is more like a template to pre-fill some of the cells in the title block for the drawings. Since the title block was a couple tabular notes combine together, I would need to give each tabular note a name before I can use the journal.

I am wondering if there is a way to use journal to assign names to the tabular notes? I looked at the "information" for the tabular notes. I found that there are object origin and tabular note section ID. Not sure if they can be use to identify the tabular notes?

Thanks again for the reply!

I suggest naming the desired tables in your template file. That way any time you create a new file from the template, the named tables will be ready for you.

You can certainly use a journal to name the desired tabular notes. The issue, as before, is how to identify the correct tables. I see at least 2 solutions here.

  • Add a selection function so the user can interactively select the desired table. An example of this would be the code posted by MANox at eng-tips:
    https://www.eng-tips.com/viewthread.cfm?qid=501472
  • If this is an established template file and you don't plan on moving the tables around, you could loop through all the tables in the part and compare the origin to the known origin location of the desired table. If you have templates of various sizes (e.g. A size, B size, etc), you'd need to check the sheet size and table origin vs. known good values. If the origin turns out to be a reliable indicator of the desired table, you could forgo naming the table and just find it by origin location instead. The advantage of naming the table would be that you wouldn't need to check the origin locations and if the tables moved around, you could still be confident that you had the correct table.

Hope this helps.