Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.UF
Module MaximumDistanceSplines
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = s.Parts.Work
Dim lw As ListingWindow = s.ListingWindow
Sub Main()
Dim spline1 As NXOpen.Tag = NXOpen.Tag.Null
Dim spline2 As NXOpen.Tag = NXOpen.Tag.Null
Dim response1 As NXOpen.Selection.Response = Selection.Response.Cancel
Dim response2 As NXOpen.Selection.Response = Selection.Response.Cancel
Dim title1 As String = "Select Spline 1"
Dim title2 As String = "Select Spline 2"
start1:
response1 = select_spline(spline1, title1)
If response1 = Selection.Response.Cancel Then Exit Sub
If response1 = Selection.Response.Back Then Exit Sub
response2 = select_spline(spline2, title2)
If response2 = Selection.Response.Cancel Then Exit Sub
If response2 = Selection.Response.Back Then GoTo start1
Dim lowerparm As Double = 0.0
Dim upperparm As Double = 1.0
Dim lowerdistance As Double = 0.0
Dim upperdistance As Double = 0.0
Dim middistance As Double = 0.0
Dim cnt2 As Integer = 0
Dim pt1(2), pt2(2), pt3(2) As Double
' first iteration
determineNewLimits(spline1, spline2, lowerparm, upperparm, lowerdistance, middistance, upperdistance, pt1, pt2, pt3)
Dim testdel As Double = Math.Abs((lowerdistance + upperdistance) / 2.0 - middistance)
While testdel > 0.0001
determineNewLimits(spline1, spline2, lowerparm, upperparm, lowerdistance, middistance, upperdistance, pt1, pt2, pt3)
testdel = Math.Abs((lowerdistance + upperdistance) / 2.0 - middistance)
cnt2 += 1
If cnt2 > 10 Then Exit While
End While
' create the points
Dim temptag As Tag = NXOpen.Tag.Null
ufs.Curve.CreatePoint(pt1, temptag)
ufs.Curve.CreatePoint(pt2, temptag)
ufs.Curve.CreatePoint(pt3, temptag)
End Sub
Sub determineNewLimits(ByVal spline1 As Tag, ByVal spline2 As Tag, ByRef lowerparm As Double, ByRef upperparm As Double, _
ByRef lowerdistance As Double, ByRef middistance As Double, ByRef upperdistance As Double, _
ByRef point3() As Double, ByRef point4() As Double, ByRef point5() As Double)
Dim junk3(2) As Double
Dim junk As Double
Dim vec1(2), vec2(2), vec3(2), vec4(2) As Double
Dim unitv0(2), unitv1(2), unitv2(2), unitv3(2), unitv4(2) As Double
Dim point1(2), point2(2) As Double
Dim parm1, parm2 As Double
Dim l0, l1, l2, del1, del2 As Double
Dim distances1(10) As Double
Dim parms1(10) As Double
Dim parms2(10) As Double
Dim cnt1 As Integer = 0
Dim delparm As Double = (upperparm - lowerparm) / 10.0
For parm As Double = lowerparm + delparm To upperparm - delparm Step delparm
cnt1 += 1
ufs.Modl.AskCurveProps(spline1, parm, point1, vec1, junk3, junk3, junk, junk)
ufs.Modl.AskCurveProps(spline2, parm, point2, vec2, junk3, junk3, junk, junk)
ufs.Vec3.Sub(point1, point2, vec3)
ufs.Vec3.Unitize(vec3, 0.001, l0, unitv1)
l0 /= 2.0
point3(0) = (point1(0) + point2(0)) / 2.0
point3(1) = (point1(1) + point2(1)) / 2.0
point3(2) = (point1(2) + point2(2)) / 2.0
ufs.Modl.AskCurveParm(spline1, point3, parm1, point4)
If parm1 < 0 Or parm1 > 1 Then Continue For
ufs.Vec3.Distance(point3, point4, l1)
ufs.Modl.AskCurveParm(spline2, point3, parm2, point5)
If parm2 < 0 Or parm2 > 1 Then Continue For
ufs.Vec3.Distance(point3, point5, l2)
del1 = l1 - l2
del2 = Math.Abs(del1)
Dim cnt2 As Integer = 0
While del2 > 0.001
l0 = l0 + del1 / 2.0
ufs.Vec3.AffineComb(point2, l0, unitv1, point3)
ufs.Modl.AskCurveParm(spline1, point3, parm1, point4)
If parm1 < 0 Or parm1 > 1 Then Continue For
ufs.Modl.AskCurveParm(spline2, point3, parm2, point5)
If parm2 < 0 Or parm2 > 1 Then Continue For
ufs.Vec3.Distance(point3, point4, l1)
ufs.Modl.AskCurveParm(spline2, point3, parm2, point5)
If parm2 < 0 Or parm2 > 1 Then Continue For
ufs.Vec3.Distance(point3, point5, l2)
del1 = l1 - l2
del2 = Math.Abs(del1)
cnt2 += 1
If cnt2 > 10 Then
MsgBox("Not converging")
Exit Sub
End If
End While
distances1(cnt1) = l1 + l2
parms1(cnt1) = parm1
parms2(cnt1) = parm2
Next
ufs.Disp.SetHighlight(spline1, 0)
ufs.Disp.SetHighlight(spline2, 0)
distances1(0) = lowerparm
distances1(10) = upperparm
For i As Integer = 2 To 8
If distances1(i - 2) < distances1(i - 1) And distances1(i - 1) > distances1(i) Then
lowerparm = lowerparm + delparm * (i - 2)
upperparm = lowerparm + delparm * i
lowerdistance = distances1(i - 2)
middistance = distances1(i - 1)
upperdistance = distances1(i)
Exit For
End If
Next
End Sub
Function select_spline(ByRef spline As NXOpen.Tag, ByVal title As String) _
As Selection.Response
Dim mssg As String = "Mid Points:"
Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
Dim response As Integer
Dim view As NXOpen.Tag
Dim cursor(2) As Double
Dim mask_splines As UFUi.SelInitFnT = AddressOf mask_for_splines
ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
Try
ufs.Ui.SelectWithSingleDialog(mssg, title, scope, mask_splines, _
Nothing, response, spline, cursor, view)
Finally
ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
End Try
If response <> UFConstants.UF_UI_OBJECT_SELECTED And _
response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
Return Selection.Response.Cancel
ElseIf response = Selection.Response.Back Then
Return Selection.Response.Back
Else
Return Selection.Response.Ok
End If
End Function
Function mask_for_splines(ByVal select_ As IntPtr, _
ByVal userdata As IntPtr) As Integer
Dim num_triples As Integer = 1
Dim mask_triples(0) As UFUi.Mask
mask_triples(0).object_type = UFConstants.UF_spline_type
mask_triples(0).object_subtype = UFConstants.UF_spline_subtype
mask_triples(0).solid_type = 0
ufs.Ui.SetSelMask(select_, _
UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _
num_triples, mask_triples)
Return UFConstants.UF_UI_SEL_SUCCESS
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