Show / Hide Table of Contents

Rotating Target

This example provides information on how to rotate a target around different axes by a specified angle using the SetRelativeTransform(Matrix4, Matrix4) method. To use this example, you need to pass an RsTarget object, a RotateAxis object, and a double value (the angle in degrees).

Solution

  1. Based on the RotateAxis and angleInDegree parameter values, create a Vector3 object.

    // Create a Vector3 object to represent the rotation vector
    // The rotation vector is determined based on the RotateAxis and angleInDegree parameters
    Vector3 rotationVector = new Vector3();
    switch (Convert.ToInt32(value))
    {
        // Set rotation around the X axis  
        case 0:
            rotationVector = new Vector3(agnleInRad, 0, 0);
            break;
    
        // Set rotation around the Y axis  
        case 1:
            rotationVector = new Vector3(0, agnleInRad, 0);
            break;
    
        // Set rotation around the Z axis  
        case 2:
            rotationVector = new Vector3(0, 0, agnleInRad);
            break;
    }
    
  2. Create a Matrix4 object that has zero translation (x = y = z = 0) and uses the rotationVector for rotation.

    Note

    This object value will be used to rotate the target without transforming it.

    // Create a Matrix4 object for the new rotation matrix
    // This matrix has zero translation (x=y=z=0) and applies the rotation specified by the rotationVector
    Matrix4 newRotationMatrix = new Matrix4(new Vector3(), rotationVector);
    
  3. Use the SetRelativeTransform method to set the target's transformation to the new rotation matrix, relative to its own local matrix.

    // Set target transformation to newRotationMatrix relative to its own local matrix. 
    target.Transform.SetRelativeTransform(target.Transform.Matrix, newRotationMatrix);
    

Example

This example provides information on rotating target around different axis by an angle.

enum RotateAxis : int
{ X = 0,
  Y = 1,
  Z = 2 
};
private static void RotateTargetRelativeToAxis(RsTarget target, RotateAxis value, double angleInDegree)
{
    Project.UndoContext.BeginUndoStep("Rotate Target");
    try
    {
        // Convert the angle from degrees to radians 
        double agnleInRad = Globals.DegToRad(angleInDegree);

        // Create a Vector3 object to represent the rotation vector
        // The rotation vector is determined based on the RotateAxis and angleInDegree parameters
        Vector3 rotationVector = new Vector3();
        switch (Convert.ToInt32(value))
        {
            // Set rotation around the X axis  
            case 0:
                rotationVector = new Vector3(agnleInRad, 0, 0);
                break;

            // Set rotation around the Y axis  
            case 1:
                rotationVector = new Vector3(0, agnleInRad, 0);
                break;

            // Set rotation around the Z axis  
            case 2:
                rotationVector = new Vector3(0, 0, agnleInRad);
                break;
        }

        // Create a Matrix4 object for the new rotation matrix
        // This matrix has zero translation (x=y=z=0) and applies the rotation specified by the rotationVector
        Matrix4 newRotationMatrix = new Matrix4(new Vector3(), rotationVector);

        // Set target transformation to newRotationMatrix relative to its own local matrix. 
        target.Transform.SetRelativeTransform(target.Transform.Matrix, newRotationMatrix);
    }
    catch
    {
        Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
        throw;
    }
    finally
    {
        Project.UndoContext.EndUndoStep();
    }
}

Required Namespaces

ABB.Robotics.Math

ABB.Robotics.RobotStudio.Stations

See Also

  • Transform Target
In this article
Back to top Copyright © 2025 ABB