Selection filter to only select componets in assembly

Hello,

I am attempting to make my selection filter work and select only object of the type component as if I changed my selection filter to component from the tool bar.

I have used the code from here to set up my selection but need to know what to add to the election filter to make it select a component.

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

What version of NX are you running? Some of the selection functions have been deprecated in newer NX versions. Below are functions for NX 8 or newer.

Select a single component:

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

Dim theUI as UI = UI.GetUI
Dim title As String = "Select a component"
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.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_all_subtype
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

Select multiple components:

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

Dim theUI as UI = UI.GetUI
Dim title As String = "Select components"
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 = UFConstants.UF_component_type
.Subtype = UFConstants.UF_all_subtype
End With

Dim resp as Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj)
If resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Hi,

Can the below part of the above be modified to make the 'OK (with nothing selected)' and 'Cancel' button work? With the use of same codes as below, shows an error while hitting OK button with nothing selected and also CANCEL button.


If resp = Selection.Response.Ok Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

Regards,
MFJ

I'm not sure that I understand your situation. I have a journal that makes use of the function above and if nothing is selected, it does not cause an error.

I suspect there is something else in your code that is causing an error when nothing has been selected...

Thanks for your comment. Below is all the codes in the vb I am using. Not sure where exactly is the problem. But suspecting that it is on line 97..where that 'commit()' is; that's where the error msg is asking to check on line 97.


' NX 11.0.0.33
' Journal created by z003h08a on Fri Apr 28 08:36:10 2017 Eastern Daylight Time
'
Imports System
Imports NXOpen
Imports System.Collections.Generic
Imports NXOpen.UF
Imports NXOpenUI

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 lw As ListingWindow = theSession.ListingWindow

' ----------------------------------------------
' Menu: Edit->Move Object...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim nullNXOpen_Features_MoveObject As NXOpen.Features.MoveObject = Nothing

Dim moveObjectBuilder1 As NXOpen.Features.MoveObjectBuilder = Nothing
moveObjectBuilder1 = workPart.BaseFeatures.CreateMoveObjectBuilder(nullNXOpen_Features_MoveObject)

moveObjectBuilder1.TransformMotion.DistanceAngle.OrientXpress.AxisOption = NXOpen.GeometricUtilities.OrientXpressBuilder.Axis.Passive

moveObjectBuilder1.TransformMotion.DistanceAngle.OrientXpress.PlaneOption = NXOpen.GeometricUtilities.OrientXpressBuilder.Plane.Passive

moveObjectBuilder1.TransformMotion.AlongCurveAngle.AlongCurve.IsPercentUsed = True

moveObjectBuilder1.TransformMotion.AlongCurveAngle.AlongCurve.Expression.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.AlongCurveAngle.AlongCurve.Expression.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.OrientXpress.AxisOption = NXOpen.GeometricUtilities.OrientXpressBuilder.Axis.Passive

moveObjectBuilder1.TransformMotion.OrientXpress.PlaneOption = NXOpen.GeometricUtilities.OrientXpressBuilder.Plane.Passive

moveObjectBuilder1.TransformMotion.Option = NXOpen.GeometricUtilities.ModlMotion.Options.Dynamic

moveObjectBuilder1.TransformMotion.DistanceValue.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.DistanceBetweenPointsDistance.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.RadialDistance.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.Angle.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.DistanceAngle.Distance.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.DistanceAngle.Angle.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.DeltaEnum = NXOpen.GeometricUtilities.ModlMotion.Delta.ReferenceWcsWorkPart

moveObjectBuilder1.TransformMotion.DeltaXc.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.DeltaYc.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.DeltaZc.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.AlongCurveAngle.AlongCurve.Expression.RightHandSide = "0"

moveObjectBuilder1.TransformMotion.AlongCurveAngle.AlongCurveAngle.RightHandSide = "0"

moveObjectBuilder1.MoveObjectResult = NXOpen.Features.MoveObjectBuilder.MoveObjectResultOptions.CopyOriginal

theSession.SetUndoMarkName(markId1, "Move Object Dialog")

moveObjectBuilder1.MoveObjectResult = NXOpen.Features.MoveObjectBuilder.MoveObjectResultOptions.MoveOriginal

moveObjectBuilder1.MoveObjectResult = NXOpen.Features.MoveObjectBuilder.MoveObjectResultOptions.CopyOriginal

'Dim body1 As NXOpen.Body = CType(workPart.Bodies.FindObject("SB_MIGRATED_GROUND_PANEL(35)"), NXOpen.Body)
Dim body1 As NXOpen.Body = Nothing

SelectBody("Select an Object", body1)

Dim added1 As Boolean = Nothing
added1 = moveObjectBuilder1.ObjectToMoveObject.Add(body1)

Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Move Object")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As NXOpen.Session.UndoMarkId = Nothing
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Move Object")

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = moveObjectBuilder1.Commit()

Dim objects1() As NXOpen.NXObject
objects1 = moveObjectBuilder1.GetCommittedObjects()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "Move Object")

moveObjectBuilder1.Destroy()

' ----------------------------------------------
' START DELETING FEATURES OTHER THAN 'BODY'
' ----------------------------------------------

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "remove non-body features")

lw.Open()

Dim nonBodyFeatures As New List(Of Features.Feature)

For Each temp As Features.Feature In theSession.Parts.Work.Features
If Not TypeOf (temp) Is NXOpen.Features.Brep Then
nonBodyFeatures.Add(temp)
End If
Next

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(nonBodyFeatures.ToArray)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId4)

lw.Close()

End Sub

Function SelectBody(ByVal prompt As String, ByRef SelBody As Body) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select A Body ::: Creating Independent Body"
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.AnyInAssembly
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,
SelBody, 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 when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

Regards,
MFJ

Dim body1 As NXOpen.Body = Nothing

SelectBody("Select an Object", body1)

Dim added1 As Boolean = Nothing
added1 = moveObjectBuilder1.ObjectToMoveObject.Add(body1)

Ok, two items of note here:
1) "SelectBody" is a function that returns a result. Specifically, it returns "Selection.Response.Ok" or "Selection.Response.Cancel". It will only return "Ok" if one or more items are actually selected, otherwise it returns "cancel". The journal code could check this return result and take the appropriate action.

2) When the "body1" variable is declared, it is given the initial value of "Nothing". If the user does not select anything, "body1" will still have the value of "Nothing" when the function returns. The journal code could check to see if "body1" is "Nothing" and take the appropriate action.

If the "body1" variable is "Nothing", you will get an error when calling .Commit on the move object builder.

A snippet from one of my journals that makes use of a similar selection function is below; if the response is "cancel" (i.e. nothing was selected or the user pressed the cancel button), the "return" statement is called. The return statement will exit the currently running sub or function. In my case, this is called from sub Main so the journal exits (user pressed cancel, do nothing and exit the journal).

If SelectComponents("Select components", myComponents) = Selection.Response.Cancel Then
Return
End If

Alternately, you can check the "body1" variable like this:

if IsNothing(body1) then
'nothing selected, take appropriate action here
end if

I have tried both under the Function SelectBody() , seems to be not working...

I have also tried put the below moveobjectBuilder command inside an if loop , but the selection.response is within the Function and moveobjectBuilder depends on the selection.response, so I am little bit lost here.


nXObject1 = moveObjectBuilder1.Commit()

Regards,
MFJ

I suggest moving the call to the selection function before you create the move object builder. This way, if nothing is selected, you can skip the rest of the code. The proposed outline is shown below:

'initialization code
Sub Main()
'sub initialization code

If SelectBody("Select an Object", body1) = Selection.Response.Cancel Then
Return
End If

'code to create and use move object builder
End Sub

'SelectBody function
End Module

Did just as you suggested and it worked. Thanks a lot.

Regards,
MFJ