Multi face select

Hi,
I have small recorded macro to change color of selected face to my desired color code I need a small change that it should allow and apply to multiple faces selected... where i need to change in code

Option Strict Off
Imports System
Imports NXOpen

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

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

Dim displayPart As NXOpen.Part = theSession.Parts.Display

Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Edit Object Display")

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

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 105

Dim objects1(0) As NXOpen.DisplayableObject
Dim theUI As UI = UI.GetUI()

objects1(0) = CType(theUI.SelectionManager.GetSelectedObject(0), NXOpen.Face)

displayModification1.Apply(objects1)

displayModification1.Dispose()

End Sub
End Module

If you want the user to specify the faces, you can add an interactive selection function. See the following links for details:

http://nxjournaling.com/content/adding-interactive-selection-routine-usi...

http://nxjournaling.com/content/using-mask-triples-selectobject-routine

If you would like the journal code to select the faces for you, you will need to write an algorithm so that it knows which faces you want changed.

Got the code from link still unable to get please check code below

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim mySelectedObjects As NXObject()
Dim displayModification1 As NXOpen.DisplayModification
Dim theUI As UI = UI.GetUI()
displayModification1 = theSession.DisplayManager.NewDisplayModification()

If SelectObjects("Hey, select multiple somethings", _
mySelectedObjects) = Selection.Response.Ok Then

For Each mySelObj As NXObject in mySelectedObjects

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 211

displayModification1.Apply(mySelectedObjects)

displayModification1.Dispose()
Next

End if

' The close method closes the text stream to the window,
' it does not close the window itself
' (use the .CloseWindow() method for that).
' Also, if you are using the listing window to write
' to a file, the close method will clean up and close the file.

End Sub

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

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

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selobj)

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

End Function

End Module

-[]-

There is a problem with how you use the displayModification object in the section of code shown below:

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

If SelectObjects("Hey, select multiple somethings", _
mySelectedObjects) = Selection.Response.Ok Then

For Each mySelObj As NXObject in mySelectedObjects

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 211

displayModification1.Apply(mySelectedObjects)

displayModification1.Dispose()
Next

End if

Here we see that the displayModification1 object is created before the loop but the .Dispose method is called within the loop. What this means is that the first time through the loop, the displayModification1 object gets destroyed. If you have selected more than one object, you will get an error (or odd/unexpected results) on subsequent iterations of the For loop.

If you are applying the same display modification(s) to every object in the array, you can eliminate the For loop entirely. The displayModification1 object will accept an array of objects as input. You can pass in the entire array at once instead of passing in the objects one at a time.

If SelectObjects("Hey, select multiple somethings", _
mySelectedObjects) = Selection.Response.Ok Then

Dim displayModification1 As NXOpen.DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 211

displayModification1.Apply(mySelectedObjects)

displayModification1.Dispose()

End if

Hi,
No luck so far
please check the code below

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim mySelectedObjects As NXObject()
'Dim displayModification1 As NXOpen.DisplayModification
Dim theUI As UI = UI.GetUI()
'displayModification1 = theSession.DisplayManager.NewDisplayModification()

If SelectObjects("Hey, select multiple somethings", _
mySelectedObjects) = Selection.Response.Ok Then

Dim displayModification1 As NXOpen.DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 211

displayModification1.Apply(mySelectedObjects)

displayModification1.Dispose()

End if
End Sub

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

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

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selobj)

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

End Function

End Module

-[]-

Try this version. I modified your code to use the version of the select objects function that uses the mask triples to limit selection to solid bodies. You can add other mask triples to the code to allow selection of other objects (faces, curves, points, etc). Your original code was allowing selection of entities (such as features) that were not allowed as input to the display modification object.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module60
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim mySelectedObjects As New List(Of DisplayableObject)
Dim theUI As UI = UI.GetUI()

If SelectObjects("Hey, select multiple somethings",
mySelectedObjects) = Selection.Response.Ok Then

Dim displayModification1 As NXOpen.DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 211

displayModification1.Apply(mySelectedObjects.ToArray)

displayModification1.Dispose()

End If
End Sub

Function SelectObjects(prompt As String,
ByRef dispObj As List(Of DisplayableObject)) As Selection.Response

Dim selObj As NXObject()
Dim theUI As UI = UI.GetUI
Dim title As String = "Select objects"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = NXOpen.UF.UFConstants.UF_solid_type
.SolidBodySubtype = NXOpen.UF.UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt,
title, scope, selAction,
includeFeatures, keepHighlighted, selectionMask_array,
selObj)

If resp = Selection.Response.ObjectSelected Or
resp = Selection.Response.ObjectSelectedByName Or
resp = Selection.Response.Ok Then
For Each item As NXObject In selObj
dispObj.Add(CType(item, DisplayableObject))
Next
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

End Module

You need a loop like Do Until... Loop instead of If... End If (for checking selection response). If I see it right, it runs the SelectObjects only once.
Use a listing window or msgbox to check which part of your code is running and how many times.

Got It.... Thank you.
My new code.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module60
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim mySelectedObjects As New List(Of DisplayableObject)
Dim theUI As UI = UI.GetUI()

If SelectObjects("Hey, select multiple somethings",
mySelectedObjects) = Selection.Response.Ok Then

Dim displayModification1 As NXOpen.DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = True

displayModification1.ApplyToOwningParts = True

displayModification1.NewColor = 211

displayModification1.Apply(mySelectedObjects.ToArray)

displayModification1.Dispose()

End If
End Sub

Function SelectObjects(prompt As String,
ByRef dispObj As List(Of DisplayableObject)) As Selection.Response

Dim selObj As NXObject()
Dim theUI As UI = UI.GetUI
Dim title As String = "Select objects"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = NXOpen.UF.UFConstants.UF_face_type
.SolidBodySubtype = NXOpen.UF.UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects(prompt,
title, scope, selAction,
includeFeatures, keepHighlighted, selectionMask_array,
selObj)

If resp = Selection.Response.ObjectSelected Or
resp = Selection.Response.ObjectSelectedByName Or
resp = Selection.Response.Ok Then
For Each item As NXObject In selObj
dispObj.Add(CType(item, DisplayableObject))
Next
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

End Module

-[]-

Thank you for sharing the code,

I tried it and it’s working fine except for one thing. After using it I can't undo what I did. There is any thing that I can do to solve this problem?

Thank you

Add an undo mark before the operation that you want the ability to undo. The code looks something like this:

Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")