RECORD access via PC-SDK

Options
Hi all,

I have an array of RECORD like below. How can I access it with struct in PC-SDK?

RECORD rec
    num myNum;
    pos myPos;
ENDRECORD

PERS rec myArray{2}:=[ [ 2, [ 1, 2, 3 ] ], [ 3,[ 4, 5, 6 ] ] ];

Thanks for the help.
Tagged:

Best Answers

  • ko414du4
    ko414du4
    Answer ✓
    Options
    Hi,

    Hereby a sample program. If you have any questions about it, just let me know :smile: 
    Thank you very much for this John_Verheij. Works like magic!

    I also took it further by making the structure implement IRapidData like below. This allows the variable to have more or less the same function as the other RapidDomain structures.
    In this sample, rec RECORD is defined in RS as follows:

    RECORD rec
            num num1;
            robtarget tgt;
    ENDRECORD

    public struct Rec : IRapidData
    {
        // Structure fields
        private Num num;
        private RobTarget tgt;
        private RapidData refRapidData;
     
        // Structure properties
        public Num NumVal
        {
            get { return num; }
            set { num = value; }
        }
     
        public RobTarget TgtVal
        {
            get { return tgt; }
            set { tgt = value; }
        }
     
        public static Rec Empty
        {
            get;
        }
     
        public Rec(RapidData input)
        {
            UserDefined ud = (UserDefined)input.Value;
     
            // Initialise struct fields from the RapidData:
            // ~ refRapidData takes the REFERENCE of the RapidData. This is useful in SaveToRapidData() method.
            // ~ num & tgt takes the VALUE of the RapidData components. This is useful in making internal temporary 
            //   changes before saving it (back) to the RapidData by SaveToRapidData() method.
            this.num = (Num)ud.Components[0];
            this.tgt = (RobTarget)ud.Components[1];
            refRapidData = input;
        }
     
        public override string ToString()
        {
            return "[" + NumVal + "," + TgtVal + "]";
        }
     
        public void SaveToRapidData()
        {
            // c is the controller defined globally. Required to request mastership.
            using (Mastership ms = Mastership.Request(c.Rapid))
            {
                refRapidData.StringValue = this.ToString();
            }
        }
     
        public void Fill(string value)
        {
            char[] delimiterChars = { ' ', ',', '[', ']' }; // Get rid of these chars from the value input
            string[] tokens = value.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
     
            // Assign struct's fields: NumVal
            NumVal = new Num(Convert.ToDouble(tokens[0]));
     
            // Assign struct's fields: TgtVal
            RobTarget newTgt = new RobTarget();
            newTgt.Trans.X = Convert.ToSingle(tokens[1]);
            newTgt.Trans.Y = Convert.ToSingle(tokens[2]);
            newTgt.Trans.Z = Convert.ToSingle(tokens[3]);
            newTgt.Rot.Q1 = Convert.ToSingle(tokens[4]);
            newTgt.Rot.Q2 = Convert.ToSingle(tokens[5]);
            newTgt.Rot.Q3 = Convert.ToSingle(tokens[6]);
            newTgt.Rot.Q4 = Convert.ToSingle(tokens[7]);
            newTgt.Robconf.Cf1 = Convert.ToInt32(tokens[7]);
            newTgt.Robconf.Cf4 = Convert.ToInt32(tokens[8]);
            newTgt.Robconf.Cf6 = Convert.ToInt32(tokens[9]);
            newTgt.Robconf.Cfx = Convert.ToInt32(tokens[10]);
            newTgt.Extax.Eax_a = Convert.ToSingle(tokens[11]);
            newTgt.Extax.Eax_b = Convert.ToSingle(tokens[12]);
            newTgt.Extax.Eax_c = Convert.ToSingle(tokens[13]);
            newTgt.Extax.Eax_d = Convert.ToSingle(tokens[14]);
            newTgt.Extax.Eax_e = Convert.ToSingle(tokens[15]);
            newTgt.Extax.Eax_f = Convert.ToSingle(tokens[16]);
            TgtVal = newTgt;
        }
     
        public void Fill(DataNode root)
        {
            throw new NotImplementedException();
        }
     
        public void FillFromString(string newValue)
        {
            Fill(newValue);
        }
     
        public DataNode ToStructure()
        {
            throw new NotImplementedException();
        }
    }

Answers

  • ko414du4
    Options
    Hi,

    Hereby a sample program. If you have any questions about it, just let me know :
    Thanks, John_Verheij!! I will give this a go and let you know how it  goes.