Author Topic: Run function in Each Document  (Read 2002 times)

0 Members and 1 Guest are viewing this topic.

Peter Jamtgaard

  • Guest
Run function in Each Document
« on: December 03, 2012, 04:54:57 PM »
Since .NET is a application level language.

Is there a simple way to have a .net function run whenever a document is opened?

I know initialize runs once when Autocad starts but I want a function to run in each document.

I have successfully done it with an event, but I figure there is an easier way to do it.

Thanks in advance

P=

zoltan

  • Guest
Re: Run function in Each Document
« Reply #1 on: December 03, 2012, 05:11:33 PM »
You can hook into the DocumentCollection.DocumentCreated event that will fire whenever a new document is added or opened.

Code - C#: [Select]
  1.         public static void HookApplicationEvents()
  2.         {
  3.             Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(DocumentManager_DocumentCreated);
  4.         }
  5.  
  6.         public static void UnhookApplicationEvents()
  7.         {
  8.             Application.DocumentManager.DocumentCreated -= new DocumentCollectionEventHandler(DocumentManager_DocumentCreated);
  9.         }
  10.  
  11.         private static void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
  12.         {
  13.             // Do some stuff.
  14.         }
  15.