×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Find Components and change color automatically

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

NX11.0.0

RE: Find Components and change color automatically

Hi Olaf,

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

(OP)
Hello Ronald.
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 Module 

Thanks in advance

Olaf

NX11.0.0

RE: Find Components and change color automatically

This should probably do it if you only want to change the color of the component instance

CODE --> vb

Dim theComponent(0) As DisplayableOjbect = CType(child, DisplayAbleObject)
displayModification1 = s.DisplayManager.NewDisplayModification()	 			
displayModification1.NewColor = 36 'Color Green
displayModification1.ApplyToAllFaces = True
displayModification1.ApplyToOwningParts = False
displayModification1.Apply(theComponent)
displayModification1.Dispose() 

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

(OP)
Hello petulf.
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

Yeah the array declaration was messed up, see the corrected code in the form of a sub procedure

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 Sub 

you could then write

CODE --> VB

If child.DisplayName.Contains("Green") Then
    changeComponentColor(child, 36)
End If 

RE: Find Components and change color automatically

(OP)
Hello petulf
Thanx very much, it works perfectly!
Olaf

NX11.0.0

RE: Find Components and change color automatically

(OP)
In summary the code look now like this:

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 Module 

Olaf

NX11.0.0

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources