Generic MyGetRapidData<T>(ControllerInfo,VarName)
Thomas J
✭
Example how to use generics for retreiving rapid data in C# - Example written for VSTA in RobotStudio API
Thomas J2010-12-01 02:31:18
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");
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;
}
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;
}
}
}
}
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 H. Johnston
PACs Application Engineer
PACs Application Engineer
0
Categories
- All Categories
- 5.5K RobotStudio
- 396 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 250 ScreenMaker
- 2.8K Robot Controller
- 316 IRC5
- 61 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 799 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 5 Job listings