Rotating with respect to axis
This example provides information on rotating targets about their local reference.
It expects that the targets are already created. You need to pass a RsTargetCollection object or station.ActiveTask.Targets
to this example.
The rotation of targets can be seen graphically.
Solution
Get translation vector of the target
// Get the translation vector of the target Vector3 translation = new Vector3(target.Transform.GlobalMatrix.Translation.x, target.Transform.GlobalMatrix.Translation.y, target.Transform.GlobalMatrix.Translation.z);
Get the z-axis vector of the target
// Get the z-axis vector of target Vector3 zAxis = new Vector3(target.Transform.GlobalMatrix.z.x, target.Transform.GlobalMatrix.z.y, target.Transform.GlobalMatrix.z.z);
Note
To adjust the rotation axis, you can modify the axis vector directly by accessing
target.Transform.GlobalMatrix.x.x
for the X-axis,target.Transform.GlobalMatrix.y.x
for the Y-axis, and similarly for other parameters. Alternatively, for a more straightforward approach, consider usingtarget.Transform.GlobalMatrix.GetAxisVector(Axis.X)
for the X-axis ortarget.Transform.GlobalMatrix.GetAxisVector(Axis.Y)
for the Y-axis.Get the target's GlobalMatrix and rotate it around the z-axis
// Get the matrix of target Matrix4 tMatrix = target.Transform.GlobalMatrix; // Rotate the matrix tMatrix.Rotate(translation, zAxis, rotationAngle);
Set the GlobalMatrix of the target to the rotated matrix
// Set the new matrix to target matrix target.Transform.GlobalMatrix = tMatrix;
Example
This example provides information on rotating targets based on axis in RobotStudio.
private static void RSRotateBasedOnAxis(RsTargetCollection targetCollection)
{
//Begin UndoStep
Project.UndoContext.BeginUndoStep("RotateBasedOnAxis");
try
{
foreach (RsTarget target in targetCollection)
{
// Convert rotation angle from degrees to radians
double rotationAngle = Globals.DegToRad(45);
// Get the translation vector of the target
Vector3 translation = new Vector3(target.Transform.GlobalMatrix.Translation.x,
target.Transform.GlobalMatrix.Translation.y,
target.Transform.GlobalMatrix.Translation.z);
// Get the z-axis vector of target
Vector3 zAxis = new Vector3(target.Transform.GlobalMatrix.z.x,
target.Transform.GlobalMatrix.z.y,
target.Transform.GlobalMatrix.z.z);
// Get the matrix of target
Matrix4 tMatrix = target.Transform.GlobalMatrix;
// Rotate the matrix
tMatrix.Rotate(translation, zAxis, rotationAngle);
// Set the new matrix to target matrix
target.Transform.GlobalMatrix = tMatrix;
}
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
}
Required Namespaces
ABB.Robotics.RobotStudio.Stations