[CommandMethod("Test_04")]
public static void test04Command()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
DateTime TimeStart = DateTime.Now;
string outputFileName = @"C:\Temp\EvalReport_04.txt";
int attributeCount = 0;
Transaction tr = null;
StreamWriter logFileWriter = null;
try
{
logFileWriter
= new StreamWriter
(outputFileName,
true,
System.Text.Encoding.ASCII); logFileWriter.AutoFlush = true;
logFileWriter.WriteLine("Starting eval...");
tr = db.TransactionManager.StartTransaction();
ObjectId btId = db.BlockTableId;
if (btId.IsNull || !btId.IsValid)
{
ed.WriteMessage("{0}Serious error: Failed to get BlockTable ObjectId!.", Environment.NewLine);
return;
}
BlockTable bt = btId.Open(OpenMode.ForRead, false) as BlockTable;
if (bt == null)
{
ed.WriteMessage("{0}Catastrophic error: Failed to open the BlockTable!", Environment.NewLine);
return;
}
// roll over the block table
foreach (ObjectId btrId in bt)
{
//BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead, false) as BlockTableRecord;
BlockTableRecord btr = btrId.Open(OpenMode.ForRead, false) as BlockTableRecord;
if (btr == null)
{
ed.WriteMessage("{0}Serious error: Failed to open a BlockTableRecord! Continuing...", Environment.NewLine);
continue;
}
if (!btr.HasAttributeDefinitions)
{
btr.Close();
continue;
}
ObjectIdCollection blkRefIds = btr.GetBlockReferenceIds(false, false);
if (blkRefIds == null || blkRefIds.Count == 0)
continue;
foreach (ObjectId blkRefId in blkRefIds)
{
//BlockReference blkRef = tr.GetObject(blkRefId, OpenMode.ForRead, false) as BlockReference;
BlockReference blkRef = blkRefId.Open(OpenMode.ForRead, false) as BlockReference;
if (blkRef == null)
{
ed.WriteMessage("{0}Serious error: Failed to open a BlockReference! Continuing...", Environment.NewLine);
continue;
}
if (blkRef.Layer != "BLOCKS")
{
blkRef.Close();
continue;
}
AttributeCollection attRefIds = blkRef.AttributeCollection;
if (attRefIds == null || attRefIds.Count == 0)
{
blkRef.Close();
continue;
}
foreach (ObjectId attRefId in attRefIds)
{
//AttributeReference attRef = tr.GetObject(attRefId, OpenMode.ForRead, false) as AttributeReference;
AttributeReference attRef = attRefId.Open(OpenMode.ForRead, false) as AttributeReference;
if (attRef == null)
{
ed.WriteMessage("{0}Serious error: Failed to open an AttributeReference! Continuing...", Environment.NewLine);
continue;
}
attributeCount++;
logFileWriter.WriteLine(attributeCount.ToString() + '\t' + attRef.Tag + '\t' + attRef.TextString);
attRef.Close();
}
blkRef.Close();
}
}
bt.Close();
tr.Commit();
TimeSpan TimeDuration = (DateTime.Now - TimeStart); ;
ed.WriteMessage("End eval...Process time: "
+ (TimeDuration.TotalSeconds + " Seconds for "
+ attributeCount.ToString() + " records of "
+ db.ApproxNumObjects.ToString()));
logFileWriter.WriteLine(("End eval...Process time: " + (TimeDuration.TotalSeconds + " Seconds.")));
}
catch (Autodesk.AutoCAD.Runtime.Exception arex)
{
ed.WriteMessage("{0}AutoCAD Runtime Exception: {1}", Environment.NewLine, arex.Message);
return;
}
catch (System.Exception ex)
{
ed.WriteMessage("{0}System Runtime Exception: {1}", Environment.NewLine, ex.ToString());
return;
}
finally
{
if (tr != null)
tr.Dispose();
if (logFileWriter != null)
logFileWriter.Close();
}
}