RobotStudio event

Generic MyGetRapidData<T>(ControllerInfo,VarName)

Example how to use generics for retreiving rapid data in C# - Example written for VSTA in RobotStudio API
 

pre-requisite - ControllerInfo set against a virtual controller. = cinfo

will return the fisrt found or a default of the type if not found or an exception.

 

Example Usage:

 

bool bVar1= MyGetRapidData<Bool>(cinfo, "bSomeVariableNameA").Value;
double dNum1 = MyGetRapidData<Num>(cinfo, "nSomeVariableName1").Value / 1000.0;
RobTarget r = MyGetRapidData<RobTarget>(cinfo, "p10");


 

//Generic to get RapidData() structure
private T MyGetRapidData<T>(ControllerInfo ci, string stVariableName)
{
  T objectToReturn = default(T);
  if (ci == null)
  {
    Logger.AddMessage(new LogMessage("ContollerInfo Parameter=NULL!", LogMessageSeverity.Warning));
    return objectToReturn;
   }
using (Controller controller = ControllerFactory.CreateFrom(ci))
{
  RapidSymbolSearchProperties sProp = RapidSymbolSearchProperties.CreateDefaultForData(true);
  Task[] tasks = controller.Rapid.GetTasks();
  foreach (Task task in tasks)
  {
    using (task)
    {
      RapidSymbol[] datas = task.SearchRapidSymbol(sProp, "^" + stVariableName + ".*");
      if (datas.GetLength(0) > 0)
      {
        using (RapidData rapidData = new RapidData(controller, datas[0]))
        {
          tasks = null;
          try { objectToReturn = (T)rapidData.Value; } 
          catch (Exception ohoh) { Logger.AddMessage(new LogMessage("GetRapidData<T>  (...) " + ohoh.Message, LogMessageSeverity.Error)); }
          return objectToReturn;
         } 

      }
    }
  }
}
Logger.AddMessage(new LogMessage("Could not find " + stVariableName, LogMessageSeverity.Warning));
return objectToReturn;
}

 

Maybe someone can clean it up - im new to c# - and may have broken some basic rules here - but it seems to work.
Thomas J2010-12-01 02:31:18
Thomas H. Johnston
PACs Application Engineer