I am trying to xref a drawing a user selects into a template we have and then perform a saveas to another directory. The file shows up in the xref manager in the newly created drawing file but says it is unresolved. I have tried using db.ResolveXRefs but this does not seem to do anything. The created file is never opened so I am trying to do this by reading the database, not sure if that is possible. Here is my code if anyone could take a look and maybe I am missing something easy.
Thanks,
Ted
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim document As Document = Application.DocumentManager.MdiActiveDocument
Dim dir As String = Path.GetDirectoryName(document.Name)
Dim dirConst As String = Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Construction"
Try
For Each obj As Object In New System.Collections.ArrayList(lstConstruction.Items)
Dim fileName As String = Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Parts" & "\" & obj.ToString
Dim db As Database = New Database(False, True)
db.ReadDwgFile("Y:\CAD STANDARDS\Blocks\Templates\CONSTRUCTION 2012 EPICOR.dwt", FileShare.ReadWrite, False, "")
Using trx As Transaction = db.TransactionManager.StartTransaction
Dim bt2 As BlockTable = trx.GetObject(db.BlockTableId, OpenMode.ForWrite)
Dim btrMs2 As BlockTableRecord = trx.GetObject(bt2(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim xRid As ObjectId = db.AttachXref(fileName, Path.GetFileNameWithoutExtension(fileName))
Dim bref As New BlockReference(Point3d.Origin, xRid)
btrMs2.AppendEntity(bref)
Dim blname As String = "XRef - " & Path.GetFileNameWithoutExtension(fileName)
'Get the layer table first...
Dim lt As LayerTable = trx.GetObject(db.LayerTableId, OpenMode.ForRead)
Dim layerId As ObjectId = ObjectId.Null
'Check if EmployeeLayer exists...
If lt.Has(blname) Then
layerId = lt.Item(blname)
Else
'If not, create the layer here.
lt.UpgradeOpen()
Dim ltr As LayerTableRecord = New LayerTableRecord()
ltr.Name = blname ' Set the layer name
ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7)
layerId = lt.Add(ltr)
trx.AddNewlyCreatedDBObject(ltr, True)
End If
bref.LayerId = layerId
trx.AddNewlyCreatedDBObject(bref, True)
db.ResolveXrefs(True, True)
trx.Commit()
db.SaveAs(Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Construction\" & obj.ToString, DwgVersion.Current)
End Using
Next
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub