Times on a machine I'm using at the moment:
Lisp = 44.594 secs.
C# Original = 27.140625 secs
C# New = 22.203125 secs
New code:
// (C) CodeHimBelonga kdub@home.
// 20090823.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(Kdub.Testing.DBX_Block.MyCommands))]
namespace Kdub.Testing.DBX_Block
{
public class MyCommands
{
[CommandMethod("Doit", CommandFlags.Modal | CommandFlags.Session)]
public void MyCommand20090823()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ed.WriteMessage("{0} This will take a couple of minutes... {0}", Environment.NewLine);
DateTime TimeStart = DateTime.Now;
StringBuilder sb = new StringBuilder();
string drawingFileName = @"C:\Temp\Test20090823.dwg";
string blockName = "BBL_V8";
string outputFileName = @"C:\Temp\Test20090823.txt";
StreamWriter logFileWriter = null;
int fileCounter = 0;
int TotalFileCount = 500;
//show the progress of the function
ProgressMeter pm = new ProgressMeter();
pm.Start("Extracting Attributes");
pm.SetLimit(TotalFileCount);
try
{
while (fileCounter < TotalFileCount)
{
try
{
//did user press ESCAPE?
if (HostApplicationServices.Current.UserBreak())
{
throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.UserBreak, "ESCAPE pressed");
}
//update progress bar
AcadApp.UpdateScreen();
pm.MeterProgress();
fileCounter++;
// use a NEW file ( overwrite existing file)
logFileWriter = new StreamWriter(outputFileName, false, System.Text.Encoding.ASCII);
logFileWriter.AutoFlush = true;
// Create a new database with no document window
using (Database acDbxDb = new Database(false, true))
{
try
{
acDbxDb.ReadDwgFile(drawingFileName, System.IO.FileShare.None, false, "");
using (Transaction tr = acDbxDb.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(acDbxDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(blockName)) return;
ObjectId btrId = bt[blockName];
BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead, false) as BlockTableRecord;
ObjectIdCollection blkRefIds = btr.GetBlockReferenceIds(false, false);
if (blkRefIds == null || blkRefIds.Count == 0) return;
foreach (ObjectId blkRefId in blkRefIds)
{
BlockReference blkRef = tr.GetObject(blkRefId, OpenMode.ForRead, false) as BlockReference;
AttributeCollection attRefIds = blkRef.AttributeCollection;
if (attRefIds == null || attRefIds.Count == 0)
{
ed.WriteMessage("{0}F.U.B.B. : Failed getting AttributeCollection! Continuing...", Environment.NewLine);
continue;
}
//instanceCount++;
foreach (ObjectId attRefId in attRefIds)
{
AttributeReference attRef = tr.GetObject(attRefId, OpenMode.ForRead, false) as AttributeReference;
if (attRef == null)
{
ed.WriteMessage("{0}F.U.B.B. : Failed to open an AttributeReference! Continuing...", Environment.NewLine);
continue;
}
sb.AppendFormat("{0}{1} . {2}{3}", "(", attRef.Tag, attRef.TextString, ")");
}
logFileWriter.Write("(");
sb.ToString();
sb.Remove(0, sb.Length);
logFileWriter.Write(")" + Environment.NewLine);
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception Ex)
{
Application.ShowAlertDialog("The following exception was caught:\n" +
Ex.Message);
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception acEx)
{
ed.WriteMessage("{0}AutoCAD Runtime Exception: {1}", Environment.NewLine, acEx.Message);
return;
}
catch (System.Exception ex)
{
ed.WriteMessage("{0}System Runtime Exception: {1}", Environment.NewLine, ex.ToString());
return;
}
finally
{
if (logFileWriter != null)
logFileWriter.Close();
}
}
TimeSpan TimeDuration = (DateTime.Now - TimeStart); ;
ed.WriteMessage("\nEnd eval...Process time: "
+ (TimeDuration.TotalSeconds + " Seconds for "
+ TotalFileCount.ToString() + " files.\n")
);
}
catch (Autodesk.AutoCAD.Runtime.Exception acEx)
{
ed.WriteMessage("{0}AutoCAD Runtime Exception: {1}", Environment.NewLine, acEx.Message);
return;
}
catch (System.Exception ex)
{
ed.WriteMessage("{0}System Runtime Exception: {1}", Environment.NewLine, ex.ToString());
return;
}
finally
{
if (pm != null)
pm.Stop();
if (logFileWriter != null)
logFileWriter.Close();
}
}
}
}
Interesting huh?