Dim sourceDb As Database = New Database(False, True)
Dim acObjIdCollsource As ObjectIdCollection = New ObjectIdCollection()
Try
Using tr As Transaction = sourceDb.TransactionManager.StartTransaction()
sourceDb.ReadDwgFile(Me.ListBox1.SelectedItem, FileOpenMode.OpenForReadAndReadShare, True, "")
sourceDb.CloseInput(True)
Dim acBlkTblSourceDoc As BlockTable
acBlkTblSourceDoc = tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, False, False)
'' Open the Block table record Model space for read
Dim acBlkTblRecSourceDoc As BlockTableRecord
acBlkTblRecSourceDoc = tr.GetObject(acBlkTblSourceDoc(BlockTableRecord.ModelSpace), OpenMode.ForRead, False, True)
For Each ObjId As ObjectId In acBlkTblRecSourceDoc
acObjIdCollsource.Add(ObjId)
Next
tr.Commit()
End Using
Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
Dim currentDb As Database = Doc.Database
Dim ed As Editor = Doc.Editor
Using acLckDocCur As DocumentLock = Doc.LockDocument()
Using tr = Doc.TransactionManager.StartTransaction()
'' Open the Block table for read
Dim acBlkTblCurrentDoc As BlockTable
acBlkTblCurrentDoc = tr.GetObject(currentDb.BlockTableId, OpenMode.ForWrite, False, False)
'' Open the Block table record Model space for read
Dim acBlkTblRecCurrentDoc As BlockTableRecord
acBlkTblRecCurrentDoc = tr.GetObject(acBlkTblCurrentDoc(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False, True)
'' Clone the objects to the new database
Dim bobject As Entity = Nothing
For Each ObjId As ObjectId In acBlkTblRecCurrentDoc
bobject = tr.GetObject(ObjId, OpenMode.ForWrite, False, True)
bobject.Erase(True)
Next
Dim acIdMap As IdMapping = New IdMapping()
sourceDb.WblockCloneObjects(acObjIdCollsource, acBlkTblRecCurrentDoc.ObjectId, acIdMap, DuplicateRecordCloning.Replace, False)
currentDb.UpdateExt(True)
'' Save the copied objects to the database
tr.Commit()
End Using
acObjIdCollsource.Dispose()
'' Unlock the document
End Using