RsDataDeclarationCollection Example
This example shows you how to create a RsDataDeclarationsCollection and add objects to it, in this case a RsRobTarget. The example prints out some information about the collection in the logger (the output window in RobotStudio).
Example
Project.UndoContext.BeginUndoStep("RsDataDeclarationCollection");
try
{
Station station = Station.ActiveStation;
// Get the RsDataDeclarationCollection for the ActiveTask.
RsDataDeclarationCollection ddCol = station.ActiveTask.DataDeclarations;
RsRobTarget rbt = new RsRobTarget();
rbt.Name = station.ActiveTask.GetValidRapidName("MyRobTarget", "_", 1);
String robTargetName = rbt.Name;
ddCol.Add(rbt);
// Output some info of the DataDeclaration in the logger.
Logger.AddMessage(new LogMessage("The RsDataDeclarationCollection on the active task contains:"));
foreach (RsDataDeclaration dd in ddCol)
{
Logger.AddMessage(new LogMessage("DataDeclaration: " + dd.Name + " with type: " + dd.TypeDisplayName));
}
// Check that RsDataDeclarationCollection contains the RobTarget that was added.
if (station.ActiveTask.FindDataDeclarationFromModuleScope(robTargetName, rbt.ModuleName) != null)
{
Logger.AddMessage(new LogMessage("The RsDataDeclarationCollection contains " + robTargetName));
}
RsDataDeclaration rbt2;
try
{
rbt2 = station.ActiveTask.FindDataDeclarationFromModuleScope("MyRobTarget_2", rbt.ModuleName);
if (rbt2 != null)
{
Logger.AddMessage(new LogMessage("Found MyRobTarget_2"));
}
else
{
Logger.AddMessage(new LogMessage("Did not find MyRobTarget_2"));
}
}
catch (ArgumentException e)
{
Logger.AddMessage(new LogMessage("The error: '" + e.Message + "' occured when trying to get 'MyRobTarget_2'"));
}
// Remove MyRobTarget_1 from the collection.
ddCol.Remove(rbt);
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}