Line l = tr.GetObject(SomeObjectIdObtainedFromElsewhere, OpenMode.ForRead, false) as Line;
If (l != null)
{
// some interesting mojo goes here
}
Line l = (Line)tr.GetObject(SomeObjectIdObtainedFromElsewhere, OpenMode.ForRead, false);
< ..>
My 2p worth.
Microsoft recommends the 'as' and test for null method :-)Is that because it wont blow up as badly or b/c they are lazy?
Returns true if Class == this or if the class represented by this Class object is descended from the class represented by the Class object pointed to by Class. Otherwise returns false.
Glenn, in both your examples, you used ObjectIds you had previously obtained. I am assuming that in the first example, there was no filtering, so there could be anything obtained. But in the second, you KNOW you have a line, so cast the object to a line, Right? I guess I am of the mind to filter my selection ahead of time so as to "know" what Im working with. Is that what you were getting at?
class Cast
{
[CommandMethod("tes")]
public void Test1()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult per = ed.GetEntity("\nSelect a curve: ");
if (per.Status != PromptStatus.OK) return;
ObjectId id = per.ObjectId;
Polyline pl;
ed.WriteMessage("\nYou selected a " + id.ObjectClass.DxfName);
long start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
pl = tr.GetObject(id, OpenMode.ForRead) as Polyline;
if (pl == null)
{
tr.Commit();
continue;
}
tr.Commit();
}
}
ed.WriteMessage("\nAs test ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
//use this only if you pick a polyline
//start = DateTime.Now.Ticks;
//for (int i = 0; i < 10000; i++)
//{
// using (Transaction tr = db.TransactionManager.StartTransaction())
// {
// pl = (Polyline)tr.GetObject(id, OpenMode.ForRead);
// tr.Commit();
// }
//}
//ed.WriteMessage("\nDirect test ellapsed time ="
// + ((DateTime.Now.Ticks - start) / 1000).ToString());
pl = null;
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent is Polyline)
pl = tr.GetObject(id, OpenMode.ForRead) as Polyline;
tr.Commit();
}
}
ed.WriteMessage("\nIs test using as method ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent is Polyline)
pl = (Polyline)tr.GetObject(id, OpenMode.ForRead);
tr.Commit();
}
}
ed.WriteMessage("\nIs test using direct cast method ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if(id.ObjectClass==RXClass.GetClass(typeof( Polyline)))
pl =(Polyline) tr.GetObject(id, OpenMode.ForRead);
tr.Commit();
}
}
ed.WriteMessage("\ndirect cast using id., ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
RXClass plineClass = RXClass.GetClass(typeof(Polyline));
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if (id.ObjectClass == plineClass)
pl = (Polyline)tr.GetObject(id, OpenMode.ForRead);
tr.Commit();
}
}
ed.WriteMessage("\ndirect cast using pre dimmed class, ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if (id.ObjectClass == plineClass)
pl = tr.GetObject(id, OpenMode.ForRead)as Polyline;
tr.Commit();
}
}
ed.WriteMessage("\nAs cast using pre dimmed class, ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if (id.ObjectClass.Name == "AcDbPolyline")
pl = (Polyline)tr.GetObject(id, OpenMode.ForRead);
tr.Commit();
}
}
ed.WriteMessage("\ndirect cast using id string comparison, ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if (id.ObjectClass.Name == "AcDbPolyline")
pl = tr.GetObject(id, OpenMode.ForRead) as Polyline;
tr.Commit();
}
}
ed.WriteMessage("\nAs cast using id string comparison, ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000; i++)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if (id.ObjectClass.DxfName == "LWPOLYLINE")
pl = tr.GetObject(id, OpenMode.ForRead) as Polyline;
tr.Commit();
}
}
ed.WriteMessage("\nAs cast using id dxf string comparison, ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString());
} //end Test1
} //End Cast
iplines = 0;
start = DateTime.Now.Ticks;
RXClass plineClass = RXClass.GetClass(typeof(Polyline));
for (int i = 0; i < 10000; i++)
{
foreach (ObjectId id in ids)
{
if (id.ObjectClass == plineClass)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
pl = (Polyline)tr.GetObject(id, OpenMode.ForRead);
tr.Commit();
iplines++;
}
}
}
}
ed.WriteMessage("\ndirect cast using pre dimmed class, ellapsed time ="
+ ((DateTime.Now.Ticks - start) / 1000).ToString()
+ " plines=" + iplines);
what mood you're in
Thx for all replies so far.I don't see that there would be a difference
I should mention in my case, I am not only looking at acad objects.
right, no difference, but I cannot use ARXclass method wich seems to be pretty fast.