This worked for me. I tried other ways, but couldn't get them to work correctly, as they would fatally error, and crash acad with and ' ePermanetlyErased ' error.
public void Main ()
{
Document toDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument as Document;
using( DocumentLock toLock = toDoc.LockDocument() ) {
Database toDb = toDoc.Database as Database;
string Path = @"c:/test/blank.dwg";
string LoName = "test";
Document fromDoc;
Database fromDb = MyUtility.GetDatabaseAtPath( Path, toDb, FileOpenMode.OpenForReadAndAllShare, FindFileHint.Default, out fromDoc );
using( Transaction toTrans = toDb.TransactionManager.StartTransaction() ) {
using( Transaction fromTrans = fromDb.TransactionManager.StartTransaction() ) {
DBDictionary fromLoDict = fromTrans.GetObject( fromDb.LayoutDictionaryId, OpenMode.ForRead ) as DBDictionary;
ObjectIdCollection IdCol = new ObjectIdCollection();
foreach ( DBDictionaryEntry de in fromLoDict ) {
if ( de.Key != LoName ) continue;
IdCol.Add( de.Value );
}
fromDb.WblockCloneObjects( IdCol, toDb.LayoutDictionaryId, new IdMapping(), DuplicateRecordCloning.Ignore, false );
}
toTrans.Commit();
}
fromDb.Dispose();
}
}
public static Database GetDatabaseAtPath ( string path, Database db, Autodesk.AutoCAD.DatabaseServices.FileOpenMode openMode, FindFileHint findOpt, out Document doc )
{
// If doc != null, then the calling function must lock the document passed out.
// Also, the calling function must NOT set the HostApplication.WorkingDatabase to the
// database returned.
//
// If doc == null, then the calling function must dispose of the database returned.
// Also, the calling function must set the HostApplication.WorkingDatabase to the
// database returned, if working with text or attributes.
//
// If null is returned, then the drawing is read-only to the calling program, and the
// calling program wants to make changes to the database.
doc = GetDocumentFrom( AcadApp.DocumentManager, path );
if ( doc != null ) return doc.Database;
if (
openMode == FileOpenMode.OpenForReadAndReadShare
&&
( ( File.GetAttributes ( path ) & FileAttributes.ReadOnly ) == FileAttributes.ReadOnly )
)
return null;
string FoundAtPath = HostApplicationServices.Current.FindFile( path, db, findOpt );
if ( string.IsNullOrEmpty( FoundAtPath ) ) return null;
Database Db = new Database( false, true );
Db.ReadDwgFile( FoundAtPath, openMode, true, null );
return Db;
}