.NET

Reply
Contributor
martinduke2653
Posts: 19
Registered: ‎12-04-2005
Message 1 of 8 (499 Views)

Document collection events

499 Views, 7 Replies
07-06-2008 08:00 PM
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
Distinguished Contributor
foruma000256
Posts: 216
Registered: ‎01-28-2004
Message 2 of 8 (499 Views)

Re: Document collection events

07-07-2008 10:24 AM in reply to: martinduke2653
Shared dc As Autodesk.AutoCAD.ApplicationServices.DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager


I listen to the dc.DocumentActivated, dc.DocumentToBeDestoryed, and dc.DocumentDestroyed events myself.

In callback_DocumentActvated (sender as object, e as documentcollectioneventargs) I filter out e.document isnot nothing andalso e.document.window isnot nothing and if e.document.name = strCurrentDoc all to determine if I am still working with the same cad file.

in callback_DocumentToBeDestroyed(sender as object, and e as documentcollectioneventargs) I only run a routine to save my user settings.

in callback_DocumentDestoryed(sender as object, e as documentdestoryedeventargs)
I check if dc.count = 1 (being that the last drawing is closing and autocad will be in 0 document state) to handle events differently.

A document will become 'active' anytime you swap from a window to a window. This includes those quick popup windows, message boxes, forms of any sort activated by AutoCAD or you. So Document Activated/Deactivated will happen LOTS. I ended up abandoning DocumentActvated for that reason.

Hope this helps. BTW AutoCAD does have a built in drawing use timer... "TIME" at the command prompt.

jvj
*Tony Tanzillo
Message 3 of 8 (499 Views)

Re: Document collection events

07-07-2008 04:57 PM in reply to: martinduke2653
>> 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
Contributor
martinduke2653
Posts: 19
Registered: ‎12-04-2005
Message 4 of 8 (499 Views)

Re: Document collection events

07-10-2008 07:33 PM in reply to: martinduke2653
Hello Jamie,

Thank you for taking the time to respond. I can see now that what I was missing was the check on the state of the arguments. With that in place I can proceed. I was aware by thr way of the time command however it pays no heed to the drawing state.


Regards


Martin Duke
Contributor
martinduke2653
Posts: 19
Registered: ‎12-04-2005
Message 5 of 8 (499 Views)

Re: Document collection events

07-10-2008 07:40 PM in reply to: martinduke2653
Hello Tony,

Thank you for taking the time to respond. The metrics of drawing production are indeed hard to measure, and whilst I accept that we can perhaps never get an exact answer to the question of 'how much does a drawing cost?' that should not prevent us from trying. In my case I hope over time to create an historical record of production costs that can be used to look at efficiency factors within groups.

Your document reactor class certainly helps me with what I am trying to do.

Regards

Martin Duke
*Tony Tanzillo
Message 6 of 8 (499 Views)

Re: Document collection events

07-11-2008 12:27 AM in reply to: martinduke2653
>> The metrics of drawing production are indeed hard to measure, and whilst I accept that we can perhaps never get an exact answer to the question of 'how much does a drawing cost?' that should not prevent us from trying. <<

Sorry, have to disagree.

How long a drawing is open and/or active in the AutoCAD editor doesn't really tell you anything about cost.

--
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
Contributor
Marco_Caprez
Posts: 18
Registered: ‎09-21-2004
Message 7 of 8 (499 Views)

Re: Document collection events

07-11-2008 12:41 AM in reply to: martinduke2653
Hi Martin,

I agree with Tony's statement above.

And for measuring the time you can use the TD* setvars. That gives you the editing time as unaccurate as your attempt. But doesn't say to much of the costs.

Cheers, Marco
Distinguished Contributor
foruma000256
Posts: 216
Registered: ‎01-28-2004
Message 8 of 8 (499 Views)

Re: Document collection events

07-11-2008 06:23 AM in reply to: martinduke2653
Sorry Martin,

I agree with the guru's above (Tony and Marco). While time spend pushing buttons and moving mice is valid, you leave out the time spent reviewing somebody's paper drawings (that you are tracing or working from), time spent discussing the job at hand, and time spent going to the bathroom, to relieve yourself of all the crap thrown at you during a normal day's work...

In short there is much other time non computer control related involved. So unless your working up a totalitarian company's CAD sweatshop hours your final result will still feel like it missed the mark. So your program will have to track other non CAD related events. Perhaps you can put a motion sensor on each drafter's chair, a microphone above their desk, and a camera to see when they are surfing the net for fun, or product research...

Good luck and happy programming!

jvj
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.