public bool copyBlock ( Database aCallingDb, string aFromPath, string aFromName, string aToPath, string aToName ) {
Document fromDoc;
DocumentLock fromLock = null;
ObjectId fromId;
Document toDoc;
DocumentLock toLock = null;
ObjectId toId;
string tName = "a";
string oldName = "b";
ObjectId tId;
BlockTableRecord btr;
Database fromDb = GetDatabaseAtPath( aFromPath, aCallingDb, FileShare.ReadWrite, FindFileHint.XRefDrawing, out fromDoc );
if ( fromDoc != null && fromDoc.LockMode() == DocumentLockMode.NotLocked ) fromLock = fromDoc.LockDocument();
fromId = GetNonErasedTableRecordId( fromDb.BlockTableId, aFromName );
if ( fromId.IsNull ) return false;
Database toDb = GetDatabaseAtPath( aToPath, aCallingDb, FileShare.ReadWrite, FindFileHint.XRefDrawing, out toDoc );
if ( toDoc != null && toDoc.LockMode() == DocumentLockMode.NotLocked ) toLock = toDoc.LockDocument();
toId = GetNonErasedTableRecordId( toDb.BlockTableId, aToName );
if ( toId.IsNull ) return false;
using ( Database tDb = new Database( true, true ) ) {
tDb.Insert( "randomAssTempNameYes3344882", fromDb, true );
if ( fromDoc == null ) fromDb.Dispose();
else if ( fromLock != null ) fromLock.Dispose();
eraseAllRecordsExcept( tDb.BlockTableId, aFromName );
while ( !( tId = GetNonErasedTableRecordId( toDb.BlockTableId, tName ) ).IsNull ) tName += tName;
while ( !( tId = GetNonErasedTableRecordId( toDb.BlockTableId, oldName ) ).IsNull ) oldName += oldName;
using ( Transaction toTrans = toDb.TransactionManager.StartTransaction() ) {
btr = toTrans.GetObject( toId, OpenMode.ForWrite, false, true ) as BlockTableRecord;
btr.Name = oldName;
btr.DowngradeOpen();
tId = toDb.Insert( tName, tDb, true );
if ( tId.IsNull ) return false;
btr = toTrans.GetObject( tId, OpenMode.ForWrite, false, true ) as BlockTableRecord;
btr.Erase();
btr.DowngradeOpen();
fromId = GetNonErasedTableRecordId( toDb.BlockTableId, aFromName );
if ( fromId.IsNull ) return false;
btr = toTrans.GetObject( fromId, OpenMode.ForWrite, false, true ) as BlockTableRecord;
btr.SwapIdWith( toId, false, false );
if ( string.Equals( btr.Name, aToName, StringComparison.OrdinalIgnoreCase ) ) {
btr.Name = aToName;
updateXData( btr.XData, 1001, "AcDbBynamicBlockTrueName", new TypedValue( 1000, aFromName ), new TypedValue( 1000, aToName ) );
}
btr.DowngradeOpen();
btr = toTrans.GetObject( fromId, OpenMode.ForWrite, false, true ) as BlockTableRecord;
btr.Erase();
btr.DowngradeOpen();
toTrans.Commit();
if ( toDoc == null ) toDb.SaveAs( aToPath, false, DwgVersion.Current, toDb.SecurityParameters );
}
}
if ( toDoc == null ) toDb.Dispose();
else if ( toLock != null ) toLock.Dispose();
return true;
}