Help with selecting faces please

Hi wonderful NXJ community!

Could someone please tell me what is wrong with what I am doing. I am trying to select faces and return them as an array. It should be simple. I am migrating from "selectTaggedObject" which is a single face to "selectTaggedObjects" which is an array (right?)

My code is this


Public Function SelectFaces(ByVal propt As String) As Face()

Dim title As String = "Select faces"
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(0) As Selection.MaskTriple
Dim selectedFaces() As Face = Nothing

With selectionMask(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
End With

Dim responce1 As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(
propt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask, selectedFaces)

If responce1 = Selection.Response.Ok Then
Return selectedFaces
Else
Return Nothing
End If

End Function

When debugging, this gives an error on the line with .selectTaggedObject( that says "Unable to cast object of type 'NXOpen.TaggedObject[]' to type 'NXOpen.Face[]'"

I don't understand. When I used the "selectTaggedObject" I passed in a variable of type Face for it to put what you selected into. Now i have a Face() and it should put all the faces you select into that array.

Am I missing something?

Pleeeeease help

PS. selectObjects says it has been superseded by selectTaggedObjects

You'll need to convert the tagged objects to face objects. The code below shows one way to do it. It makes use of a list object, which I prefer to using an array; but, in keeping with your intent, it converts the list to an array and returns that array.

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

Module Module67

Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

lw.Open()

Dim theFaces() As Face = Nothing
theFaces = SelectFaces("select some faces")

If IsNothing(theFaces) Then
Return
End If

For Each item As Face In theFaces
lw.WriteLine("face tag: " & item.Tag.ToString)
Next

lw.Close()
End Sub

Public Function SelectFaces(ByVal propt As String) As Face()

Dim theUI As UI = UI.GetUI
Dim title As String = "Select faces"
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(0) As Selection.MaskTriple
Dim selectedObjects() As TaggedObject = Nothing
Dim selectedFaces As New List(Of Face)

With selectionMask(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
End With

Dim responce1 As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(
propt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask, selectedObjects)

If responce1 = Selection.Response.Ok Then
For Each item As TaggedObject In selectedObjects
selectedFaces.Add(item)
Next
Return selectedFaces.ToArray
Else
Return Nothing
End If

End Function

End Module

Thanks... it worked!

Dim faces1() As NXOpen.Face = Nothing
faces1 = SelectFaces("Select Some faces to delete")

Dim faceDumbRule1 As NXOpen.FaceDumbRule = Nothing
faceDumbRule1 = workPart.ScRuleFactory.CreateRuleFaceDumb(faces1)

Dim rules1(0) As NXOpen.SelectionIntentRule
rules1(0) = faceDumbRule1
deleteFaceBuilder1.FaceCollector.ReplaceRules(rules1, False)
deleteFaceBuilder1.Type = NXOpen.Features.DeleteFaceBuilder.SelectTypes.Face
Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = deleteFaceBuilder1.Commit()
deleteFaceBuilder1.Destroy()
lw.WriteLine("Faces removed")

End Sub

Public Function SelectFaces(ByVal propt As String) As Face()

Dim theUI As UI = UI.GetUI
Dim title As String = "Select faces"
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(0) As Selection.MaskTriple
Dim selectedObjects() As TaggedObject = Nothing
Dim selectedFaces As New List(Of Face)

With selectionMask(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
End With

Dim responce1 As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(
propt, title, scope, selAction, includeFeatures, keepHighlighted, selectionMask, selectedObjects)

If responce1 = Selection.Response.Ok Then
For Each item As TaggedObject In selectedObjects
selectedFaces.Add(item)
Next
Return selectedFaces.ToArray
Else
Return Nothing
End If

End Function

Regards,
MFJ