Let User Select an Object by clicking on it

Hi!

In my current Makro I want to have the feature that the user can click on an object and can change it. The changing stuff all works, my problem is that I don't know how he can select an object by clicking on it.
Right now he can use a dialogbox and type in the name, but I want to change this to simply clicking on the item. My code is right now:
----------------------------------------
Public Function getGewaehlteVerbindung()

Dim returnGewaehlteVerbindung As NXObject
Dim theUI As UI = UI.GetUI
Dim cursor As Point3d = Nothing
Dim selectionMask_array(1) As Selection.MaskTriple

Try
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 mySelectedObject As NXObject

lw.Open()

Do Until SelectAnObject("Wählen Sie bitte die zu ändernde Verbindung aus", _
mySelectedObject) = Selection.Response.Cancel 'war Davor .Cancel
lw.WriteLine("Object Tag: " & mySelectedObject.Tag)
lw.WriteLine("Object Type: " & mySelectedObject.GetType.ToString)
lw.WriteLine("")
Loop

lw.Close()
returnGewaehlteVerbindung = mySelectedObject
Finally

End Try

Return returnGewaehlteVerbindung

End Function

----------------------------------------------
Public Function SelectAnObject(ByVal prompt As String, _
ByRef selObj As NXObject) As Selection.Response
Dim udobject As NXObject
Dim numberSelected As Integer
Dim theUI As UI = ui.GetUI
Dim cursor As Point3d
Dim typeArray() As Selection.SelectionType = _
{Selection.SelectionType.All, _
Selection.SelectionType.Faces, _
Selection.SelectionType.Edges, _
Selection.SelectionType.Features}

Dim resp As Selection.Response = theUI.SelectionManager.SelectObject(prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selObj, cursor)
numberSelected = theUI.SelectionManager.GetNumSelectedObjects

--------------------------------------

My problem is that the object should be selected by CLICKING on it not by typing the name. Something like that:

1. Makro asks user "Do you want to change something"?
2. User says yes
3.User can click on assembly Navigator
4. User can select an Object

I tried everything from getSelectedObject, SelectionManager,....

Thank you for your help!!

The .SelectObject method will allow you to select something by typing in the name OR by clicking on it. When you run the code, make sure that your selection filter is set to "no selection filter" or to the type of object you wish to select.

After you start the journal above and click on an object, the variable "mySelectedObject" will refer to the object that was selected. You can edit or query this object as necessary; as shown by the code that writes out the object type to the listing window. You may want to remove the Do loop from the code above, otherwise it will keep prompting the user to select an object.

If the code does not allow you to select an object (with the mouse), please describe the behavior you are experiencing (including any error messages).

Thank you for your reply! Sadly it is still not working. I deleted the Loop and the SelectioScope is set to "All in Assembly" so it should be possible to select the object by clicking on it. Or is the Selection Filter something different? If so where can I set it?

Nevertheless, "Clicking" is still not working. The behavior is as following:

1: A Dialog Box (aka the Selection.Response) is opening in NX and it is asking for the Name of the Object I want to select. ( If I type a Name in it is working)
2: If I want to click on something outside that Dialog Box I just get the typical Error Sound and I can't select anything.

I post the transformed code below.
-------------------------------------------------

Public Function getGewaehlteVerbindung()
Dim InputObjectType(0)
Dim returnGewaehlteVerbindung As NXObject ' Ut: War daov rMIcrosoft COllection
Dim theUI As UI = UI.GetUI
Dim cursor As Point3d = Nothing
Dim selectionMask_array(1) As Selection.MaskTriple

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 mySelectedObject As NXObject
lw.Open()

Dim selResp As Selection.Response = SelectAnObject("Wählen Sie bitte die zu ändernde Verbindung aus", _ mySelectedObject)
lw.WriteLine("Object Tag: " & mySelectedObject.Tag)
lw.WriteLine("Object Type: " & mySelectedObject.GetType.ToString)
lw.WriteLine("")
lw.Close()
returnGewaehlteVerbindung = mySelectedObject
Return returnGewaehlteVerbindung

End Function
------------------------------------------------
Public Function SelectAnObject(ByVal prompt As String, _
ByRef selObj As NXObject) As Selection.Response
Dim udobject As NXObject
Dim numberSelected As Integer
Dim theUI As UI = UI.GetUI
Dim cursor As Point3d
Dim object1 As NXObject
Dim selectionMask_array() As Selection.MaskTriple

Dim typeArray() As Selection.SelectionType = _
{Selection.SelectionType.All, _
Selection.SelectionType.Faces, _
Selection.SelectionType.Edges, _
Selection.SelectionType.Features
}
Dim resp As Selection.Response = theUI.SelectionManager.SelectObject(prompt, "VerbindungAuswählen", Selection.SelectionScope.WorkPart, False, typeArray, object1, cursor)

End Function

I can't run your code directly as it isn't a complete journal. I don't understand the need for the getGewaehlteVerbindung function, it seems to only add a layer on top of the SelectObject function.

The journal below should allow you to select anything in the graphics window by clicking on it. You can change your selection filter (found in the selection bar) after starting the journal (and before selecting something).

Also, make sure the layer settings allow you to select objects. If the layer setting is "visible only", you will not be allowed to select with the mouse.

selection filter


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 mySelectedObject As NXObject

lw.Open

If SelectAnObject("Hey, select something", mySelectedObject) = Selection.Response.Cancel Then return

lw.WriteLine("Object Tag: " & mySelectedObject.Tag)
lw.WriteLine("Object Type: " & mySelectedObject.GetType.ToString)
lw.WriteLine("")

lw.Close

End Sub

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

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

Dim resp As Selection.Response = theUI.SelectionManager.SelectObject( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selobj, 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

End Module

Below is an example that uses a reworked version of your function (some unnecessary code was removed). In Sub Main, the "mySelectedObject" is set by calling your function then it is used to write info to the listing window. Instead of writing to the info window, your program would make the desired changes to the "mySelectedObject".

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 mySelectedObject As NXObject

lw.Open

mySelectedObject = getGewaehlteVerbindung

'replace the following 3 lines of code with your desired changes.
lw.WriteLine("Object Tag: " & mySelectedObject.Tag)
lw.WriteLine("Object Type: " & mySelectedObject.GetType.ToString)
lw.WriteLine("")

lw.Close

End Sub

Public Function getGewaehlteVerbindung() as NXObject
Dim returnGewaehlteVerbindung As NXObject ' Ut: War daov rMIcrosoft COllection
SelectAnObject("Wählen Sie bitte die zu ändernde Verbindung aus", returnGewaehlteVerbindung)
Return returnGewaehlteVerbindung

End Function

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

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

Dim resp As Selection.Response = theUI.SelectionManager.SelectObject( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selobj, 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

End Module

Thank you very much for your answer. Your code is working if I use it separately from the main Makro. Then I am able to select some parts.

If I use it in the "Main Makro" it is not working. I can start the selection process but I am not able to click on the Product window. I am forced to type something into the Selection window.

My guess is, that I create a modular window, but I need a non-modular one... If that makes any sense?

Are you using an NX dialog box or a Winform?

If you are using a Winform inside a journal, you must use a modal form (one that takes control away from NX). But, if you have an author license, you can compile the code to a .dll which will allow you to use a non-modal form.

If you have an author license along with a block styler license, you can create a selection dialog similar to a native NX dialog.