.NET

Reply
Active Contributor
JasonSelf
Posts: 35
Registered: ‎08-30-2010
Message 1 of 5 (195 Views)
Accepted Solution

Adding item to transaction?

195 Views, 4 Replies
06-17-2011 08:30 AM

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? 

 

Thank you,

Jason

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.

 

Cheers,

 

Stephen Preston

Autodesk Developer Network

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.

 

 

Board Manager
StephenPreston
Posts: 400
Registered: ‎05-22-2006
Message 2 of 5 (191 Views)

Re: Adding item to transaction?

06-17-2011 09:05 AM in reply to: JasonSelf

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.

 

Cheers,

 

Stephen Preston

Autodesk Developer Network

Cheers,

Stephen Preston
Autodesk Developer Network
Active Contributor
JasonSelf
Posts: 35
Registered: ‎08-30-2010
Message 3 of 5 (181 Views)

Re: Adding item to transaction?

06-17-2011 10:13 AM in reply to: StephenPreston

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....?

Mentor
MarkPendergraft
Posts: 375
Registered: ‎06-27-2005
Message 4 of 5 (172 Views)

Re: Adding item to transaction?

06-17-2011 12:06 PM in reply to: JasonSelf

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()

 

-Mark P.

Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 5 of 5 (161 Views)

Re: Adding item to transaction?

06-17-2011 01:47 PM in reply to: MarkPendergraft

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.

 

 

You can also find your answers @ TheSwamp
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.