OK, here is version 2 which works. Now to try and figure out how to do it without opening the dwg in the editor
[CommandMethod("BindTep2")]
static public void BindTitleBlocks2()
{
Document Doc = AcadApp.DocumentManager.MdiActiveDocument;
Database Db = Doc.Database;
Editor Ed = Doc.Editor;
XrefGraph XG = Db.GetHostDwgXrefGraph(true);
if (XG.IsEmpty || XG.NumNodes == 1)
{
return;
}
using (Transaction Trans = Doc.TransactionManager.StartTransaction())
{
BlockTable BT = Trans.GetObject(Db.BlockTableId, OpenMode.ForWrite) as BlockTable;
for (int i = 0; i < XG.NumNodes; i++)
{
XrefGraphNode XGN = XG.GetXrefNode(i) as XrefGraphNode;
if (string.Compare(XGN.Name, "TEP", true).Equals(0) ||
string.Compare(XGN.Name, "UES", true).Equals(0) ||
string.Compare(XGN.Name, "VTEP", true).Equals(0) ||
string.Compare(XGN.Name, "VUES", true).Equals(0))
{
BlockTableRecord BTR = Trans.GetObject(XGN.BlockTableRecordId, OpenMode.ForWrite, false) as BlockTableRecord;
ObjectIdCollection BlkRefCol = BTR.GetBlockReferenceIds(true, true);
if (BlkRefCol.Count > 0)
{
ObjectIdCollection OIC = new ObjectIdCollection();
OIC.Add(BTR.Id);
Db.BindXrefs(OIC, true);
}
else
{
Db.DetachXref(BTR.Id);
}
}
}
Trans.Commit();
}
Db.SaveAs(Doc.Name , DwgVersion.Current);
}