×
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

VB Help!!! Changing Part color in a assembly

VB Help!!! Changing Part color in a assembly

VB Help!!! Changing Part color in a assembly

(OP)
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

RE: VB Help!!! Changing Part color in a assembly

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

RE: VB Help!!! Changing Part color in a assembly

(OP)
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?

RE: VB Help!!! Changing Part color in a assembly

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

RE: VB Help!!! Changing Part color in a assembly

(OP)
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
'**********************************************************

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