Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Components and change color automatically 1

Status
Not open for further replies.

OlafU

Automotive
Jan 31, 2012
19
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
 
Replies continue below

Recommended for you

Hi Olaf,

Please use the code option when placing bits of code...
Makes it easier for us to read...
code_yxwx6o.png


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
 
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
 
This should probably do it if you only want to change the color of the component instance
Code:
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.
 
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
 
Yeah the array declaration was messed up, see the corrected code in the form of a sub procedure
Code:
    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:
If child.DisplayName.Contains("Green") Then
    changeComponentColor(child, 36)
End If
 
Hello petulf
Thanx very much, it works perfectly!
Olaf

NX11.0.0
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor