Does anyone worked on creating a WinForm UI to select a point or curve in NX using C#?

Hi All,
Does anyone worked on selecting a point or curve in NX using winform UI in C#? I'm well aware of Block and UI styler and all the options. But as we have constrains using both the options, I need your help in creating a winform UI to select a point or curve in NX and pass it as a tag in code for further use. Any help will be appreciated. Thanks in advance.

It's going to be hard. You need to read the cursor position, and then translate this into a 3D "ray" (an infinite line in 3D space). Then you need to cycle through all the objects in the part file, finding the one the ray hits, or passes close to. You would essentially be rewriting the NX selection functionality. Getting it work at all will require a lot of effort. Getting it to work with good performance will require even more effort. Unless you have vast amounts of time, and zero money, I'd recommend buying additional SW licenses. You could buy a Block Styler license. Or you could buy a SNAP license, which lets you create "BlockForm" dialogs without Block Styler.

If you are working with plain vanilla journals (no author license, no block styler license), then your options are fairly limited. You can show a winform as a modal dialog, which means that it will have focus and you will not be able to interact with NX while it is shown.

I have some old example code (written in VB, posted below) that uses a winform to allow the user to set a selection filter; those choices are then passed to the main journal and used in the selection function. By passing information between the journal and the form, a more elaborate interaction would be possible. For instance, one could select options on the form, close the form, select the objects in the NX window, then show the form again with information about the selected objects. So, depending on your desired workflow, it may be possible to accomplish with a journal and a winform.

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

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim theForm As New Form1
theForm.ShowDialog()

If theForm.DialogResult = System.Windows.Forms.DialogResult.Cancel Then
MsgBox("user pressed 'Cancel', exit journal")
Return
End If

MsgBox("continue journal code in Sub Main()")

Dim selectionMask() As Selection.MaskTriple = Nothing
BuildSelectionMask(selectionMask, theForm.chkFaces.Checked, theForm.chkCurves.Checked)

Dim userSelectedObjects() As TaggedObject = Nothing
If SelectObjects(selectionMask, userSelectedObjects) = Selection.Response.Cancel Then
lw.WriteLine("object selection canceled")
Return
End If

lw.WriteLine("Options selected:")
lw.WriteLine("Select Solids: " & theForm.chkSolids.Checked.ToString)
lw.WriteLine("Select Faces: " & theForm.chkFaces.Checked.ToString)
lw.WriteLine("Select Curves: " & theForm.chkCurves.Checked.ToString)

lw.WriteLine("")
lw.WriteLine(userSelectedObjects.Length.ToString & " object(s) selected")

For Each temp As TaggedObject In userSelectedObjects
lw.WriteLine("object type: " & temp.GetType.ToString)
lw.WriteLine("object tag: " & temp.Tag.ToString)
lw.WriteLine("")
Next

lw.Close()

End Sub

Function SelectObjects(ByVal selMask() As Selection.MaskTriple, ByRef selObj() As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim prompt As String = "Select Objects"
Dim title As String = "Selection"
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.WorkPart

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects( _
prompt, title, scope, selAction, _
includeFeatures, keepHighlighted, selMask, selObj)

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

End Function

Sub BuildSelectionMask(ByRef maskArray() As Selection.MaskTriple, ByVal faces As Boolean, ByVal curves As Boolean)

Dim theSelectionMasks As New List(Of Selection.MaskTriple)

Dim newSelMask As Selection.MaskTriple
With newSelMask
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

theSelectionMasks.Add(newSelMask)

If faces Then
newSelMask.Type = UFConstants.UF_solid_type
newSelMask.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
theSelectionMasks.Add(newSelMask)
End If

If curves Then
newSelMask.Type = UFConstants.UF_line_type
newSelMask.Subtype = UFConstants.UF_line_normal_subtype
theSelectionMasks.Add(newSelMask)

newSelMask.Type = UFConstants.UF_circle_type
newSelMask.Subtype = UFConstants.UF_all_subtype
theSelectionMasks.Add(newSelMask)

newSelMask.Type = UFConstants.UF_conic_type
newSelMask.Subtype = UFConstants.UF_all_subtype
theSelectionMasks.Add(newSelMask)

newSelMask.Type = UFConstants.UF_spline_type
newSelMask.Subtype = UFConstants.UF_all_subtype
theSelectionMasks.Add(newSelMask)

End If

maskArray = theSelectionMasks.ToArray

End Sub

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

'************************************************************************************************

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()

End Sub

End Class

'************************************************************************************************

_
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
_
Private Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.chkCurves = New System.Windows.Forms.CheckBox()
Me.chkFaces = New System.Windows.Forms.CheckBox()
Me.chkSolids = New System.Windows.Forms.CheckBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.chkCurves)
Me.GroupBox1.Controls.Add(Me.chkFaces)
Me.GroupBox1.Controls.Add(Me.chkSolids)
Me.GroupBox1.Location = New System.Drawing.Point(12, 26)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(260, 118)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Selection Options"
'
'chkCurves
'
Me.chkCurves.AutoSize = True
Me.chkCurves.Location = New System.Drawing.Point(22, 76)
Me.chkCurves.Name = "chkCurves"
Me.chkCurves.Size = New System.Drawing.Size(59, 17)
Me.chkCurves.TabIndex = 2
Me.chkCurves.Text = "Curves"
Me.chkCurves.UseVisualStyleBackColor = True
'
'chkFaces
'
Me.chkFaces.AutoSize = True
Me.chkFaces.Location = New System.Drawing.Point(22, 53)
Me.chkFaces.Name = "chkFaces"
Me.chkFaces.Size = New System.Drawing.Size(55, 17)
Me.chkFaces.TabIndex = 1
Me.chkFaces.Text = "Faces"
Me.chkFaces.UseVisualStyleBackColor = True
'
'chkSolids
'
Me.chkSolids.AutoSize = True
Me.chkSolids.Checked = True
Me.chkSolids.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkSolids.Enabled = False
Me.chkSolids.Location = New System.Drawing.Point(22, 30)
Me.chkSolids.Name = "chkSolids"
Me.chkSolids.Size = New System.Drawing.Size(54, 17)
Me.chkSolids.TabIndex = 0
Me.chkSolids.Text = "Solids"
Me.chkSolids.UseVisualStyleBackColor = True
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 165)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(84, 34)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Select Objects"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Button2.Location = New System.Drawing.Point(188, 165)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(84, 34)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Cancel"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AcceptButton = Me.Button1
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.CancelButton = Me.Button2
Me.ClientSize = New System.Drawing.Size(284, 218)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.GroupBox1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.ResumeLayout(False)

End Sub
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents chkCurves As System.Windows.Forms.CheckBox
Friend WithEvents chkFaces As System.Windows.Forms.CheckBox
Friend WithEvents chkSolids As System.Windows.Forms.CheckBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
End Class

After selecting all items, it's possible to display the ''Form1'' again and repeat the selection procedure...one...two...more time?

Thank You!

Pat

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

Module Module1

Public TheSession As Session = Session.GetSession()
Public TheUI As UI = UI.GetUI()
Public TheUFSession As UFSession = UFSession.GetUFSession()
Public ufs As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()

Sub Main(ByVal args() As String)

Dim workPart As Part = TheSession.Parts.Work
Dim lw As ListingWindow = TheSession.ListingWindow

If IsNothing(TheSession.Parts.Work) Then
'active part required
Return
End If

lw.Open()
Echo("PTS-0")
Dim theForm As New Form1()
theForm.ShowDialog()
Echo("PTS-1")

If theForm.DialogResult = System.Windows.Forms.DialogResult.Cancel Then
MsgBox("User pressed 'Cancel', exit journal")
Return
End If

'MsgBox("continue journal code in Sub Main()")

Dim selectionMask() As Selection.MaskTriple = Nothing
BuildSelectionMask(selectionMask, theForm.chkFaces.Checked, theForm.chkCurves.Checked)

Dim userSelectedObjects() As TaggedObject = Nothing
If SelectObjects(selectionMask, userSelectedObjects) = Selection.Response.Cancel Then
Echo("object selection canceled")
Return
End If

Echo("Options selected:")
Echo("Select Solids: " & theForm.chkSolids.Checked.ToString)
Echo("Select Faces: " & theForm.chkFaces.Checked.ToString)
Echo("Select Curves: " & theForm.chkCurves.Checked.ToString)

Echo("")
Echo(userSelectedObjects.Length.ToString & " object(s) selected")

For Each temp As TaggedObject In userSelectedObjects
Echo("object type: " & temp.GetType.ToString)
Echo("object tag: " & temp.Tag.ToString)
Echo("")
Next

lw.Close()

End Sub

Function SelectObjects(ByVal selMask() As Selection.MaskTriple, ByRef selObj() As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim prompt As String = "Select Objects"
Dim title As String = "Selection"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific

Dim scope As Selection.SelectionScope = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY 'Selection.SelectionScope.WorkPart

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, title, scope, selAction, includeFeatures, keepHighlighted, selMask, selObj)

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

End Function

Sub BuildSelectionMask(ByRef maskArray() As Selection.MaskTriple, ByVal faces As Boolean, ByVal curves As Boolean)

Dim theSelectionMasks As New List(Of Selection.MaskTriple)

Dim newSelMask As Selection.MaskTriple
With newSelMask
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

theSelectionMasks.Add(newSelMask)

If faces Then
newSelMask.Type = UFConstants.UF_solid_type
newSelMask.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE
theSelectionMasks.Add(newSelMask)
End If

If curves Then
newSelMask.Type = UFConstants.UF_line_type
newSelMask.Subtype = UFConstants.UF_line_normal_subtype
theSelectionMasks.Add(newSelMask)

newSelMask.Type = UFConstants.UF_circle_type
newSelMask.Subtype = UFConstants.UF_all_subtype
theSelectionMasks.Add(newSelMask)

newSelMask.Type = UFConstants.UF_conic_type
newSelMask.Subtype = UFConstants.UF_all_subtype
theSelectionMasks.Add(newSelMask)

newSelMask.Type = UFConstants.UF_spline_type
newSelMask.Subtype = UFConstants.UF_all_subtype
theSelectionMasks.Add(newSelMask)

End If

maskArray = theSelectionMasks.ToArray
End Sub

'FUNCTION: Echo
Public Function Echo(ByVal output As String)
TheSession.ListingWindow.Open()
TheSession.ListingWindow.WriteLine(output)
TheSession.LogFile.WriteLine(output)
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

'************************************************************************************************

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Close()

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()

End Sub

End Class

'************************************************************************************************
'Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

End Sub

'Form overrides dispose to clean up the component list.
'
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.

Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents chkCurves As System.Windows.Forms.CheckBox
Friend WithEvents chkFaces As System.Windows.Forms.CheckBox
Friend WithEvents chkSolids As System.Windows.Forms.CheckBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button

'Private Sub InitializeComponent()
Private Sub InitializeComponent()
'Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form_NXOpen_Template))
'Dim ico As Icon = Icon.ExtractAssociatedIcon("C:\Temp_NX\SettingNX\NxToolbar\ug_ModSimon\Application\EportParasolid\ExportParasolid\ExportParasolid_V4\Icon_NXOpen_Template.ico"

Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.chkCurves = New System.Windows.Forms.CheckBox()
Me.chkFaces = New System.Windows.Forms.CheckBox()
Me.chkSolids = New System.Windows.Forms.CheckBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.chkCurves)
Me.GroupBox1.Controls.Add(Me.chkFaces)
Me.GroupBox1.Controls.Add(Me.chkSolids)
Me.GroupBox1.Location = New System.Drawing.Point(12, 26)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(260, 118)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Selection Options"
'
'chkCurves
'
Me.chkCurves.AutoSize = True
Me.chkCurves.Location = New System.Drawing.Point(22, 76)
Me.chkCurves.Name = "chkCurves"
Me.chkCurves.Size = New System.Drawing.Size(59, 17)
Me.chkCurves.TabIndex = 2
Me.chkCurves.Text = "Curves"
Me.chkCurves.UseVisualStyleBackColor = True
'
'chkFaces
'
Me.chkFaces.AutoSize = True
Me.chkFaces.Location = New System.Drawing.Point(22, 53)
Me.chkFaces.Name = "chkFaces"
Me.chkFaces.Size = New System.Drawing.Size(55, 17)
Me.chkFaces.TabIndex = 1
Me.chkFaces.Text = "Faces"
Me.chkFaces.UseVisualStyleBackColor = True
'
'chkSolids
'
Me.chkSolids.AutoSize = True
Me.chkSolids.Checked = True
Me.chkSolids.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkSolids.Enabled = False
Me.chkSolids.Location = New System.Drawing.Point(22, 30)
Me.chkSolids.Name = "chkSolids"
Me.chkSolids.Size = New System.Drawing.Size(54, 17)
Me.chkSolids.TabIndex = 0
Me.chkSolids.Text = "Solids"
Me.chkSolids.UseVisualStyleBackColor = True
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 165)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(84, 34)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Select Objects"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Button2.Location = New System.Drawing.Point(188, 165)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(84, 34)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Cancel"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AcceptButton = Me.Button1
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.CancelButton = Me.Button2
Me.ClientSize = New System.Drawing.Size(284, 218)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.GroupBox1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.ResumeLayout(False)

End Sub
#End Region

End Class
End Module

Pat