Align Targets in Path direction
This example demonstrates how to align all targets based on the direction from the first to the last target in the collection. The aligning of targets can be seen graphically.
Solution
Get the active station, and then get the targets from the active task
// Get the active station, then get the targets from the active task Station station = Station.ActiveStation; RsTargetCollection existingTargets = Station.ActiveStation.ActiveTask.Targets;Find targets from RsRobTarget
// Find targets from Robtarget RsTarget[] firstTarget = station.ActiveTask.FindTargetsByRobTarget(existingTargets[0].RobTarget); RsTarget[] secondTarget = station.ActiveTask.FindTargetsByRobTarget(existingTargets[existingTargets.Count - 1].RobTarget); // Get the translation values of targets Vector3 firstTargetVector = firstTarget[0].Transform.GlobalMatrix.Translation; Vector3 secondTargetVector = secondTarget[0].Transform.GlobalMatrix.Translation;Create a normal vector
// Create a normal vector Vector3 normalVector = new Vector3(firstTarget[0].Transform.GlobalMatrix.z.x, firstTarget[0].Transform.GlobalMatrix.z.y, firstTarget[0].Transform.GlobalMatrix.z.z);Find the direction vector between the first and second point
// Find the direction vector between p1 and p2 Vector3 dir_p1p2 = secondTargetVector.Subtract(firstTargetVector); dir_p1p2.Normalize();Find Cross product of direction vector and normal vector
// Find Cross product of direction vector and normal vector Vector3 y = dir_p1p2.Cross(normalVector); y.Normalize();Assign new values to targets
// Assign new values to targets foreach (RsTarget target in existingTargets) { Matrix4 normalMatrix = new Matrix4(y, dir_p1p2, normalVector, target.Transform.GlobalMatrix.Translation); target.RobTarget.Frame.Matrix = normalMatrix; }
Example
// Begin UndoStep
Project.UndoContext.BeginUndoStep("AlignTarget");
try
{
// Get the active station, then get the targets from the active task
Station station = Station.ActiveStation;
RsTargetCollection existingTargets = Station.ActiveStation.ActiveTask.Targets;
// To align targets, there must be more than 1 target in the collection
if (existingTargets.Count > 1)
{
// Find targets from Robtarget
RsTarget[] firstTarget = station.ActiveTask.FindTargetsByRobTarget(existingTargets[0].RobTarget);
RsTarget[] secondTarget = station.ActiveTask.FindTargetsByRobTarget(existingTargets[existingTargets.Count - 1].RobTarget);
// Get the translation values of targets
Vector3 firstTargetVector = firstTarget[0].Transform.GlobalMatrix.Translation;
Vector3 secondTargetVector = secondTarget[0].Transform.GlobalMatrix.Translation;
// Create a normal vector
Vector3 normalVector = new Vector3(firstTarget[0].Transform.GlobalMatrix.z.x,
firstTarget[0].Transform.GlobalMatrix.z.y,
firstTarget[0].Transform.GlobalMatrix.z.z);
// Find the direction vector between p1 and p2
Vector3 dir_p1p2 = secondTargetVector.Subtract(firstTargetVector);
dir_p1p2.Normalize();
// Find Cross product of direction vector and normal vector
Vector3 y = dir_p1p2.Cross(normalVector);
y.Normalize();
// Assign new values to targets
foreach (RsTarget target in existingTargets)
{
Matrix4 normalMatrix = new Matrix4(y, dir_p1p2, normalVector, target.Transform.GlobalMatrix.Translation);
target.RobTarget.Frame.Matrix = normalMatrix;
}
}
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
Required Namespaces
ABB.Robotics.RobotStudio.Stations