Author Topic: EventHandler for Entity Selection  (Read 2883 times)

0 Members and 1 Guest are viewing this topic.

Offline Kevin

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
EventHandler for Entity Selection
« on: November 23, 2012, 08:15:16 PM »
Hi everybody,

Im looking for an eventHandler which will be fired if I have selected an entity.
I tried it with PromptForEntityEndingEventHandler but the event will be fired everytime
I cross an entity.

Do you know a simple example of that?

Kind regards,
Kevin

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: EventHandler for Entity Selection
« Reply #1 on: November 24, 2012, 05:34:30 PM »
Hi,

The Editor.SelectionAdded event is fired every time entities are added to a selection set.

Offline Kevin

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: EventHandler for Entity Selection
« Reply #2 on: November 25, 2012, 12:41:00 PM »
Hi,

now I tried it with ed.SelectionAdded and ed.SelectionRemoved.

The problem now is:
If I dont deselect the entity direct, it will be further in my ObjectIdCollection.

The adding Code looks like that:

Code: [Select]
public static void ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
        {
            Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            ObjectId[] ids = e.AddedObjects.GetObjectIds();

            foreach (ObjectId id in ids)
            {
                if (!IdCollection.Contains(id))
                {
                    IdCollection.Add(id);
                }
            }
        } 

My main problem is, that the SelectionRemoved only fires if the entity is directly deselected.

Do you have any idea?

Many thanks!
« Last Edit: November 25, 2012, 01:43:22 PM by Kevin »

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: EventHandler for Entity Selection
« Reply #3 on: November 25, 2012, 03:04:18 PM »
Hi,

I do not understand what you're trying to do, but you'll find an example of using the SelectionAdded event here.
« Last Edit: November 25, 2012, 03:12:39 PM by (gile) »

Offline Kevin

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: EventHandler for Entity Selection
« Reply #4 on: November 25, 2012, 04:17:24 PM »
I´m trying to get all entites (ids...) which are currently selected for displaying xdatas
in a palette.
If I select for example lines I want the ObjectIds saved in a Collection or array. If I unselect
the Entity/ies I want to remove the Id from the Collection.
If I run a command or something else, the Entites will be unselected too, but with the Method
ed.SelectionRemoved the entite Ids wont be removed / the event isnt fired.

And I dont want to run a command, this should be "interactive" like the autocad property palette...


« Last Edit: November 25, 2012, 04:21:16 PM by Kevin »

Offline BlackBox

  • Newbie
  • *
  • Posts: 4
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: EventHandler for Entity Selection
« Reply #5 on: April 09, 2013, 09:18:35 PM »
I´m trying to get all entites (ids...) which are currently selected for displaying xdatas
in a palette.
If I select for example lines I want the ObjectIds saved in a Collection or array. If I unselect
the Entity/ies I want to remove the Id from the Collection.
If I run a command or something else, the Entites will be unselected too, but with the Method
ed.SelectionRemoved the entite Ids wont be removed / the event isnt fired.

And I dont want to run a command, this should be "interactive" like the autocad property palette...

This is an older thread, so not sure if you already got it sorted, but you might consider the Document.ImpliedSelectionChanged Event.
"Potential has a shelf life." - Margaret Atwood

Offline keithbrown

  • Newbie
  • *
  • Posts: 1
  • Karma: +2/-0
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: EventHandler for Entity Selection
« Reply #6 on: February 26, 2014, 01:34:12 PM »
This is an older thread but i thought i would post anyway in case anyone heading this way wanted a solution.  As BlackBox said the event that you want is the ImpliedSelectionChanged event.  In the code snippet below I am checking the implied selection to see if a single object is selected.

Code: [Select]
       private void DocumentManager_ImpliedSelectionChanged(object sender, EventArgs e)
        {
            Document document = Application.DocumentManager.MdiActiveDocument;
            PromptSelectionResult promptSelectionResult = document.Editor.SelectImplied();

            // If no objects are selected then do something and return
            if (promptSelectionResult.Value == null)
            {
                // Do Something
                return;
            }

            // If more than one object is selected then do something
            if (promptSelectionResult.Value.Count > 1)
            {
                // Do Something
                return;
            }

            // At this point only one object should be selected.
            // Do Something             

 
            }

And below is the code that i would use to initiate the event handler

Code: [Select]
var document = Application.DocumentManager.MdiActiveDocument;
document.ImpliedSelectionChanged += DocumentManager_ImpliedSelectionChanged;

I just make sure to setup the event handler in each document as it is created and remove it as each document is destroyed.