Let us look at something very common but not so clear actually about AutoCAD .NET coding in this post. Every AutoCAD .NET programmer has to do some object or entity type checking now and then, and there exist many different ways.
We have demonstrated, done a small experiment about, and analyzed a few previously. Let us recap a bit here.
• Using the keyword ‘is’ to check what type the object of concern is.
• Retrieving the Type from the object of concern through calling the GetType() method against it and doing a comparison to the target type which can be got using the .NET typeof keyword.
• Comparing the ObjectClass instance of the object of concern with the target one.
• Comparing the DxfName property of the object of concern with an arbitrary string.
The results yielded that the fourth DxfName way was way faster than the other three, the third one was not good, the second had better performance that the first one which it seems many people favor of, take it for granted and like to recommend to others.
Interesting enough, a smart reader suggested another way, using the ‘soft’ cast, the ‘as’ keyword. Though the casting is not the major concern of the previous article and may be worth of another post to discuss in detail in the future, you can definitely use it to do the object type-checking if really want to.
Here you go.
public static ObjectIdCollection TypeCheckApproach5()
{
ObjectIdCollection ids = new ObjectIdCollection();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in btr)
{
Circle obj = tr.GetObject(id, OpenMode.ForRead) as Circle;
if (obj != null)
{
ids.Add(obj.Id);
}
}
tr.Commit();
}
return ids;
}
But the question is: is it really more performant (at least than the first two popular ones we already introduced)?
Not likely! Here is the test command having the new test code appended:
[CommandMethod("TestVariousTypeCheck")]
public static void TestVariousTypeCheck_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
DateTime begin = DateTime.Now;
int count = TypeCheckApproach1().Count;
TimeSpan elapsed = DateTime.Now.Subtract(begin);
ed.WriteMessage("Time elapsed in TypeCheckApproach1 for {0} circles: {1}\n", count, elapsed.TotalMilliseconds);
…
…
begin = DateTime.Now;
count = TypeCheckApproach5().Count;
elapsed = DateTime.Now.Subtract(begin);
ed.WriteMessage("Time elapsed in TypeCheckApproach5 for {0} circles: {1}\n", count, elapsed.TotalMilliseconds);
}
Here is the concrete data again:
Command: TESTVARIOUSTYPECHECK
Time elapsed in TypeCheckApproach1 for 5000 circles: 124.8002
Time elapsed in TypeCheckApproach2 for 5000 circles: 109.2002
Time elapsed in TypeCheckApproach3 for 5000 circles: 468.0009
Time elapsed in TypeCheckApproach4 for 5000 circles: 15.6
Time elapsed in TypeCheckApproach5 for 5000 circles: 171.6003
As can be seen, though the absolute milliseconds were changed, the trend was still the same. The DxfName approach was the quickest, the GetType one was the second, the ‘is’ one was next to the last. The new ‘as’ one as suggested was not better than the two popular ones and needless to say was way slower than the DxfName approach.
If any ideas or tips please feel free to post comments again. This is an open place and everybody can present and share her opinions.
Enjoy it! More coding gadgets will be created and demonstrated in the future. Please stay tuned.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Recent Comments