I am trying to get a list of selected entities and their layers, but I want to capture it from the editor, not a command.
I currently have
Private Sub ed_SelectionAdded(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.EditorInput.SelectionAddedEventArgs) Handles ed.SelectionAdded
For Each objid As ObjectId In e.AddedObjects.GetObjectIds
Using tr As Transaction = ed.Document.TransactionManager.StartTransaction
Dim ent As Entity = tr.GetObject(objid, OpenMode.ForRead)
ed.WriteMessage(vbLf & objid.ObjectClass.DxfName & " is on " & ent.Layer)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
but when select a circle on Layer 1, I get the result below in the commandline
CIRCLE is on Layer1 CIRCLE is on Layer1 CIRCLE is on Layer1 CIRCLE is on Layer1 CIRCLE is on Layer1 CIRCLE is on Layer1 Command: CIRCLE is on Layer1
If I hover over the circle while it is selected, it also treates it as a selection.
SelectionAdded is called multiple times during PICKFIRST selection, whether you are calling SelectAll inside it or not. it is as designed. If a command starts the selection (i.e. not PICKFIRST) then SelectionAdded is only called once per each selection change.
One suggestion depending on what you are looking for is to use Document.ImpliedSelectionChanged event to monitor PICKFIRST selection change – this is only fired once after a selection change.
However, if you also want to monitor when the user removes entities from the selection so that you would also remove related objects from the selection, then you would need to keep track of the selection set, so you can see what the user removed from the selection. And if you keep track of the selection you might as well keep listening to SelectionAdded all the time, and if the selection did not change, then just return from it. This should not cause too much overhead.
I hope it helps.
Philippe Leefsma Developer Consultant Developer Technical Services Global Subscription & Support