Showing results for 
Search instead for 
Did you mean: 

SelectObjects() with Conditions


Hello at all,

I have a problem with my SelectObjects function. I want to select various drilling holes, but if the distance between the centre of the holes are smaller than a given a range, I want to get a warning. Important is:

I select the circle of the holes and later I get the centre in a table.
For example:
Hole1 have to be minimum 100 mm away from Hole2
Hole2 have to be minimum 100 mm away from Hole3
But it isn't important which distance is between Hole1 and Hole3

If I get the warning that the distance is smaller than 100mm, I should choose another Hole.


I didn't found anything by now for putting conditions in the SelectObjects function and also having a problem in getting the distance between two centres by only choosing the holes. I tested some codes but didn't get any solution.


I hope somebody can help me, a wink to another thread would also help me if there is something.


Thank you


If SelectObjects("Select holes",
                       mySelectedObjects) = Selection.Response.Ok Then
    Function SelectObjects(prompt As String,
               ByRef selObj As NXObject()) As Selection.Response
        Dim theUI As UI = UI.GetUI
        'only choose edges
        Dim typeArray() As Selection.SelectionType =
        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(
                prompt, "Selection",
                False, typeArray, selObj)
        If resp = Selection.Response.ObjectSelected Or
                resp = Selection.Response.ObjectSelectedByName Or
                resp = Selection.Response.OK Then
            Return Selection.Response.Ok
            Return Selection.Response.Cancel
        End If
    End Function

Re: SelectObjects() with Conditions

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Some code was provided by NXJournaling for this issue. It illustrates what you can do with some filtering during the selection process.


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module1
    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow
    Sub Main()
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ")
        Dim circularEdges As New List(Of Edge)
        If SelectCircularEdges(circularEdges) = Selection.Response.Cancel Then
        End If
        For Each temp As Edge In circularEdges
            lw.WriteLine("edge tag: " & temp.Tag.ToString)
    End Sub
    Public Function SelectCircularEdges(ByRef theEdges As List(Of Edge)) As Selection.Response
        Dim response As Integer = 0
        Dim user_data As System.IntPtr
        Dim selCount As Integer
        Dim selObj() As Tag = Nothing
        Dim myCursor(2) As Double
        Dim curCursorView As Integer
            theUfSession.Ui.SelectWithClassDialog("Select: ", "Select circular edges",
                UFConstants.UF_UI_SEL_SCOPE_WORK_PART, AddressOf Mask1,
                user_data, response, selCount, selObj)
        End Try
        If response = UFConstants.UF_UI_BACK Or response = UFConstants.UF_UI_CANCEL Then
            Return Selection.Response.Cancel
            If Not selObj.Equals(Tag.Null) Then
                'the SelectWithClassDialog method returns an array of tags,
                'return the objects from the given tags
                For Each tempTag As Tag In selObj
                    Dim circularEdge As Edge = Utilities.NXObjectManager.Get(tempTag)
                'selObj = Tag.Null
                'should not happen
                Return Selection.Response.Cancel
            End If
            Return Selection.Response.Ok
        End If
    End Function
    Public Function Mask1(ByVal select_ As IntPtr,
                              ByVal userdata As IntPtr) As Integer
        'this function must have the same signature as UFUi.SelInitFnT Delegate
        'setup mask to filter for circular edges
        Dim num_triples As Integer = 1
        Dim mask_triples(num_triples - 1) As UFUi.Mask
        With mask_triples(0)
            .object_type = UFConstants.UF_solid_type
            .solid_type = UFConstants.UF_UI_SEL_FEATURE_CIRCULAR_EDGE
        End With
                          num_triples, mask_triples)
        theUfSession.Ui.SetSelProcs(select_, AddressOf Filter1, Nothing, userdata)
        Return UFConstants.UF_UI_SEL_SUCCESS
    End Function
    Public Function Filter1(ByVal _object As Tag,
                                        ByVal type As Integer(),
                                        ByVal user_data As IntPtr,
                                        ByVal select_ As IntPtr) As Integer
        'type and user_data are unused, but this function must have
        'the same signature as UFUi.SelFilterFnT Delegate
        'get the edge currently being considered for selection
        Dim tempEdge As Edge = Utilities.NXObjectManager.Get(_object)
        Dim pt1 As Point = theSession.Parts.Work.Points.CreatePoint(tempEdge, SmartObject.UpdateOption.WithinModeling)
        'get the previously selected edges (if any)
        Dim count As Integer
        Dim selTags() As Tag = Nothing
        theUfSession.Ui.AskSelObjectList(select_, count, selTags)
        If count = 0 Then
            'no other edges selected, allow this selection
            Return UFConstants.UF_UI_SEL_ACCEPT
        End If
        'get the last selected edge
        Dim lastEdge As Edge = Utilities.NXObjectManager.Get(selTags(count - 1))
        'get center point of last selected edge
        Dim pt2 As Point = theSession.Parts.Work.Points.CreatePoint(lastEdge, SmartObject.UpdateOption.WithinModeling)
        'measure distance between center points of last selected edge and current edge being considered
        Dim minDist As Double
        Dim outPt1(2) As Double
        Dim outPt2(2) As Double
        theUfSession.Modl.AskMinimumDist(pt1.Tag, pt2.Tag, 0, Nothing, 0, Nothing, minDist, outPt1, outPt2)
        'if distance is greater or equal to 100, accept the current selection
        If minDist >= 100 Then
            Return UFConstants.UF_UI_SEL_ACCEPT
            Return UFConstants.UF_UI_SEL_REJECT
        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