Author Topic: Serialize info  (Read 4170 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Serialize info
« 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() { } }
}


LE

  • Guest
Re: Serialize info
« Reply #1 on: October 31, 2007, 06:41:34 PM »
Daniel;

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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Serialize info
« Reply #2 on: October 31, 2007, 07:36:08 PM »
I've been keeping my eye on that one Luis, very interesting.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Glenn R

  • Guest
Re: Serialize info
« Reply #3 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.

LE

  • Guest
Re: Serialize info
« Reply #4 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/

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: Serialize info
« Reply #5 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 ....
)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Serialize info
« Reply #6 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

LE

  • Guest
Re: Serialize info
« Reply #7 on: October 31, 2007, 11:34:49 PM »
Siht, I jsut sawllwoed my tnouge.

Be careful....  :-P

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Serialize info
« Reply #8 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 .... )
)
:|

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: Serialize info
« Reply #9 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:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Serialize info
« Reply #10 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 .... )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Serialize info
« Reply #11 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 ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Serialize info
« Reply #12 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.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.