UserDefined data |
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 objectThe 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:
RapidDataType rdt; rdt = this.controller.Rapid.GetRapidDataType("T_ROB1", "MyModule", "processdata"); UserDefined processdata = new UserDefined(rdt);
Dim rdt As RapidDataType rdt = Me.controller.Rapid.GetRapidDataType("T_ROB1", "MyModule", "processdata") Dim processdata As UserDefined = New UserDefined(rdt)
![]() |
---|
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"); |
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.
UserDefined processdata = (UserDefined) rd.Value; string no1 = processdata.Components[0].ToString(); string no2 = processdata.Components[1].ToString();
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 dataIf 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.
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.
![]() |
---|
For Records, if FillFromNum() does not work, use FillFromString(). |
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 typeRapidDataType rdt = this.aRapid.GetRapidDataType("T_ROB1", "MyModule", "processdata"); ProcessData p = new ProcessData(rdt); p.FillFromString(rd.Value.ToString());
Dim rdt As RapidDataType = Me.ARapid.GetRapidDataType("T_ROB1", "MyModule", "processdata") Dim p As ProcessData = New ProcessData(rdt) p.FillFromString(rd.Value.ToString())
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.
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); } } }
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
The following piece of code shows how the two IRapidData methods ToString and FillFromString can be implemented.
public void FillFromString(string newValue) { IntData.FillFromString(newValue); } public override string ToString() { return IntData.ToString(); }
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
![]() |
---|
The ToString method has to use the Overrides keyword in Visual Basic and the override keyword in C#. |
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.
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; } }
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