cancel
Showing results for 
Search instead for 
Did you mean: 

Select Point on Face Dialog with VB.NET/Snap

Valued Contributor
Valued Contributor

I want to measure the linear distance between 2 points on a object but I am encountering 1 problem - I do not know how to show a dialog and the user to select a point on a object (Point on Face) programmingly and user can select a face to have a point shown like the picture

Capture.PNG

 Tried something like

 

 Dim dialog = Snap.UI.Selection.SelectObject("Please select a Point")
        dialog.SetFilter(Snap.NX.ObjectTypes.Type.Point)
        dialog.Scope = Snap.UI.Selection.Dialog.SelectionScope.WorkPart
        Dim result = dialog.Show()

but it did not do what I want.

 

I also found the code froms solution center but I have not idea how to convert it to VB.NET

 

https://solutions.industrysoftware.automation.siemens.com/view.php?sort=desc&pd=nx&q=%22point+on+face%22&dt=nx-api&file_type=text&i=nx_api1784&k=2&o=0

 

 

 

 

 

 

 

 

5 REPLIES

Re: Select Point on Face Dialog with VB.NET/Snap

Legend
Legend

Below is the same code in VB for selecting point on face.

 

  Private Function MapAbs2View(aView As View, loc As Point3d) As Point3d
        Dim vmx As Matrix3x3 = aView.Matrix
        Dim center As Point3d = aView.AbsoluteOrigin
        Dim vw As Double() = {center.X, center.Y, center.Z, vmx.Xx, vmx.Xy, vmx.Xz, _
            vmx.Yx, vmx.Yy, vmx.Yz}
        Dim abs As Double() = {0, 0, 0, 1, 0, 0, _
            0, 1, 0}
        Dim mx As Double() = New Double(11) {}
        Dim irc As Integer = 0
        Dim c As Double() = {loc.X, loc.Y, loc.Z}

        theUFSession.Trns.CreateCsysMappingMatrix(abs, vw, mx, irc)
        theUFSession.Trns.MapPosition(c, mx)

        Return New Point3d(c(0), c(1), c(2))
    End Function

    Private Function MapView2Abs(aView As View, loc As Point3d) As Point3d
        Dim vmx As Matrix3x3 = aView.Matrix
        Dim center As Point3d = aView.AbsoluteOrigin
        Dim vw As Double() = {center.X, center.Y, center.Z, vmx.Xx, vmx.Xy, vmx.Xz, _
            vmx.Yx, vmx.Yy, vmx.Yz}
        Dim abs As Double() = {0, 0, 0, 1, 0, 0, _
            0, 1, 0}
        Dim mx As Double() = New Double(11) {}
        Dim irc As Integer = 0
        Dim c As Double() = {loc.X, loc.Y, loc.Z}

        theUFSession.Trns.CreateCsysMappingMatrix(vw, abs, mx, irc)
        theUFSession.Trns.MapPosition(c, mx)

        Return New Point3d(c(0), c(1), c(2))
    End Function

    Public Function SelectPointOnFace(prompt As String, ByRef location As Point3d) As Face
        Dim selobj As TaggedObject = Nothing
        Dim masks As Selection.MaskTriple() = {New Selection.MaskTriple(UFConstants.UF_solid_type, 0, UFConstants.UF_UI_SEL_FEATURE_ANY_FACE)}
        Dim cursor As Point3d

        Dim resp As Selection.Response = UI.GetUI().SelectionManager.SelectTaggedObject(prompt, "Select a face", Selection.SelectionScope.AnyInAssembly, Selection.SelectionAction.ClearAndEnableSpecific, False, False, _
            masks, selobj, cursor)

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

        cursor = MapAbs2View(workPart.Views.WorkView, cursor)

        Dim start As New Point3d(cursor.X, cursor.Y, cursor.Z + 1000)
        Dim endp As New Point3d(cursor.X, cursor.Y, cursor.Z - 1000)

        start = MapView2Abs(workPart.Views.WorkView, start)
        endp = MapView2Abs(workPart.Views.WorkView, endp)

        Dim tempLine As Line = workPart.Curves.CreateLine(start, endp)

        Dim min_dist As Double
        Dim pt_on_curve As Double() = New Double(2) {}
        Dim pt_on_line As Double() = New Double(2) {}

        theUFSession.Modl.AskMinimumDist(selobj.Tag, tempLine.Tag, 0, Nothing, 0, Nothing, _
            min_dist, pt_on_curve, pt_on_line)

        theSession.UndoToMark(markId2, "")
        theSession.DeleteUndoMark(markId2, Nothing)

        location = New Point3d(pt_on_curve(0), pt_on_curve(1), pt_on_curve(2))

        Return CType(selobj, Face)
    End Function

Re: Select Point on Face Dialog with VB.NET/Snap

Siemens Phenom Siemens Phenom
Siemens Phenom

You're not really "selecting" a point. In NX, "selecting" means clicking on an existing object, and you don't have an existing point object.

 

Actually, what you are doing (I think) is selecting a face, and then you want to know where on the face the user clicked. So, there are two steps:

(1) Select the face, then

(2) Use the cursor position to calculate a position on the face

 

The general technique is described in the "Getting Started with SNAP" guide in a section called "Using the Cursor Ray". In your case, you need to take the cursor ray and intersect it with the selected face. To compute the intersection, call Snap.Compute.Intersection(ray, face).

 

The C code you found is doing a similar computation. But it doesn't have a cursor ray to work with, so the computation is more laborious.

山田
yamada

Re: Select Point on Face Dialog with VB.NET/Snap

Siemens Phenom Siemens Phenom
Siemens Phenom

Here is some sample code:

 

Option Infer On
Imports Snap, Snap.UI, Snap.NX.ObjectTypes

Public Class PointOnFace

   Public Shared Sub Main

      Dim cue = "Please click on a face"
      Dim dialog = Selection.SelectObject(cue, Type.Face)
      Dim selectionResult As Selection.Result = dialog.Show()

      If selectionResult.Response <> NXOpen.Selection.Response.Cancel Then
         Dim face As Snap.NX.Face = selectionResult.Object
         Dim intersectionPoints = Snap.Compute.Intersect(selectionResult.CursorRay, face) 
         Snap.Create.Point(intersectionPoints(0))
      End If

   End Sub

End Class

 

The cursor ray might intersect the selected face in more than one place. In that case, the intersectionPoints array will have more than one member, and you have to figure out which one you want.

山田
yamada

Re: Select Point on Face Dialog with VB.NET/Snap

Valued Contributor
Valued Contributor

@Technisites@Yamada

First, I would like to thank you both.  Smiley Very Happy thanks, Yamada for the explanation - I had a trouble last nigh because the distance I calculated from the 2 points on the same face = super small. I thought I had my things off somewhere.

Re: Select Point on Face Dialog with VB.NET/Snap

Valued Contributor
Valued Contributor

Because I saw some conflict, I tried

 Dim dialog = Snap.UI.Selection.SelectObject(cue, Snap.NX.ObjectTypes.Type.Face)

I got the error

************** Exception Text **************
NXOpen.NXException: The second given face tag does not refer to a face.
   at NXOpen.UF.UFModl.IntersectCurveToFace(Tag curve_id, Tag face_id, Int32& num_intersections, Double[]& data)
   at Snap.Compute.IntersectInfo(Ray ray, Face face)

Any way to fix this