Random appearance color to selected components?

Hello,

Thanks for taking the time to look at this. I know very little about this subject but am fumbling my way through it. What I would like is a journal that applies a random color to each of the selected components. I know there is an appearance setting for this but that does not give me the results I desire. I have searched and found code originally posted here by NXJournaling that is very close. I have modified it to produce a random color, however it applies this same random color to each component. I would like each component that is selected to have a different random color. I know it needs to be in some sort of loop but I have no idea how to make that happen. Anyway, below is the code:

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

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI

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

Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()
Dim theComps As New List(Of Assemblies.Component)

For i As Integer = 0 To numsel - 1
Dim selObj As TaggedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
If TypeOf (selObj) Is Assemblies.Component Then
theComps.Add(selObj)

End If

Next

If theComps.Count = 0 Then
'no components found among the preselected objects
Return
End If

randomize
Dim mycolor as integer = Int ((216-1) * Rnd + 1)

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

With displayModification1
.ApplyToAllFaces = False
.ApplyToOwningParts = False

.NewColor = mycolor
.Apply(theComps.ToArray)

End With
displayModification1.Dispose()

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

Try the code below. It will choose a random color for any pre-selected component. Select one or more components before running the journal.

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

Module Module156

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI

Sub Main()

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

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()
Dim theComps As New List(Of Assemblies.Component)

For i As Integer = 0 To numsel - 1
Dim selObj As TaggedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
If TypeOf (selObj) Is Assemblies.Component Then
theComps.Add(selObj)
End If
Next

If theComps.Count = 0 Then
'no components found among the preselected objects
Return
End If

Dim r As New Random
For Each tempComp As Assemblies.Component In theComps
Dim mycolor As Integer = r.Next(1, 217)

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

With displayModification1
.ApplyToAllFaces = False
.ApplyToOwningParts = False
.NewColor = mycolor
.Apply({tempComp})
End With
displayModification1.Dispose()

Next

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

That is awesome, works perfectly. Thank you so much!

Hi, Very interesting, can you modify it to change the color of selected bodies whatever it is on assembly or part working, i also wonder how to change
Dim theComps As New List(Of Assemblies.Component)
into the modeling environment.
How it realize if we already selected part it will execute the command, if not it will bring the Response to select objects.
Thanks so much

Hi, I written a code to change color on bodies in part, but it not run, please check it for me, thank for your help:

Imports System
Imports NXOpen
Imports System.Collections.Generic
Imports NXOpen.UF
Module NXJournal
Sub Main(ByVal args() As String)
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim mySelectedObjects As New List(Of DisplayableObject)
Dim theUI As UI = UI.GetUI()

Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Edit Object Display")

If SelectObjects("Hey, select multiple somethings",
mySelectedObjects) = Selection.Response.Ok Then

Dim r As New Random
For Each tempComp As Body In mySelectedObjects
Dim mycolor As Integer = r.Next(1, 217)

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

With displayModification1
.ApplyToAllFaces = False
.ApplyToOwningParts = False
.NewColor = mycolor
.Apply({tempComp})
End With
displayModification1.Dispose()

Next

End If

End Sub
Function SelectObjects(prompt As String,
ByRef dispObj As List(Of DisplayableObject)) As Selection.Response

Dim selObj As NXObject()
Dim theUI As UI = UI.GetUI
Dim title As String = "Select objects"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = NXOpen.UF.UFConstants.UF_solid_type
.SolidBodySubtype = NXOpen.UF.UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt,
title, scope, selAction,
includeFeatures, keepHighlighted, selectionMask_array,
selObj)

If resp = Selection.Response.ObjectSelected Or
resp = Selection.Response.ObjectSelectedByName Or
resp = Selection.Response.Ok Then
For Each item As NXObject In selObj
dispObj.Add(CType(item, DisplayableObject))
Next
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function

End Module

It works for me. Do you get an error message or does the code just not do what you expect?

Tks for your Respone, i also no error message, but the selected bodies is not changed to random color

I'd suggest selecting multiple bodies (5 or more) when running the code. If only one body is selected, there is a chance that the journal will select the existing color of the body (or one that is very close), making it appear that nothing has changed.

Hi, i tested again, and the trouble is the color just applied to wireframe view, in Shaded is not change, i mean it not apply to all faces of selected Body.
How can i change the option.
Thanks for your help

Change the displayModification's .ApplyToAllFaces option to TRUE. This should take care of the issue for you. When a body is created, the faces inherit their color from the body. If a face is later explicitly assigned a color, it no longer inherits the color from the body. With the .ApplyToAllFaces option set to FALSE, the display modification ignores any faces with explicitly set color and only changes those that inherit from the body. When set to TRUE, it changes all the faces. In my test part, all the faces were inheriting their color from the body; that's why I was seeing different results than you were.

This is the same program in Python

import NXOpen
import NXOpen.UF
import NXOpen.Features
import random

theSession = NXOpen.Session.GetSession()
workPart = theSession.Parts.Work
displayPart = theSession.Parts.Display
theLw = theSession.ListingWindow
theUI = NXOpen.UI.GetUI()
theLw.Open()

def main():

displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.ApplyToAllFaces = False

theobjects = select_solids()

for i in theobjects:
displayModification1.NewColor = int(random.random() * 217)
t=[]
t.append(i)
displayModification1.Apply(t)
displayModification1.Dispose()

def select_solids():

message = "Select object"
title = "Selection"
scope = NXOpen.SelectionSelectionScope.WorkPart
keepHighlighted = False

typeArray = [NXOpen.SelectionSelectionType.All]
selTuple = theUI.SelectionManager.SelectObjects(message, title, scope, keepHighlighted, typeArray)

if selTuple[0] == NXOpen.SelectionResponse.ObjectSelected:
theLw.WriteLine(" selTuple[0]" + str(selTuple[0]))

return selTuple[1]

if __name__ == '__main__':
main()

Thanks for the translation!