Cancel
Showing results for 
Search instead for 
Did you mean: 

NXOpen.UF.Evalsf.FindClosestPoint

Experimenter
Experimenter

How do I create the required face and point objects for the FindClosestPoint

function in NXOpen (NX11) using the Python API to avoid getting the error "FindClosestPoint requires a NXOpen.UF.Evalsf object" ?

 

from NXOpen.UF import Evalsf

my_face = ??

my_point = ??

result = Evalsf.FindClosestPoint(my_face, my_point)

 

 

1 REPLY 1

Re: NXOpen.UF.Evalsf.FindClosestPoint

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

The face will belong to a body that you create or will already exist in your part file. The point is just a list of doubles that represents the X,Y,Z coordinates near the face that you want to evaluate. The FindClosestPoint method will find the point that lies on the given face that is closest to the input point. It will also return the distance from the input point to the face and the UV parameters of the point found on the face.

 

Open the example part and run the code below. It will prompt you to select a face; it will report the distance from (0,0,0) to the face, the closest point coordinates on the face, and the UV parameters of the point on the face.

 

import NXOpen
import NXOpen.UF

theSession  = NXOpen.Session.GetSession()
theUFSession = NXOpen.UF.UFSession.GetUFSession()
theUI = NXOpen.UI.GetUI()

def echo(what): 
    theSession.LogFile.WriteLine(what)
    theSession.ListingWindow.Open()
    theSession.ListingWindow.WriteFullline(what)
    #theUFSession.Ui.UpdateListingWindow()    

def select_face():
    face = 0
    message = "Select a Face"
    title = "Selection"
    scope = NXOpen.SelectionSelectionScope.WorkPart
    keepHighlighted = False
    typeArray = [NXOpen.SelectionSelectionType.Faces]

    selTuple = theUI.SelectionManager.SelectTaggedObject(
        message, title, scope, keepHighlighted, typeArray)

    if selTuple[0] == NXOpen.SelectionResponse.ObjectSelected:
        face = selTuple[1]

    return face

def main():
    try:
        theFace = select_face()
        if theFace != 0:

            echo("Face: " + str(theFace.Tag))
            
            evalsf = theUFSession.Evalsf.Initialize2(theFace.Tag)
            inputPt = [0.0,0.0,0.0]
            evalsfPos3 = theUFSession.Evalsf.FindClosestPoint2(evalsf, inputPt)
            echo("input point: " + str(inputPt))
            echo("Distance from reference point to face: " + str(evalsfPos3.Distance))
            echo("UV parameter on face: " + str(evalsfPos3.Uv))
            echo("Point on face: " + str(evalsfPos3.Pnt3))
            

    except Exception as ex:
        echo("*** Error: " + str(ex))

if __name__ == '__main__':
    main()