TheSwamp

Code Red => .NET => Topic started by: It's Alive! on October 31, 2007, 03:22:00 PM

Title: Serialize info
Post by: It's Alive! on October 31, 2007, 03:22:00 PM
Here is a fun one, guess what it does..  :-o

Code: [Select]
//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() { } }
}

Title: Re: Serialize info
Post by: LE on October 31, 2007, 06:41:34 PM
Daniel;

Since you liked all about the new tech and languages.... have you seen F#
Title: Re: Serialize info
Post by: MP on October 31, 2007, 07:36:08 PM
I've been keeping my eye on that one Luis, very interesting.
Title: Re: Serialize info
Post by: Glenn R on October 31, 2007, 07:51:59 PM
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.
Title: Re: Serialize info
Post by: LE on October 31, 2007, 10:25:22 PM
I've been keeping my eye on that one Luis, very interesting.

Yes it is....

Michael;

Have you seen the short sample for AutoCAD here:

http://through-the-interface.typepad.com/
Title: Re: Serialize info
Post by: It's Alive! on October 31, 2007, 11:06:23 PM
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...

It’s just on a trial bases, I am beginning to like it though

Code: [Select]
(if (= (car (C#_func)) nil)
 Do something ....
)
Title: Re: Serialize info
Post by: MP on October 31, 2007, 11:19:29 PM
Michael;

Have you seen the short sample for AutoCAD here:

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

Siht, I jsut sawllwoed my tnouge.
Title: Re: Serialize info
Post by: LE on October 31, 2007, 11:34:49 PM
Siht, I jsut sawllwoed my tnouge.

Be careful....  :-P
Title: Re: Serialize info
Post by: Kerry on October 31, 2007, 11:35:56 PM
Code: [Select]
(if (= (car (C#_func)) nil)
 Do something ....
)


Wouldn't that be something like :-
Code: [Select]
(if (= (car [color=blue](SETQ RESULT[/color] (C#_func)  [color=blue]) [/color] ) nil)
 (throw_error (cdr result))
;; else
 (proceed .... )
)
:|

Title: Re: Serialize info
Post by: It's Alive! on November 01, 2007, 04:50:33 AM
Code: [Select]
(if (= (car (C#_func)) nil)
 Do something ....
)


Wouldn't that be something like :-
Code: [Select]
(if (= (car [color=blue](SETQ RESULT[/color] (C#_func)  [color=blue]) [/color] ) nil)
 (throw_error (cdr result))
;; else
 (proceed .... )
)
:|



Yes! Yes! Perfect!  :wink:
Title: Re: Serialize info
Post by: T.Willey on November 01, 2007, 11:10:21 AM
Code: [Select]
(if (= (car (C#_func)) nil)
 Do something ....
)


Wouldn't that be something like :-
Code: [Select]
(if (= (car [color=blue](SETQ RESULT[/color] (C#_func)  [color=blue]) [/color] ) nil)
 (throw_error (cdr result))
;; else
 (proceed .... )
)
:|


or
Code: [Select]
(if (car [color=blue](SETQ RESULT[/color] (C#_func)  [color=blue]) [/color] )
 (throw_error (cdr result))
;; else
 (proceed .... )
)
Title: Re: Serialize info
Post by: Kerry on November 01, 2007, 11:16:08 AM

You're correct Tim .. I was more concerned about logic rather than semantics
.... UNLESS the first item is optionally the correct result, and nil only in the case of an error ..
Title: Re: Serialize info
Post by: T.Willey on November 01, 2007, 11:40:13 AM

You're correct Tim .. I was more concerned about logic rather than semantics
.... UNLESS the first item is optionally the correct result, and nil only in the case of an error ..
True.  I didn't look at the whole code, just the lisp example.