I have a SelectionAdded event handler that reacts three times when selecing a single block and once when deselecting it.
If there are two blocks in the drawing but only selected one it reacts 5 or 6 times.
And it reacts every time I pass the mouse over the selected block.
Is there any way to sidestep the events in the handler so that AutoCAD doesn't grind to a halt if I have multiple blocks in the drawing. And decide to move my mouse over all of them?
You have to add a event handler for it fire.
Maybe showing your code how you add them would help someone help you out.
Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.ApplicationServices Imports AcApp = Autodesk.AutoCAD.ApplicationServices.Application Imports Autodesk.AutoCAD.EditorInput <Assembly: ExtensionApplication(GetType(VbExtApp))> Public Class VbExtApp Implements Autodesk.AutoCAD.Runtime.IExtensionApplication Private DocMan As DocumentCollection 'Initialize sub Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize DocMan = AcApp.DocumentManager AddHandler DocMan.DocumentCreated, AddressOf callback_documentCreated AddHandler DocMan.DocumentActivated, AddressOf callback_documentActivated Dim doc As Document For Each doc In DocMan Dim db As Database = doc.Database Dim ed As Editor = doc.Editor AddHandler db.ObjectModified, AddressOf callback_ObjectModified AddHandler db.ObjectAppended, AddressOf callback_ObjectAppended AddHandler db.ObjectErased, AddressOf callback_ObjectErased AddHandler ed.SelectionAdded, AddressOf callback_SelectionAdded ' AddHandler ed.SelectionRemoved, AddressOf callback_SelectionRemoved Next 'Import Blocks ImportBlocks("D:\My Documents\Visual Studio Projects\Detailer\Blocks.dwg") 'Load Palette Loading = True pal = New Palette() pal.LoadPalette() Loading = False 'Initialise Thumbnail Class Dim thumb As ThumbNailClass thumb = New ThumbNailClass End Sub 'SelectionAdded Public Sub callback_SelectionAdded(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.EditorInput.SelectionAddedEventArgs) 'MsgBox("Selection Made") For Each objid As ObjectId In e.AddedObjects.GetObjectIds ID = objid ReDim Preserve ObjectCollection(0) ReDim Preserve ObjectCollection(0).Set_(0) ReDim Preserve ObjectCollection(0).Set_(0).View(0) ReDim Preserve ObjectCollection(0).Set_(0).Range(0) ReDim Preserve ObjectCollection(0).Set_(0).Label(0) NoViews = 0 : NoRanges = 0 : NoLabels = 0 ObjectsSelected() Next End Sub 'Terminate sub Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate RemoveHandler DocMan.DocumentCreated, AddressOf callback_documentCreated End Sub Public Shared Sub callback_ObjectModified(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs) 'MsgBox("Something was modified") ID = e.DBObject.ObjectId ModifyEventHander() End Sub Public Shared Sub callback_ObjectAppended(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs) 'MsgBox("Something was Appended") End Sub Public Shared Sub callback_ObjectErased(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectErasedEventArgs) 'MsgBox("Something was Erased") ID = e.DBObject.ObjectId DeleteEventHandler() End Sub Private Sub callback_documentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs) If e.Document = Nothing Then Exit Sub Else ' MsgBox("Document Created") End If End Sub Private Sub callback_documentActivated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs) If e.Document = Nothing Then Exit Sub Else ' MsgBox("Document Active") End If End Sub End Class
I just added break points on the ObjectModified and ObjectAppended to see which event was triggering it.
On starting the debug they were triggered an enless number of times.
Computers are fast these days but I don't want my application to slow things down.
Are there any step overs I should be considering in my code to handle these multiple fire events?
This is all very confusing.
If I press Escape with objects selected the SelectionAdded event handler is triggered containing the selection instead of the SelectionRemoved.
Is there a handler that deals with the current selection which reacts after any objects are added or removed?
Took a while but I found my answer here.
I needed the ImpliedSelectionChanged event
AddHandler doc.ImpliedSelectionChanged, AddressOf Document_ImpliedSelectionChanged
Private Sub Document_ImpliedSelectionChanged(ByVal sender As [Object], ByVal e As EventArgs) Dim doc As Document = DirectCast(sender, Document) Dim ed As Editor = doc.Editor Dim sr As PromptSelectionResult = ed.SelectImplied() If sr.Value Is Nothing Then Return End If ed.WriteMessage(vbLf & "Selection Count: {0}", sr.Value.Count) End Sub