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