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
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