Change font in parts list

Hi,

I'm trying to create a journal that changes the font of every cell in the parts list (cell style -> font) of the work part to Arial, but I'm struggling. Any suggestions would be very much appreciated. Thanks!

A parts list is a type of tabular note. The code in the following link shows one way to change the font of the tabular note cells:
http://nxjournaling.com/content/change-font-drafting-table

For a parts list, you will need to also change the header rows. Below is a quick modification of the code to do that:

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

Module Module3

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 = "NXJ change parts list font"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

'find/assign font number
Dim fntArial As Integer = workPart.Fonts.AddFont("Arial", FontCollection.Type.Standard)
'lw.WriteLine("Arial font number: " & fntArial.ToString)

Dim partsListTags() As Tag
Dim numPartsLists As Integer

theUfSession.Plist.AskTags(partsListTags, numPartsLists)

For Each tableNote As Tag In partsListTags

Dim numRows As Integer
theUfSession.Tabnot.AskNmRows(tableNote, numRows)

Dim numCols As Integer
theUfSession.Tabnot.AskNmColumns(tableNote, numCols)

Dim tableOrigin(2) As Double
Dim tableSectionTag As Tag
theUfSession.Tabnot.AskNthSection(tableNote, 0, tableSectionTag)

'## update the header rows
Dim numHeaderRows As Integer
theUfSession.Tabnot.AskNmHeaderRows(tableNote, numHeaderRows)

'lw.WriteLine("number of header rows: " & numHeaderRows.ToString)
'lw.WriteLine("number of rows: " & numRows.ToString)
'lw.WriteLine("number of columns: " & numCols.ToString)

For i As Integer = 0 To numHeaderRows - 1
Dim rowTag As Tag
theUfSession.Tabnot.AskNthHeaderRow(tableNote, i, rowTag)

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

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

'get the current cell preferences
Dim theCellPrefs As UFTabnot.CellPrefs
theUfSession.Tabnot.AskCellPrefs(cellTag, theCellPrefs)

'change the font preference setting
theCellPrefs.text_font = fntArial

'apply the new settings to the cell
theUfSession.Tabnot.SetCellPrefs(cellTag, theCellPrefs)

Next

Next
'## done with header rows

'@@ update normal rows
For i As Integer = 0 To numRows - 1
Dim rowTag As Tag
theUfSession.Tabnot.AskNthRow(tableNote, i, rowTag)

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

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

'get the current cell preferences
Dim theCellPrefs As UFTabnot.CellPrefs
theUfSession.Tabnot.AskCellPrefs(cellTag, theCellPrefs)

'change the font preference setting
theCellPrefs.text_font = fntArial

'apply the new settings to the cell
theUfSession.Tabnot.SetCellPrefs(cellTag, theCellPrefs)

Next

Next
'@@ done with normal rows

Next

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

Editied to remove reference to cell "zero display".

Thanks a lot for your quick reply. This code worked on a smaller assembly, but unfortunately it was really slow on a bigger one, and on a third assy it made NX crash (the program just ended after one second). Is there no way to set the value for"right-click parts list-> Cell style -> Font" directly from a journal? That change makes it update a lot faster than changing every row.

Thanks again!

BR

Stian LA

The following code will change the default preferences for the cell font:

Dim cellPrefs As UFTabnot.CellPrefs
theUfSession.Tabnot.AskDefaultCellPrefs(cellPrefs)
cellPrefs.text_font = fntArial
theUfSession.Tabnot.SetDefaultCellPrefs(cellPrefs)

New rows added to the parts list will have the Arial font; however, by itself, the code won't change existing rows.

The previously posted code is the only way that I'm currently aware of that will change the font for existing parts list rows. If you find an easier way, please post your solution!

Edit: the previously posted code had a reference to the "zero display" of the cell. This was a left over from some previous code (I copied and pasted from code that changes tabular notes); it may have caused some problems with the parts list cells. Please try the revised code on parts lists, hopefully it won't crash NX.

Ok. It still crashed with the new code, but that might be a problem with that specific part. I'll test it on a few others. Thank you very much!

BR

Stian LA

An alternative would be to query the existing parts list for the important parameters, delete the parts list, set the parts list defaults to the desired settings, and finally re-create the parts list.

This method might avoid the crash.

Thanks to user hebmwo, I've run across another possible solution; however, it is only a viable solution if you want to completely replace one font with another (the replaced font will no longer exist in the file, the new font will completely take over all notes, dimensions, etc where the old font was used). If this condition applies to your situation, try using the .ReplaceFont method. An example journal can be found here:
http://nxjournaling.com/comment/2421#comment-2421