Revit .NET API provides two transaction modes now, Manual and Automatic.
The TransactionMode.Manual mode obviously has more flexibility, e.g. we can start and commit transactions whenever we need at wherever we like. However, regarding performance, which might be better?
In this article, let’s try to answer this question.
To make things fair, we do exactly the same thing in two external commands, one of which is marked as Automatic TransactionMode and the other Manual, and we start only one transaction at the beginning of the external command with the Manual Transaction mode and commit it before the command returns.
Here is the external command with the Automatic TransactionMode:
[Transaction(TransactionMode.Automatic)]
[Regeneration(RegenerationOption.Manual)]
public class ExtCmd1 : IExternalCommand
{
#region Cached Variables
private static ExternalCommandData _cachedCmdData;
public static UIApplication CachedUiApp
{
get
{
return _cachedCmdData.Application;
}
}
public static RvtApplication CachedApp
{
get
{
return CachedUiApp.Application;
}
}
public static RvtDocument CachedDoc
{
get
{
return CachedUiApp.ActiveUIDocument.Document;
}
}
#endregion
#region IExternalCommand Members
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
string catInfo = string.Empty;
string timeInfo = string.Empty;
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
Reference picked = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "Pick any element");
watch.Restart();
for (int i = 0; i < 10000; i++)
{
catInfo += string.Format("Element {0} name: {1}\r\n", picked.ElementId, ChangeElementName(CachedDoc, picked.ElementId));
}
watch.Stop();
timeInfo += string.Format("The Automatic Mode took {0} ms.\r\n", watch.Elapsed.TotalMilliseconds);
using (StreamWriter sw = new StreamWriter(@"c:\temp\ElementNameInfo1.txt", true))
{
sw.Write(catInfo);
}
MessageBox.Show(timeInfo);
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
#endregion
public static string ChangeElementName(Document doc, ElementId id)
{
string ret;
Element e = doc.get_Element(id);
ret = e.Name = "2";
return ret;
}
}
Here is the external command with the Manual TransactionMode:
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class ExtCmd2 : IExternalCommand
{
#region Cached Variables
private static ExternalCommandData _cachedCmdData;
public static UIApplication CachedUiApp
{
get
{
return _cachedCmdData.Application;
}
}
public static RvtApplication CachedApp
{
get
{
return CachedUiApp.Application;
}
}
public static RvtDocument CachedDoc
{
get
{
return CachedUiApp.ActiveUIDocument.Document;
}
}
#endregion
#region IExternalCommand Members
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
string catInfo = string.Empty;
string timeInfo = string.Empty;
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
Reference picked = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "Pick any element");
watch.Restart();
using (Transaction trans = new Transaction(CachedDoc, "ChangeElementName"))
{
trans.Start();
for(int i=0; i<10000; i++)
{
catInfo += string.Format("Element {0} name: {1}\r\n", picked.ElementId, ChangeElementName(CachedDoc, picked.ElementId));
}
trans.Commit();
}
watch.Stop();
timeInfo += string.Format("The Manual Mode took {0} ms.\r\n", watch.Elapsed.TotalMilliseconds);
using (StreamWriter sw = new StreamWriter(@"c:\temp\ElementNameInfo2.txt", true))
{
sw.Write(catInfo);
}
MessageBox.Show(timeInfo);
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
#endregion
public static string ChangeElementName(Document doc, ElementId id)
{
string ret;
Element e = doc.get_Element(id);
ret = e.Name = "2";
return ret;
}
}
After they are all defined in a single Revit .NET API assembly, it’s registered properly and loaded into a Revit session, the two commands may give the following results:


As can be seen, the manual mode seems a little bit faster than the automatic one, but the difference is very small, only about 2.8%. Wait a moment, if we try the first command again, we may get another result:

This time, the Automatic mode seems a bit faster than the Manual mode. If we try many times, we will find that though sometimes one is a little bit faster than the other, generally they take almost equal time. So, we can reach the conclusion pretty safely that there is no big performance difference between the automatic transaction mode and the manual transaction mode supposing we only start the transaction manually once at the beginning of the command and commit it at the end.
Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.
Recent Comments