Using System.Drawing.Color with NX "journal" ?

To all

I have a small piece of code which allocate a (pre-defined) colour for a mesh type. Nothing fancy. The colour codes and names are "hard coded", see bleo for an example. Works just fine


Case 1
iColourCode = 181
sColourName = "Magenta"

'etc

End Select
mesh.Color = iColourCode
theLW.WriteLine ("--Mesh Collector: " & mesh.Name & ". Displayed colour set to: " & sColourName)

I am trying to see if I could use the option System.Drawing.Color to either get the name of the colour (if I know the code) or the code If I have the colour name. I am simply trying to use existing functions/capabilities rather than hard code to many things

Does anyone know if it's possible to set that up with a NX "journal"?

Thanks
Regards

The color names in NX are defined separately from those in System.Drawing. NX has a limited "palette" of colors that it uses (216 colors). Each color has a defined RGB value and name (found in the color definition file or CDF). The System.Drawing color functions may not work as expected with the NX palette. However, the NX API does offer some functions to work with the colors in NX. The code below shows a few of these functions (and the comments allude to a few more that may be of interest).

'NXJournaling
'July 10, 2013
'get color information from selected object
' also see the UF functions:
' UF_DISP_ask_color
' UF_DISP_ask_closest_color

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
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim myObj As NXObject
Dim myDispObj As DisplayableObject

Do Until SelectAnObject(myObj) = Selection.Response.Cancel

lw.WriteLine("object type: " & myObj.GetType.ToString)
myDispObj = DirectCast(myObj, DisplayableObject)

lw.WriteLine("color number: " & myDispObj.Color)
Dim myColor As NXColor
myColor = theSession.Parts.Work.Colors.Find(myDispObj.Color)
lw.WriteLine("color label: " & myColor.Label)
lw.WriteLine("color rgb: " & myColor.GetRgb.ToString)
Dim red As Integer = myColor.GetRgb.R * 255
Dim green As Integer = myColor.GetRgb.G * 255
Dim blue As Integer = myColor.GetRgb.B * 255

lw.WriteLine("red: " & red.ToString)
lw.WriteLine("green: " & green.ToString)
lw.WriteLine("blue: " & blue.ToString)
lw.WriteLine("")

Loop

End Sub

Function SelectAnObject(ByRef selObj As NXObject) As Selection.Response

Dim prompt As String = "Select an object"
Dim theUI As UI = UI.GetUI
Dim cursor As Point3d
Dim typeArray() As Selection.SelectionType = _
{Selection.SelectionType.All, _
Selection.SelectionType.Faces, _
Selection.SelectionType.Edges}

Dim resp As Selection.Response = theUI.SelectionManager.SelectObject( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selObj, cursor)

If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

for those remotely interested. The NX Color Palette is (possibly) the following

'Index Name
'----- ---------------------
' 1 White
' 2 Pale Weak Yellow
' 3 Pale Dull Yellow
' 4 Light Faded Yellow
' 5 Light Hard Yellow
' 6 Yellow
' 7 Pale Weak Cyan
' 8 Pale Weak Green
' 9 Pale Dull Spring
' 10 Light Spring Yellow
' 11 Light Yellow Spring
' 12 Yellow Yellow Spring
' 13 Pale Dull Cyan
' 14 Pale Dull Teal
' 15 Pale Dull Green
' 16 Light Spring Green
' 17 Light Hard Spring
' 18 Spring Spring Yellow
' 19 Light Faded Cyan
' 20 Light Teal Cyan
' 21 Light Teal Green
' 22 Light Faded Green
' 23 Light Green Spring
' 24 Spring Spring Green
' 25 Light Hard Cyan
' 26 Light Cyan Teal
' 27 Light Hard Teal
' 28 Light Green Teal
' 29 Light Hard Green
' 30 Green Green Spring
' 31 Cyan
' 32 Cyan Cyan Teal
' 33 Teal Teal Cyan
' 34 Teal Teal Green
' 35 Green Green Teal
' 36 Green
' 37 Pale Weak Magenta
' 38 Pale Weak Red
' 39 Pale Dull Orange
' 40 Light Orange Yellow
' 41 Light Yellow Orange
' 42 Yellow Yellow Orange
' 43 Pale Weak Blue
' 44 Pale Gray
' 45 Light Weak Yellow
' 46 Light Dull Yellow
' 47 Medium Faded Yellow
' 48 Dark Hard Yellow
' 49 Pale Dull Azure
' 50 Light Weak Cyan
' 51 Light Weak Green
' 52 Light Dull Spring
' 53 Medium Spring Yellow
' 54 Dark Yellow Spring
' 55 Light Azure Cyan
' 56 Light Dull Cyan
' 57 Light Dull Teal
' 58 Light Dull Green
' 59 Medium Spring Green
' 60 Dark Hard Spring
' 61 Light Cyan Azure
' 62 Medium Faded Cyan
' 63 Medium Teal Cyan
' 64 Medium Teal Green
' 65 Medium Faded Green
' 66 Dark Green Spring
' 67 Cyan Cyan Azure
' 68 Dark Hard Cyan
' 69 Dark Cyan Teal
' 70 Dark Hard Teal
' 71 Dark Green Teal
' 72 Dark Hard Green
' 73 Pale Dull Magenta
' 74 Pale Dull Pink
' 75 Pale Dull Red
' 76 Light Orange Red
' 77 Light Hard Orange
' 78 Orange Orange Yellow
' 79 Pale Dull Violet
' 80 Light Weak Magenta
' 81 Light Weak Red
' 82 Light Dull Orange
' 83 Medium Orange Yellow
' 84 Dark Yellow Orange
' 85 Pale Dull Blue
' 86 Light Weak Blue
' 87 Light Gray
' 88 Medium Weak Yellow
' 89 Dark Dull Yellow
' 90 Dark Faded Yellow
' 91 Light Azure Blue
' 92 Light Dull Azure
' 93 Medium Weak Cyan
' 94 Medium Weak Green
' 95 Dark Dull Spring
' 96 Dark Spring Yellow
' 97 Light Hard Azure
' 98 Medium Azure Cyan
' 99 Dark Dull Cyan
' 100 Dark Dull Teal
' 101 Dark Dull Green
' 102 Dark Spring Green
' 103 Azure Azure Cyan
' 104 Dark Cyan Azure
' 105 Dark Faded Cyan
' 106 Dark Teal Cyan
' 107 Dark Teal Green
' 108 Dark Faded Green
' 109 Light Faded Magenta
' 110 Light Pink Magenta
' 111 Light Pink Red
' 112 Light Faded Red
' 113 Light Red Orange
' 114 Orange Orange Red
' 115 Light Violet Magenta
' 116 Light Dull Magenta
' 117 Light Dull Pink
' 118 Light Dull Red
' 119 Medium Orange Red
' 120 Dark Hard Orange
' 121 Light Violet Blue
' 122 Light Dull Violet
' 123 Medium Weak Magenta
' 124 Medium Weak Red
' 125 Dark Dull Orange
' 126 Dark Orange Yellow
' 127 Light Faded Blue
' 128 Light Dull Blue
' 129 Medium Weak Blue
' 130 Dark Gray
' 131 Dark Weak Yellow
' 132 Obscure Dull Yellow
' 133 Light Blue Azure
' 134 Medium Azure Blue
' 135 Dark Dull Azure
' 136 Dark Weak Cyan
' 137 Dark Weak Green
' 138 Obscure Dull Spring
' 139 Azure Azure Blue
' 140 Dark Hard Azure
' 141 Dark Azure Cyan
' 142 Obscure Dull Cyan
' 143 Obscure Dull Teal
' 144 Obscure Dull Green
' 145 Light Hard Magenta
' 146 Light Magenta Pink
' 147 Light Hard Pink
' 148 Light Red Pink
' 149 Light Hard Red
' 150 Red Red Orange
' 151 Light Magenta Violet
' 152 Medium Faded Magenta
' 153 Medium Pink Magenta
' 154 Medium Pink Red
' 155 Medium Faded Red
' 156 Dark Red Orange
' 157 Light Hard Violet
' 158 Medium Violet Magenta
' 159 Dark Dull Magenta
' 160 Dark Dull Pink
' 161 Dark Dull Red
' 162 Dark Orange Red
' 163 Light Blue Violet
' 164 Medium Violet Blue
' 165 Dark Dull Violet
' 166 Dark Weak Magenta
' 167 Dark Weak Red
' 168 Obscure Dull Orange
' 169 Light Hard Blue
' 170 Medium Faded Blue
' 171 Dark Dull Blue
' 172 Dark Weak Blue
' 173 Obscure Gray
' 174 Obscure Weak Yellow
' 175 Blue Blue Azure
' 176 Dark Blue Azure
' 177 Dark Azure Blue
' 178 Obscure Dull Azure
' 179 Obscure Weak Cyan
' 180 Obscure Weak Green
' 181 Magenta
' 182 Magenta Magenta Pink
' 183 Pink Pink Magenta
' 184 Pink Pink Red
' 185 Red Red Pink
' 186 Red
' 187 Magenta Magenta Violet
' 188 Dark Hard Magenta
' 189 Dark Magenta Pink
' 190 Dark Hard Pink
' 191 Dark Red Pink
' 192 Dark Hard Red
' 193 Violet Violet Magenta
' 194 Dark Magenta Violet
' 195 Dark Faded Magenta
' 196 Dark Pink Magenta
' 197 Dark Pink Red
' 198 Dark Faded Red
' 199 Violet Violet Blue
' 200 Dark Hard Violet
' 201 Dark Violet Magenta
' 202 Obscure Dull Magenta
' 203 Obscure Dull Pink
' 204 Obscure Dull Red
' 205 Blue Blue Violet
' 206 Dark Blue Violet
' 207 Dark Violet Blue
' 208 Obscure Dull Violet
' 209 Obscure Weak Magenta
' 210 Obscure Weak Red
' 211 Blue
' 212 Dark Hard Blue
' 213 Dark Faded Blue
' 214 Obscure Dull Blue
' 215 Obscure Weak Blue
' 216 Black

Thanks
Regards

You can get the current NX color palette information by going to Menu -> preferences -> color palette... and clicking on the information icon (found in the Options section of the dialog). The information window will list all of the color names & numbers along with the RGB values of each.

Thanks for the example. Learned something new.

As part of my testing, I ended up created 2 dictionaries
1. a (fixed) dictionary of Colour Code, colour Name
2. a dictionary of stringtosearch, Colour code

Based on a Microsoft example there might be a way of creating a Class but it is a touch OTT for what I need to do

Based on the dictionary approach I ended up with

If theNametoSearch.Contains(thestringtofind) Then
iColourCode = dicoItemColourCode.Item(thestringtofind) 'Each 'thestringtofind' has a colour code allocated
sColourName = dicoColourCodeName.Item(iColourCode) 'a dico of colour code , colour name
'Change colour based on the iColourCode
End

Thanks
Regards