Well I submitted this to ADN late last week. After 2 techs looked at it, it was concluded that Civil3D (and, presumably, all the verticals that use the Aec* base) needs to have the target drawing for the blocks to be current at the time of the WblockCloneObjects(). I still have a bunch of testing to do with my full code, but initial tests show that this does solve the problem.
Thanks to everyone for their input!
[CommandMethod("InsertBlocks", CommandFlags.Session)]
static public void InsertBlocks()
{
List<string> blockNames = new List<string>();
DocumentCollection documents = Application.DocumentManager;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
foreach (Document openDocument in documents)
{
ObjectIdCollection objIdCollection = new ObjectIdCollection();
IdMapping idMap = new IdMapping();
string fileName = openDocument.Name;
if (doc.Name == fileName || fileName == null || fileName == String.Empty)
continue;
// Needed for Civil 3D since it works on the active document.
Application.DocumentManager.MdiActiveDocument = openDocument;
using (Transaction openDbTrx = openDocument.Database.TransactionManager.StartTransaction())
{
BlockTable openBlockTable = openDbTrx.GetObject(openDocument.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach (ObjectId btrId in openBlockTable)
{
BlockTableRecord btr = openDbTrx.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;
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;
blockNames.Add(btr.Name);
objIdCollection.Add(btr.ObjectId);
}
openDbTrx.Commit();
}
using (DocumentLock doclock = doc.LockDocument())
{
// Needed for Civil 3D since the destination database has to be active.
Application.DocumentManager.MdiActiveDocument = doc;
openDocument.Database.WblockCloneObjects(objIdCollection, db.BlockTableId, idMap, DuplicateRecordCloning.Ignore, false);
}
}
}