0 Members and 1 Guest are viewing this topic.
//Written by Daniel Marcotte 10.31.2007//Requires VS 2008 to compile for .NET 2.0using 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 successnamespace 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() { } }}
I've been keeping my eye on that one Luis, very interesting.
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...
(if (= (car (C#_func)) nil) Do something ....)
Michael;Have you seen the short sample for AutoCAD here:http://through-the-interface.typepad.com
Siht, I jsut sawllwoed my tnouge.
Code: [Select](if (= (car (C#_func)) nil) Do something ....)
(if (= (car [color=blue](SETQ RESULT[/color] (C#_func) [color=blue]) [/color] ) nil) (throw_error (cdr result));; else (proceed .... ))
Quote from: Daniel on October 31, 2007, 11:06:23 PMCode: [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 .... ))
(if (car [color=blue](SETQ RESULT[/color] (C#_func) [color=blue]) [/color] ) (throw_error (cdr result));; else (proceed .... ))
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 ..