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!

VB Help!!! Changing Part color in a assembly

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.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Assemblies
Imports System.Windows.Forms
Imports System.IO
Imports System.Collections
Imports System.Collections.Generic
Imports System.Environment
Imports NXOpenUI

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


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
if not IsNothing(c.RootComponent) then
ReportComponentChildren(c.RootComponent, 0)
else
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try

End Sub
'*******************************************************************

Public Function GenerateRandomString(ByRef len As Integer, ByRef upper As Boolean) As String
Dim rand As New Random()
Dim allowableChars() As Char = "0123456789".ToCharArray()
Dim final As String = String.Empty
For i As Integer = 0 To len - 1
final += allowableChars(rand.Next(allowableChars.Length - 1))
Next

Return IIf(upper, final.ToUpper(), final)
End Function


'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, _
ByVal indent As Integer)
Dim S As Session = Session.GetSession()
Dim workPart As Part = S.Parts.Work
Dim ufs As UFSession = UFSession.GetUFSession()
Dim selobj As NXObject
Dim arc1 As ibasecurve
Dim cpoint As point = Nothing
Dim kk As Integer = 0
Dim type As Integer
Dim subtype As Integer
Dim lw As ListingWindow = s.ListingWindow
Dim layerObjects() As NXObject
Dim layerObjectsDisplayable() As DisplayableObject
Dim layerColorID(256) As Integer
layerColorID(1) = 6

Dim arcList As New List(Of face)

For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly



'Get visible objects in work view
Dim visibleObjects() As DisplayableObject
visibleObjects = workPart.Views.WorkView.AskVisibleObjects
Dim tempArc As face

Dim displayModification1 As DisplayModification
displayModification1 = S.DisplayManager.NewDisplayModification()
'displayModification1.NewColor = layerColorID(1)
'displayModification1.NewFont = DisplayableObject.ObjectFont.LongDashed
displayModification1.NewWidth = DisplayableObject.ObjectWidth.Thick
displayModification1.ApplyToAllFaces = false
displayModification1.ApplyToOwningParts = False

For Each tempObj As DisplayableObject In visibleObjects
'test if object is an arc
If TypeOf tempObj Is face Then
tempArc = tempObj
If tempArc.color <> 13 Then
'do something with small arc
arcList.Add(tempArc)
End If
End If
Next

If arcList.Count > 0 Then

displayModification1.NewColor = GenerateRandomString(2, False)
displayModification1.Apply(arcList.ToArray)
End If
displayModification1.Dispose()
Next
End Sub
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************




End Module

 
Replies continue below

Recommended for you

NX uses an indexed color palette; the color numbers range from 1 to 216 (inclusive). There is no need to build a random string for the color number, just pick a random integer within the bounds.

In interactive NX, when you select a solid body in the assembly and change the color, you have the option to "apply changes to owning part". Try recording a journal while using this option to see what code you need to add to your own journal.

www.nxjournaling.com
 
What I am trying to obtain is a journal that cycles through every individual part and changes the color to one of the selected random colors. so for my journal changes each part to the came color. also it doesn't change the actual part itself just the assembly representation. Can you help me achieve this?
 
One of the problems is that every time through the "reportComponentChildren" subroutine, you process all of the visible objects in the work part. Nothing in your code causes the work part to change, therefore you are processing the same visible objects each time through. All of the visible faces will end up as color 13 or a random color.

www.nxjournaling.com
 
Is there someone out there that can help me rewrite this code to get it to do what I want it to do?

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Assemblies
Imports System.Windows.Forms
Imports System.IO
Imports System.Collections
Imports System.Collections.Generic
Imports System.Environment
Imports NXOpenUI

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


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
if not IsNothing(c.RootComponent) then
ReportComponentChildren(c.RootComponent, 0)
else
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try

End Sub
'*******************************************************************

Public Function GenerateRandomString(ByRef len As Integer, ByRef upper As Boolean) As String
Dim rand As New Random()
Dim allowableChars() As Char = "0123456789".ToCharArray()
Dim final As String = String.Empty
For i As Integer = 0 To len - 1
final += allowableChars(rand.Next(allowableChars.Length - 1))
Next

Return IIf(upper, final.ToUpper(), final)
End Function


'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, _
ByVal indent As Integer)
Dim S As Session = Session.GetSession()
Dim workPart As Part = S.Parts.Work
Dim ufs As UFSession = UFSession.GetUFSession()
Dim selobj As NXObject
Dim arc1 As ibasecurve
Dim cpoint As point = Nothing
Dim kk As Integer = 0
Dim type As Integer
Dim subtype As Integer
Dim lw As ListingWindow = s.ListingWindow
Dim layerObjects() As NXObject
Dim layerObjectsDisplayable() As DisplayableObject
Dim layerColorID(256) As Integer
layerColorID(1) = 6

Dim arcList As New List(Of face)

For Each child As Component In comp.GetChildren()
'*** insert code to process component or subassembly



'Get visible objects in work view
Dim visibleObjects() As DisplayableObject
visibleObjects = workPart.Views.WorkView.AskVisibleObjects
Dim tempArc As face

Dim displayModification1 As DisplayModification
displayModification1 = S.DisplayManager.NewDisplayModification()
'displayModification1.NewColor = layerColorID(1)
'displayModification1.NewFont = DisplayableObject.ObjectFont.LongDashed
displayModification1.NewWidth = DisplayableObject.ObjectWidth.Thick
displayModification1.ApplyToAllFaces = false
displayModification1.ApplyToOwningParts = False

For Each tempObj As DisplayableObject In visibleObjects
'test if object is an arc
If TypeOf tempObj Is face Then
tempArc = tempObj
If tempArc.color <> 13 Then
'do something with small arc
arcList.Add(tempArc)
End If
End If
Next

If arcList.Count > 0 Then

displayModification1.NewColor = GenerateRandomString(2, False)
displayModification1.Apply(arcList.ToArray)
End If
displayModification1.Dispose()
Next
End Sub
'**********************************************************
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
'**********************************************************

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor