>> Specifically, I have been trying to measure time in drawings for costing purposes. <<
Aside from the fact that IMO, the above metric is meaningless, unless you jump through the many hoops you need to, in order to take into account 'idle' time (e.g., the user takes a phone call and stops working on the project but leaves the drawing open, or the application itself is not in the foreground), you shouldn't use the DocumentBecameCurrent event because 'Current' and 'Active' are two different things. A document can be made current without becoming the active document (although it is rare).
The right way to detect a change in the active document is to use the DocumentActivated event, along with a private member that stores the document passed into the last call to the method, and use that to filter out superfluous notifications (e.g., when a modal window is closed, or when the application itself is brought into the foreground, this event will fire even thoough the active document has not changed).
This is C#:
[code]
public static class DocumentReactor
{
public static event DocumentCollectionEventHandler DocumentActivated = null;
static DocumentReactor()
{
Application.DocumentManager.DocumentActivated += documentActivated;
}
static void documentActivated( object sender, DocumentCollectionEventArgs e )
{
if( m_active != e.Document )
{
if( e.Document != null && DocumentActivated != null )
DocumentActivated( sender, e );
m_active = e.Document;
}
}
static Document m_active = null
}
[/code]
With the above class in your project, you can handle its DocumentActivated event and you will only be notified when the active document has actually changed.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com
Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
wrote in message news:5974434@discussion.autodesk.com...
Hello Everyone,
Recently I have been do some work with the document collection events. Specifically, I have been trying to measure time in drawings for costing purposes. To this end I want to catch the DocumentBecameCurrent event to begin timing and also another event to stop timing when a docment stops being current.
What I noticed in my coding is that the event triggers multiple times, this surpises me and makes it more complicated to do what I want to do.
The code below is just a skeleton to demonstrate what happens. If you run it with 2 or more drawings, the event will fire three times, once with the current drawing and then twice with the new(current drawing) when you change drawings (ie CTL+TAB).
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application
< extensionapplication="">
Public Class CWCHClass
Implements IExtensionApplication
Public Sub Initialize() Implements IExtensionApplication.Initialize
AddHandler AcadApp.DocumentManager.DocumentBecameCurrent, AddressOf OnDocumentBecameCurrent
End Sub
Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
End Sub
Private Sub OnDocumentBecameCurrent(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
Debug.Print(e.Document.Name)
Msg("TEST: Document Became Current: " & e.Document.Name)
End Sub
Public Sub Msg(ByVal Text As String)
Dim Ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor()
Ed.WriteMessage(vbCrLf & Text)
End Sub
End Class
My questions to the group are:
1. Is the multiple triggering normal behaviour with the DocumentBecameCurrent event? And if so are there strategies to deal with it?
2. What is the best event to monitor, so that I can catch when a document ceases to be current (ie DocumentToBeDeactivated or DocumentActivationChanged)
Regards
Martin Duke>