Single Object Selection with Selection Mask

Single Object Selection with Selection Mask
This document looks at the single object selection method. The dialog allows the user to selects a single object.
The object can be selected with the cursor or by entering a name. The object may be highlighted or not. You can
call the dialog in a loop to select multiple objects. For this option the user indicates they are done selecting by
choosing OK. You must have an active part to call this function. Figure 1 shows a typical selection dialog.
Figure 1: Atypical single object selection dialog.
One SelectObject method is:
SelectObject Method (message, title, scope, action, includeFeatures, keepHighlighted, maskArray, object, cursor)
Namespaces ► NXOpen ► Selection ► SelectObject(String, String, Selection..::.SelectionScope,
Selection..::.SelectionAction, Boolean, Boolean, array<Selection..::.MaskTriple>[]()[], NXObject%, Point3d%)
Function SelectObject ( _
message As String, _
title As String, _
scope As Selection.SelectionScope, _
action As Selection.SelectionAction, _
includeFeatures As Boolean, _
keepHighlighted As Boolean, _
maskArray As Selection.MaskTriple(), _
<OutAttribute> ByRef object As NXObject, _
<OutAttribute> ByRef cursor As Point3d _
) As Selection.Response
Message
Title
Scope
Action
IncludeFeatures
KeepHighlighted
MaskArray
NXObject
Cursor
Selection.Response
The cue line message to display.
The dialog title.
Selection scope. This may be set to
AnyInAssembly
UseDefault
WorkPart
WorkPartAndOccurence
Indicates how the mask array will modify the selection filter
Whether to allow the selection of features
Whether to keep the selection highlighted after it has been selected
Modifies the list of object types that can be selected. How it modifies the list of object
types is determined by the SelectionAction parameter.
Selected object or NULL_TAG if no object selected
Absolute coordinates of cursor position. This is undefined if object is selected by name
Dialog selection response. This can be:
Selection.Response.Back
Selection.Response.Cancel
Selection.Response.Ok
Selection.Response.ObjectSelected
Selection.Response.ObjectSelectedByName.
Two overload functions of SelectObject arealso listed in Appedix 2.
Program Description
The program is used to create a point at the intersection of a datum axis with either a datum plane or a surface.
The user is first asked to select a datum axis. The user is then asked to select either a datum plane or a face.
Depending on the second selection the program then determines the appropriate point. We use the selection
response to determine the path for the program. We ask the object type to determine the appropriate algorithm
for determining the point.
For the point on the face, we first create a point at origin of the datum axis. This point can then be projected
(moved) along the datum axis to the face. The result is a group. We must delete the group which leaves the
group members intact. In our case this is the single projected point.
For the point to the datum plane we use vector algebra which is described in appendix 1.
Flow Diagram
The flow diagram below shows the possible path when users select Back or Cancel when using the selection
dialogs.
Flow Diagram
Program listing
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Module DatumAxisIntesection
Dim s As Session = Session.GetSession()
Dim ui As UI = ui.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Sub Main()
Dim object1 As NXObject = Nothing
Dim dAxis1 As DatumAxis = Nothing
Dim featureTag As Tag = Tag.Null
Dim response As Selection.Response = Nothing
Dim response2 As Selection.Response = Nothing
Dim daxisOrigin() As Double = {0.0, 0.0, 0.0}
Dim daxisNormal() As Double = {1.0, 0.0, 0.0}
Dim dplaneOrigin() As Double = {0.0, 0.0, 0.0}
Dim dplaneNormal() As Double = {1.0, 0.0, 0.0}
Dim point1() As Double = {0.0, 0.0, 0.0}
Dim sub1 As Double = Nothing
Dim sub2 As Double = Nothing
Dim w As Double = Nothing
Dim pointTag As Tag = Tag.Null
Dim type1 As Integer = Nothing
Dim subtype1 As Integer = Nothing
Dim nCurves As Integer = 1
Dim projCurves() As Tag = {Tag.Null}
Dim nFaces As Integer = 1
Dim projFaces() As Tag = {Tag.Null}
Dim flag2 As Integer = 2
Dim projectingData As NXOpen.UF.UFCurve.Proj = Nothing
Dim projectedPoint() As Tag = {Tag.Null}
Dim projectedGroup As Tag = Tag.Null
Dim tempTag As Tag = Tag.Null
Dim error1 As Integer = Nothing
Start1:
response = select_a_DatumAxis(dAxis1)
If response = Selection.Response.Cancel Then GoTo End1
Start2:
response2 = selectDPlaneOrFace(object1)
If response2 = Selection.Response.Back Then GoTo Start1
If response2 = Selection.Response.Cancel Then GoTo End1
' get datum axis data
ufs.Modl.AskObjectFeat(dAxis1.Tag, featureTag)
ufs.Modl.AskDatumAxisParms(featureTag, daxisOrigin, daxisNormal)
' determine if selected object is face or datum plane
ufs.Obj.AskTypeAndSubtype(object1.Tag, type1, subtype1)
If type1 = 70 And subtype1 = 2 Then
' selected object is a face
' create a temporary point on the datum axis
ufs.Curve.CreatePoint(daxisOrigin, tempTag)
' set projecting data
projCurves(0) = tempTag ' the temp point to be projected
projFaces(0) = object1.Tag ' the face to which we are projecting
projectingData.proj_type = 3 ' using a vector
projectingData.proj_vec = daxisNormal ' the projection vector
projectingData.multiplicity = 2 ' this allows projecting in both directions
' create the projected point. The result is a group
ufs.Curve.CreateProjCurves(nCurves, projCurves, nFaces, projFaces, _
flag2, projectingData, projectedGroup)
' Ungrouping all results in just the projected point.
ufs.Group.UngroupAll(projectedGroup)
Else
' selected object is a datum plane
ufs.Modl.AskDatumPlane(object1.Tag, dplaneOrigin, dplaneNormal)
' determine point on dplane
' dplaneOrigin - daxisOrigin
point1(0) = dplaneOrigin(0) - daxisOrigin(0)
point1(1) = dplaneOrigin(1) - daxisOrigin(1)
point1(2) = dplaneOrigin(2) - daxisOrigin(2)
' dplaneNormal dot point1
ufs.Vec3.Dot(dplaneNormal, point1, sub1)
' dplaneNormal dot daxisNormal
ufs.Vec3.Dot(dplaneNormal, daxisNormal, sub2)
' w is the parameter value of a line from daxisOrigin to the dplane along
daxisNormal
w = sub1 / sub2
' calculate the point on the dpane
point1(0) = daxisOrigin(0) + w * daxisNormal(0)
point1(1) = daxisOrigin(1) + w * daxisNormal(1)
point1(2) = daxisOrigin(2) + w * daxisNormal(2)
' create the point
ufs.Curve.CreatePoint(point1, pointTag)
End If
GoTo Start2
End1:
End Sub
Function selectDPlaneOrFace(ByRef object1 As NXObject) As Integer
Dim selectionMask_array(1) As Selection.MaskTriple
selectionMask_array(0).Type = UFConstants.UF_datum_plane_type
selectionMask_array(0).Subtype = 0
selectionMask_array(0).SolidBodySubtype = 0
selectionMask_array(1).Type = UFConstants.UF_solid_type
selectionMask_array(1).Subtype = 0
selectionMask_array(1).SolidBodySubtype =
UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
Dim cursor As Point3d = Nothing
Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Intersection Point: Datum Plane or Face
Selection", "Select a DPlane or Face", _
Selection.SelectionScope.WorkPart, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, selectionMask_array, object1, cursor)
If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
ElseIf resp = Selection.Response.Back Then
Return Selection.Response.Back
ElseIf resp = Selection.Response.Cancel Then
Return Selection.Response.Cancel
End If
End Function
Function select_a_DatumAxis(ByRef dAxis1 As DatumAxis)
Dim selectionMask_array(1) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_datum_axis_type
.Subtype = 0
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Intersection Point: Datum Axis Selection",
"Pick a Datum Axis", _
Selection.SelectionScope.WorkPart, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, selectionMask_array, dAxis1, 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
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'----Other unload options------'Unloads the image when the NX session terminates
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'------------------------------End Function
End Module
The two functions for selecting objects differ only in terms of the type of object selected and that two different
object types may be selected. The selection mask for single objects is set for Datum Axis selection using:
Dim selectionMask_array(1) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_datum_axis_type
.Subtype = 0
.SolidBodySubtype = 0
End With
The selection mask for multiple object types is set for Datum Planes and Faces using:
Dim selectionMask_array(1) As Selection.MaskTriple
selectionMask_array(0).Type = UFConstants.UF_datum_plane_type
selectionMask_array(0).Subtype = 0
selectionMask_array(0).SolidBodySubtype = 0
selectionMask_array(1).Type = UFConstants.UF_solid_type
selectionMask_array(1).Subtype = 0
selectionMask_array(1).SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
The Selection.MaskTriple has a Type, Subtype, and SoliBodySubtype. The SolidBodySubtype is used to access
feature specific data. Some of the more common SolidBodySubtypes are listed in the table below.
SolidBodySubtypes
UF_UI_SEL_FEATURE_ANY_EDGE
UF_UI_SEL_FEATURE_ANY_FACE
UF_UI_SEL_FEATURE_ANY_SHEET
UF_UI_SEL_FEATURE_BCURVE_EDGE
UF_UI_SEL_FEATURE_BLENDING_FACE
UF_UI_SEL_FEATURE_BODY
UF_UI_SEL_FEATURE_CIRCULAR_EDGE
UF_UI_SEL_FEATURE_CONICAL_FACE
UF_UI_SEL_FEATURE_CYLINDRICAL_FACE
UF_UI_SEL_FEATURE_ELLIPTICAL_EDGE
UF_UI_SEL_FEATURE_INTERSECTION_EDGE
UF_UI_SEL_FEATURE_LINEAR_EDGE
UF_UI_SEL_FEATURE_OFFSET_FACE
UF_UI_SEL_FEATURE_PLANAR_FACE
UF_UI_SEL_FEATURE_SHEET_BODY
UF_UI_SEL_FEATURE_SWEPT_FACE
We have a large range of types and subtypes. These are listed in the NXOpen .NET API Reference in the
UFConstants Class. A page showing a number of drafting object types and subtypes is shown below:
The example program listing has many comments included to understand the code. It should also be pointed out
that the program is written with clarity in mind. For example all definitions are placed at the beginning of main to
keep them clear actual code.
Appendix 1
Point on Plane from Point-Vector Projection
To find the projection of a point along a vector to a plane we can use a plane equation and vector algebra as
follows:
The plane equation can be written as follows:
P(u,v) = a + ub + vc
Where the vectors a, b, and c are shown in the figure. The equation of the projection line is given by
P(w) = P0 + wr
with r as a unit vector in the projection direction, P0. Is a point being projected and Q is the final point determined
from P(w)
The projection point Q is the intersection point between the line and the plane; that is, the following equation
must be solved for u, v, and w:
P(u,v) – P(w) = 0
or
a + ub +vc = P0 + wr
To solve for w, dot-multiply both sides of the above equation by (b x c) to get
(b cross c) dot a = (b cross c) dot (P0 + wr)
Since (b cross c) is perpendicular to both b and c the previous equation gives
(b cross c) dot (a – P0)
w=
(b cross c) dot r
Appendix 2
SelectObject Overload Functions
Three overload functions for SelectObject are available.
These are listed below.
SelectObject Method (message, title, scope, includeFeatures, keepHighlighted, object, cursor)
Function SelectObject ( _
message As String, _
title As String, _
scope As Selection.SelectionScope, _
includeFeatures As Boolean, _
keepHighlighted As Boolean, _
<OutAttribute> ByRef object As NXObject, _
<OutAttribute> ByRef cursor As Point3d _
) As Selection.Response
Message
Title
Scope
IncludeFeatures
KeepHighlighted
NXObject
Cursor
Selection.Response
The cue line message to display.
The dialog title.
Selection scope. This may be set to
AnyInAssembly
UseDefault
WorkPart
WorkPartAndOccurence
Whether to allow the selection of features
Whether to keep the selection highlighted after it has been selected
Selected object or NULL_TAG if no object selected
Absolute coordinates of cursor position. This is undefined if object is selected by name
Dialog selection response. This can be:
Selection.Response.Back
Selection.Response.Cancel
Selection.Response.Ok
Selection.Response.ObjectSelected
Selection.Response.ObjectSelectedByName.
SelectObject Method (message, title, scope, action, includeFeatures, keepHighlighted, maskArray,
object, cursor)
Function SelectObject ( _
message As String, _
title As String, _
scope As Selection.SelectionScope, _
action As Selection.SelectionAction, _
includeFeatures As Boolean, _
keepHighlighted As Boolean, _
maskArray As Selection.MaskTriple(), _
<OutAttribute> ByRef object As NXObject, _
<OutAttribute> ByRef cursor As Point3d _
) As Selection.Response
Message
Title
Scope
The cue line message to display.
The dialog title.
Selection scope. This may be set to
Action
IncludeFeatures
KeepHighlighted
MaskArray
NXObject
Cursor
Selection.Response
AnyInAssembly
UseDefault
WorkPart
WorkPartAndOccurence
Indicates how the mask array will modify the selection filter
Whether to allow the selection of features
Whether to keep the selection highlighted after it has been selected
Modifies the list of object types that can be selected. How it modifies the list of object
types is determined by the SelectionAction parameter.
Selected object or NULL_TAG if no object selected
Absolute coordinates of cursor position. This is undefined if object is selected by name
Dialog selection response. This can be:
Selection.Response.Back
Selection.Response.Cancel
Selection.Response.Ok
Selection.Response.ObjectSelected
Selection.Response.ObjectSelectedByName.
SelectObject Method (message, title, scope, keepHighlighted, typeArray, object, cursor)
Function SelectObject ( _
message As String, _
title As String, _
scope As Selection.SelectionScope, _
keepHighlighted As Boolean, _
typeArray As Selection.SelectionType(), _
<OutAttribute> ByRef object As NXObject, _
<OutAttribute> ByRef cursor As Point3d _
) As Selection.Response
Message
Title
Scope
The cue line message to display.
The dialog title.
Selection scope. This may be set to
AnyInAssembly
UseDefault
WorkPart
WorkPartAndOccurence
KeepHighlighted
TypeArray
NXObject
Cursor
Selection.Response
Whether to keep the selection highlighted after it has been selected
The types of objects that can be selected
Selected object or NULL_TAG if no object selected
Absolute coordinates of cursor position. This is undefined if object is selected by name
Dialog selection response. This can be:
Selection.Response.Back
Selection.Response.Cancel
Selection.Response.Ok
Selection.Response.ObjectSelected
Selection.Response.ObjectSelectedByName.