Updating vintage part files

Hi there,
I have made up a nice journal which should help making a "face lift" to vintage UG and NX files as far as appearance is affected: Background colors, body colors, color palette, vizualization preferences - especially selection and pre-selection color.
My aim is to have this run against all parts that are currently opened in an NX session so it cycles all parts in the session. But this has 2 donwsides:
1. I cannot make use of MACRO_playback (see the commented call @ line 115) to just set the color palette to system standard (a function which is not supported by the jounal recorder, so I used macro instead) so I used a code to open the .CDF file and set all colors accordingly,
2. For whatever reason, when it comes to setting the vizualization standards, especially selection colors, my journal has only effect on the single part which was the display part when I start running the journal.
I have tried to make NX switch the display part while cycling through the parts but maybe there's something wrong at that point (@ line 49ff).

Here's my code:
' Dieses Programm...
' - stellt die Hintergrundfarbe für schattierte Darstellung ein auf den NX1899 Standard
' - orientiert das Modell auf "TRIMETRIC", passt es ein und
' richtet die Anzeigefacetten neu aus (wirksam ab NX1899)
' - Startet ein externes Makro, das die System-Farbpalette lädt
' Den Pfad für das externe Makro unbedingt an die Implementierungsumgebung anpassen!
' - Prüft alle Flächen aller Körper im Part auf zulässige Farbe
' und ändert unzulässige Farben auf 129
' - und noch mehr (tbd!)
' 03.04.2020

' Anwendung: Partfiles in NX Modeling laden
' [Alt]+[F8] -> Zur Journaldatei browsen, dann Journal ausführen

Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Globalization

Imports NXOpen
Imports NXOpen.UF '.Net wrapped User Function

Module NXJournal

Declare Sub MACRO_playback_from_usertool Lib "libugui" Alias "?MACRO_playback_from_usertool@@YAXPEBD@Z" (ByVal lpName As String)

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()

Sub Main (ByVal args() As String)
Dim markId42 As NXOpen.Session.UndoMarkId = Nothing
markId42 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")
theSession.SetUndoMarkName(markId42, "Display Adjustment")

Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display

Dim allParts() As BasePart = theSession.Parts.ToArray

Dim nXObject1 As NXOpen.NXObject = Nothing

' Iterate through the parts in the session
For Each thisBasePart As BasePart In allParts
Dim thisPart As Part = CType(thisBasePart, Part)
Echo("Adjusting: " & thisPart.FullPath())

' Change the Displayed part
Dim partLoadStat As PartLoadStatus
'Dim displayPart As Part
theSession.Parts.SetDisplay(thisBasePart, False, False, partLoadStat)
'displayPart = thisBasePart

' Hintergrundfarbe angleichen
Dim background1 As NXOpen.Display.Background = Nothing
background1 = displayPart.Views.CreateBackground(displayPart.ModelingViews.WorkView, False)
Dim topcolor1(2) As Double
topcolor1(0) = 0.70588000000000000 '180
topcolor1(1) = 0.71373000000000000 '182
topcolor1(2) = 0.72157000000000000 '184
background1.SetBackgroundShadedViewsGraduatedTop(topcolor1)

'Dim nXObject1 As NXOpen.NXObject = Nothing

Dim bottomcolor1(2) As Double
bottomcolor1(0) = 0.90196000000000000 '230
bottomcolor1(1) = 0.90980000000000000 '232
bottomcolor1(2) = 0.91765000000000000 '234
background1.SetBackgroundShadedViewsGraduatedBottom(bottomcolor1)

Dim nXObject2 As NXOpen.NXObject = Nothing
nXObject2 = background1.Commit()

' Teil auf "TRIMETRIC" orientieren, einpassen und Anzeigefacetten neu ausrichten (ab NX1899)
displayPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Trimetric, NXOpen.View.ScaleAdjustment.Fit)
displayPart.ModelingViews.WorkView.UpdateDisplay()

'Körperfarben neu setzen
Dim okColor As Boolean = False
Dim acceptableColors() As Integer = {129, 171} 'Solid, Face

' Iterate through the bodies in the part
Dim allBodies() As Body = thisPart.Bodies.ToArray

For Each thisBody As Body In allBodies
' Iterate through all of the faces on that body
Dim allFaces() As Face = thisBody.GetFaces()
For Each thisFace As Face In allFaces
' Start out by assuming that the color of this face is bad
okColor = False
' Check it against all of the approved colors.
For inx As Integer = 0 To acceptableColors.GetUpperBound(0)
If thisFace.Color = acceptableColors(inx) Then
' As soon as we find that it matches a good color, we jump
' out of that loop and move to the next face.
okColor = True
Continue For
Else
'Echo("Changing the Colour...")
thisFace.Color = 129
thisFace.RedisplayObject()
End If
Next inx
If okColor = False Then
'Echo(" Bad color found in this part.")
End If
Next thisFace
Next thisBody

' System-Farbpalette laden via Macro
'MACRO_playback_from_usertool("C:\Daten_UM\05 - in Arbeit\Marcel Schmid\LoadSystemColorPalette.macro")

' Farbpalette aus .CDF laden
Dim colorName As String = ""
Dim red As String = ""
Dim green As String = ""
Dim blue As String = ""
Dim rgbColor(2) As Double

Dim provider As NumberFormatInfo = new NumberFormatInfo( )
provider.NumberDecimalSeparator = "."
provider.NumberGroupSeparator = ","
provider.NumberGroupSizes = New Integer( ) { 3 }
provider.NumberDecimalDigits = 6

Dim thisColor As Integer = 0
Dim textLine As String = ""
Dim lineCounter As Integer = 0
'Dim myStream As Stream = Nothing
Dim UGIIBaseDir As String = Environment.GetEnvironmentVariable("UGII_BASE_DIR")
Dim Filename As String = "C:\PLM\NX1899\UGII\ugcolor.cdf"
Dim myStream As New System.IO.StreamReader( Filename )
'Dim openFileDialog1 As New OpenFileDialog()

Dim rootDir As String = _
Environment.GetEnvironmentVariable("UGII_BASE_DIR")&"\ugii"

Try
Dim charsToTrim() As Char = {" "c}

If (myStream IsNot Nothing) Then
'Dim fileReader As New StreamReader(myStream)
Do
textLine = myStream.ReadLine()

If lineCounter > 3 Then

colorName = textLine.Substring(0, 30)
colorName = colorName.TrimEnd(charsToTrim) ' Remove trailing spaces

red = textLine.Substring(34, 8)
green = textLine.Substring(45, 8)
blue = textLine.Substring(56, 8)

rgbColor(0) = Convert.ToDouble(red,provider)
rgbColor(1) = Convert.ToDouble(green,provider)
rgbColor(2) = Convert.ToDouble(blue,provider)
thisColor = lineCounter - 4

' Echo("Color Name=" + colorName + " -- " + _
' "This Color=" + thisColor.ToString() + _
' " R=" + rgbColor(0).ToString() + _
' " G=" + rgbColor(1).ToString() + _
' " B=" + rgbColor(2).ToString())
'
theUFSession.Disp.SetColor(thisColor, UFConstants.UF_DISP_rgb_model, colorName, rgbColor)
End If
lineCounter += 1
Loop Until myStream.EndOfStream().Equals(True)

theUFSession.Disp.LoadColorTable()
End If
Catch Ex As NXException
MessageBox.Show("Error: " & Ex.Message)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
theUFSession.Ui.SetStatus("Loaded CDF")
End If
End Try

' Visualisierungseinstellungen auf Standard setzen

Dim pixelwidths1(8) As Integer
pixelwidths1(0) = 1
pixelwidths1(1) = 1
pixelwidths1(2) = 1
pixelwidths1(3) = 1
pixelwidths1(4) = 2
pixelwidths1(5) = 2
pixelwidths1(6) = 3
pixelwidths1(7) = 3
pixelwidths1(8) = 3
displayPart.Preferences.LineVisualization.SetPixelWidths(pixelwidths1)

displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.ShadedEdgeStyle = NXOpen.Preferences.ViewVisualizationVisual.ShadedEdgeStyleType.ShadedEdgeColor
displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.ShadedEdgeColor = 173
displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.HiddenEdgeStyle = NXOpen.Preferences.ViewVisualizationVisual.HiddenEdgeStyleType.Invisible
displayPart.Preferences.ColorSettingVisualization.HiddenGeometryColor = 44
displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.TwoSidedLight = True
displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.ShininessData = 0.34999999999999998

Dim displayAppearanceOptions1 As NXOpen.Preferences.ViewVisualizationVisual.DisplayAppearanceOptions = Nothing
displayAppearanceOptions1.RenderingStyle = NXOpen.Preferences.ViewVisualizationVisual.RenderingStyle.Shaded
displayAppearanceOptions1.HiddenEdges = NXOpen.Preferences.ViewVisualizationVisual.HiddenEdges.Invisible
displayAppearanceOptions1.Silhouettes = True
displayAppearanceOptions1.SmoothEdges = True
displayAppearanceOptions1.SmoothEdgeColor = 0
displayAppearanceOptions1.SmoothEdgeFont = NXOpen.Preferences.ViewVisualizationVisual.SmoothEdgeFont.Original
displayAppearanceOptions1.SmoothEdgeWidth = NXOpen.Preferences.ViewVisualizationVisual.SmoothEdgeWidth.Original
displayAppearanceOptions1.SmoothEdgeAngleTolerance = 0.19999999999999996
displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.DisplayAppearance = displayAppearanceOptions1

Dim displayAppearanceOptions2 As NXOpen.Preferences.ViewVisualizationVisual.DisplayAppearanceOptions = Nothing
displayAppearanceOptions2.RenderingStyle = NXOpen.Preferences.ViewVisualizationVisual.RenderingStyle.Shaded
displayAppearanceOptions2.HiddenEdges = NXOpen.Preferences.ViewVisualizationVisual.HiddenEdges.Invisible
displayAppearanceOptions2.Silhouettes = True
displayAppearanceOptions2.SmoothEdges = True
displayAppearanceOptions2.SmoothEdgeColor = 0
displayAppearanceOptions2.SmoothEdgeFont = NXOpen.Preferences.ViewVisualizationVisual.SmoothEdgeFont.Original
displayAppearanceOptions2.SmoothEdgeWidth = NXOpen.Preferences.ViewVisualizationVisual.SmoothEdgeWidth.Original
displayAppearanceOptions2.SmoothEdgeAngleTolerance = 0.20000000000000001
displayPart.ModelingViews.WorkView.VisualizationVisualPreferences.DisplayAppearance = displayAppearanceOptions2
displayPart.Preferences.ShadeVisualization.ShadedViewTolerance = NXOpen.Preferences.PartVisualizationShade.ShadedViewToleranceType.Standard
displayPart.Preferences.ShadeVisualization.ShadedViewUpdateMode = NXOpen.Preferences.PartVisualizationShade.ViewUpdateModeType.VisibleBodiesAndCurves

Dim facetSettingsBuilder1 As NXOpen.Display.FacetSettingsBuilder = Nothing
facetSettingsBuilder1 = displayPart.CreateFacetSettingsBuilder()
facetSettingsBuilder1.ShadedAlignFacets = False

displayPart.Preferences.ShadeVisualization.AdvancedVisViewUpdateMode = NXOpen.Preferences.PartVisualizationShade.ViewUpdateModeType.VisibleBodiesAndCurves

facetSettingsBuilder1.AdvVisAlignFacets = True
facetSettingsBuilder1.AdvVisUpdate = 3
facetSettingsBuilder1.ShadedUpdate = 3
nXObject1 = facetSettingsBuilder1.Commit()
facetSettingsBuilder1.Destroy()

displayPart.Preferences.PerformanceVisualization.SaveAdvancedDisplayFacets = True
displayPart.Preferences.NamesBorderVisualization.ShowModelViewNames = False

Dim shelloptions1 As NXOpen.Preferences.PartVisualizationEmphasis.ShellOptions = Nothing
shelloptions1.Rgb.R = 0.69411764705882351
shelloptions1.Rgb.G = 0.69411764705882351
shelloptions1.Rgb.B = 0.69411764705882351
shelloptions1.Edges = NXOpen.Preferences.PartVisualizationEmphasis.EdgesType.Normal
shelloptions1.EdgesRgb.R = 0.59999999999999998
shelloptions1.EdgesRgb.G = 0.59999999999999998
shelloptions1.EdgesRgb.B = 0.59999999999999998
shelloptions1.Translucency = 75
displayPart.Preferences.EmphasisVisualization.SetShellOptions(shelloptions1)

Dim originalcolorshelloptions1 As NXOpen.Preferences.PartVisualizationEmphasis.OriginalColorShellOptions = Nothing
originalcolorshelloptions1.Edges = NXOpen.Preferences.PartVisualizationEmphasis.EdgesType.Normal
originalcolorshelloptions1.EdgesRgb.R = 0.59999999999999998
originalcolorshelloptions1.EdgesRgb.G = 0.59999999999999998
originalcolorshelloptions1.EdgesRgb.B = 0.59999999999999998
originalcolorshelloptions1.Translucency = 75
displayPart.Preferences.EmphasisVisualization.SetOriginalColorShellOptions(originalcolorshelloptions1)

Dim layersoptions1 As NXOpen.Preferences.PartVisualizationEmphasis.LayersOptions = Nothing
layersoptions1.Rgb.R = 0.80000000000000004
layersoptions1.Rgb.G = 0.80000000000000004
layersoptions1.Rgb.B = 0.80000000000000004
layersoptions1.Edges = NXOpen.Preferences.PartVisualizationEmphasis.EdgesType.Normal
layersoptions1.EdgesRgb.R = 0.59999999999999998
layersoptions1.EdgesRgb.G = 0.59999999999999998
layersoptions1.EdgesRgb.B = 0.59999999999999998
layersoptions1.Translucency = 85
displayPart.Preferences.EmphasisVisualization.SetLayersOptions(layersoptions1)
displayPart.Preferences.LineVisualization.ShowWidths = True
displayPart.Preferences.LineVisualization.ShowWidths = True
Dim pixelwidths2(8) As Integer
pixelwidths2(0) = 1
pixelwidths2(1) = 1
pixelwidths2(2) = 1
pixelwidths2(3) = 1
pixelwidths2(4) = 2
pixelwidths2(5) = 2
pixelwidths2(6) = 3
pixelwidths2(7) = 3
pixelwidths2(8) = 3
' UI Colors (Selection etc.)
displayPart.Preferences.LineVisualization.SetPixelWidths(pixelwidths2)
displayPart.Preferences.ColorSettingVisualization.PreselectionColor = 149
displayPart.Preferences.ColorSettingVisualization.SelectionColor = 76
displayPart.Preferences.ColorSettingVisualization.HiddenGeometryColor = 44
displayPart.Preferences.ColorSettingVisualization.MonochromePreselectionColor = 149
displayPart.Preferences.ColorSettingVisualization.MonochromeSelectionColor = 76
displayPart.Preferences.ColorSettingVisualization.MonochromeBackgroundColor = 1
theSession.CleanUpFacetedFacesAndEdges()

Next thisBasePart

End Sub

Sub Echo(ByVal output As String)

theSession.ListingWindow.Open()
theSession.ListingWindow.WriteLine(output)
theSession.LogFile.WriteLine(output)

End Sub

End Module

If you want to run it, dont' forget to change the filename in line 135 to an appropriate value. I have tested on NX1907 but most of it should also work in older NX versions.
I'd like to figure out how to set the color palette to system standard using vb and how to have all the parts visualization preferences set correclty, not just the 1st part's.
Any help is much appreciated.
Cheers,
Udo

When you declare "displayPart" and set it equal to "theSession.Parts.Display" this will refer to the current display part at that time. It will not update automatically when the display part changes. You will need to update the reference, or (my personal preference) use "theSession.Parts.Display" in place of "displayPart". This will always refer to the current display part (NX takes care of updating the reference for you). It looks like you had the right idea in line 53 (updating the "displayPart" variable):

'displayPart = thisBasePart

but it is currently commented out. Also, get rid of line 51. You do not need (or want) another "displayPart" variable declared in the For loop.