Author Topic: Updating value in a result buffer array.  (Read 2429 times)

0 Members and 1 Guest are viewing this topic.

tik

  • Guest
Updating value in a result buffer array.
« on: January 12, 2012, 02:04:19 PM »
I have a ResultBuffer with XData in it. I want to update value in the ResultBuffer at a particular Index. I wrote a function to achieve that functionality but it is not working. I am passing the ResultBuffer as reference and disposing the ResultBuffer in the code that is calling this function. Can someone point to me what I am doing wrong.

Code: [Select]
public bool SetXDataAtIndex(ref ResultBuffer rb, int index, Object value)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;           
                TypedValue val = new TypedValue();
                if (rb != null)
                {
                    try
                    {
                        if (index >= rb.AsArray().Length)
                        {
                            return false;
                        }
                        val = new TypedValue(rb.AsArray()[index].TypeCode, value);
                        rb.AsArray()[index] = val;
                    }
                    catch (System.Exception)
                    {
                        return false;
                    }
                }
                return true;           
        }

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Updating value in a result buffer array.
« Reply #1 on: January 12, 2012, 02:21:09 PM »
Hi,

When doing :
rb.AsArray()[index] = val;
you're changing the value in a TypedValue array object not in your ResultBuffer.

Try somethinf like this:
Code: [Select]
        public bool SetXDataAtIndex(ref ResultBuffer rb, int index, TypedValue val)
        {
            TypedValue[] values = rb.AsArray();
            if (values.Length > index)
            {
                values[index] = val;
                rb = new ResultBuffer(values);
                return true;
            }
            return false;
        }
« Last Edit: January 12, 2012, 02:35:09 PM by gile »
Speaking English as a French Frog

tik

  • Guest
Re: Updating value in a result buffer array.
« Reply #2 on: January 12, 2012, 02:28:23 PM »
Gile,

My idea was to update value at a given index in the TypedValue array and assign the new TypedValue array to the Result Buffer again. Sorry for using wrong terminology.

cincir

  • Guest
Re: Updating value in a result buffer array.
« Reply #3 on: January 13, 2012, 08:50:25 PM »
hi this is my first answer :)

you must create a new ResultBuffer and use your new TypedValue to create one. Because AsArray returns and array of TypedValues that are copies of the ones in ResultBuffer.

maybe something like this :

ResultBuffer newResultBuffer = new ResultBuffer();
TypedValue[] arrTypedValue = rb.AsArray();

for(int i = 0; i < arrTypedValue.Length ; i++)
{
     if (i == myIndex)
     {
            newResultBuffer.Add(new TypedValue((int)DxfCode.something,something));
     }
     else
     {
            newResultBuffer.Add(arrTypedValue);
     }
}

Hope this helps...

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Updating value in a result buffer array.
« Reply #4 on: January 14, 2012, 03:48:39 AM »
Quote
you must create a new ResultBuffer and use your new TypedValue to create one. Because AsArray returns and array of TypedValues that are copies of the ones in ResultBuffer.

This is exactly what the code I posted does...
Speaking English as a French Frog

fixo

  • Guest
Re: Updating value in a result buffer array.
« Reply #5 on: January 14, 2012, 05:40:11 AM »

My idea was to update value at a given index in the TypedValue array and assign the new TypedValue array to the Result Buffer again. Sorry for using wrong terminology.

See quick example, you have to set the old TypeCode with new value
for partcular item in the ResultBuffer:

Code: [Select]
        public void ShowBuffer()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed=doc.Editor;
           
            try
            {
             ResultBuffer buf  = new ResultBuffer(new TypedValue[]
            { new TypedValue((int)DxfCode.Text, "ahha"),
               new TypedValue( (int)DxfCode.Real,1.2345),
            new TypedValue( (int)DxfCode.Int32,1000)});//old value
             TypedValue[] oldvalues = buf.AsArray();
            foreach (TypedValue tv in oldvalues)
                ed.WriteMessage("\nOld Value: {0}", tv.Value);
            int size = buf.AsArray().Length;
            int index = 1;
            object newvalue = 2000;// new value
                 TypedValue[] newvalues = new TypedValue[size];
               for (int n =0;n<size;n++)
               {
                   if (n != index)
                       newvalues[n] = oldvalues[n];
                   else
                       // set new value by given idex:
                       newvalues[n] = new TypedValue((int)oldvalues[n].TypeCode, newvalue);
               }
               buf = new ResultBuffer(newvalues);
               foreach (TypedValue tv in buf.AsArray())
                   ed.WriteMessage("\nNew Value: {0}", tv.Value);
            buf.Dispose();
          }

                catch (System.Exception ex)
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
                }
                finally
                {               
                    //do nothing or display your message here
                }
            }

~'J'~