.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to determinate the type of an entity?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
cdinten
913 Views, 6 Replies

How to determinate the type of an entity?

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, :)), 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.

6 REPLIES 6
Message 2 of 7
Ajilal.Vijayan
in reply to: cdinten
Message 3 of 7
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 7
DiningPhilosopher
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.
}

 

Message 5 of 7
cdinten
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.

Message 6 of 7
cdinten
in reply to: _gile

ok ,Gile, your code works well,thanks a lot
Message 7 of 7
cdinten
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost