Click or drag to resize

UserDefined data

Overview

You often work with RECORD structures in RAPID code. To handle these unique data types, a UserDefined class has been implemented. This class has properties and methods to handle individual components of a RECORD.

In some cases implementing your own .NET structure can improve application design and code maintenance.

Creating UserDefined object

The UserDefined constructor takes a UserDefined object as argument. To retrieve a RapidDataType object the path to the declaration of the RAPID data type is passed as argument.

The following example creates a UserDefined object referencing the RAPID RECORD processdata:

C#
RapidDataType rdt;
rdt = this.controller.Rapid.GetRapidDataType("T_ROB1", "MyModule", "processdata");
UserDefined processdata = new UserDefined(rdt);
VB
Dim rdt As RapidDataType
rdt = Me.controller.Rapid.GetRapidDataType("T_ROB1", "MyModule", "processdata")
Dim processdata As UserDefined = New UserDefined(rdt)
Note Note

If the module where the RECORD is defined is configured as you only provide the name of the data type as argument, like this: rdt = this.controller.Rapid.GetRapidDataType("processdata");

Reading UserDefined data

You can use a UserDefined object to read any kind of RECORD variable from the controller. The individual components of the RECORD are accessible using the Component property and an index. Each Component can be read as a string.

C#
UserDefined processdata = (UserDefined) rd.Value;
string no1 = processdata.Components[0].ToString();
string no2 = processdata.Components[1].ToString();
VB
Dim processdata As UserDefined = DirectCast(rd.Value, UserDefined)
Dim No1 As String = processdata.Components(0).ToString()
Dim No2 AS String = processdata.Components(1),ToString()

Each individual string can then be used in a FillFromString method to convert the component into a specific data type, for example RobTarget or ToolData. For more information, see IRapidData.FillFromString method.

Writing to UserDefined data

If you want to modify UserDefined data and write it to the RECORD in the controller, you must first read the UserDefined object and the apply new values using the FillFromString method. Then you perform a write operation using the RapidData.Value property.

C#
processdata.Components[0].FillFromString("[0,0,0]");
processdata.Components[1].FillFromString("10");
rd.Value = ud;

For more information and code samples, see IRapidData.FillFromString method and Working with Rapid Data.

Note Note

For Records, if FillFromNum() does not work, use FillFromString().

Implement your own struct representing a RECORD

The following example shows how you can create your own .NET data type representing a RECORD in the controller instead of using the UserDefined type.

Creating ProcessData type
C#
RapidDataType rdt = this.aRapid.GetRapidDataType("T_ROB1", "MyModule", "processdata");
ProcessData p = new ProcessData(rdt);
p.FillFromString(rd.Value.ToString());
VB
Dim rdt As RapidDataType = Me.ARapid.GetRapidDataType("T_ROB1", "MyModule", "processdata")
Dim p As ProcessData = New ProcessData(rdt)
p.FillFromString(rd.Value.ToString())
Implementing ProcessData struct

The following example shows how the new data type ProcessData may be implemented. This is done by using a .NET struct and letting ProcessData wrap the UserDefined object.

The struct implementation should include a FillFromString and ToString method, that is, inherit the IRapidData interface. Any properties and methods may also be implemented.

C#
 public struct ProcessData: IRapidData
 {
 private UserDefined data;
 public ProcessData(RapidDataType rdt)
 {
 data = new UserDefined(rdt);
 }
 private UserDefined IntData
 {
 get { return data; }
 set { data = value; }
 }
public int StepOne
 {
 get
 {
 int res = Convert.ToInt32(IntData.Components[0].ToString());
 return res;
 }
 set
 {
 IntData.Components[0] = new Num(value);
 }
 }
 }
VB
 Public Structure ProcessData
 Implements IRapidData
 Private data As UserDefined
 Public Sub New(ByVal rdt As RapidDataType)
 data = New UserDefined(rdt)
 End Sub
 Private Property IntData() As UserDefined
 Get
 Return data
 End Get
Set(ByVal Value As UserDefined)
 data = Value
 End Set
 End Property
 .....
 End Structure
Implementing IRapidData methods

The following piece of code shows how the two IRapidData methods ToString and FillFromString can be implemented.

C#
public void FillFromString(string newValue)
{
IntData.FillFromString(newValue);
}
public override string ToString()
{
return IntData.ToString();
}
VB
 Public Sub FillFromString(ByVal newValue As String) Implements ABB.Robotics.Controllers.RapidDomain.IRapidData.FillFromString
 IntData.FillFromString(newValue)
 End Sub
Public Overrides Function ToString() As String Implements ABB.Robotics.Controllers.RapidDomain.IRapidData.ToString
 Return IntData.ToString()
 End Function

Note Note
The ToString method has to use the Overrides keyword in Visual Basic and the override keyword in C#.

Property implementation

Each item in the RECORD structure should have a corresponding property in the extended .NET data type. The get and set methods have to implement the conversion from/to controller data type to .NET data type.

C#
 public int Step
{
 get
 {
 int res = Convert.ToInt32(IntData.Components[0].ToString());
 return res;
 }
 set
 {
 Num tmp = new Num();
tmp.FillFromNum(value);
 IntData.Components[0] = tmp;
 }
 }
VB
 Public Property Step() As Integer
 Get
 Dim res As Integer = Convert.ToInt32(IntData.Components(0).ToString())
 Return res
 End Get
 Set(ByVal Value As Integer)
Dim tmp As Num = New Num
 tmp.FillFromNum(Value)
 IntData.Components(0) = tmp
 End Set
 End Property