Code Red > .NET

Serialize info

(1/3) > >>

It's Alive!:
Here is a fun one, guess what it does..  :-o


--- Code: ---//Written by Daniel Marcotte 10.31.2007
//Requires VS 2008 to compile for .NET 2.0

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.IO;
//
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;

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

//(SerializeSummaryInfo "c:\\drawings" "c:\\Test.xml")

//returns '(nil . error message) on error
//'(T) on success
namespace CadLinQ
{
  public class Commands
  {
    [LispFunction("SerializeSummaryInfo")]
    public static ResultBuffer testcmd(ResultBuffer Args)
    {
      ResultBuffer RetRb = new ResultBuffer();
      try
      {
        if (Args == null)
          return err("too few arguments", ref RetRb);

        List<TypedValue> ArgsList = new List<TypedValue>(Args.AsArray());

        if (ArgsList.Count < 2)
          return err("too few arguments", ref RetRb);

        if (ArgsList.Count > 2)
          return err("too many arguments", ref RetRb);

        if (ArgsList[0].TypeCode != (int)LispDataType.Text)
          return err("argument 1 is a string", ref RetRb);

        if (ArgsList[0].TypeCode != (int)LispDataType.Text)
          return err("argument 2 is a string", ref RetRb);

        string Inpath = (string)ArgsList[0].Value;
        string Outpath = (string)ArgsList[1].Value;

        List<string> info = GetFileList("*.dwg", Inpath);

        SummaryInfoList sList = new SummaryInfoList();
        info.ForEach(p => sList.Add(GetInfo(p)));

        XmlSerializer Serializer = new XmlSerializer(typeof(SummaryInfoList));
        TextWriter Writer = new StreamWriter(Outpath);
        Serializer.Serialize(Writer, sList);
        Writer.Close();

        RetRb.Add(new TypedValue((int)LispDataType.T_atom));
        string path = (string)ArgsList[0].Value;
      }
      catch (System.Exception ex)
      {
        return err(ex.Message, ref RetRb);
      }
      return RetRb;
    }

    private static SummaryInfo GetInfo(string path)
    {
      SummaryInfo SumInfo = new SummaryInfo();
      using (Database db = new Database(false, true))
      {
        db.ReadDwgFile(path, FileShare.Read, true, "");

        DatabaseSummaryInfoBuilder InfoBuilder =
           new DatabaseSummaryInfoBuilder(db.SummaryInfo);

        SumInfo.FileName = path;
        SumInfo.Title = InfoBuilder.Title;
        SumInfo.Subject = InfoBuilder.Subject;
        SumInfo.Author = InfoBuilder.Author;
        SumInfo.Keywords = InfoBuilder.Keywords;
        SumInfo.Comments = InfoBuilder.Comments;
        SumInfo.LastSavedBy = InfoBuilder.LastSavedBy;
        SumInfo.RevisionNumber = InfoBuilder.RevisionNumber;
        SumInfo.TotalEditingTime = db.Tdindwg.ToString();
        SumInfo.Created = db.Tducreate.ToString();
        SumInfo.Modified = db.Tdupdate.ToString();

        db.CloseInput(true);
      }
      return SumInfo;
    }

    private static List<string> GetFileList(string SearchString, string SearchPath)
    {
      return new List<string>(Directory.GetFiles(SearchPath, SearchString));
    }

    private static ResultBuffer err(string str, ref ResultBuffer Rb)
    {
      Rb.Add(new TypedValue((int)LispDataType.Nil));
      Rb.Add(new TypedValue((int)LispDataType.DottedPair));
      Rb.Add(new TypedValue((int)LispDataType.Text, str));
      return Rb;
    }
  }

  [Serializable]
  public class SummaryInfo
  {
    public string FileName { get; set; }
    public string Title { get; set; }
    public string Subject { get; set; }
    public string Author { get; set; }
    public string Keywords { get; set; }
    public string Comments { get; set; }
    public string LastSavedBy { get; set; }
    public string RevisionNumber { get; set; }
    public string TotalEditingTime { get; set; }
    public string Created { get; set; }
    public string Modified { get; set; }
  }

  [Serializable]
  public class SummaryInfoList : List<SummaryInfo>
  { public SummaryInfoList() { } }
}


--- End code ---

LE:
Daniel;

Since you liked all about the new tech and languages.... have you seen F#

MP:
I've been keeping my eye on that one Luis, very interesting.

Glenn R:
Hehe...nice one Dan.

I still don't like returning nil and a string to lisp if there is an error, but that might be as good a way as any...

Cheers,
Glenn.

LE:

--- Quote from: MP on October 31, 2007, 07:36:08 PM ---I've been keeping my eye on that one Luis, very interesting.

--- End quote ---

Yes it is....

Michael;

Have you seen the short sample for AutoCAD here:

http://through-the-interface.typepad.com/

Navigation

[0] Message Index

[#] Next page

Go to full version