TheSwamp

Code Red => .NET => Topic started by: Peter Jamtgaard on December 03, 2012, 04:54:57 PM

Title: Run function in Each Document
Post by: Peter Jamtgaard 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=
Title: Re: Run function in Each Document
Post by: zoltan 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.