Handling arrays
Overview
In RAPID you can have up to three dimensional arrays.
These are accessible by using a RapidData object like for any other RAPID data.
There are mainly two ways of accessing each individual element of an array: by indexers or by an enumerator.
ArrayData object
If the RapidData references a RAPID array is Value property returns an object of ArrayData type. Before making a cast,
check the type using the is operator or by using the IsArray property on the RapidData object.
RapidData rd = aController.Rapid.GetRapidData("T_ROB1", "user", "num_array");
if (rd.IsArray)
{
ArrayData ad = (ArrayData)rd.Value;
.....
}
The ArrayData class lets you read and write data to a RAPID array. There are two different modes of transferring the data
from the controller to the ArrayData class.
Snapshot: In this mode all items of an array is transferred to the
ArrayDatainstance at a time.Dynamic: In this mode data for the specific item is transferred. For example when you access an item using
ArrayData[i,j,k]data for that specific item is transferred. Both Snapshot and Dynamic modes do not impose any limitation on the number of bytes that can be transferred. Data transfer using Dynamic mode offer improved performance over Snapshot since data is transferred on demand. To use Dynamic mode, you must set mode to 'dynamic' ifArrayDatais used in loops.
arrayData.Mode = ArrayModes.Dynamic;
foreach (IRapidData rapidData in arrayData)
{
Console.WriteLine(rapidData.ToString());
}
Note
The ArrayData modes are not effective when RapidData.ReadItem() is used to access a single item.
Limitation
The RapidData.Value.ToString() method is not supported when it is used to view a large RAPID array in snapshot mode.
Array dimensions
The dimension of the array is returned by the Rank property.
If you need to check the length of the individual arrays you can use the GetLength method on the ArrayData object passing
the dimension index as argument.
int aRank = ad.Rank;
int len = ad.GetLength(aRank);
Array item access by using indexers
By the use of indexers you can access each array element, even in three dimensional arrays.
A combination of the GetLength method and for loops makes it possible to access any item:
double aSum = 0d;
Num aNum;
RapidData rd = aController.Rapid.GetRapidData("T_ROB1", "user", "num_array");
ArrayData ad = (ArrayData)rd.Value;
int aRank = ad.Rank;
if (ad.Rank == 1)
{
for (int i = 1; i <= ad.Length; i++)
{
aNum = (Num)ad[i];
aSum += (double)aNum;
}
}
else if (ad.Rank == 2)
{
for (int i = 1; i <= ad.GetLength(0); i++)
{
for (int j = 1; j <= ad.Length; j++)
{
aNum = (Num)ad[i, j]; aSum += (double)aNum;
}
}
}
else
{
for (int i = 0; i < ad.GetLength(0); i++)
{
for (int j = 0; j < ad.GetLength(1); j++)
{
for (int k = 0; k < ad.GetLength(2); k++)
{
aNum = (Num)ad[i, j, k]; aSum += (double)aNum;
}
}
}
}
Array item access using enumerator
You can also use the enumerator operation (foreach) like it is used by collections in .NET. Note that it can be used for both one
dimension and multi-dimensional arrays to access each individual element. The previous example is a lot simpler this way:
double sum = 0d;
foreach (Num aNum in ad)
{
sum += (double)aNum;
}