Find Components and change color automatically
Find Components and change color automatically
(OP)
Hello all.
I´m looking for a way, to Change the Color of components automatically. My idea is, to run through an assembly structure, search for a certain component name and change the color of this component to a defined color. I tried it with some bits and pieces from other scripts, I´m able to find the components in the assembly structure and highlight them. But I´m not able to select the components, neither to modify the Color of the components.
I created a small example assembly with the components Yellow, Green, Blue
Here is my script for this simple example:
'Search for Components, Highlight Components, Modify Coloring
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpen.Utilities
Module ListStructureOnlyComponents
Dim s As Session = Session.GetSession()
Dim lw As ListingWindow = s.ListingWindow
Dim ufs As UFSession = UFSession.GetUFSession()
Dim theComps As New List(Of Assemblies.Component)
Sub Main()
Dim dp As Part = s.Parts.Display
Try
Dim c As Component = dp.ComponentAssembly.RootComponent
WalkAssemblyTree(c, "")
Catch e As Exception
End Try
End Sub
Sub Echo(ByVal output As String)
s.ListingWindow.Open()
s.ListingWindow.WriteLine(output)
s.LogFile.WriteLine(output)
End Sub
Sub WalkAssemblyTree(ByVal c As Component, ByVal indent As String)
Dim children As Component() = c.GetChildren()
Dim newIndent As String
Dim loadStatus1 As UFPart.LoadStatus = Nothing
Dim displayModification1 As DisplayModification
For Each child As Component In children
If indent.Length = 0 Then
newIndent = " "
Else
newIndent = indent & " "
End If
Dim theInst As NXOpen.Tag = ufs.Assem.AskInstOfPartOcc(child.Tag)
Try
If child.DisplayName.Contains("Yellow") Then
Echo ("Yellow found")
'Dim my_select As SelectionHandle = TransientObject()
'Selection.AddToTaggedObjectsSelectionList(my_select, child,True)
child.Highlight ()
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.NewColor = 6 'Color Yellow
displayModification1.Dispose()
End If
If child.DisplayName.Contains("Green") Then
Echo ("Green found")
'Dim my_select As SelectionHandle = TransientObject()
'Selection.AddToTaggedObjectsSelectionList(my_select, child,True)
child.Highlight ()
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.NewColor = 36 'Color Green
displayModification1.Dispose()
End If
If child.DisplayName.Contains("Blue") Then
Echo ("Blue found")
'Dim my_select As SelectionHandle = TransientObject()
'Selection.AddToTaggedObjectsSelectionList(my_select, child,True)
child.Highlight ()
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.NewColor = 211 'Color Blue
displayModification1.Dispose()
End If
Catch ex As Exception
Echo("Exception: " & ex.Message())
End Try
WalkAssemblyTree(child, newIndent)
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
Is there a way to to solve this issue?
Thanks in advance
I´m looking for a way, to Change the Color of components automatically. My idea is, to run through an assembly structure, search for a certain component name and change the color of this component to a defined color. I tried it with some bits and pieces from other scripts, I´m able to find the components in the assembly structure and highlight them. But I´m not able to select the components, neither to modify the Color of the components.
I created a small example assembly with the components Yellow, Green, Blue
Here is my script for this simple example:
'Search for Components, Highlight Components, Modify Coloring
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpen.Utilities
Module ListStructureOnlyComponents
Dim s As Session = Session.GetSession()
Dim lw As ListingWindow = s.ListingWindow
Dim ufs As UFSession = UFSession.GetUFSession()
Dim theComps As New List(Of Assemblies.Component)
Sub Main()
Dim dp As Part = s.Parts.Display
Try
Dim c As Component = dp.ComponentAssembly.RootComponent
WalkAssemblyTree(c, "")
Catch e As Exception
End Try
End Sub
Sub Echo(ByVal output As String)
s.ListingWindow.Open()
s.ListingWindow.WriteLine(output)
s.LogFile.WriteLine(output)
End Sub
Sub WalkAssemblyTree(ByVal c As Component, ByVal indent As String)
Dim children As Component() = c.GetChildren()
Dim newIndent As String
Dim loadStatus1 As UFPart.LoadStatus = Nothing
Dim displayModification1 As DisplayModification
For Each child As Component In children
If indent.Length = 0 Then
newIndent = " "
Else
newIndent = indent & " "
End If
Dim theInst As NXOpen.Tag = ufs.Assem.AskInstOfPartOcc(child.Tag)
Try
If child.DisplayName.Contains("Yellow") Then
Echo ("Yellow found")
'Dim my_select As SelectionHandle = TransientObject()
'Selection.AddToTaggedObjectsSelectionList(my_select, child,True)
child.Highlight ()
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.NewColor = 6 'Color Yellow
displayModification1.Dispose()
End If
If child.DisplayName.Contains("Green") Then
Echo ("Green found")
'Dim my_select As SelectionHandle = TransientObject()
'Selection.AddToTaggedObjectsSelectionList(my_select, child,True)
child.Highlight ()
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.NewColor = 36 'Color Green
displayModification1.Dispose()
End If
If child.DisplayName.Contains("Blue") Then
Echo ("Blue found")
'Dim my_select As SelectionHandle = TransientObject()
'Selection.AddToTaggedObjectsSelectionList(my_select, child,True)
child.Highlight ()
displayModification1 = s.DisplayManager.NewDisplayModification()
displayModification1.NewColor = 211 'Color Blue
displayModification1.Dispose()
End If
Catch ex As Exception
Echo("Exception: " & ex.Message())
End Try
WalkAssemblyTree(child, newIndent)
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
Is there a way to to solve this issue?
Thanks in advance
NX11.0.0





RE: Find Components and change color automatically
Please use the code option when placing bits of code...
Makes it easier for us to read...
Ronald van den Broek
Senior Application Engineer
Winterthur Gas & Diesel Ltd
NX9 / TC10.1.2
HPZ420 Intel(R) Xeon(R) CPU E5-1620 0 @ 3.60GHz, 32 Gb Win7 64B
Nvidea Quadro4000 2048MB DDR5
HP Zbook15
Intel(R) Core(TM) i7-4800MQ
CPU @ 2.70 GHz Win7 64b
Nvidia K1100M 2048 MB DDR5
RE: Find Components and change color automatically
Thanks for hint.
Here is the code again:
CODE -->
'Search for Components, Highlight Components, Modify Coloring Option Strict Off Imports System Imports System.Collections.Generic Imports System.IO Imports NXOpen Imports NXOpen.Assemblies Imports NXOpen.UF Imports NXOpen.Utilities Module ListStructureOnlyComponents Dim s As Session = Session.GetSession() Dim lw As ListingWindow = s.ListingWindow Dim ufs As UFSession = UFSession.GetUFSession() Dim theComps As New List(Of Assemblies.Component) Sub Main() Dim dp As Part = s.Parts.Display Try Dim c As Component = dp.ComponentAssembly.RootComponent WalkAssemblyTree(c, "") Catch e As Exception End Try End Sub Sub Echo(ByVal output As String) s.ListingWindow.Open() s.ListingWindow.WriteLine(output) s.LogFile.WriteLine(output) End Sub Sub WalkAssemblyTree(ByVal c As Component, ByVal indent As String) Dim children As Component() = c.GetChildren() Dim newIndent As String Dim loadStatus1 As UFPart.LoadStatus = Nothing Dim displayModification1 As DisplayModification For Each child As Component In children If indent.Length = 0 Then newIndent = " " Else newIndent = indent & " " End If Dim theInst As NXOpen.Tag = ufs.Assem.AskInstOfPartOcc(child.Tag) Try If child.DisplayName.Contains("Yellow") Then Echo ("Yellow found") 'Dim my_select As SelectionHandle = TransientObject() 'Selection.AddToTaggedObjectsSelectionList(my_select, child,True) child.Highlight () displayModification1 = s.DisplayManager.NewDisplayModification() displayModification1.NewColor = 6 'Color Yellow displayModification1.Dispose() End If If child.DisplayName.Contains("Green") Then Echo ("Green found") 'Dim my_select As SelectionHandle = TransientObject() 'Selection.AddToTaggedObjectsSelectionList(my_select, child,True) child.Highlight () displayModification1 = s.DisplayManager.NewDisplayModification() displayModification1.NewColor = 36 'Color Green displayModification1.Dispose() End If If child.DisplayName.Contains("Blue") Then Echo ("Blue found") 'Dim my_select As SelectionHandle = TransientObject() 'Selection.AddToTaggedObjectsSelectionList(my_select, child,True) child.Highlight () displayModification1 = s.DisplayManager.NewDisplayModification() displayModification1.NewColor = 211 'Color Blue displayModification1.Dispose() End If Catch ex As Exception Echo("Exception: " & ex.Message()) End Try WalkAssemblyTree(child, newIndent) 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 ModuleThanks in advance
Olaf
NX11.0.0
RE: Find Components and change color automatically
CODE --> vb
But if this is only for status information / inspection then i would suggest you use HD3D Visual Reporting which is easy to setup and use.
RE: Find Components and change color automatically
Thanks for the reply.
I tried it, but I received an error message: "Explicit initialization is not permitted for Arrays declared with ecplicit bounds."
Olaf
NX11.0.0
RE: Find Components and change color automatically
CODE --> VB
Sub changeComponentColor(ByRef theComponent As Assemblies.Component, ByVal colorId As Integer) Try Dim theDisplayObject() As DisplayableObject = {CType(theComponent, DisplayableObject)} Dim displayModification1 As DisplayModification = S.DisplayManager.NewDisplayModification() displayModification1.NewColor = colorId displayModification1.ApplyToAllFaces = True displayModification1.ApplyToOwningParts = False displayModification1.Apply(theDisplayObject) displayModification1.Dispose() Catch ex As Exception End Try End Subyou could then write
CODE --> VB
If child.DisplayName.Contains("Green") Then changeComponentColor(child, 36) End IfRE: Find Components and change color automatically
Thanx very much, it works perfectly!
Olaf
NX11.0.0
RE: Find Components and change color automatically
CODE -->
'Search for Components, Highlight Components, Modify Coloring Option Strict Off Imports System Imports System.Collections.Generic Imports System.IO Imports NXOpen Imports NXOpen.Assemblies Imports NXOpen.UF Imports NXOpen.Utilities Module ListStructureOnlyComponents Dim s As Session = Session.GetSession() Dim lw As ListingWindow = s.ListingWindow Dim ufs As UFSession = UFSession.GetUFSession() Dim theComps As New List(Of Assemblies.Component) Sub Main() Dim dp As Part = s.Parts.Display Try Dim c As Component = dp.ComponentAssembly.RootComponent WalkAssemblyTree(c, "") Catch ex As Exception End Try End Sub Sub changeComponentColor(ByRef theComponent As Assemblies.Component, ByVal colorId As Integer) Try Dim theDisplayObject() As DisplayableObject = {CType(theComponent, DisplayableObject)} Dim displayModification1 As DisplayModification = S.DisplayManager.NewDisplayModification() displayModification1.NewColor = colorId displayModification1.ApplyToAllFaces = True displayModification1.ApplyToOwningParts = False displayModification1.Apply(theDisplayObject) displayModification1.Dispose() Catch ex As Exception End Try End Sub Sub WalkAssemblyTree(ByVal c As Component, ByVal indent As String) Dim children As Component() = c.GetChildren() Dim newIndent As String Dim loadStatus1 As UFPart.LoadStatus = Nothing Dim displayModification1 As DisplayModification For Each child As Component In children If indent.Length = 0 Then newIndent = " " Else newIndent = indent & " " End If Dim theInst As NXOpen.Tag = ufs.Assem.AskInstOfPartOcc(child.Tag) Try If child.DisplayName.Contains("Green") Then changeComponentColor(child, 36) End If If child.DisplayName.Contains("Yellow") Then changeComponentColor(child, 6) End If If child.DisplayName.Contains("Blue") Then changeComponentColor(child, 211) End If Catch ex As Exception End Try WalkAssemblyTree(child, newIndent) 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 ModuleOlaf
NX11.0.0