UGperson
Automotive
- Mar 18, 2004
- 104
can you project curves away from a point onto a surface?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
' Journal to project lines from a point to a datum plane
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Module ProjFromPointToPlane
Dim s As Session = Session.GetSession()
Dim ui As UI = ui.GetUI()
Dim ufs As UFSession = UFSession.GetUFSession()
Sub Main()
Dim wp As Part = s.Parts.Work
PointStart:
' select a point
Dim pnt1 As Point = Nothing
Dim response1 As Selection.Response = Selection.Response.Cancel
response1 = select_a_Point(pnt1)
If response1 = Selection.Response.Back Then GoTo End1
If response1 = Selection.Response.Cancel Then GoTo End1
Dim pnt0 As Point3d = pnt1.Coordinates
DatumStart:
Dim dplane1 As DatumPlane = Nothing
Dim response2 As Selection.Response = Selection.Response.Cancel
response2 = selectDPlane(dplane1)
If response2 = Selection.Response.Back Then GoTo PointStart
If response2 = Selection.Response.Cancel Then GoTo End1
Dim response3 As Selection.Response = Selection.Response.Cancel
CurvesStart:
' datum plane normal
Dim dplanenormal1 As Vector3d = Nothing
Dim dplaneorigin1 As Point3d = Nothing
dplanenormal1 = dplane1.Normal
dplaneorigin1 = dplane1.Origin
' should check that datum plane normal is directed towards the point
' and that the projection is possible. This is not implemented
' select a line to project
Dim line1 As Line = Nothing
Dim startpnt As Point3d = Nothing
Dim endpoint As Point3d = Nothing
Dim vec1 As Vector3d = Nothing
Dim uvec2 As Vector3d
Dim vec2 As Vector3d
Dim sub1 As Double = Nothing
Dim sub2 As Double = Nothing
Dim param1 As Double = Nothing
Dim dplanepnt1 As Point3d = Nothing
Dim dplanepnt2 As Point3d = Nothing
Dim line2 As Line = Nothing
curvesContinue:
response3 = select_a_line("Select line to project", line1)
If line1 Is Nothing Then GoTo End1
' Get line endpoints
startpnt = line1.StartPoint
endpoint = line1.EndPoint
' project each point to datum plane
' startpoint
vec1 = SubtractPoint3d(dplaneorigin1, pnt0)
vec2 = SubtractPoint3d(startpnt, pnt0)
uvec2 = UnitizeVector(vec2)
sub1 = Dot(dplanenormal1, vec1)
sub2 = Dot(dplanenormal1, uvec2)
param1 = sub1 / sub2
dplanepnt1 = AffineComb(pnt0, param1, uvec2)
' endpoint
vec2 = SubtractPoint3d(endpoint, pnt0)
uvec2 = UnitizeVector(vec2)
sub2 = Dot(dplanenormal1, uvec2)
param1 = sub1 / sub2
dplanepnt2 = AffineComb(pnt0, param1, uvec2)
' now create line
line2 = wp.Curves.CreateLine(dplanepnt1, dplanepnt2)
GoTo curvesContinue
End1:
End Sub
Public Function SubtractPoint3d(ByVal p1 As Point3d, ByVal p2 As Point3d) As Vector3d
Dim v1 As Vector3d
v1.X = p1.X - p2.X
v1.Y = p1.Y - p2.Y
v1.Z = p1.Z - p2.Z
Return v1
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 SubtractVectors(ByVal v1 As Vector3d, ByVal v2 As Vector3d) As Vector3d
Dim v3 As Vector3d
v3.X = v1.X - v2.X
v3.Y = v1.Y - v2.Y
v3.Z = v1.Z - v2.Z
Return v3
End Function
Public Function Dot(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 AffineComb(ByVal p1 As Point3d, ByVal s As Double, ByVal v2 As Vector3d) As Point3d
Dim p2 As Point3d
p2.X = p1.X + s * v2.X
p2.Y = p1.Y + s * v2.Y
p2.Z = p1.Z + s * v2.Z
Return p2
End Function
Function selectDPlane(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.SelectObject("Select a Datum Plane", "Select a Datum Plane", _
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
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
Function select_a_Point(ByRef pnt1 As Point) As Selection.Response
Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_point_type
.Subtype = 0
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Select point to project from", "Select a Point", _
Selection.SelectionScope.AnyInAssembly, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, pnt1, 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 select_a_line(ByVal prompt As String, ByRef line1 As Line) As Selection.Response
Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_line_type
.Subtype = 0
.SolidBodySubtype = 0
End With
Dim cursor As Point3d = Nothing
Dim resp As Selection.Response = _
ui.SelectionManager.SelectObject("Select line to project", "Select a line", _
Selection.SelectionScope.WorkPart, _
Selection.SelectionAction.ClearAndEnableSpecific, _
False, False, mask, line1, cursor)
If resp = UFConstants.UF_UI_OBJECT_SELECTED Or _
resp = UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
Return resp
ElseIf resp = Selection.Response.Back Then
Return Selection.Response.Back
Else
Return Selection.Response.Cancel
End If
End Function
End Module