.NET

Reply
Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 1 of 7 (306 Views)
Accepted Solution

How to determinate the type of an entity?

306 Views, 6 Replies
01-26-2014 03:48 AM

dear everyone:

when i move mouse onto a graphic object(most likely an entity) and stay for a while, it shows this little window:

CX_0126_193734.png

here “圆弧”means arc(a chinese user, :smileyhappy:), and for some other types derive from Entity, such as Line, Circle, Polyline, or even MText,the type of them changes as i move my nouse onto it.

now the question is, how does autocad managed to get the derived type form Entity?

I know we could try an type phase like this:

Transction.GetObject(ent.Objectid,OpenMode.ForRead) as Line;

and then check if it's null,then prase to another type...

but this would be so many if-statements  and it will be so clumsy,could anyone tell me a swift way to get the final type? thanks a lot.

Hi,

 

You don't need to open the entity.

The Objectid.ObjectClass property returns a RXClass instance which have a Name and a DxfName property.

 

You can try this little sample:

 [CommandMethod("EntityName")]
public void EntityName()
{
Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
PromptEntityResult per = ed.GetEntity("\nSelect an entity: ");
if (per.Status != PromptStatus.OK) return;
ed.WriteMessage("\nName: {0}\nDxfName: {1}",
per.ObjectId.ObjectClass.Name,
per.ObjectId.ObjectClass.DxfName);
}

 

 

Getting the final type of the entity isn't really the problem.

 

That's trival. The problem is determineing what course or branch of code

should be taken based on what type of entity you are handed.

 

Below is an example use of dynamic-dispatch, to dynamically bind

to one of several different overloads of a method based on the

runtime type of the argument. The example relies on System.Dyanmic,

but you can also do the same using Type.InvokeMember(), albeit much

more slowly.

 

using System;
using System.Dynamic;
using Autodesk.AutoCAD.DatabaseServices;
/// Create an instance of this class, and
/// call the DispatchEntity() method, and
/// pass it an instance of an Entity or any
/// derived type, and it will dynamically
/// delegate the call to one of the overloads
/// of the Dispatch() method.
public class DynamicDispatchExample
{
/// Given an Entity of any derived type,
/// call this method passing the entity,
/// and the overload of Dispatch() with
/// the closest-matching argument type
/// will be called and passed the entity
/// cast to the type of the argument.
/// The default implementation of Dispatch()
/// which takes an Entity as an argument is
/// called if there is no other overload with
/// a more specific type (including abstract
/// types like Curve or Dimension).
/// What's notworty about this example, is
/// that there is absolutely no conditional
/// branching (if/then/else) whatsoever.
///
public void DispatchEntity( Entity entity );
{
((dynamic) this).Dispatch( (dynamic) entity);
}
/// Helper to display a message:
static void WriteLine( string fmt, params object[] args )
{
Application.DocumentManager.MdiActiveDocument
.Editor.WriteMessage( "\n" + fmt, args );
}
/// A default implementation that will be called
/// when there is no other overload with a more-
/// specific matching type:
public void Dispatch( Entity entity )
{
WriteLine("No specific method defined for {0}",
entity.GetType().Name );
}
/// One of the following three overloads will
/// be called when the entity passed in is an
/// instance of one of the argument types:
public void Dispatch( Line line )
{
WriteLine("The entity is a Line");
}
public void Dispatch( Circle circle )
{
WriteLine("The entity is a Circle");
}
public void Dispatch( Arc arc )
{
WriteLine("The entity is an Arc");
}
/// TODO: Add additional overloads for other
/// entity types.
}

 

Valued Mentor
Ajilal.Vijayan
Posts: 439
Registered: ‎01-12-2008
Message 2 of 7 (293 Views)

Re: How to determinate the type of an entity?

01-26-2014 05:16 AM in reply to: cdinten
*Expert Elite*
_gile
Posts: 2,086
Registered: ‎04-29-2006
Message 3 of 7 (278 Views)

Re : How to determinate the type of an entity?

01-26-2014 05:43 AM in reply to: cdinten

Hi,

 

You don't need to open the entity.

The Objectid.ObjectClass property returns a RXClass instance which have a Name and a DxfName property.

 

You can try this little sample:

        [CommandMethod("EntityName")]
        public void EntityName()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityResult per = ed.GetEntity("\nSelect an entity: ");
            if (per.Status != PromptStatus.OK) return;
            ed.WriteMessage("\nName: {0}\nDxfName: {1}",
                per.ObjectId.ObjectClass.Name,
                per.ObjectId.ObjectClass.DxfName);
        }

 

 

Gilles Chanteau
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 4 of 7 (263 Views)

Re: How to determinate the type of an entity?

01-26-2014 08:45 AM in reply to: cdinten

Getting the final type of the entity isn't really the problem.

 

That's trival. The problem is determineing what course or branch of code

should be taken based on what type of entity you are handed.

 

Below is an example use of dynamic-dispatch, to dynamically bind

to one of several different overloads of a method based on the

runtime type of the argument. The example relies on System.Dyanmic,

but you can also do the same using Type.InvokeMember(), albeit much

more slowly.

 

using System;
using System.Dynamic;
using Autodesk.AutoCAD.DatabaseServices;

/// Create an instance of this class, and
/// call the DispatchEntity() method, and
/// pass it an instance of an Entity or any
/// derived type, and it will dynamically
/// delegate the call to one of the overloads 
/// of the Dispatch() method.

public class DynamicDispatchExample
{ 
   /// Given an Entity of any derived type,
   /// call this method passing the entity,
   /// and the overload of Dispatch() with 
   /// the closest-matching argument type 
   /// will be called and passed the entity 
   /// cast to the type of the argument.
   
   /// The default implementation of Dispatch()
   /// which takes an Entity as an argument is 
   /// called if there is no other overload with 
   /// a more specific type (including abstract 
   /// types like Curve or Dimension).
   
   /// What's notworty about this example, is
   /// that there is absolutely no conditional 
   /// branching (if/then/else) whatsoever.
   ///

   public void DispatchEntity( Entity entity );
   {
      ((dynamic) this).Dispatch( (dynamic) entity);
   }

   /// Helper to display a message:   
   static void WriteLine( string fmt, params object[] args )
   {
      Application.DocumentManager.MdiActiveDocument
         .Editor.WriteMessage( "\n" + fmt, args );
   }

   /// A default implementation that will be called
   /// when there is no other overload with a more-
   /// specific matching type:
   
   public void Dispatch( Entity entity )
   {
      WriteLine("No specific method defined for {0}",
         entity.GetType().Name );
   }
   
   /// One of the following three overloads will 
   /// be called when the entity passed in is an 
   /// instance of one of the argument types:
   
   public void Dispatch( Line line )
   {
      WriteLine("The entity is a Line");
   }

   public void Dispatch( Circle circle )
   {
      WriteLine("The entity is a Circle");
   }
   
   public void Dispatch( Arc arc )
   {
      WriteLine("The entity is an Arc");
   }
   
   /// TODO: Add additional overloads for other
   ///       entity types.
}

 

Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 5 of 7 (249 Views)

Re: How to determinate the type of an entity?

01-26-2014 04:48 PM in reply to: Ajilal.Vijayan

Yes, I know that Event and i can get the entity once i had moved my mouse onto it. the problem is i can't determinate what type of the entity is.

Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 6 of 7 (245 Views)

Re : How to determinate the type of an entity?

01-26-2014 04:52 PM in reply to: _gile
ok ,Gile, your code works well,thanks a lot
Valued Contributor
cdinten
Posts: 61
Registered: ‎11-19-2010
Message 7 of 7 (241 Views)

Re: How to determinate the type of an entity?

01-26-2014 04:55 PM in reply to: DiningPhilosopher
Thanks, you thinking goes further than my question, and it is really a good way to dynamicly react according the context, it is really awesome
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.