https://adndevblog.typepad.com/autocad/2012/07/using-readdwgf ... or-objectarx-acdbattachxref.htmlUsing ReadDwgFile with .NET AttachXRef or ObjectARX acdbAttachXRef
' test code to show how to use AttachXref with Database.ReadDwgFile
<Autodesk.AutoCAD.Runtime.CommandMethod("testXref")> _
Public Sub testXref()
' save old database
Dim oldDb As Database = HostApplicationServices.WorkingDatabase
' when using ReadDwgFile, never specify True to buildDefaultDwg
' also, set noDocument=True because this drawing has no
' AutoCAD Document associated with it
Using db As New Database(False, True)
db.ReadDwgFile("c:\temp\fenton.dwt",
FileOpenMode.OpenForReadAndWriteNoShare, True, "")
' closing the input makes sure the whole dwg is read from disk
' it also closes the file so you can SaveAs the same name
db.CloseInput(True)
' now attach my xref
Dim XrefObject As ObjectId = db.AttachXref("c:\temp\test.dwg", "test")
' ok time to set the working database
HostApplicationServices.WorkingDatabase = db
Using br As New BlockReference(New Point3d(0, 0, 0), XrefObject)
br.SetDatabaseDefaults()
br.Layer = "0"
Using bt As BlockTable = db.BlockTableId.Open(OpenMode.ForRead)
Using ModelSpace As BlockTableRecord =
bt(BlockTableRecord.ModelSpace).Open(OpenMode.ForWrite)
ModelSpace.AppendEntity(br)
End Using
End Using
End Using
' reset it back ASAP
HostApplicationServices.WorkingDatabase = oldDb
db.SaveAs("c:\temp\dwgs\XrefTest.dwg", DwgVersion.Current)
End Using
End Sub