Cancel
Showing results for 
Search instead for 
Did you mean: 

How to add a component with position and orientation

Hi All,

I can add a component in assy, but need to set also the orientation,

given angles in rx,ry,rz angles.

 

But I get components for each axis

 

        orientation1.Xx = 1.0
        orientation1.Xy = 0.0
        orientation1.Xz = 0.0

How to transform rx (rotation about x axis) in components?

Thanks in advance for any feedback.

Regards Massimo

MAssimo

 

 


Sub AddComponent(filename As String, x As Double, y As Double, z As Double, Optional ry As Double = 0, Optional rz As Double = 0) 'Add A component to the assembly Dim basePoint1 As Point3d = New Point3d(x, y, z) Dim orientation1 As Matrix3x3 'Orientation of the component (understand how to setup a vector) orientation1.Xx = 1.0 orientation1.Xy = 0.0 orientation1.Xz = 0.0 orientation1.Yx = 0.0 orientation1.Yy = 1.0 orientation1.Yz = 0.0 orientation1.Zx = 0.0 orientation1.Zy = 0.0 orientation1.Zz = 1.0 Dim partLoadStatus1 As PartLoadStatus = Nothing Dim component1 As Assemblies.Component Dim compName As String compName = System.IO.Path.GetFileNameWithoutExtension(filename) component1 = workPart.ComponentAssembly.AddComponent(filename, "SOLID", compName, basePoint1, orientation1, 0, partLoadStatus1, True) End Sub

 

2 REPLIES

Re: How to add a component with position and orientation

Honored Contributor
Honored Contributor

Below is one way to do it:

 

    Sub AddComponent(filename As String, x As Double, y As Double, z As Double,
                     Optional rx As Double = 0, Optional ry As Double = 0, Optional rz As Double = 0)

        'initialize matrix
        Dim rotMat(8) As Double
        theUfSession.Mtx3.Identity(rotMat)

        'rotate about X axis
        Dim rotMatX(8) As Double
        Dim rotAxis() As Double = {1, 0, 0}
        Dim rotationAngle As Double = rx * Math.PI / 180
        theUfSession.Mtx3.RotateAboutAxis(rotAxis, rotationAngle, rotMatX)
        theUfSession.Mtx3.Multiply(rotMat, rotMatX, rotMat)

        'rotate about Y axis
        Dim rotMatY(8) As Double
        rotAxis = {0, 1, 0}
        rotationAngle = ry * Math.PI / 180
        theUfSession.Mtx3.RotateAboutAxis(rotAxis, rotationAngle, rotMatY)
        theUfSession.Mtx3.Multiply(rotMat, rotMatY, rotMat)

        'rotate about Z axis
        Dim rotMatZ(8) As Double
        rotAxis = {0, 0, 1}
        rotationAngle = rz * Math.PI / 180
        theUfSession.Mtx3.RotateAboutAxis(rotAxis, rotationAngle, rotMatZ)
        theUfSession.Mtx3.Multiply(rotMat, rotMatZ, rotMat)


        Dim basePoint1 As Point3d = New Point3d(x, y, z)
        Dim orientation1 As Matrix3x3
        orientation1.Xx = rotMat(0)
        orientation1.Yx = rotMat(1)
        orientation1.Zx = rotMat(2)
        orientation1.Xy = rotMat(3)
        orientation1.Yy = rotMat(4)
        orientation1.Zy = rotMat(5)
        orientation1.Xz = rotMat(6)
        orientation1.Yz = rotMat(7)
        orientation1.Zz = rotMat(8)
        Dim partLoadStatus1 As PartLoadStatus
        Dim component1 As Assemblies.Component
        component1 = theSession.Parts.Work.ComponentAssembly.AddComponent(filename, "MODEL", IO.Path.GetFileNameWithoutExtension(filename), basePoint1, orientation1, -1, partLoadStatus1, True)

        partLoadStatus1.Dispose()


    End Sub

 

Edit: It should be noted that the rotations are applied in XYZ order and the absolute X, Y, and Z axes are used as reference. If you take the same rotation angles but use a different order (e.g. rotate about Y then X then Z), you will end up with a different orientation.

Re: How to add a component with position and orientation

Hello,
thanks for the help. regards Massimo