Show / Hide Table of Contents

Copying Targets

This example shows you how to copy a target. First, a target is created and added to the active task. The target is then copied and the name is changed. The copied target is added to the active task, after the original target.

Then, the copied target is moved 0.5 meter in the positive X-axis direction.

The Highlight color is the color of the target when it is selected in the graphics. This color is set to green, using the color property of the target.

The last part removes the original target, leaving only the copy left. You can see the copied target in the Paths&Targets browser.

Compiling the Code

Note

You need to add System.Drawing as a reference for this example to work!

Example

Project.UndoContext.BeginUndoStep("Copy Target");
try
{
    // Instance active station.
    Station stn = Station.ActiveStation;

    // Get the active workobject.
    RsWorkObject wobj = stn.ActiveTask.ActiveWorkObject;

    // Create a new RobTarget and add it to the ActiveTask.
    RsRobTarget robTarget = new RsRobTarget();
    robTarget.Name = stn.ActiveTask.GetValidRapidName("MyTarget", "_", 10);
    stn.ActiveTask.DataDeclarations.Add(robTarget);

    // Create an RsTarget from wobj and RobTarget.
    RsTarget target = new RsTarget(wobj, robTarget);

    // Set the name of the target.
    target.Name = robTarget.Name;

    // Add RsTarget to ActiveTask.
    stn.ActiveTask.Targets.Add(target);

    // Copy the target.
    RsTarget targetCopy = (RsTarget)target.Copy();

    // Set the new name to the copied targets.
    targetCopy.Name = target.Name + "_Copy";

    // Add the copied target, after the original target, to the ActiveTask.
    stn.ActiveTask.Targets.Add(targetCopy, target);

    // Move the new target 0.5 meters along the X-axis.
    targetCopy.Transform.X = targetCopy.Transform.X + 0.5;

    // Set the highlight color of the target.
    target.Highlight(Color.Green);

    // Reset the highlight color of the target copy.
    targetCopy.ResetHighlight();

    // Remove the original target.
    stn.ActiveTask.Targets.Remove(target);
}
catch (Exception)
{
    Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
    throw;
}
finally
{
    Project.UndoContext.EndUndoStep();
}

See Also

  • RsTarget
  • Copy()
  • RsRobTarget
  • RsWorkObject
  • RobotStudio Community
In this article
Back to top Copyright © 2025 ABB