I believe people have noticed that there are two kinds of TransactionManager in AutoCAD .NET API.
Maybe not! It is OK. The fact is that there are really two TransactionManager there, which are defined in two different namespaces:
Autodesk.AutoCAD.ApplicationServices.TransactionManager
Autodesk.AutoCAD.DatabaseServices.TransactionManager
and can be retrieved from two different places again, Database and Document instances.
The question is, are they created equal or not?
I see some people like to use the Database TransactionManager as I do, some others prefer to the Document TransactionManager and others use both of them interchangeably by intension or not. The good thing is that none of such seems to cause serious problems or inconsistencies. So can we say they are equal to each other?
Maybe! But I don’t take it for granted. Let us find it out with a small tiny experiment then. It does not sound as scary.
The following code and command should speak it clearly enough if not all.
[CommandMethod("DbTmNDocTm")]
public static void DbTmNDocTm_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
try
{
Autodesk.AutoCAD.ApplicationServices.TransactionManager docTM = doc.TransactionManager;
Autodesk.AutoCAD.DatabaseServices.TransactionManager dbTM = db.TransactionManager;
ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
if (id.IsValid)
{
DBObjectCollection objs = null;
using (Transaction dbTr = dbTM.StartTransaction())
{
Entity ent = dbTr.GetObject(id, OpenMode.ForWrite) as Entity;
ent.ColorIndex = 1;
objs = docTM.GetAllObjects();
if (docTM.NumberOfActiveTransactions == 1 && dbTM.NumberOfActiveTransactions == 1 &&
objs != null && objs.Count == 1 && objs[0].Id == ent.Id)
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are likely the same thing.");
}
else
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are different animals.");
}
dbTr.Commit();
}
if (docTM.NumberOfActiveTransactions == 0)
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are more likely the same thing.");
}
else
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are different animals.");
}
using (Transaction docTr = dbTM.StartTransaction())
{
Entity ent = docTr.GetObject(id, OpenMode.ForWrite) as Entity;
ent.ColorIndex = 1;
objs = dbTM.GetAllObjects();
if (docTM.NumberOfActiveTransactions == 1 && dbTM.NumberOfActiveTransactions == 1 &&
objs != null && objs.Count == 1 && objs[0].Id == ent.Id)
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are most likely the same thing.");
}
else
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are different animals.");
}
docTr.Commit();
}
if (dbTM.NumberOfActiveTransactions == 0)
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are surely the same thing.");
}
else
{
ed.WriteMessage("\nDatabase.TransactionManager and Document.TransactionManager are different animals.");
}
}
else
{
ed.WriteMessage("\nThere is no entity at all in the drawing.");
}
}
catch (System.Exception ex)
{
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}
Here is the output:
Command: DbTmNDocTm
Database.TransactionManager and Document.TransactionManager are likely the same thing.
Database.TransactionManager and Document.TransactionManager are more likely the same thing.
Database.TransactionManager and Document.TransactionManager are most likely the same thing.
Database.TransactionManager and Document.TransactionManager are surely the same thing.
Based on the above test and output, we can safely reach the conclusion that they are created equal, can’t we?
It is a great thing!
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Posted by: |