Author Topic: Executing a command when switching window (drawing)  (Read 6439 times)

0 Members and 1 Guest are viewing this topic.

Eycki

  • Guest
Executing a command when switching window (drawing)
« on: March 13, 2012, 09:09:32 AM »
Hello,

I got several commands added to my acad.mnl file.  These commands will be executed when opening an existing drawing or starting a new drawing.
But when you have several drawings opened and switch to another drawing, I want these commands to be executed aswell.

Somebody an idea how to solve this?

kaefer

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #1 on: March 13, 2012, 12:21:00 PM »
I got several commands added to my acad.mnl file.  These commands will be executed when opening an existing drawing or starting a new drawing.

I would rather think that those are executed when a menu is loaded. Do you mean acad.lsp / acaddoc.lsp?

But when you have several drawings opened and switch to another drawing, I want these commands to be executed aswell.

You may want to look into the DocumentCollection Events.

elliottpd

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #2 on: March 13, 2012, 01:50:22 PM »
'Handler for running programs when a drawing is opened:
AddHandler DocumentManager.DocumentCreated, AddressOf DocumentCreated

        Public Sub DocumentCreated(ByVal sender As Object, ByVal e As System.EventArgs)
            'add code here
        End Sub

elliottpd

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #3 on: March 13, 2012, 01:54:55 PM »
Besides DocumentCreated, there is also the DocumentActivated event.

kaefer

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #4 on: March 13, 2012, 01:58:29 PM »
Code - Visual Basic: [Select]
  1. 'Handler for running programs when a drawing is opened:
  2. AddHandler DocumentManager.DocumentCreated, AddressOf DocumentCreated
  3.  
  4.         Public Sub DocumentCreated(ByVal sender As Object, ByVal e As System.EventArgs)
  5.             'add code here
  6.        End Sub

From arxmgd.chm:
Quote
DocumentCollection.DocumentCreated Event
...
This notification is sent when a new Document has been constructed.

OP is looking for one of DocumentActivated, DocumentBecameCurrent or DocumentLockModeChanged.

Eycki

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #5 on: March 15, 2012, 10:15:54 AM »
The commands does work perfectly in acad.mnl when loading AutoCAD, but also when opening a new drawing.
But actually I want more than that.  I want my commands also to be exectuted when switching to another (already opened in the background) drawing.

I got several commands added to my acad.mnl file.  These commands will be executed when opening an existing drawing or starting a new drawing.

I would rather think that those are executed when a menu is loaded. Do you mean acad.lsp / acaddoc.lsp?

But when you have several drawings opened and switch to another drawing, I want these commands to be executed aswell.


You may want to look into the DocumentCollection Events.
« Last Edit: March 15, 2012, 10:21:31 AM by Eycki »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Executing a command when switching window (drawing)
« Reply #6 on: March 15, 2012, 12:11:18 PM »
I want my commands also to be exectuted when switching to another (already opened in the background) drawing.

Besides DocumentCreated, there is also the DocumentActivated event.

OP is looking for one of DocumentActivated, DocumentBecameCurrent or DocumentLockModeChanged.

 

kaefer

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #7 on: March 15, 2012, 01:03:26 PM »
Besides DocumentCreated, there is also the DocumentActivated event.

Heck, how did I miss that? Apologies...

Eycki

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #8 on: March 15, 2012, 03:23:13 PM »
I think I have enough info now to make it work, but what is the difference between DocumentActived and DocumentBecameCurrent?

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Executing a command when switching window (drawing)
« Reply #9 on: March 15, 2012, 04:41:27 PM »
Handle Document events
 
& little similar from docs
 
Quote

 
Applications cannot keep per-document data in global or static variables. This includes AcDbDatabase objects, AcDbObject objects, AcDbObjectId values, header variable values, document variable values, selection sets, and other document-specific information. Any occurrence of per-document data in global and static variables might be corrupted if your application is run in multiple concurrent edit sessions.
To avoid corruption of data, you can encapsulate command behavior and data into classes. An instance of the class can be instantiated for each call to the command. As the command acquires document-specific data, it keeps its own per-instance copies of that data.
Another solution is to encapsulate all of the global and static data into a structure or class. A copy of the data is instantiated for each document. A local pointer to the appropriate instance is set at each entry point in the application. The local pointer is then used to access the per-document data. Use the documentActivated() reactor to switch between instances of the encapsulated data.
You can create the per-document data on an as-needed basis, or create it when the application is first loaded. If created on an as-needed basis, as the application's registered commands or reactors are called, the current document is determined and a query is made to get the document's data. If it is not found, it is created at that time.
To create the per-document data when the application is first loaded, use an AcApDocumentIterator in the AcRx::kInitAppMsg handler to get a list of all open documents. Then use AcApDocManagerReactor::documentCreated() to know when to create additional per-document data for documents opened after the application is loaded.
Whichever method is used to allocate the per-document data, the application must use the AcApDocManagerReactor::documentToBeDestroyed() reactor in order to know when to delete the data. Applications should also delete the remaining data during the AcRx::kUnloadAppMsg handler

kaefer

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #10 on: March 15, 2012, 05:27:17 PM »
Handle Document events

Using my tools #1 (Object Browser)

event                      delegate                               EventArgs
DocumentActivated          DocumentCollectionEventHandler         DocumentCollectionEventArgs
DocumentBecameCurrent      DocumentCollectionEventHandler         DocumentCollectionEventArgs
DocumentLockModeChanged    DocumentLockModeChangedEventHandler    DocumentLockModeChangedEventArgs
DocumentLockModeWillChange DocumentLockModeWillChangeEventHandler DocumentLockModeWillChangeEventArgs
DocumentToBeActivated      DocumentCollectionEventHandler         DocumentCollectionEventArgs


Interpreting the data #1: DocumentCollectionEventArgs transports almost nothing except the Document, while DocumentLockModeWillChangeEventArgs is highly interesting: There's a Veto() method, inhibiting the execution of the command indicated by GlobalCommandName.

Using my tools #2 (e.g. MGDDBGEVENTS, command of MgdDbg.dll redux)

DocumentLockModeWillChange : NotLocked -> Write
DocumentLockModeChanged : NotLocked -> Write
DocumentLockModeWillChange : Write -> NotLocked
DocumentLockModeChanged : Write -> NotLocked
DocumentToBeActivated
DocumentBecameCurrent
DocumentBecameCurrent
DocumentActivated


Interpreting the data #2: While the order of events generally should not be relied upon, it is clear that at least a partial order should be guaranteed (DocumentLockModeWillChange before DocumentLockModeChanged, DocumentToBeActivated before DocumentActivated). And they could be fired more than once.

TheMaster

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #11 on: March 15, 2012, 06:13:30 PM »
I think I have enough info now to make it work, but what is the difference between DocumentActived and DocumentBecameCurrent?

'Active' and 'Current' are two different things.

Active means that the document's window is Active.

Current means that a document's thread is running.

Both are exclusive (only one document can be active,
and only one document's thread can be running at any
given time).

What's important is that the 'Current' document is not necessarily
the 'Active' document (although that's commonly the case).

A document can temporarily be made the 'current' document without
also being activated, which is necessary in order for changes to the
associated database to be updated on the display.

You shouldn't rely on any events signaling that a document has
or will become the current document, since they don't mean that
the document is or will become the active document.

« Last Edit: March 16, 2012, 07:28:58 AM by TheMaster »

fixo

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #12 on: March 17, 2012, 08:45:30 AM »
Sorry to all for off-topic, I can't silent
Dear ...ony,
I want to express the gratitude that with your moving back this group has taught a new breath of fresh air,
it is a lot of moon back the same it was watched on discussion group
Hope all is well with you
Your fan, you've knew it

Oleg

Eycki

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #13 on: April 02, 2012, 01:18:42 PM »
Ok, it works now.  But when I choose use 'DocumentActivated' it will execute 4 times when I open a new document or switch to another opened drawing.  Why?
I would like it to be executed only once.  Any suggestions?
« Last Edit: April 02, 2012, 01:26:45 PM by Eycki »

TheMaster

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #14 on: April 02, 2012, 03:00:12 PM »
Ok, it works now.  But when I choose use 'DocumentActivated' it will execute 4 times when I open a new document or switch to another opened drawing.  Why?
I would like it to be executed only once.  Any suggestions?

The 'DocumentActivated' event does not mean that the active document changed.

It means more simply, that the active document's window has been activated.

Blame that one on Autodesk, because they didn't bother to provide
a rational implementation of an event that actually means that the
active document has been changed.

So, here is the pattern you need to use:

Code - C#: [Select]
  1.  
  2. public static class Class1
  3. {
  4.   // stores the most-recently activated document:
  5.   private static Document active = null;
  6.  
  7.   static Class1
  8.   {
  9.     Application.DocumentManager.DocumentActivated += documentActivated;
  10.   }
  11.  
  12.   static void documentActivated( object sender, DocumentCollectionEventArgs e )
  13.   {
  14.     if( e.Document != active )  // then the active document changed
  15.     {
  16.       active = e.Document;  // update the most-recently activated document
  17.      
  18.       // TODO: Do what needs to be done when active document changes
  19.     }
  20.   }
  21. }
  22.  
  23.  
  24.  

Eycki

  • Guest
Re: Executing a command when switching window (drawing)
« Reply #15 on: April 04, 2012, 07:14:37 AM »
Thank you so much!  This solved my problem  :-)