Creating Local Targets
This example shows how to create a workobject and a local target with unique names, using the associated APIs. This topic also provides information on how to find the first target by workobject and data declaration from module scope.
An active station is required to replicate this example.
Solution
Start by obtaining the active station and initializing the position values and module name.
// Get the active station Station station = Station.ActiveStation; // Define a position and a module name Vector3 position = new Vector3(-0.50629, -3, 0.67950); string moduleName = "myModule";Create a workobject, assigning it a unique name and specifying its module.
// Create work object RsWorkObject wobj = new RsWorkObject(); wobj.Name = station.ActiveTask.GetValidRapidName("wobj", "_", 1); wobj.ModuleName = moduleName; station.ActiveTask.DataDeclarations.Add(wobj);Create a robtarget, setting its name, position, locality, and module name. Then, create the visual representation of the target and add it to the active task.
// Create robtarget RsRobTarget robTarget = new RsRobTarget(); robTarget.Name = station.ActiveTask.GetValidRapidName("Target", "_", 10); robTarget.Frame.Translation = position; // Translation robTarget.Local = true; // Set LOCAL to true robTarget.ModuleName = moduleName; // Set module name station.ActiveTask.DataDeclarations.Add(robTarget); // Create the visual target representation RsTarget target = new RsTarget(wobj, robTarget); target.Name = robTarget.Name; // Add target to active task station.ActiveTask.Targets.Add(target);Find first target by workobject.
// Find first target by workobject RsTarget firstTarget = station.ActiveTask.FindFirstTargetByWorkObject(wobj); Logger.AddMessage(new LogMessage($"First target: {firstTarget.Name}"));Find data declaration from module scope.
// Find data declaration from module scope RsDataDeclaration dataDecl = station.ActiveTask.FindDataDeclarationFromModuleScope(wobj.Name, moduleName); Logger.AddMessage(new LogMessage($"Datadeclaration: {dataDecl.Name}"));
Example
This example provides information on creating a local target and how to use its associated APIs.
Project.UndoContext.BeginUndoStep("CreateTarget");
try
{
// Get the active station
Station station = Station.ActiveStation;
// Define a position and a module name
Vector3 position = new Vector3(-0.50629, -3, 0.67950);
string moduleName = "myModule";
// Create work object
RsWorkObject wobj = new RsWorkObject();
wobj.Name = station.ActiveTask.GetValidRapidName("wobj", "_", 1);
wobj.ModuleName = moduleName;
station.ActiveTask.DataDeclarations.Add(wobj);
// Create robtarget
RsRobTarget robTarget = new RsRobTarget();
robTarget.Name = station.ActiveTask.GetValidRapidName("Target", "_", 10);
robTarget.Frame.Translation = position; // Translation
robTarget.Local = true; // Set LOCAL to true
robTarget.ModuleName = moduleName; // Set module name
station.ActiveTask.DataDeclarations.Add(robTarget);
// Create the visual target representation
RsTarget target = new RsTarget(wobj, robTarget);
target.Name = robTarget.Name;
// Add target to active task
station.ActiveTask.Targets.Add(target);
// Find first target by workobject
RsTarget firstTarget = station.ActiveTask.FindFirstTargetByWorkObject(wobj);
Logger.AddMessage(new LogMessage($"First target: {firstTarget.Name}"));
// Find data declaration from module scope
RsDataDeclaration dataDecl = station.ActiveTask.FindDataDeclarationFromModuleScope(wobj.Name, moduleName);
Logger.AddMessage(new LogMessage($"Datadeclaration: {dataDecl.Name}"));
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
Required Namespaces
ABB.Robotics.RobotStudio.Stations