INHERIT

Hello,

I recorded a journal for changing surface or body color by inherit method but it is not working on different surface or body.

My requirement is to apply a color for surface and solid bodies by inherit method.

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Edit->Object Display...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId1, "Class Selection Dialog")

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "Class Selection")

theSession.DeleteUndoMark(markId1, Nothing)

' ----------------------------------------------
' Dialog Begin Edit Object Display
' ----------------------------------------------
Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId4, "Inherit Dialog")

' ----------------------------------------------
' Dialog Begin Inherit
' ----------------------------------------------
Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Inherit")

theSession.DeleteUndoMark(markId5, Nothing)

Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Inherit")

theSession.DeleteUndoMark(markId6, Nothing)

theSession.SetUndoMarkName(markId4, "Inherit")

theSession.DeleteUndoMark(markId4, Nothing)

' ----------------------------------------------
' Dialog Begin Edit Object Display
' ----------------------------------------------
Dim markId7 As Session.UndoMarkId
markId7 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = False

displayModification1.NewColor = 5

displayModification1.NewWidth = DisplayableObject.ObjectWidth.One

Dim objects1(0) As DisplayableObject
Dim body1 As Body = CType(workPart.Bodies.FindObject("UNPARAMETERIZED_FEATURE(5)"), Body)

objects1(0) = body1
displayModification1.Apply(objects1)

displayModification1.Dispose()
theSession.CleanUpFacetedFacesAndEdges()

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

The following code will prompt you to select an object; the color of the object chosen will determine the color to inherit. The journal will then prompt to select a solid or sheet body, the color of the body will be changed to that of the previously chosen object. The prompt to select a new body will loop until "cancel" is chosen.

'inherit_color

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "inherit color"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

'select parent object to inherit color from
Dim theParentObj As TaggedObject
Dim theParentColor As Integer
If SelectParentObject("select object to inherit color from", theParentObj) = Selection.Response.Cancel Then
Return
End If

theParentColor = CType(theParentObj, DisplayableObject).Color

'select objects to inherit color
Dim theChildObj As TaggedObject
Do While SelectObject("select object to change color", theChildObj) = Selection.Response.Ok
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.NewColor = theParentColor

Dim objects1(0) As DisplayableObject

objects1(0) = theChildObj
displayModification1.Apply(objects1)

displayModification1.Dispose()
Loop

lw.Close()

End Sub

Function SelectParentObject(prompt As String, _
ByRef selObj As NXObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim cursor As Point3d
Dim typeArray() As Selection.SelectionType = _
{Selection.SelectionType.All, _
Selection.SelectionType.Faces, _
Selection.SelectionType.Edges, _
Selection.SelectionType.Curves}

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selObj, cursor)

If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Function SelectObject(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select an object"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module