Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
'Imports NXOpen.Drawings
'Imports NXOpen.Assemblies
'Imports NXOpen.PDM
'Imports System.Collections
Imports System.Collections.Generic
'Imports NXOpen.Annotations
'Imports System.Text
'Imports NXOpen.Utilities
'Imports System.Text.RegularExpressions
'Imports System.Diagnostics
'Imports NXOpen.Routing
'Imports NXOpen.Features
Module Lkl_rename_My_MODULE
Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim ufs As UFSession = UFSession.GetUFSession()
Dim output_To_NXListingWindow As Boolean = True
Dim output_To_NXLogFile As Boolean = True
Sub Main()
ufs.Ui.ExitListingWindow() ' close windows before displaying it again
workPart.ModelingViews.WorkView.Regenerate()
'START HERE
Dim thePlanarFace As Face = Nothing
'select Planar Face...
While select_a_Planarface("The Planar Face", thePlanarFace) = Selection.Response.Ok
'Select cylindrical Face...
Dim theCylindricalFace As Face = select_a_CylindricalFace("The cylindrical hole Face")
'Some output to NX Lising Window...TAG
NX(" ")
NX("PlanarFace tag: " & thePlanarFace.Tag)
NX("CylindricalFace tag: " & theCylindricalFace.Tag)
' Calculat Point3D...
Dim tempPoint1 As NXOpen.Point3d = CalcIntersectionPoint(thePlanarFace, theCylindricalFace)
' Set point as double...
Dim axisPt(2) As Double '> the Intersection point between the Hole-axis and the Planar Face...
axisPt(0) = tempPoint1.X
axisPt(1) = tempPoint1.Y
axisPt(2) = tempPoint1.Z
'Some output to NX Lising Window...COORDINATE
NX("Intersection Point > point/hole")
NX("AbsXPos: " & Math.Round(axisPt(0), 3).ToString("0.000"))
NX("AbsYPos: " & Math.Round(axisPt(1), 3).ToString("0.000"))
NX("AbsZPos: " & Math.Round(axisPt(2), 3).ToString("0.000"))
NX(" ")
' display temp point at intersection...
Dim poicolour As New NXOpen.UF.UFObj.DispProps()
poicolour.color = 36
ufs.Disp.DisplayTemporaryPoint(NXOpen.Tag.Null, UFDisp.ViewType.UseWorkView, axisPt, poicolour, UFDisp.PolyMarker.Asterisk)
' Lets play with face props
Dim param(1) As Double
Dim newOrigin(2) As Double
Dim unitNormal(2) As Double
Dim u1(2) As Double
Dim u2(2) As Double
Dim v1(2) As Double
Dim v2(2) As Double
Dim radii(1) As Double
' ask Face Properties...we need the normal vector of the Planar Face...
ufs.Modl.AskFaceProps(thePlanarFace.Tag, param, newOrigin, u1,
v1, u2, v2, unitNormal, radii)
'Some output to NX Lising Window...DIRECTION of the Planar Face...
NX("PlanarFaceDirection X: " & unitNormal(0).ToString("0.000"))
NX("PlanarFaceDirection Y: " & unitNormal(1).ToString("0.000"))
NX("PlanarFaceDirection Z: " & unitNormal(2).ToString("0.000"))
'Some output to NX Lising Window...U & V of the Planar Face...
NX("PlanarFace-u1(0) : " & u1(0))
NX("PlanarFace-u1(1) : " & u1(1))
NX("PlanarFace-u1(2) : " & u1(2))
NX("PlanarFace-v1(0) : " & v1(0))
NX("PlanarFace-v1(1) : " & v1(1))
NX("PlanarFace-v1(2) : " & v1(2))
NX("PlanarFace-u2(0) : " & u2(0))
NX("PlanarFace-u2(1) : " & u2(1))
NX("PlanarFace-u2(2) : " & u2(2))
NX("PlanarFace-v2(0) : " & v2(0))
NX("PlanarFace-v2(1) : " & v2(1))
NX("PlanarFace-v2(2) : " & v2(2))
NX(" ")
' ask Face Properties...we need the normal vector of the Planar Face...
ufs.Modl.AskFaceProps(theCylindricalFace.Tag, param, newOrigin, u1,
v1, u2, v2, unitNormal, radii)
'Some output to NX Lising Window...DIRECTION of the Planar Face...
NX("CylindricalFaceDirection X: " & unitNormal(0).ToString("0.000"))
NX("CylindricalFaceDirection Y: " & unitNormal(1).ToString("0.000"))
NX("CylindricalFaceDirection Z: " & unitNormal(2).ToString("0.000"))
'Some output to NX Lising Window...U & V of the Cylindrical Face...
NX("CylindricalFace-u1(0) : " & u1(0))
NX("CylindricalFace-u1(1) : " & u1(1))
NX("CylindricalFace-u1(2) : " & u1(2))
NX("CylindricalFace-v1(0) : " & v1(0))
NX("CylindricalFace-v1(1) : " & v1(1))
NX("CylindricalFace-v1(2) : " & v1(2))
NX("CylindricalFace-u2(0) : " & u2(0))
NX("CylindricalFace-u2(1) : " & u2(1))
NX("CylindricalFace-u2(2) : " & u2(2))
NX("CylindricalFace-v2(0) : " & v2(0))
NX("CylindricalFace-v2(1) : " & v2(1))
NX("CylindricalFace-v2(2) : " & v2(2))
'Some output to NX Lising Window...MATRIX 3X3 based on the Planar Face
' ask Face Properties...we need the normal vector of the Planar Face...
ufs.Modl.AskFaceProps(thePlanarFace.Tag, param, newOrigin, u1,
v1, u2, v2, unitNormal, radii)
Dim thePlanarFaceOrientation As Matrix3x3 = CalcMatrix(thePlanarFace)
NX(" ")
NX("Mátrix 3x3 - Planar Face ")
NX(thePlanarFaceOrientation.Xx)
NX(thePlanarFaceOrientation.Xy)
NX(thePlanarFaceOrientation.Xz)
NX(thePlanarFaceOrientation.Yx)
NX(thePlanarFaceOrientation.Yy)
NX(thePlanarFaceOrientation.Yz)
NX(thePlanarFaceOrientation.Zx)
NX(thePlanarFaceOrientation.Zy)
NX(thePlanarFaceOrientation.Zz)
'Some output to NX Lising Window...MATRIX 3X3 based on the cylindrical Face
' ask Face Properties...we need the normal vector of the Planar Face...
ufs.Modl.AskFaceProps(theCylindricalFace.Tag, param, newOrigin, u1,
v1, u2, v2, unitNormal, radii)
Dim theCylindricalFaceOrientation As Matrix3x3 = CalcMatrix(theCylindricalFace)
NX(" ")
NX("Mátrix 3x3 - Cylindrical Face ")
NX(theCylindricalFaceOrientation.Xx)
NX(theCylindricalFaceOrientation.Xy)
NX(theCylindricalFaceOrientation.Xz)
NX(theCylindricalFaceOrientation.Yx)
NX(theCylindricalFaceOrientation.Yy)
NX(theCylindricalFaceOrientation.Yz)
NX(theCylindricalFaceOrientation.Zx)
NX(theCylindricalFaceOrientation.Zy)
NX(theCylindricalFaceOrientation.Zz)
'#########################################################################################################################
Dim myDelta As Double = 12 ' the offset distance that the Point shall be calculated from...
' Inside here a function call is needed..'current axisPt shall have the value of myDelta added to the ABS coordinate, based on the normaldirection from either planars face ot hole axis
'#########################################################################################################################
' display temp point at Delta distance...
poicolour.color = 42
ufs.Disp.DisplayTemporaryPoint(NXOpen.Tag.Null, UFDisp.ViewType.UseWorkView, axisPt, poicolour, UFDisp.PolyMarker.Asterisk)
NX(" ")
NX("--------------------------------------------")
NX("--------------------------------------------")
End While
End Sub
'=========================== NX LISTING WINDOW FUNCTION=======================================
Public Sub NX(ByVal input As String) '' output variable listwindow - just for building VB
If output_To_NXListingWindow Then
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow()
lw.Open()
lw.WriteLine(input)
End If
End Sub
'===============================================================================================
'=========================== NX WRITE TO LOGFILE FUNCTION=======================================
Public Sub NX_log(ByVal input As String) '' output variable listwindow - just for building VB
If output_To_NXLogFile Then
theSession.LogFile.WriteLine(input)
End If
End Sub
'================================================================================================
'=========================== NX UNLOAD FUNCTION ================================================
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination ' typically used in Checkmate only
End Function
'================================================================================================
'================================================================================================
'================================================================================================
Function select_a_Planarface(ByVal prompt As String, ByRef obj As Face)
Dim ui As UI = GetUI()
Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_solid_type
.Subtype = UFConstants.UF_solid_face_subtype
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_PLANAR_FACE
End With
Dim cursor As Point3d = Nothing
Dim resp As Selection.Response =
ui.SelectionManager.SelectObject(prompt, prompt,
Selection.SelectionScope.AnyInAssembly,
Selection.SelectionAction.ClearAndEnableSpecific,
False, False, mask, obj, 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
'================================================================================================
'================================================================================================
Function select_a_CylindricalFace(ByRef prompt As String) As Face
Dim mask(0) As Selection.MaskTriple
With mask(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_CYLINDRICAL_FACE
End With
Dim cursor As Point3d = Nothing
select_a_CylindricalFace = Nothing
ufs.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
UI.GetUI.SelectionManager.SelectTaggedObject(prompt, "Select a Cylindrical Hole Face:",
Selection.SelectionScope.AnyInAssembly,
Selection.SelectionAction.ClearAndEnableSpecific, False,
False, mask, select_a_CylindricalFace, cursor)
ufs.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
End Function
'================================================================================================
'================================================================================================
Public Function CalcIntersectionPoint(ByVal whichPlanarFace As Face, ByVal whichCylindricalFace As Face) As Point3d
Dim newPoint As Point3d = Nothing
newPoint.X = 0
newPoint.Y = 0
newPoint.Z = 0
Try
' Variables for ufs.Curve.Intersect..
Dim mystartpoint(2) As Double
Dim intersectdata1 As NXOpen.UF.UFCurve.IntersectInfo = Nothing
' create temporary line
Dim line1 As NXOpen.Line = Nothing
line1 = workPart.Lines.CreateFaceAxis(whichCylindricalFace, NXOpen.SmartObject.UpdateOption.WithinModeling)
' get the intersection point
ufs.Curve.Intersect(line1.Tag, whichPlanarFace.Tag, mystartpoint, intersectdata1)
If Not intersectdata1.type_of_intersection = 1 Then
' list the intersection type
NX(intersectdata1.type_of_intersection)
MsgBox("no face/line-axis intersect found")
Return newPoint ' just in case no intersection was calculated...
End If
' delete the line straight after...
Dim objectstoDelete(0) As NXOpen.NXObject
objectstoDelete(0) = line1
Dim nErrs3 As Integer = Nothing
nErrs3 = theSession.UpdateManager.AddObjectsToDeleteList(objectstoDelete)
' Transfer the calculated Point Data into Point3d...
newPoint.X = intersectdata1.curve_point(0)
newPoint.Y = intersectdata1.curve_point(1)
newPoint.Z = intersectdata1.curve_point(2)
Return newPoint
Catch
MsgBox("no face/line-axis intersect found")
Return newPoint ' just in case no intersection was calculated...
End Try
End Function
'================================================================================================
'================================================================================================
Public Function CalcMatrix(ByVal whichPlanarFace As Face) As Matrix3x3
' Matrix to be returned -
Dim newMtx3x3 As Matrix3x3
newMtx3x3.Xx = 1.0
newMtx3x3.Xy = 0.0
newMtx3x3.Xz = 0.0
newMtx3x3.Yx = 0.0
newMtx3x3.Yy = 1.0
newMtx3x3.Yz = 0.0
newMtx3x3.Zx = 0.0
newMtx3x3.Zy = 0.0
newMtx3x3.Zz = 1.0
Try
'Dim uvMinMax(3) As Double
'ufs.Modl.AskFaceUvMinmax(BoltHeadFace(0).Tag, uvMinMax)
Dim param(1) As Double
'param(0) = ((uvMinMax(0) + uvMinMax(1)) / 2)
'param(1) = ((uvMinMax(2) + uvMinMax(3)) / 2)
Dim newOrigin(2) As Double
Dim unitNormal(2) As Double
Dim u1(2) As Double
Dim u2(2) As Double
Dim v1(2) As Double
Dim v2(2) As Double
Dim radii(1) As Double
' ask Face Properties...we need the normal vector...
ufs.Modl.AskFaceProps(whichPlanarFace.Tag, param, newOrigin, u1,
v1, u2, v2, unitNormal, radii)
Dim newMtx(8) As Double
' convert vector 2 Matrix...nice one....
ufs.Mtx3.InitializeZ(unitNormal, newMtx)
'build the Matrix 3x3...based on Bolt/Nut Face-vector...
newMtx3x3.Xx = newMtx(0)
newMtx3x3.Xy = newMtx(1)
newMtx3x3.Xz = newMtx(2)
newMtx3x3.Yx = newMtx(3)
newMtx3x3.Yy = newMtx(4)
newMtx3x3.Yz = newMtx(5)
newMtx3x3.Zx = newMtx(6)
newMtx3x3.Zy = newMtx(7)
newMtx3x3.Zz = newMtx(8)
'Return the Matrix
Return newMtx3x3
Catch
MsgBox("cant calculate Matrix 3x3 - we returned the Absolute one instead...")
Return newMtx3x3
End Try
End Function
End Module