MeasureManager vs AskMinimumDist

Hello,

I am attempting to measure the distance between two components for a large quantity of components. Currently I am using this code to measure:

Dim measureDistance as MeasureDistance
Dim currentSteel as Component
Dim sourceComp as Component
'Do stuff get your components
measureDistance = workPart.MeasureManager.NewDistance(unit, MeasureManager.MeasureType.Minimum, currentSteel, sourceComp)

However the measuremanager seems very expensive! Measuring through 500+ components is very process heavy and takes a while. I'm looking for ways to cut back on execution time.

My first thoughts were to try out some ufuncts. For example:

Dim minDistance As Double = Nothing
Dim curSteelPoint(2) As Double
Dim sourceCompPoint(2) As Double
UfSession.Modl.AskMinimumDist(currentSteel.Tag, sourceComp.Tag, 0, Nothing, 0, Nothing, minDistance, curSteelPoint, sourceCompPoint)

Sadly this produces the following error.
"Caught exception while running: Main
NXOpen.NXException: Invalid object type
at NXOpen.UF.UFModl.AskMinimumDist(Tag object1, Tag object2, Int32 guess1_given, Double[] guess1, Int32 guess2_given, Double[] guess2, Double& min_dist, Double[] pt_on_obj1, Double[] pt_on_obj2)
"

Looking at the documentation for AskMinimumDist, it only accepts these data types:
The objects can be:
1. any combination of points, curves, planes, edges, faces or bodies.
2. a faceted body and another faceted body or a solid edge, face
or body or a point

I think this is caused from passing in a component. I suppose I'm asking does anyone have any idea on what I can do to improve processing or utilize the ask minimum distance call? I thought of possibly using the component to get the source part and then try to measure using a solid body? But I'm unsure of if that will cause an issue with the measurement. How can one ensure the measurement will be accurate if you're no longer measuring the component but the part?

Thanks, if you have any questions or comments let me know!

You are correct that the AskMinimumDist function does not like the component input. A "component" in NX is not a geometrical entity, but rather like a container to hold a body or other geometry. There is no way to measure a component, you must measure to a body, face, edge, curve, etc.

The display part controls the WCS. As long as the assembly is the display part, NX will take care of measuring to the correct locations.

Try the following code (if you pick a component, the code will error out).

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If

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

Dim obj1 As TaggedObject
Dim obj2 As TaggedObject

If SelectAnObject("select object 1", obj1) = Selection.Response.Cancel Then
Return
End If

If SelectAnObject("select object 1", obj2) = Selection.Response.Cancel Then
Return
End If

Dim guess1(2) As Double
Dim guess2(2) As Double
Dim pt1(2) As Double
Dim pt2(2) As Double
Dim minDist As Double

theUfSession.Modl.AskMinimumDist(obj1.Tag, obj2.Tag, 0, guess1, 0, guess2, minDist, pt1, pt2)

lw.WriteLine("minimum distance: " & minDist.ToString)

lw.Close()

End Sub

Function SelectAnObject(prompt As String, _
ByRef selObj As TaggedObject) 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.CurvesAndEdges}

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject( _
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

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