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!

Get Component Rotation Angles 4

Status
Not open for further replies.

ramakrishna90589

Automotive
May 13, 2013
19
Hi All,

i have an assembly and i want collect component rotation angles about X, Y & Z.

i searched for this in Eng-tips forum and i got following code to get component rotation matrix.

Code:
Dim RotMat as Matrix3x3
Dim child As Component
Dim LW As ListingWindow = theSession.ListingWindow
LW.Open()
child.GetPosition(pt, RotMat)
        lw.WriteLine ( "|" & child.displayname() & "," & _
"" & pt.x & ", " & pt.y & ", " & pt.z & "," & _
"" & "" & _
RotMat.xx & ", " & RotMat.yx & ", " & RotMat.zx & "," & _
RotMat.xy & ", " & RotMat.yy & ", " & RotMat.zy & "," & _
RotMat.xz & ", " & RotMat.yz & ", " & RotMat.zz )

and i want to is there any way to get component angles rather than rotation matrix.

Thanks in Advance.


 
Replies continue below

Recommended for you

Angles relative to what?

John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Industry Sector
Cypress, CA
Siemens PLM:
UG/NX Museum:

To an Engineer, the glass is twice as big as it needs to be.
 
Hi John R. Baker,

Component angle with respect to absolute co-ordinate system.

for example i have a axle assembly consist one axle and 2 wheels. now i want to get axle angles with respect to absolute co-ordinate system of axle assembly.

Thanks
 
Relative to a coordinate system still does not tell us anything. What 'feature' of a component is going to be used as the reference? Not all objects have readily recognizable 'features' which could used when measuring angles. Take for instance something which is basically rectangular or spherical.

John R. Baker, P.E.
Product 'Evangelist'
Product Engineering Software
Siemens PLM Software Inc.
Industry Sector
Cypress, CA
Siemens PLM:
UG/NX Museum:

To an Engineer, the glass is twice as big as it needs to be.
 
You have added a component to the assembly and moved it around to position it in the assembly; now you want to know the rotation angles to get it from the initial position to the assembled position, is this correct?

www.nxjournaling.com
 
Cowski Your right. That is what i want. But i want component rotation angle (About X,Y & Z) in Degrees not as a rotation matrix.

Thanks
 
What i need is, i have a component in an assembly and i want to get this component absolute co-ordinate system (Component Reference Feature is component absolute co-ordinate system) rotation angles (in degrees about X,Y & Z) with respect to assembly absolute co-ordinate system.

Thanks
 
I had a chance to do some coding this afternoon; this isn't heavily tested, but seems to work.

Code:
[COLOR=blue]Option Strict Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  
[COLOR=blue]Imports[/color] NXOpen.UF  
[COLOR=blue]Imports[/color] NXOpen.Assemblies  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Sub[/color] Main()  

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
        [COLOR=blue]Dim[/color] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] theSession.ListingWindow  
        lw.Open()  

        [COLOR=blue]Dim[/color] myComponents() [COLOR=blue]As[/color] TaggedObject  

        [COLOR=blue]If[/color] SelectComponents("Select components", myComponents) [COLOR=blue]=[/color] Selection.Response.Cancel [COLOR=blue]Then[/color]  
            [COLOR=blue]Return[/color]  
        End [COLOR=blue]If[/color]  

        [COLOR=blue]For Each[/color] tempComp [COLOR=blue]As[/color] Component [COLOR=blue]In[/color] myComponents  
            lw.WriteLine(tempComp.Name)  
            [COLOR=blue]Dim[/color] rotX [COLOR=blue]As Double[/color]  [COLOR=green]'rotation about X axis[/color]
            [COLOR=blue]Dim[/color] rotY [COLOR=blue]As Double[/color]  [COLOR=green]'rotation about Y axis[/color]
            [COLOR=blue]Dim[/color] rotZ [COLOR=blue]As Double[/color]  [COLOR=green]'rotation about Z axis[/color]
            CompRot(tempComp, rotX, rotY, rotZ)  
            lw.WriteLine(" rotation about X axis: " [COLOR=blue]&[/color] rotX)  
            lw.WriteLine(" rotation about Y axis: " [COLOR=blue]&[/color] rotY)  
            lw.WriteLine(" rotation about Z axis: " [COLOR=blue]&[/color] rotZ)  

        [COLOR=blue]Next[/color]  

    End [COLOR=blue]Sub[/color]  


    [COLOR=blue]Function[/color] SelectComponents(ByVal prompt [COLOR=blue]As[/color] String, [COLOR=blue]ByRef[/color] selObj() [COLOR=blue]As[/color] TaggedObject) [COLOR=blue]As[/color] Selection.Response  

        [COLOR=blue]Dim[/color] theUI [COLOR=blue]As[/color] UI [COLOR=blue]=[/color] UI.GetUI  
        [COLOR=blue]Dim[/color] title [COLOR=blue]As String =[/color] "Select components"  
        [COLOR=blue]Dim[/color] includeFeatures [COLOR=blue]As Boolean = False[/color]  
        [COLOR=blue]Dim[/color] keepHighlighted [COLOR=blue]As Boolean = False[/color]  
        [COLOR=blue]Dim[/color] selAction [COLOR=blue]As[/color] Selection.SelectionAction [COLOR=blue]=[/color] Selection.SelectionAction.ClearAndEnableSpecific  
        [COLOR=blue]Dim[/color] scope [COLOR=blue]As[/color] Selection.SelectionScope [COLOR=blue]=[/color] Selection.SelectionScope.AnyInAssembly  
        [COLOR=blue]Dim[/color] selectionMask_array(0) [COLOR=blue]As[/color] Selection.MaskTriple  

        [COLOR=blue]With[/color] selectionMask_array(0)  
            .Type [COLOR=blue]=[/color] UFConstants.UF_component_type  
            .Subtype [COLOR=blue]=[/color] UFConstants.UF_component_subtype  
        End [COLOR=blue]With[/color]  

        [COLOR=blue]Dim[/color] resp [COLOR=blue]As[/color] Selection.Response [COLOR=blue]=[/color] theUI.SelectionManager.SelectTaggedObjects(prompt, _  
         title, scope, selAction, _  
         includeFeatures, keepHighlighted, selectionMask_array, _  
         selobj)  
        [COLOR=blue]If[/color] resp [COLOR=blue]=[/color] Selection.Response.Ok [COLOR=blue]Then[/color]  
            [COLOR=blue]Return[/color] Selection.Response.Ok  
        [COLOR=blue]Else[/color]  
            [COLOR=blue]Return[/color] Selection.Response.Cancel  
        End [COLOR=blue]If[/color]  

    End [COLOR=blue]Function[/color]  

    [COLOR=blue]Sub[/color] CompRot(ByVal someComponent [COLOR=blue]As[/color] Component, [COLOR=blue]ByRef[/color] RX1 [COLOR=blue]As[/color] Double, [COLOR=blue]ByRef[/color] RY1 [COLOR=blue]As[/color] Double, [COLOR=blue]ByRef[/color] RZ1 [COLOR=blue]As Double[/color])  

        [COLOR=green]'extract euler angles from rotation matrix:[/color]
        [COLOR=green]'[URL unfurl="true"]https://d3cw3dd2w32x2b.cloudfront.net/wp-content/uploads/2012/07/euler-angles.pdf[/URL][/color]

        [COLOR=blue]Dim[/color] pt [COLOR=blue]As[/color] Point3d  
        [COLOR=blue]Dim[/color] RotMat [COLOR=blue]As[/color] Matrix3x3  
        someComponent.GetPosition(pt, RotMat)  

        [COLOR=blue]Dim[/color] c1, c2, s1 [COLOR=blue]As Double[/color]  

        RX1 [COLOR=blue]=[/color] Math.Atan2(RotMat.Yz, RotMat.Zz)  
        c2 [COLOR=blue]=[/color] Math.Sqrt(RotMat.Xx [COLOR=blue]^[/color] 2 [COLOR=blue]+[/color] RotMat.Xy [COLOR=blue]^[/color] 2)  
        RY1 [COLOR=blue]=[/color] Math.Atan2(-RotMat.Xz, c2)  
        s1 [COLOR=blue]=[/color] Math.Sin(RX1)  
        c1 [COLOR=blue]=[/color] Math.Cos(RX1)  
        RZ1 [COLOR=blue]=[/color] Math.Atan2(s1 [COLOR=blue]*[/color] RotMat.Zx [COLOR=blue]-[/color] c1 [COLOR=blue]*[/color] RotMat.Yx, c1 [COLOR=blue]*[/color] RotMat.Yy [COLOR=blue]-[/color] s1 [COLOR=blue]*[/color] RotMat.Zy)  

        [COLOR=green]'convert angles from radians to degrees[/color]
        RX1 [COLOR=blue]*=[/color] (180 [COLOR=blue]/[/color] Math.PI)  
        RY1 [COLOR=blue]*=[/color] (180 [COLOR=blue]/[/color] Math.PI)  
        RZ1 [COLOR=blue]*=[/color] (180 [COLOR=blue]/[/color] Math.PI)  

    End [COLOR=blue]Sub[/color]  
    [COLOR=blue]Public Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As String[/color]) [COLOR=blue]As Integer[/color]  

        [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]

I'm still curious what you plan on using the angles for. There might be an easier way to go about it. For instance, if you want to move the component back to the original position, you might be interested in thread561-311299. No calculation of angles necessary.

www.nxjournaling.com
 
cowski, This program worked perfectly for me. thanks for your support.

actually we got a new project in this project we can't use constrains (as per customer request). Because of this we got a some problems like some components are moved by user accidentally in final design. now we want to make a log file that will record all the assembly component position and orientation so that if any one moved accidentally then we can able to recover those position from log file.
 
I should mention that the journal reports the rotations as if the component were rotated about the X axis then the Y axis and finally the Z axis (order is important). If you want to recreate the position, you will need to follow XYZ order, otherwise you will end up with a different orientation.

For your application, you would be better off recording the position matrix. If the new matrix is different from the old, you can do a "csys to csys" type move (a csys is easily derived from the matrix) and not have to worry about calculating angles or the order you apply them. It would also avoid some potential rounding errors with the angle calculations.

www.nxjournaling.com
 
Yes, this is better way. i want to how to create a CSys using Vb journal (Rotation matrix). We have one problem at here, generally we will insert parts from Customer library and many of these part does not contains CSYS (at absolute) in this case how to use CSYS to Csys move command (we don't have write access to this file so we can't create a CSYS in the part) . please help me in this regards.

Thanks for your valuable suggestion
 
uwam2ie,
Glad you like it, I hope someone finds it useful.

ramakrishna90589,
Below is a link to some sample code, 2 separate journals, one prompts you to select a component and writes the position information to file (record_pos.vb); the other prompts you to select a component and moves it to the saved position (reset_pos.vb). The code is rudimentary, but I think functional enough to show you a way forward.

Create some way to record the component's original position (extract edge curves, all in body would do nicely) then run record_pos.vb and select the component. Reposition the component (any mix of rotations and translations) then run reset_pos.vb and select the same component. It should move back to its previous position. Do this on a test file, I do not guarantee the code in any way, shape, or form. [smile]


www.nxjournaling.com
 
cowski i have tested two programs and those working very perfectly. this programs are really good for us. thanks a lot.

i have a small question, as i described in my previous post some time we are facing a problem in moving components in assembly using CSYS to CSys move command because some components that doesn't consists any csys at absolute. is there any way in unigraphics to show absolute csys?

Thanks
 
elaborating my previous post ,

Sometimes we will insert component from library and we want to move this component to specified csys using CSYS to CSYS move command at here we are facing following problem. because component doesn't consists any csys so we can't able to use CSYS to CSYS move command. see attached image, in this image part was inserted using "entire part" as a reference set still it won't show any CSYS. i Want to know is there any way in unigraphics to show absolute co-ordinate system so that we can use CSYS to CSYS Move command very easily while inserting new component in an assembly.


CSYS%20constraints%20problems.JPG


Thanks
Ramakrishna
 
When you add the component, if you use the "Absolute origin" positioning option, the absolute csys of the part will align to the absolute csys of the assembly. If you want a visual indication of the absolute part csys, make it the display part and move the WCS to absolute. Now create a datum csys or save the WCS (this will create a visible csys object), you will be able to reference one of these while working in the assembly. Even if you do not have write access to the part, you will be able to save a csys and reference it while it is in session.

www.nxjournaling.com
 
I really like the journal posted on 14 May 13 16:16 in this thread. Is it possible to add the xyz locations of the components that I pick in the assembly to this journal? This is exactly what we are looking for. Thanks for posting.
 
The following code will also report the component position.

Code:
'NXJournaling
'May 14, 2013
'Select a component, report the rotations about the X, Y, and Z axes
'that orient the component from its initial position to its current position.

'ref: eng-tips thread561-344785

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim myComponents() As TaggedObject

        If SelectComponents("Select components", myComponents) = Selection.Response.Cancel Then
            Return
        End If

        For Each tempComp As Component In myComponents
            lw.WriteLine(tempComp.Name)
            Dim rotX As Double      'rotation about X axis
            Dim rotY As Double      'rotation about Y axis
            Dim rotZ As Double      'rotation about Z axis
            CompRot(tempComp, rotX, rotY, rotZ)
            lw.WriteLine(" rotation about X axis: " & rotX)
            lw.WriteLine(" rotation about Y axis: " & rotY)
            lw.WriteLine(" rotation about Z axis: " & rotZ)
            lw.WriteLine(" " & CompPos(tempComp).ToString)
            lw.WriteLine("")
        Next

    End Sub


    Function SelectComponents(ByVal prompt As String, ByRef selObj() As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select components"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_component_type
            .Subtype = UFConstants.UF_component_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selobj)
        If resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    Sub CompRot(ByVal someComponent As Component, ByRef RX1 As Double, ByRef RY1 As Double, ByRef RZ1 As Double)

        'extract euler angles from rotation matrix:
        '[URL unfurl="true"]https://d3cw3dd2w32x2b.cloudfront.net/wp-content/uploads/2012/07/euler-angles.pdf[/URL]

        Dim pt As Point3d
        Dim RotMat As Matrix3x3
        someComponent.GetPosition(pt, RotMat)

        Dim c1, c2, s1 As Double

        RX1 = Math.Atan2(RotMat.Yz, RotMat.Zz)
        c2 = Math.Sqrt(RotMat.Xx ^ 2 + RotMat.Xy ^ 2)
        RY1 = Math.Atan2(-RotMat.Xz, c2)
        s1 = Math.Sin(RX1)
        c1 = Math.Cos(RX1)
        RZ1 = Math.Atan2(s1 * RotMat.Zx - c1 * RotMat.Yx, c1 * RotMat.Yy - s1 * RotMat.Zy)

        'convert angles from radians to degrees
        RX1 *= (180 / Math.PI)
        RY1 *= (180 / Math.PI)
        RZ1 *= (180 / Math.PI)

    End Sub

    Function CompPos(ByVal someComponent As Component) As Point3d

        Dim pt As Point3d
        Dim RotMat As Matrix3x3
        someComponent.GetPosition(pt, RotMat)
        Return pt

    End Function

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

    End Function

End Module

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor