Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Trying to change the color of parts in a Assembly!!! PLEASE CAN SOMEONE HELP ME!!!!!!

Status
Not open for further replies.

mnash60

Materials
Feb 21, 2012
29
I have started a vb where it runs through each part in a assembly and changes the color. I have run into a problem I need to create a different color for each part. Also I need the color to the actual part not just the color in the assembly. Below you'll find the code that I have started. PLEASE CAN SOMEONE HELP ME

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpen.part
Imports NXOpen.UI
Imports NXOpen.Utilities


Module NXJournal

Public theSession As Session = Session.GetSession()
Public ufs As UFSession = UFSession.GetUFSession()
Public lw As ListingWindow = theSession.ListingWindow

Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
Dim myBody As Body
Dim displayModification1 As DisplayModification



lw.Open
Try
' Dim c As ComponentAssembly = dispPart.ComponentAssembly
'to process the work part rather than the display part,
' comment the previous line and uncomment the following line
Dim c As ComponentAssembly = workPart.ComponentAssembly
'Dim rnd1 As New Random()
'Dim myPt1 As interger




if not IsNothing(c.RootComponent) then

'*** insert code to process 'root component' (assembly file)



' do an action on the collected solids

Dim visibleObjects() As DisplayableObject
visibleObjects = workpart.Views.WorkView.AskVisibleObjects()
For Each displayableObject As DisplayableObject In visibleObjects
If TypeOf displayableObject Is Body Then
Dim body As Body = displayableObject
Dim rnd1 As New Random()



Randomize()
' Generate random value between 1 and 6.
Dim value As Integer = CInt(Int((97 * Rnd()) + 1))


Lw.WriteLine("Body: " & body.JournalIdentifier)
body.highlight()



displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = true

displayModification1.NewColor = value


Dim objects1(0) As DisplayableObject

objects1(0) = myBody
displayModification1.Apply(visibleobjects)

displayModification1.Dispose()


End If
Next

lw.WriteLine("Assembly: " & c.RootComponent.DisplayName)
lw.WriteLine(" + Active Arrangement: " & c.ActiveArrangement.Name)
'*** end of code to process root component
ReportComponentChildren(c.RootComponent, 0)
else
'*** insert code to process piece part

' Dim visibleObjects() As DisplayableObject
' visibleObjects = workPart.Views.WorkView.AskVisibleObjects()
' For Each displayableObject As DisplayableObject In visibleObjects
' If TypeOf displayableObject Is Body Then
' Dim body As Body = displayableObject
' Lw.WriteLine("Body: " & body.JournalIdentifier)
' body.highlight()
'


' displayModification1 = theSession.DisplayManager.NewDisplayModification()
'
' displayModification1.ApplyToAllFaces = True
'
' displayModification1.ApplyToOwningParts = false
' displayModification1.NewColor = 46


' Dim objects1(0) As DisplayableObject
'
' objects1(0) = myBody
' displayModification1.Apply(visibleobjects)
'
' displayModification1.Dispose()
'

'End If
' Next



lw.WriteLine("Part has no components")
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
lw.Close

End Sub

'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, _
ByVal indent As Integer)

For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly
lw.WriteLine(New String(" ", indent * 2) & child.DisplayName())
'*** end of code to process component or subassembly
if child.GetChildren.Length <> 0 then
'*** this is a subassembly, add code specific to subassemblies
lw.WriteLine(New String(" ", indent * 2) & _
"* subassembly with " & _
child.GetChildren.Length & " components")
lw.WriteLine(New String(" ", indent * 2) & _
" + Active Arrangement: " & _
child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
'*** end of code to process subassembly
else
'this component has no children (it is a leaf node)
'add any code specific to bottom level components
end if
reportComponentChildren(child, indent + 1)
Next
End Sub

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









 
Replies continue below

Recommended for you

You are making it much more difficult than necessary.

When you right click any component in your assembly and select "properties", then there is a Check box titled "Specific Component Colour".

If its Checked ... un-Check it and it will inherit the colour from the individual part.
If its 'greyed out' ... then this gets automatically Checked when you change the colour within "Edit Display".
So if you apply this in your code you don't need to "read" the color from the original body and change it..


Ronald van den Broek
Senior Application Engineer
Winterthur Gas & Diesel Ltd
NX9 / TC10.1.2

Building new PLM environment from Scratch using NX11 / TC11
 
Also NX only has what 216 colors?, which would put a hard limit on the number of unique components.
 
How do I apply "Specific Component Colour" to my code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor