Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.UF
Module PointCloudTools2
Dim s As Session = Session.GetSession()
Dim ui As UI = UI.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = s.ListingWindow
Dim wp As Part = s.Parts.Work
Sub Main()
Dim response As Selection.Response = Nothing
Dim dplane1 As DatumPlane = Nothing
Dim dplaneorigin As Point3d = Nothing
Dim dplanenormal As Vector3d = Nothing
Dim dplanelayer As Integer = Nothing
Dim pntcol As PointCollection = wp.Points
Dim pntarray() As Point = pntcol.ToArray
Dim workLayer As Integer = wp.Layers.WorkLayer
Dim stateArray1(0) As Layer.StateInfo
Dim m1 As Session.UndoMarkId = s.SetUndoMark(Session.MarkVisibility.Visible, "M1")
Start1:
response = selectDatumPlane(dplane1)
If response = Selection.Response.Cancel Or response = Selection.Response.Back _
Then GoTo End1
dplaneorigin = dplane1.Origin
dplanenormal = dplane1.Normal
dplanelayer = dplane1.Layer
If dplanelayer <> workLayer Then
stateArray1(0).Layer = dplanelayer
stateArray1(0).State = Layer.State.WorkLayer
wp.Layers.ChangeStates(stateArray1, False)
End If
Dim dist1 As Double = Nothing
Dim mindist As Double = 500.0
Dim nearest1 As Integer = Nothing
Dim neglist As New ArrayList
Dim poslist As New ArrayList
Dim planelist As New ArrayList
Dim planept3d As New Point3d
Dim planepoint As Point = Nothing
For Each pt As Point In pntarray
dist1 = NormalDistance1(dplaneorigin, dplanenormal, pt)
If dist1 < 0.0 And dist1 > -1.0 Then
neglist.Add(pt)
ElseIf dist1 > 0.0 And dist1 < 1.0 Then
poslist.Add(pt)
ElseIf dist1 = 0.0 Then
planelist.Add(pt)
End If
Next
For Each pt As Point In planelist
pt.Layer = dplanelayer
Next
For i As Integer = 0 To poslist.Count - 1
For j As Integer = 0 To neglist.Count - 1
dist1 = Math.Sqrt((poslist(i).Coordinates.X - neglist(j).Coordinates.X) ^ 2 + _
(poslist(i).Coordinates.Y - neglist(j).Coordinates.Y) ^ 2 + _
(poslist(i).Coordinates.Z - neglist(j).Coordinates.Z) ^ 2)
If dist1 < mindist Then
mindist = dist1
nearest1 = j
End If
Next
planept3d = PlanePoint1(dplaneorigin, dplanenormal, _
poslist(i), neglist(nearest1))
planepoint = wp.Points.CreatePoint(planept3d)
planepoint.SetVisibility(SmartObject.VisibilityOption.Visible)
planelist.Add(planepoint)
mindist = 500.0
Next
neglist.Clear()
poslist.Clear()
planelist.Clear()
GoTo Start1
End1:
s.UpdateManager.DoUpdate(m1)
End Sub
Function selectDatumPlane(ByRef object1 As DatumPlane) As Selection.Response
Dim selectionMask_array(0) As Selection.MaskTriple
selectionMask_array(0).Type = UFConstants.UF_datum_plane_type
selectionMask_array(0).Subtype = 0
selectionMask_array(0).SolidBodySubtype = 0
Dim cursor As Point3d = Nothing
Dim resp As Selection.Response = _
ui.SelectionManager.SelectTaggedObject("Datum Plane Selection", "Select a DatumPlane", _
Selection.SelectionScope.WorkPart, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, selectionMask_array, object1, cursor)
If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
ElseIf resp = Selection.Response.Back Then
Return Selection.Response.Back
ElseIf resp = Selection.Response.Cancel Then
Return Selection.Response.Cancel
End If
End Function
Public Function DotVecPoint(ByVal v1 As Vector3d, ByVal p1 As Point3d) As Double
Return v1.X * p1.X + v1.Y * p1.Y + v1.Z * p1.Z
End Function
Public Function DotVecVec(ByVal v1 As Vector3d, ByVal v2 As Vector3d) As Double
Return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z
End Function
Public Function SubtractPoints1(ByVal p1 As Point3d, ByVal p2 As Point3d) As Point3d
Dim p3 As Point3d
p3.X = p1.X - p2.X
p3.Y = p1.Y - p2.Y
p3.Z = p1.Z - p2.Z
Return p3
End Function
Public Function SubtractPoints2(ByVal p1 As Point3d, ByVal p2 As Point3d) As Vector3d
Dim p3 As Vector3d
p3.X = p1.X - p2.X
p3.Y = p1.Y - p2.Y
p3.Z = p1.Z - p2.Z
Return p3
End Function
Public Function NormalDistance1(ByVal dplaneorigin As Point3d, ByVal dplanenormal As Vector3d, _
ByVal testpoint As Point) As Double
Dim dist1 As Double = Nothing
Dim vec2 As Vector3d = Nothing
Dim temppnt As Point3d = Nothing
Dim testpnt As Point3d = New Point3d(testpoint.Coordinates.X, testpoint.Coordinates.Y, _
testpoint.Coordinates.Z)
temppnt = SubtractPoints1(dplaneorigin, testpnt)
dist1 = DotVecPoint(dplanenormal, temppnt)
Return dist1
End Function
Public Function UnitizeVector(ByVal v1 As Vector3d) As Vector3d
Dim v2 As vector3d
Dim length As Double = math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z)
v2.x = v1.x / length
v2.y = v1.y / length
v2.z = v1.z / length
Return v2
End Function
Public Function AffineCombPointVec(ByVal p1 As Point3d, ByVal s As Double, ByVal v2 As Vector3d) As Point3d
Dim p3 As Point3d
p3.X = p1.X + s * v2.X
p3.Y = p1.Y + s * v2.Y
p3.Z = p1.Z + s * v2.Z
Return p3
End Function
Public Function PlanePoint1(ByVal dplaneorigin As Point3d, ByVal dplanenormal As Vector3d, _
ByVal pospoint As Point, ByVal negpoint As Point) As Point3d
Dim sub1 As Double = Nothing
Dim sub2 As Double = Nothing
Dim vec2 As Vector3d = Nothing
Dim temppnt As Point3d = Nothing
Dim upperpnt As Point3d = New Point3d(pospoint.Coordinates.X, pospoint.Coordinates.Y, _
pospoint.Coordinates.Z)
Dim lowerpnt As Point3d = New Point3d(negpoint.Coordinates.X, negpoint.Coordinates.Y, _
negpoint.Coordinates.Z)
temppnt = SubtractPoints1(dplaneorigin, upperpnt)
sub1 = DotVecPoint(dplanenormal, temppnt)
Dim vect1 As Vector3d = Nothing
vect1 = SubtractPoints2(lowerpnt, upperpnt)
Dim vect2 As Vector3d = Nothing
vect2 = UnitizeVector(vect1)
sub2 = DotVecVec(dplanenormal, vect2)
Dim sub3 As Double = sub1 / sub2
Dim planept3d As New Point3d
planept3d = AffineCombPointVec(upperpnt, sub3, vect2)
Return planept3d
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