TheSwamp

Code Red => .NET => Topic started by: It's Alive! on October 26, 2007, 01:55:17 AM

Title: [Serializable] not!
Post by: It's Alive! on October 26, 2007, 01:55:17 AM

Does anyone know why Autodesk chose not to mark some of the basic types
such as Point3d, Point2d, ObjectId, etc [Serializable]   ?
Title: Re: [Serializable] not!
Post by: MP on October 26, 2007, 04:31:39 AM
Insomniac response so it's probably way out to lunch but - perhaps because they are all primitive data types the overhead / bloat isn't justifiable, in particular because of the cascaded effect upon the object model greater.
Title: Re: [Serializable] not!
Post by: MP on October 26, 2007, 05:33:34 AM
http://msdn2.microsoft.com/en-us/library/ms979193.aspx
Title: Re: [Serializable] not!
Post by: Kerry on October 26, 2007, 06:18:37 AM
Hi Daniel .. I'm playing guessing games as well, but Michael's conjecture seems to make sense.

... I'd also guess that the serialization would need to be purpose specific'

..  .. from memory very few of the library classes are Serializable.

There is a .net sample in the ObjectARX SDK called TestFiler.cs demonstrating Object dumping ... but there probably won't be much new stuff there for you :-)

sorry I can't be of help.
   
Title: Re: [Serializable] not!
Post by: It's Alive! on October 26, 2007, 06:30:59 AM
Thanks for the replies. I’ll just wrap the objects I need serialized in new serializable objects and/or breakout the primitives I need stored.
Title: Re: [Serializable] not!
Post by: MickD on October 26, 2007, 07:58:08 PM

Does anyone know why Autodesk chose not to mark some of the basic types
such as Point3d, Point2d, ObjectId, etc [Serializable]   ?

My guess would be is seeing that acad looks after serialisation itself and the .net lib's are just a wrapper/interface to the 'real' objects it wasn't required...from their point of view of course.
Title: Re: [Serializable] not!
Post by: It's Alive! on October 27, 2007, 01:36:17 AM
Having a little fun here

Code: [Select]
//(Serialize "c:\\test.dat" "hola")
//(Serialize "c:\\test.dat" '(1 1 1))
//(Serialize "c:\\test.dat" '(1 2 3 1.2 "Wow This is almost cool"))
//(UnSerialize "c:\\test.dat")

//(SaveVars "c:\\test.dat")
//(RestoreVars "c:\\test.dat")

//(defun savevars (path / outlist varlist)
// (setq outlist '())
// (setq varlist '("osmode" "cmdecho" "attdia" "pickstyle"))
// (foreach e varlist
//  (setq outlist (cons (cons e (getvar e)) outlist))
// )
// (Serialize path outlist)
//)

//(defun RestoreVars (path / e inlist)
// (setq inlist (UnSerialize path))
// (if (/=(car inlist) nill)
//  (foreach e inlist
//   (setvar (car e)(cdr e) )
//  )
// )
// (princ)
//)


using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(LispSerialization.Commands))]

namespace LispSerialization
{
  public class Commands
  {
    public Commands()
    {
    }
    [LispFunction("Serialize")]
    static public ResultBuffer dome(ResultBuffer Rb)
    {
      ResultBuffer RetRb = new ResultBuffer();
      try
      {
        TypedValue[] tvArray = Rb.AsArray();
        Stream stream = new FileStream((string)tvArray[0].Value, FileMode.Create);
        try
        {
          List<KeyValuePair<int, Object>>
            kvList = new List<KeyValuePair<int, object>>();
          for (int i = 1; i < tvArray.Length; i++)
          {
            switch ((int)tvArray[i].TypeCode)
            {
              case (int)LispDataType.Point2d:
                {
                  Point2d pt = (Point2d)tvArray[i].Value;
                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.ListBegin, null));

                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.Double, pt.X));

                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.Double, pt.Y));

                  kvList.Add(new KeyValuePair<int, object>
                    ((int)LispDataType.ListEnd, null));
                }
                break;
              case (int)LispDataType.Point3d:
                {
                  Point3d pt = (Point3d)tvArray[i].Value;

                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.ListBegin, null));

                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.Double, pt.X));

                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.Double, pt.Y));

                  kvList.Add(new KeyValuePair<int, object>
                     ((int)LispDataType.Double, pt.Z));

                  kvList.Add(new KeyValuePair<int, object>
                    ((int)LispDataType.ListEnd, null));
                }
                break;
              case (int)LispDataType.Angle:
              case (int)LispDataType.DottedPair:
              case (int)LispDataType.Double:
              case (int)LispDataType.Int16:
              case (int)LispDataType.Int32:
              case (int)LispDataType.ListBegin:
              case (int)LispDataType.ListEnd:
              case (int)LispDataType.Nil:
              case (int)LispDataType.None:
              //case (int)LispDataType.Orientation:
              case (int)LispDataType.T_atom:
              case (int)LispDataType.Text:
              case (int)LispDataType.Void:
                kvList.Add(new KeyValuePair<int, object>
                       ((int)tvArray[i].TypeCode, (object)tvArray[i].Value));
                break;
            }
          }
          IFormatter formatter = new BinaryFormatter();
          formatter.Serialize(stream, kvList);
          RetRb.Add(new TypedValue((int)LispDataType.T_atom));
        }
        finally
        {
          stream.Close();
        }
      }
      catch
      {
        RetRb.Add(new TypedValue((int)LispDataType.Nil));
      }
      return RetRb;
    }

    [LispFunction("UnSerialize")]
    static public ResultBuffer undome(ResultBuffer Rb)
    {
      TypedValue[] tvArray = Rb.AsArray();
      ResultBuffer RetRb = new ResultBuffer();
      Stream stream = null;
      try
      {
        stream = new FileStream((string)tvArray[0].Value, FileMode.Open);
        IFormatter formatter = new BinaryFormatter();

        List<KeyValuePair<int, Object>> kvList =
          (List<KeyValuePair<int, Object>>)formatter.Deserialize(stream);

        for (int i = 0; i < kvList.Count; i++)
        {
          RetRb.Add(new TypedValue(kvList[i].Key, kvList[i].Value));
        }
      }
      catch
      {
        RetRb.Add(new TypedValue((int)LispDataType.Nil));
      }
      finally
      {
        stream.Close();
      }
      return RetRb;
    }
  }
}