I have not had time to really work on it, but here is something too see how it does.(If it is too slow, just plain crap etc.....)
This is just a basic method to copy all blocks from every open drawing into the current drawing.
If one drawing is open or none of the other open drawings pass the logical conditions then nothing will happen
I went a little different route.
Basically here is the run down,
To keep each document from becoming active I just opened the databases and read it from there.
I iterated the BlockTable and made sure it was not a Layout, from x-ref, anonymous, some things to weed out MEP objects, and has been at least inserted once etc.....
Maybe is not any better or probably worst than what you have just throwing it out there.
[CommandMethod("InsertBlocks", CommandFlags.Session)]
public void InsertBlocks()
{
List<string> blockNames = new List<string>(); // A generic list to keep up with all blocks to check later if a block has already inserted.
DocumentCollection documents = Application.DocumentManager; // All the current open documents
Document doc = Application.DocumentManager.MdiActiveDocument; // Active Document
Editor ed = doc.Editor; // Active Document's Editor
Database db = doc.Database; // Active Document Database
Document openDocument; // Temporay variable to hold each non-active Document
Database openDatabase; //Temporay variable to hold each non-active Document's Database
using (DocumentLock doclock = doc.LockDocument()) // Start Lock on Active Document
using (Transaction trx = db.TransactionManager.StartTransaction()) // Start Transaction with Active Document's Database
{
// Start looping through each document
foreach (Document document in documents)
{
openDocument = document;
string fileName = openDocument.Name;
// If the document is the current or Active document skip it
if (doc.Name == fileName || fileName == null || fileName == String.Empty)
continue;
openDatabase = new Database(false, true);
// 'Open' Open documents database
openDatabase.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, "");
using (Transaction openDbTrx = openDatabase.TransactionManager.StartTransaction())
{//Start Transaction with Open Document's Database
BlockTable openBlockTable = openDbTrx.GetObject(openDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
// Loop through each BlockTableRecord
foreach (ObjectId btrId in openBlockTable)
{
BlockTableRecord btr = openDbTrx.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;
// Make sure it is not a ModelSpace or any of the PaperSpaces, any type of Xref
// or MEP type Object, or has already been clone before etc.... & and the block has been inserted at least once
if (btr.IsLayout || btr.IsAnonymous || btr.IsFromExternalReference || btr.IsFromOverlayReference || blockNames.Contains(btr.Name)
|| btr.Name.StartsWith("Aec", StringComparison.OrdinalIgnoreCase) || btr.Name.StartsWith("_")
|| btr.GetBlockReferenceIds(true, false).Count < 1)
continue;
//Add Block to list of blocks
blockNames.Add(btr.Name);
//Do Cloning
ObjectIdCollection objIdCollection = new ObjectIdCollection();
objIdCollection.Add(btr.ObjectId);
IdMapping idMap = new IdMapping();
openDatabase.WblockCloneObjects(objIdCollection, db.BlockTableId, idMap, DuplicateRecordCloning.Ignore, false);
}
openDbTrx.Commit();
}//End Transaction with Open Document's Database
openDatabase.Dispose(); // Dispose Database
}// End foreach
trx.Commit();
}// End Lock on Active Document & Transaction with Active Document's Database
}