Ehaviv
Computer
- Jul 2, 2003
- 1,012
This journl is based on a journal I foun in the internet
I want that the Z-Axis to be aling with face normal
bat some run its ok and some run its not OK (Z-Axis Not to be aling with face normal)
can some one help.
Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Module set_wcs_at_brd_corner
Sub Main()
SetWcsAtBrdCorner()
End Sub
Sub SetWcsAtBrdCorner()
Dim s As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = s.Parts.Work
Dim planarFace As NXOpen.Tag = NXOpen.Tag.Null
Dim longEdge As NXOpen.Tag = NXOpen.Tag.Null
Dim otherEdge As NXOpen.Tag = NXOpen.Tag.Null
Dim origin() As Double = {0, 0, 0}
Dim origin2 As Point3d = New Point3d(0,0,0)
Dim xVector(2) As Double
Dim yVector(2) As Double
If select_a_planar_face(planarFace) = Selection.Response.Ok Then
ufs.Disp.SetHighlight(planarFace, 0)
End If
Dim faceObject As Face = NXObjectManager.Get(planarFace)
Dim edges() As Edge = faceObject.GetEdges()
longEdge = get_longest_edge(edges)
Dim sp(2) As Double
Dim ep(2) As Double
Dim lineData As UFCurve.Line
ufs.Curve.AskLineData(longEdge, lineData)
sp = lineData.start_point
ep = lineData.end_point
ufs.Vec3.Sub(ep, sp, xVector)
ufs.Vec3.Copy(sp, origin)
Dim sp2(2) As Double
Dim ep2(2) As Double
otherEdge = find_edge_adjacent_to_start_point(edges, sp, longEdge)
ufs.Curve.AskLineData(otherEdge, lineData)
sp2 = lineData.start_point
ep2 = lineData.end_point
If sp2(0) = sp(0) And sp2(1) = sp(1) And sp2(2) = sp(2) Then
ufs.Vec3.Sub(ep2, sp2, yVector)
Else
ufs.Vec3.Sub(sp2, ep2, yVector)
End If
Dim mtx_vals(8) As Double
ufs.Mtx3.Initialize(xVector, yVector, mtx_vals)
Dim amtx3x3 As Matrix3x3
amtx3x3.Xx = mtx_vals(0)
amtx3x3.Xy = mtx_vals(1)
amtx3x3.Xz = mtx_vals(2)
amtx3x3.Yx = mtx_vals(3)
amtx3x3.Yy = mtx_vals(4)
amtx3x3.Yz = mtx_vals(5)
amtx3x3.Zx = mtx_vals(6)
amtx3x3.Zy = mtx_vals(7)
amtx3x3.Zz = mtx_vals(8)
workPart.WCS.SetOriginAndMatrix(origin2, amtx3x3)
End Sub
Function find_edge_adjacent_to_start_point(ByVal edges() As Edge, _
ByVal org() As Double, ByVal longEdge As NXOpen.Tag) As NXOpen.Tag
Dim ufs As UFSession = UFSession.GetUFSession()
Dim thisEdge As NXOpen.Tag = NXOpen.Tag.Null
Dim inx As Integer = 0
For inx = 0 To edges.GetUpperBound(0)
thisEdge = edges(inx).Tag
If thisEdge <> longEdge Then ' to make sure we skip the xVector edge
Dim edgeType As Integer = 0
ufs.Modl.AskEdgeType(thisEdge, edgeType)
If edgeType = UFConstants.UF_MODL_LINEAR_EDGE Then
Dim sp(2) As Double
Dim ep(2) As Double
Dim lineData As UFCurve.Line
'lineData.start_point = sp
'lineData.end_point = ep
ufs.Curve.AskLineData(thisEdge, lineData)
sp = lineData.start_point
ep = lineData.end_point
If sp(0) = org(0) And sp(1) = org(1) And sp(2) = org(2) Then
Return thisEdge
End If
If ep(0) = org(0) And ep(1) = org(1) And ep(2) = org(2) Then
Return thisEdge
End If
End If
End If
Next
End Function
Function get_longest_edge(ByVal edges() As Edge) As NXOpen.Tag
Dim ufs As UFSession = UFSession.GetUFSession()
Dim lgth As Double = 0
Dim inx As Integer = 0
Dim thisEdge As NXOpen.Tag = NXOpen.Tag.Null
Dim longestNumber As Double = 0
Dim longestEdge As NXOpen.Tag = NXOpen.Tag.Null
For inx = 0 To edges.GetUpperBound(0)
thisEdge = edges(inx).Tag
Dim edgeType As Integer = 0
ufs.Modl.AskEdgeType(thisEdge, edgeType)
If edgeType = UFConstants.UF_MODL_LINEAR_EDGE Then
ufs.Curve.AskArcLength(thisEdge, 0, 1, ModlUnits.ModlUnitsPart, lgth)
If lgth > longestNumber Then
longestEdge = thisEdge
longestNumber = lgth
End If
End If
Next inx
Return longestEdge
End Function
Function select_a_planar_face(ByRef face As NXOpen.Tag) As Selection.Response
Dim ufs As UFSession = UFSession.GetUFSession()
Dim message As String = "Select BRD Planar Face:"
Dim title As String = "Select BRD PLANAR FACE"
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_face As UFUi.SelInitFnT = AddressOf mask_for_planar_faces
ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
Try
ufs.Ui.SelectWithSingleDialog(message, title, scope, mask_face, _
Nothing, response, face, 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
Else
Return Selection.Response.Ok
End If
End Function
Function mask_for_planar_faces(ByVal select_ As IntPtr, _
ByVal userdata As IntPtr) As Integer
Dim ufs As UFSession = UFSession.GetUFSession()
Dim num_triples As Integer = 1
Dim mask_triples(0) As UFUi.Mask
mask_triples(0).object_type = UFConstants.UF_solid_type
mask_triples(0).object_subtype = UFConstants.UF_solid_face_subtype
mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_PLANAR_FACE
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
Return Session.LibraryUnloadOption.Immediately
End Function
End Module