Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotation angle between two coordinate systems

Status
Not open for further replies.

Karol94

Mechanical
Sep 4, 2023
3
Hi
is it possible in NX journal .vb to get rotation angles between two coordinate systems that are
flying loosely in space? I found a method to get these angles between two components in thread561-344785.
I'm not a programmer, i'm fighting with this for 2 days with a chat-gpt help, but the result is that
there is no method for easy getting the angles without 'Selectcomponents' function. Btw, distance between
two coordinate systems is super easy with selecting a csys only.
 
Replies continue below

Recommended for you

Thanks, for the answer, the method from this thread561-354338 works
really well, but unfortunately i do not always have the permission to save a Csys object inside the assembly in my work.
I need this function to set Csyses in robot arms and calibrate them with our coordinate systems. To simplify this, i get
the models, which contains few csyses and i only want to click two coordinate systems, get results and put it into robots
calibration menu. But even if i get permission to modify and save csyses inside the assembly it would be another step to
create Csys files, so i will be looking for simplier solution of that for sure

 
You don't need to create a csys, name it, and save it in the file. The named csys objects were just for illustration so that forum users could run the journal with no user interaction. You can, of course, modify the code to allow user selection of the csys objects or use other existing objects that have orientation data.

www.nxjournaling.com
 
Hello,
with code i found in this thread561-354338 and from
other forums i found a solution for my problem. This program ask an user for two coordinate systems
and return xyz translation between two coordinate systems and rotations (R,P,Y) and (Z,Y,Z) which
is commonly used in robots industry. Maybe some lines of the code are unnecessarily, but i'm not
a professional as i mentioned. Btw you select by click on 0 point of the csys'es

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
 
Module Module1
 
    Sub Main()
 
        Dim theSession As Session = Session.GetSession()
        Dim theUfSession As UFSession = UFSession.GetUFSession()
        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If
 
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()
 
        Const undoMarkName As String = "reposition component"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
 
        Dim fromCsys As CoordinateSystem
        Dim toCsys As CoordinateSystem
 
        If SelectCsys("Select 'From' coordinate system", fromCsys) = Selection.Response.Cancel Then
            Return
        End If
 
        If SelectCsys("Select 'To' coordinate system", toCsys) = Selection.Response.Cancel Then
            Return
        End If
 
        Dim fromOrigin() As Double = {fromCsys.Origin.X, fromCsys.Origin.Y, fromCsys.Origin.Z}
        Dim fromXAxis() As Double = {fromCsys.Orientation.Element.Xx, fromCsys.Orientation.Element.Xy, fromCsys.Orientation.Element.Xz}
        Dim fromYAxis() As Double = {fromCsys.Orientation.Element.Yx, fromCsys.Orientation.Element.Yy, fromCsys.Orientation.Element.Yz}
 
        Dim toOrigin() As Double = {toCsys.Origin.X, toCsys.Origin.Y, toCsys.Origin.Z}
        Dim toXAxis() As Double = {toCsys.Orientation.Element.Xx, toCsys.Orientation.Element.Xy, toCsys.Orientation.Element.Xz}
        Dim toYAxis() As Double = {toCsys.Orientation.Element.Yx, toCsys.Orientation.Element.Yy, toCsys.Orientation.Element.Yz}
 
        Dim mtx4Transform(15) As Double
 
        theUfSession.Mtx4.CsysToCsys(fromOrigin, fromXAxis, fromYAxis, toOrigin, toXAxis, toYAxis, mtx4Transform)

        Dim rotMatrix(8) As Double
        theUfSession.Mtx4.AskRotation(mtx4Transform, rotMatrix)
        Dim transVec(2) As Double
        theUfSession.Mtx4.AskTranslation(mtx4Transform, transVec)
 
        Dim translateVector As Vector3d = New Vector3d(transVec(0), transVec(1), transVec(2))
        Dim rotationMatrix As Matrix3x3 = convertToMatrix3x3(rotMatrix)
        Dim comp As Assemblies.Component = fromCsys.OwningComponent
        Dim componentPositioner1 As Positioning.ComponentPositioner = theSession.Parts.Work.ComponentAssembly.Positioner
 
        Dim network1 As Positioning.Network
        network1 = componentPositioner1.EstablishNetwork()
 
        Dim componentNetwork1 As Positioning.ComponentNetwork = CType(network1, Positioning.ComponentNetwork)
        Dim markId2 As Session.UndoMarkId = Nothing
 
        theSession.UpdateManager.AddToDeleteList(componentNetwork1)
        theSession.UpdateManager.DoUpdate(markId2)

 	        dim AngleX As Double = -Math.Atan2(rotationMatrix.Yz, rotationMatrix.Zz)
		dim AngleY As Double = -Math.Asin(-rotationMatrix.Xz)
		dim AngleZ As Double = -Math.Atan2(rotationMatrix.Xy, rotationMatrix.Xx)

		Dim Z1a As Double = Math.Atan2(rotationMatrix.Yz, rotationMatrix.Xz)
		Dim Ya As Double = Math.Acos(rotationMatrix.Zz)
		Dim Z2a As Double = Math.Atan2(rotationMatrix.Zy, -rotationMatrix.Zx)

		Dim Z1 As Double = Z1a * 180 / Math.PI
		Dim Y As Double = Ya * 180 / Math.PI
		Dim Z2 As Double = Z2a * 180 / Math.PI

		Dim AStopnie As Double = AngleX * 180 / Convert.ToDecimal(Math.PI)
		Dim BStopnie As Double = AngleY * 180 / Convert.ToDecimal(Math.PI)
		Dim CStopnie As Double = AngleZ * 180 / Convert.ToDecimal(Math.PI)
 
                lw.WriteLine("przesuniecie x: " & translateVector.X.ToString)
		lw.WriteLine("przesuniecie y: " & translateVector.Y.ToString)
		lw.WriteLine("przesuniecie z: " & translateVector.Z.ToString)

		lw.WriteLine("R: " & AStopnie & " stopni")
		lw.WriteLine("P: " & BStopnie & " stopni")
		lw.WriteLine("Y: " & CStopnie & " stopni")

		lw.WriteLine("Z: " & Z1 & " stopni")
		lw.WriteLine("Y: " & Y & " stopni")
		lw.WriteLine("Z: " & Z2 & " stopni")

        lw.Close()
 
    End Sub
 
    Function SelectCsys(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response
 
        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a Coordinate system"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple
 
        With selectionMask_array(0)
            .Type = UFConstants.UF_coordinate_system_type
            .Subtype = UFConstants.UF_all_subtype
        End With
 
        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selobj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
 
    End Function
 
    Function convertToMatrix3x3(ByVal mtx As Double()) As Matrix3x3
 
        Dim mx As Matrix3x3
        With mx
            .Xx = mtx(0)
            .Xy = mtx(1)
            .Xz = mtx(2)
            .Yx = mtx(3)
            .Yy = mtx(4)
            .Yz = mtx(5)
            .Zx = mtx(6)
            .Zy = mtx(7)
            .Zz = mtx(8)
        End With
 
        Return mx
 
    End Function
 
End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor