Thanks to code I found here I managed to add an Xref to my drawing, now I need to move that newly added Xref to a new position and I'm having a problem with that.
A portion of the full code follows
For Each fileName As String In fileNames
Dim options As PromptPointOptions = New PromptPointOptions(("Pick insertion point for " _
+ (fileName + ": ")))
options.AllowNone = False
Dim pt As PromptPointResult = ed.GetPoint(options)
If (pt.Status <> PromptStatus.OK) Then
'TODO: Warning!!! continue If
End If
Dim xrefScale As Double = getDwgScale(fileName)
Dim scaleFactor As Double = (dimScale / xrefScale)
Dim tr As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction
Dim xrefId As ObjectId = db.AttachXref(fileName, Path.GetFileNameWithoutExtension(fileName))
Dim blockRef As BlockReference = New BlockReference(pt.Value, xrefId)
Dim blockTblRec As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
blockRef.ScaleFactors = New Scale3d(scaleFactor, scaleFactor, scaleFactor)
blockRef.Layer = "0"
blockTblRec.AppendEntity(blockRef)
tr.AddNewlyCreatedDBObject(blockRef, True)
tr.Commit()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction(again?)
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for read
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)
Dim bref As BlockReference
'' Step through the Block table record
For Each acObjId As ObjectId In acBlkTblRec
bref = acTrans.GetObject(acObjId, OpenMode.ForWrite, False, True) '<========================== ERROR HERE
bref.Position = bref.Position.TransformBy(Matrix3d.Displacement(New Vector3d(100, 100, 0)))
acTrans.Commit()
Next
'' Dispose of the transaction
acTrans.Dispose()
End Using
Next
On the first run in a blank drawing it runs fine, Inserts and moves Xref, on subsequent runs I get an error "Operation is not valid due to the current state of the object" at the line indicated, yet I can continue past it and it still inserts and moves an Xref.
It feels like I'm trying to get and move the last Xref inserted the "hard way".
What is the "easy way" that I should be moving the xref?