Found the answer. I needed to get the ObjectId from the BlockTable, and then use that with the DocDbTrans (my variable) to get the BlockTableReference from the ObjectId. This will take some getting used to.
Here are my two codes. Is one better than the other? I would think that PrintXrefs2 is better because it used the "using" type of calls, and that is supposed to dispose of them when they are done, at least that is my understanding.
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass (typeof(Test.Xref))]
namespace Test
{
/// <summary>
/// Description of Xref.
/// </summary>
public class Xref
{
[CommandMethod("PrintXrefs")]
public void XrefList()
{
Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
BlockTable BlkTbl =(BlockTable) DocDbTrans.GetObject(DocDb.BlockTableId, OpenMode.ForRead);
foreach (ObjectId ObjId in BlkTbl)
{
BlockTableRecord BlkTblRec = (BlockTableRecord) DocDbTrans.GetObject(ObjId, OpenMode.ForRead);
if (BlkTblRec.IsFromExternalReference)
{
DocEd.WriteMessage("\n " + BlkTblRec.Name);
}
}
}
}
[CommandMethod("PrintXrefs2")]
public void XrefList2()
{
Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
using (BlockTable BlkTbl =(BlockTable) DocDbTrans.GetObject(DocDb.BlockTableId, OpenMode.ForRead))
{
foreach (ObjectId ObjId in BlkTbl)
{
using (BlockTableRecord BlkTblRec = (BlockTableRecord) DocDbTrans.GetObject(ObjId, OpenMode.ForRead))
{
if (BlkTblRec.IsFromExternalReference)
{
DocEd.WriteMessage("\n " + BlkTblRec.Name);
}
}
}
}
}
}
}
}