.NET

.NET

Reply
Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 1 of 6 (528 Views)

Event handler triggered multiple times

528 Views, 5 Replies
07-15-2011 10:12 AM

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?

Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 2 of 6 (520 Views)

Re: Event handler triggered multiple times

07-15-2011 10:53 AM in reply to: SRSDS

You have to add a event handler for it fire.

 

Maybe showing your code how you add them would help someone help you out.

 

 

You can also find your answers @ TheSwamp
Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 3 of 6 (514 Views)

Re: Event handler triggered multiple times

07-15-2011 11:32 AM in reply to: Jeffrey_H
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

 

Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 4 of 6 (503 Views)

Re: Event handler triggered multiple times

07-15-2011 02:58 PM in reply to: SRSDS

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?

Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 5 of 6 (490 Views)

Re: Event handler triggered multiple times

07-16-2011 01:07 AM in reply to: SRSDS

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?

 

Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 6 of 6 (469 Views)

Re: Event handler triggered multiple times

07-17-2011 12:14 AM in reply to: SRSDS

Took a while but I found my answer here.

 

http://forums.autodesk.com/t5/NET/Emulate-Properties-Dialog-by-selecting-object-on-screen/td-p/25267...

 

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

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.