Show / Hide Table of Contents

Target Properties Example

This example will show you how to use the different properties on RsTarget class. First, the target will be created and added to the station. You can find the target in the Paths&Targets browser. Go to Station Elements -> Default Task -> Workobjects & Targets -> wobj0 -> wobj0_of.

The Add-In will print the properties in the logger (the output window in RobotStudio).

Note

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

Example

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

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

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

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

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

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

    // Add a custom attribute to the target.
    target.Attributes.Add("MyAttribute", "10");

    // Output the project that the target is part of, and the parent of the target.
    Logger.AddMessage(new LogMessage("The target: " + target.Name + " is part of the project: " +
            target.ContainingProject.Name + ". The targets parent is: " + target.Parent.Name));

    // Output the ToString-reprentation of the target and its types diplay name.
    Logger.AddMessage(new LogMessage("The targets ToString representation is '" +
            target.ToString() + "' and its TypeDisplayName is '" + target.TypeDisplayName + "'."));

    // Set the workobject of the target as the active workobject.
    stn.ActiveTask.ActiveWorkObject = target.WorkObject;

    // Create a copy of the RobTarget.
    RsRobTarget newRobTarget = (RsRobTarget)robTarget.Copy();

    // Assign the copy to the target.
    target.RobTarget = newRobTarget;

    // Check if the target is invisible, and make it visible and yellow.
    if (!target.Visible)
    {
        target.Visible = true;
        Color col = Color.Yellow;
        target.Color = col;
    }

    // Make the target's grapical display size twice as large.
    target.FrameSize = target.FrameSize * 2;

    // Get the transform and output its values using the logger.
    Logger.AddMessage(new LogMessage("Transform of target: " + target.Name
                                    + "; X: " + target.Transform.X
                                    + "; Y: " + target.Transform.Y
                                    + "; Z: " + target.Transform.Z
                                    + "; Rx: " + target.Transform.RX
                                    + "; Ry: " + target.Transform.RY
                                    + "; Rz: " + target.Transform.RZ
                                    ));

    // Get the ReferenceFrame of the target.
    Transform refFrame = target.ReferenceFrame;

    // Show the reference frame in the graphics.
    target.ShowReferenceFrame = true;

    // If the AxisDirection of the target is undefined then set it to negative x.
    if (target.ApproachVector == AxisDirection.Undefined)
    {
        target.ApproachVector = AxisDirection.NegativeX;
    }
}
catch
{
    Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
    throw;
}
finally
{
    Project.UndoContext.EndUndoStep();
}

See Also

  • RsRobTarget
  • RsTarget
  • RsWorkObject
  • RobotStudio Community
In this article
Back to top Copyright © 2024 ABB