How to load plugin before a drawing is loaded

How to load plugin before a drawing is loaded

nshupeFMPE3
Advocate Advocate
497 Views
2 Replies
Message 1 of 3

How to load plugin before a drawing is loaded

nshupeFMPE3
Advocate
Advocate

I have the Initialize() method implemented from IExtensionApplication

But if I'm starting AutoCAD from a cold start (its not running and I start it up), my plugin is not loaded until I start opening a drawing. 

This caused me to first handle that "active" document during my initializtion, then create an event to handle future documents that are opened. 

Is there a way to add the event before the first document is opened so I can simplify my code to only adding the event?

public void Initialize()
        {
            //running setup for the current document
            setupDocument(Application.DocumentManager.MdiActiveDocument);
            //running setup for future opened documents
            Application.DocumentManager.DocumentCreated += docColDocCreate;
            Application.DocumentManager.DocumentToBeDestroyed += DocumentManager_DocumentToBeDestroyed;

            
            
            

        }



0 Likes
498 Views
2 Replies
Replies (2)
Message 2 of 3

Ed__Jobe
Mentor
Mentor

Your code needs to implement the IExtensionApplication interface as shown in the code below. Create a new cls, maybe call it myPlugin.cls, then add the following code and add your tasks to the Initialize event. You can use the template found in this thread for some ideas on how to set up your plugin.

 

 

 

//// (C) Copyright 2002-2009 by Autodesk, Inc. 
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted, 
// provided that the above copyright notice appears in all copies and 
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting 
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. 
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC. 
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to 
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

// This line is not mandatory, but improves loading performances
[assembly: ExtensionApplication(typeof(MP.MyPlugin))]

//customise namespace to your requirements.
namespace MP
{

    /// This class is instantiated by AutoCAD once and kept alive for the 
    /// duration of the session. If you don't do any one time initialization 
    /// then you should remove this class.

    public class MyPlugin : IExtensionApplication
    {

        void IExtensionApplication.Initialize()
        {
            // Add one time initialization here
            // One common scenario is to setup a callback function here that 
            // unmanaged code can call. 
            // To do this:
            // 1. Export a function from unmanaged code that takes a function
            //    pointer and stores the passed in value in a global variable.
            // 2. Call this exported function in this function passing delegate.
            // 3. When unmanaged code needs the services of this managed module
            //    you simply call acrxLoadApp() and by the time acrxLoadApp 
            //    returns  global function pointer is initialized to point to
            //    the C# delegate.
            // For more info see: 
            // http://msdn2.microsoft.com/en-US/library/5zwkzwf4(VS.80).aspx
            // http://msdn2.microsoft.com/en-us/library/44ey4b32(VS.80).aspx
            // http://msdn2.microsoft.com/en-US/library/7esfatk4.aspx
            // as well as some of the existing AutoCAD managed apps.

            // Initialize your plug-in application here
        }

        void IExtensionApplication.Terminate()
        {
            // Do plug-in application clean up here
        }

    }

}

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 3

ActivistInvestor
Mentor
Mentor

Your extension must be loaded in order for your Initialize() method to be called by AutoCAD.

 

So it depends on how your extension is being loaded. If it is being loaded automatically at startup then your Initialize() method should be called before any document is opened.

 

0 Likes