Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross 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
IN
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

Thanks for the quick reply. Is there a way to get this [X=6.52,Y=10.35,Z=0.625] which is the result from your journal to look like this X6.52 Y10.35 Z0.625 . No Brackets, commas or equals signs. Thanks
 
Code:
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
            Dim compPt As Point3d = CompPos(tempComp)
            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(" X" & compPt.X.ToString & " Y" & compPt.Y.ToString & " Z" & compPt.Z.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
 
I have revised the journal file to create a separate file for each component. Is it possible if there are identical components to send that group to a file. For example component1 has 3 locations which would get written to file 1 and component2 has only 1 location which would get written to file 2. Thanks

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

Module Module1

    Public s As Session = Session.GetSession()

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = s.Parts.Work
        Dim wpName As String = workPart.FullPath.ToString()
        Dim textFileName As String = Nothing
        Dim myComponents() As TaggedObject

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

        

        For Each tempComp As Component In myComponents
        
            textFileName = "C:\temp\" & tempComp.Name & ".NC"

            Dim outFile As IO.StreamWriter

            outFile = My.Computer.FileSystem.OpenTextFileWriter(textFileName, _
                                                                      False)
            outFile.AutoFlush = True
            
            outFile.WriteLine("/" & wpname)


            Dim rotX As Double      'rotation about X axis
            Dim rotY As Double      'rotation about Y axis
            Dim rotZ As Double      'rotation about Z axis
            Dim compPt As Point3d = CompPos(tempComp)
            CompRot(tempComp, rotX, rotY, rotZ)
            rotZ = rotZ + 180

            outFile.WriteLine()
            outFile.WriteLine("/" & tempComp.Name)
            outFile.WriteLine("X" & compPt.X.ToString("F4") & " Y" & compPt.Y.ToString("F4") & " Z" & compPt.Z.ToString("F4") & " C" & rotZ.ToString("F4"))
            
            outFile.Close()

            'MsgBox("Text File Name: " & textFileName, MsgBoxStyle.Information)


        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.SelectObjects(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
 
I'd suggest using the .AppendText method of the stream writer; see for some example code.

Also, I noticed that you are using the component's .Name property. This will return the custom name assigned by the user; it is possible that 2 identical components have different names. This might never be an issue for you, but if it is, you may want to look into using the .DisplayName or some other mechanism to ensure you are grouping components properly.

www.nxjournaling.com
 
Thanks. I think I will go through the list of chosen components and create the files. Then I will go through the list of components again and append the information to the existing files.
 
You should be able to use the Append mode and go through the components just once. If the file doesn't exist, Append mode will create it; if it does exist, it will append to it.

www.nxjournaling.com
 
I found a good example on the Microsoft webpage for appending files. Thanks. Do you know where I can look at a sample for manipulating strings. I have strings like this 1234abcd1 and 1234abcd10 sometimes the text in between is longer. I need to end up with just the right end number 1 and 10. Thanks
 
In the above NX journal is there a way to filter the selection of components to supply me with only the unique components. I am looking for a way to append text only once to the bottom of each file I created.

Thanks
 
If you have multiples of a component, each one can be positioned independently. You only need the position of 1?

If so, my first thought would be to keep a list of component names; if the list contains the component name, skip it - otherwise write the info to your file then add the component name to the list.

Code:
dim compList as new list (of string)

'process components
for each myComp in componentCollection
  if not compList.contains(myComp.name) then
    write info to file
    compList.add(myComp.name)
  end if
next

www.nxjournaling.com
 
I needed the positions of all of the components. For example Part 4 had 6 locations which were appended to the file. At the end of the file I needed to add some NC code but only once not one time for every part location. So I had it go through the components one more time leaving out the duplicate parts just to add the NC code. Everything worked out great. Thanks for the help on this project.
 
Is it possible to get XYZ values of a named point within a component just by selecting the component as we do in the above journals.
 
It is possible. Are you looking for a named point object, or a named point feature?

Something like this?

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

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 theComponent As Assemblies.Component
        If SelectComponent("Select a component", theComponent) = Selection.Response.Cancel Then
            Exit Sub
        End If

        Const dumbPointName As String = "DUMB_POINT"
        Const featurePointName As String = "FEATURE_POINT"

        Dim thePart As Part = theComponent.Prototype.OwningPart

        For Each temp As Point In thePart.Points
            If temp.Name = dumbPointName Then
                lw.WriteLine(dumbPointName & " found, location: " & temp.Coordinates.ToString)
            End If
        Next

        For Each temp As Features.Feature In thePart.Features
            If temp.FeatureType = "POINT" Then
                If temp.Name = featurePointName Then
                    Dim thePoint As Point = temp.GetEntities(0)
                    lw.WriteLine(featurePointName & " found, location: " & thePoint.Coordinates.ToString)
                End If
            End If
        Next

        lw.Close()

    End Sub

    Function SelectComponent(ByVal prompt As String, ByRef myComp As Assemblies.Component) As Selection.Response

        Dim selObj As TaggedObject
        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a component"
        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_component_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
            myComp = selObj
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    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
 
I am trying to get a location of a named 3D point per component. Also I am trying to make this work with the above journal. Thanks
 
In the original journal that we created I pick the component and it writes a file with the xyz and rotation angles. Consider this as the finish location of the component. I want to add start location for the component within the same journal. The start location of the component would be straight up in the +Z direction an amount ex. Z1.0 or in a direction based on an amount and a given angle which would modify the xyz values. I need all of this to write to the file all at the same time. I thought that if we just created a point in the component we could report on the component(finish location) and its point(start location) at the same time but the journal you gave me recently doesn't seen to mesh well with the one we created earlier. Any help would be greatly appreciated. Thank-you very much.
 
If all you need is the component start position, that's easy:
pt: (0,0,0)
0° rotation about X axis
0° rotation about Y axis
0° rotation about Z axis

www.nxjournaling.com
 
wasn't really what I was looking for. How about this? Can I read attributes from a component and have them set to variables in my journal?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top