Is there any way to insert a block when there is an active transaction? I've seen examples of inserting blocks similar to http://forums.autodesk.com/t5/NET/How-to-insert-blockreference-not-in-drawing/m-p/1370564 - but if I use an existing open transaction (rather than commiting and starting a new transaction), I receive an eWasOpenForWrite error when calling doc.Database.Insert. Ultimately, I would like to be able to pass an active transaction as a parameter to the function.
I don't think there's any way to do that without ending the transaction.
You can pass a transaction like this:
Imports acaddbs = Autodesk.AutoCAD.DatabaseServices
Public Class Commands
Public Shared Sub quickScan()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As acaddbs.Database = doc.Database
Using mytx As acaddbs.Transaction = db.TransactionManager.StartTransaction
'Open the Block table for read
Dim acBlkTbl As acaddbs.BlockTable
acBlkTbl = mytx.GetObject(db.BlockTableId, acaddbs.OpenMode.ForRead)
'Open the Block table record Modelspace for read
Dim acBlkTblRec As acaddbs.BlockTableRecord
acBlkTblRec = mytx.GetObject(acBlkTbl(acaddbs.BlockTableRecord.ModelSpace), _
acaddbs.OpenMode.ForRead)
For Each objid As acaddbs.ObjectId In acBlkTblRec
Using ent As acaddbs.Entity = DirectCast(mytx.GetObject(objid, acaddbs.OpenMode.ForRead), acaddbs.Entity)
dim myentdata as string = entData(ent, mytx)
End Using
Next
End Using
End Sub
Public Shared Function entData(ByRef myent As acaddbs.Entity, ByRef mytr As acaddbs.Transaction) as string
'return data about entity
End Sub 'entData
End Class
I have not tried making any database changes with this - it might not work.
I understand the concept of passing a transaction as a parameter - in fact, I do it quite often in situations where opening and closing transactions would end up being slow. The problem lies in adding a block to the drawing with an active transaction. I've tried quite a few things - including trying to clone objects from the source database into a new BlockTableRecord in the destination database - but so far everything I've tried raises some error (eWasOpenForWrite for example).
That doesn't sound safe anyway.
Who knows what's going on in that transaction!
No one can tell you much without seeing some code.
For example, what objects are open for write when you call Insert()?
The BlockTable? Something else?