I am walking through someone elses code in order to make sure I unerstand some principles (inserting blocks via a jig) and I cam upon a couple of lines that I do not understand:
space.AppendEntity(br) tr.AddnewlyCreatedDBObject(br, True)
space is the blocktaclerecord for the current space and tr is the active transaction.
Here is what I don't get: If I append the entity to the space in the blocktablerecord inside of the transaction why do I also need to add this newly created object to the transaction with the AddnewlyCreatedDBObject method as well?
Solved! Go to Solution.
The short answer is - because that's how it works.
A slightly longer answer is - The BlockTableRecord doesn't know anything about the active transaction, so you have to separately add the object to the BTR and to the transaction. Remember that there are two ways to edit DWG resident entities - through the Open/Close model or through the Transaction Model (the latter is the recommended approach). API calls other than the open/close or transaction -related calls are the same whichever model you're using.
Autodesk Developer Network
So, if I understand correctly: The block is added as to the block table a result of a process inside the transaction, but the transaction itself doesn't know about that block yet so I need to tell the transaction about the block so that I can work with said block in the transaction....?
Yes, you need to tell the transaction that you have made changes to the blocktablerecord, and furthermore you need to commit the changes for them to actually take place in the drawing with tr.Commit()
Stephen is way smarter and knows way more than I do so he would know better.
I might be wrong since I have never tried using it until now but in the old days I think they used a open/close method.
So I can insert a blockreference like this (I think you use VB since I did not see ';' at end)
assuming a block named "C" is in the drawing
<CommandMethod("InsertBlockReference")> _ Sub InsertBlockReference() Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database Dim ed As Editor = doc.Editor Dim bt As BlockTable = db.BlockTableId.Open(OpenMode.ForRead) Dim space As BlockTableRecord = db.CurrentSpaceId.Open(OpenMode.ForWrite) Dim blk As BlockTableRecord = bt("C").Open(OpenMode.ForRead) Dim bref As New BlockReference(Point3d.Origin, blk.ObjectId) space.AppendEntity(bref) blk.Close() bref.Close() space.Close() bt.Close() End Sub
Now if you comment out
'bref.Close()' nothing will happen.
Even worst if you comment out 'space.Close()' nothing will happen but if you try to draw a line or add anything to the current space it will crash.
So transactions make it easier and keep from having to open and close objects and doing it many times etc.....
If the blockreference was already in the drawing when you use Transaction.GetObject() or ObjectID.GetObjectId()(Will use most recent transaction) now the transaction knows about it and will properly close it. So you do not have to add it to the transaction. So I would guess that GetObject() must also add it to the transaction.
When you create a new BlockReference the transaction does not know about it and that is the reason for the AddNewlyCreatedDBObject() call.
Each object needs to be owned so AppendEntity() is telling it which space will own it ModelSpace or one if the paperspaces etc....
Hopefully that is a decent enough explanation and not too far off.
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register
Start with some of our most frequented solutions to get help installing your software.