.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Difference between: StartOpenCloseTransaction vs StartTransaction?

1 REPLY 1
SOLVED
Reply
Message 1 of 2
BKSpurgeon
3214 Views, 1 Reply

Difference between: StartOpenCloseTransaction vs StartTransaction?

Estimable colleagues

 

This is the documentation on the relevant point.

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUI...

 

 

  • StartTransaction – Starts a new transaction by creating a new instance of a Transaction object. Use this method when you need to write to an object multiple times and control how the changes can be rolled back through the use of different nesting levels.
  • StartOpenCloseTransation – Creates an OpenCloseTransaction object, which behaves similar to a Transaction object, that wraps the Open and Close methods of an object making it easier to close all opened objects instead of having to explicitly close each opened object. (what is meant by this - we do not have to explicitly close objects when we use a transaction do we!?) Recommended for use in support or utility functions that might be called an unknown number of times, and used when working with most event handlers.

 

I still do not understand how the OpenCloseTransaction object is different from using StartTransaction. Any expalantions/guidance would be much appreciated.

 

 

 

1 REPLY 1
Message 2 of 2


@BKSpurgeon wrote:

Estimable colleagues

 

This is the documentation on the relevant point.

 

 

 

  • StartTransaction – Starts a new transaction by creating a new instance of a Transaction object. Use this method when you need to write to an object multiple times and control how the changes can be rolled back through the use of different nesting levels.
  • StartOpenCloseTransation – Creates an OpenCloseTransaction object, which behaves similar to a Transaction object, that wraps the Open and Close methods of an object making it easier to close all opened objects instead of having to explicitly close each opened object. (what is meant by this - we do not have to explicitly close objects when we use a transaction do we!?) Recommended for use in support or utility functions that might be called an unknown number of times, and used when working with most event handlers.

 

I still do not understand how the OpenCloseTransaction object is different from using StartTransaction. Any expalantions/guidance would be much appreciated.

 

 

 


public static class Commands
{
   [CommandMethod("WHICHTRANS")]
   public static void WhichTrans()
   {
      var pko = new PromptKeywordOptions("\nUse");
      pko.Keywords.Add("Transaction");
      pko.Keywords.Add("Open", "Open", "OpenCloseTransaction");
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Editor ed = doc.Editor;
      PromptResult pr = ed.GetKeywords(pko);
      if(pr.Status != PromptStatus.OK)
         return;
      bool openclose = pr.StringResult == "Open";
      var tm = doc.TransactionManager;
      using(var trans = openclose ? new OpenCloseTransaction() : tm.StartTransaction())
      {
         ed.WriteMessage(
            "Before responding to the next prompt, use the middle mouse button\n" +
            "to pan/zoom and the status bar (or 'SETVAR) to change settings,");

         var res = ed.GetPoint("\nPick a point: ");

         ed.WriteMessage("\nAborting transaction...");
      }
   }
}

Add the above code to a new ClassLibrary project, build and NETLOAD it.

 

Use the command once with each of the two types of transactions, but before answering the "Pick a point: " prompt, use the middle mouse button to transparently pan/zoom (there should be some objects in the drawing so you can see the effects). Also before answering the prompt, use either 'SETVAR or the status bar to change some settings.

 

The code does not commit the transaction, but rather allows it to abort. Notice that, after using a regular transaction, any changes you made to settings, and your transparent pan or zoom was undone by aborting the transaction. When you try the same thing using an OpenCloseTransaction, any changes made to settings or the current view persist after the transaction is aborted.

 

IOW, a regular transaction returned by StartTransaction() is also a fancy wrapper around the UNDO/Begin and UNDO/End subcommands. When you start one, it is as if you had issued the UNDO Begin subcommand to start an undo group. When you commit one, it is as if you had issued the UNDO End subcommand to end the undo group. When you abort one, it is as if you had issued UNDO/End followed by U/UNDO 1 to both end and undo the undo group. Aborting a regular transaction will not only roll back changes to the database, but also any other changes made to the state of the drawing editor while the transaction was active, as well.

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report