Replace Character font

Hello,
can somebody give me help about the "UF_UGFONT_replace_font" - API function?
The code below is not working. There is an error message that "Arial" font was not
found in the character font directory. But if I open the Character Fonts (File/Utilities/Character Fonts)
I have several fonts (Arial too) which will not be found.
I have tried to find out the correct font name with "UF_UGFONT_ask_font_name", but there I have a
Problem with the correct declaration of "font_name".

Option Strict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module NXJournal
Public ufs As UFSession = UFSession.GetUFSession()
Public theSession As Session = Session.GetSession()
Public workPart As Part = theSession.Parts.Work
Public displayPart As Part = theSession.Parts.Display
Public part As NXOpen.Tag = workPart.Tag

Sub Main
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim fte As NXOpen.Tag
ufs.Obj.CycleObjsInPart(part, UFConstants.UF_font_table_type, fte)
Dim fName As String = "Arial"
ufs.UGFont.ReplaceFont(fte, 1, fName)

End Sub
End Module

Thanks!

The function that you are calling, ufs.UGFont.ReplaceFont, only works with the NX fonts (the "stroked" fonts that ship with NX: blockfont, leroy, etc). If you want to work with the 'standard' fonts (true type fonts), you will need to use the function: ufs.UGFont.ReplaceFont1.

Alternatively, since it appears that you are using VB.net, I'd suggest using the .ReplaceFont method of the part's font collection. The code below will add a true type font to the work part (Cooper Black) and replace font #1 (usually the NX font "blockfont") with the newly added font.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

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

lw.WriteLine("Fonts before replacement")
For i As Integer = 1 To workPart.Fonts.GetLength

lw.WriteLine(i.ToString & ": " & workPart.Fonts.GetFontName(i) & "; Type: " & workPart.Fonts.GetFontType(i).ToString)

Next

'replace font #1 (most likely the NX "blockfont") with a new font
workPart.Fonts.ReplaceFont(1, FontCollection.Type.Standard, "Cooper Black")

lw.WriteLine("")
lw.WriteLine("Fonts after replacement")
For i As Integer = 1 To workPart.Fonts.GetLength

lw.WriteLine(i.ToString & ": " & workPart.Fonts.GetFontName(i) & "; Type: " & workPart.Fonts.GetFontType(i).ToString)

Next

UpdateTextObjects(workPart)

lw.Close()

End Sub

Sub UpdateTextObjects(ByVal thePart As Part)

For Each temp As DisplayableObject In thePart.Dimensions
temp.RedisplayObject()
Next

For Each temp As DisplayableObject In thePart.Annotations.TableSections
temp.RedisplayObject()
Next

For Each temp As DisplayableObject In thePart.Notes
temp.RedisplayObject()
Next

'May need to update other text objects, not every possibility was tested...

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 your solution and quick reply,
it has saved me from more gray hair :-)

How is this journal changed to replace an NX font with another NX font? Does the '1' in this line refer to the font character number you want to replace?

workPart.Fonts.ReplaceFont(1, FontCollection.Type.Standard

Thank you!

Yes, the font number will be replaced with the font that you specify. To use an NX font, use the Type.NX and specify the font name. Something like below:

workPart.Fonts.ReplaceFont(1, FontCollection.Type.Nx, "leroy")

Thank you!