嗨,
如果我没有误解挑战,这是我的C#2美分
- using System;
- using System.IO;
- using System.Text;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
- namespace SerializeChallenge
- {
- public class Class1
- {
- [CommandMethod("Test")]
- public void Test()
- {
- DateTime t0 = DateTime.Now;
- Document doc = acadApp.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- string filename = "C:\\SerializeChallenge.txt";
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
- using (TextWriter writer = new StreamWriter(filename))
- {
- foreach (ObjectId id in btr)
- {
- Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);
- ResultBuffer resbuf = ent.XData;
- if (resbuf == null)
- continue;
- TypedValue[] datas = resbuf.AsArray();
- StringBuilder sb = new StringBuilder(datas.Length);
- foreach (TypedValue tv in datas)
- {
- sb.Append(tv.Value);
- sb.Append(" ");
- }
- writer.WriteLine(sb.ToString());
- }
- writer.Close();
- }
- tr.Commit();
- }
- using (TextReader reader = new StreamReader(filename))
- {
- string line = reader.ReadToEnd();
- }
- DateTime t1 = DateTime.Now;
- TimeSpan ts = t1 - t0;
- ed.WriteMessage("\nEllapsed milliseconds: " + ts.TotalMilliseconds.ToString());
- }
- }
- }
|