by Fenton Webb
I’m sure you have all seen code like this before, where you open an entity in the DWG database to find out its type…
Database db =
Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// open the entities
Entity ent1 = (Entity)trans.GetObject(id1, OpenMode.ForRead);
Entity ent2 = (Entity)trans.GetObject(id2, OpenMode.ForRead);
// find their type
Type entType1 = ent1.GetType();
Type entType2 = ent2.GetType();
// the two entities should be the same type
if (!entType1.Equals(entType2))
return;
// …
There’s a much more efficient way to obtain the class name/entity type… That’s using ObjectId.ObjectClass property.
Using this property saves you opening an object for read, thus saving valuable CPU cycles.
e.g.
// the two entities should be the same type
if (id1.ObjectClass.Name != id2.ObjectClass.Name))
return;
Database db =
Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// open the entities
Entity ent1 = (Entity)trans.GetObject(id1, OpenMode.ForRead);
Entity ent2 = (Entity)trans.GetObject(id2, OpenMode.ForRead);
// …