print a message on load to public the name of the command

print a message on load to public the name of the command

J-Rocks
Collaborator Collaborator
2,814 Views
12 Replies
Message 1 of 13

print a message on load to public the name of the command

J-Rocks
Collaborator
Collaborator

Hi guys ,

 

I am trying to write a simple codes to print a message at the command line after loading the .dll file to inform the user about the command name , but 

I failed and my codes bring an error message .

 

Can someone help me please ?

 

Thanks in advance .

 

namespace AutoCAD_Test
{
    public class MyCommands
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        ed.WriteMessage("\nCommand name is <Test> :");  

        [CommandMethod("Test")]
        public static void one()
        {
        }
    }
}
0 Likes
Accepted solutions (2)
2,815 Views
12 Replies
Replies (12)
Message 2 of 13

Ajilal.Vijayan
Advisor
Advisor
0 Likes
Message 3 of 13

J-Rocks
Collaborator
Collaborator

Hi,

 

That codes shows the message after calling the command name and that's not what I am after .

 

I need to see a message telling the user about the name of the program to run straight after calling the load command NETLOAD .

 

Thank you.

0 Likes
Message 4 of 13

Ed__Jobe
Mentor
Mentor

It appears that you're using the plugin template and it has a MyPlugin class. Put your WriteMessage statement in the MyPlugin.Initialize event. It is already created for you.

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 5 of 13

J-Rocks
Collaborator
Collaborator

@eljobe wrote:

It appears that you're using the plugin template and it has a MyPlugin class. Put your WriteMessage statement in the MyPlugin.Initialize event. It is already created for you.


Correct I am using the AutoCAd plugin and usually I delete the MyPlugin and keep the MyCommands file to work only on one file .

 

I did not get your point , can you please clarify it more ?

 

Thank you

0 Likes
Message 6 of 13

Ed__Jobe
Mentor
Mentor
Accepted solution

If you've deleted it, you can add a new class file to your project and paste this code into it. Then put your code where it says "Initialize your plug-in application here". Don't forget to change the namespace to match what you have in MyCommands.

// (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(TID_.MyPlugin))]

namespace TID_
{

    /// 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

Message 7 of 13

J-Rocks
Collaborator
Collaborator

Nice , that works very well .

 

Thank you so much .

 

What is for we can use the Terminate function in that inherited interface IExtensionApplication to MyPlugin ?

If you have any example , it would be great .

 

Kind regards,

John

0 Likes
Message 8 of 13

dgorsman
Consultant
Consultant

Thats useful when dealing with resource management, but isn't all that common with more mundane, day-to-day functions.

 

Consider that you might be connected to a database in real-time.  The initialize method would establish the connection, do some initial auditing, record locking, and so on.  When you are finished you may want to ensure the connection is closed/released, possibly signalling other applications that changes are complete and they are clear to use it.

 

Another example may be a custom licensing method.  The initialize method would check out a license if available, or lock out functions if not.  When done any checked out license would be released.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 9 of 13

J-Rocks
Collaborator
Collaborator

Thank you dgorsman for you reply .

 

Can I say that the Initialize method is to start immediately as soon as the program being loaded and just before the command name is called ?

 

Also , can I say that the Terminate method works like error handler intervenes to assure closing , ending a process .... etc ?

 

Kind regard.

0 Likes
Message 10 of 13

_gile
Consultant
Consultant

Hi,

 

Initialize() method is run as soon as the app is loaded, for some tasks, it will be a better way to wait for the AutoCAD initialization complete which can be done using the Application.Idle event.

 

Anyway, in my opinion, the best way to uderstand this is to make some simple tests by yourself:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Windows.Forms;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: ExtensionApplication(typeof(ExtensionApplicationTest.TestApp))]

namespace ExtensionApplicationTest
{
    public class TestApp : IExtensionApplication
    {
        public void Initialize()
        {
            MessageBox.Show("Initialize", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
            AcAp.Idle += OnIdle;
        }

        void OnIdle(object sender, EventArgs e)
        {
            AcAp.Idle -= OnIdle;
            MessageBox.Show("Idle", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public void Terminate()
        {
            MessageBox.Show("Terminate", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

 

 

 

Depending on how the application loads, you'd see when Initialize and OnIdle occur (you'd also see that Terminate never occurs in AutoCAD...).

 

You can use the attached plugin to test what happens with the Autoloader mechanism (unblock the ZIP file if neessary and extract the ExtensionApplicationTest.bundle folder in %appdata%\Autodesk\ApplicationPlugins).

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 13

J-Rocks
Collaborator
Collaborator

Thank you gile for the practical example , its clear enough to me but I still wonder when the Terminate method would not run ! and since that is not needed, why it is included in the Interface then in the class ?

 

I have added a few codes to your example to test that out and I released that the Test command is unknown in Autocad hahaha strange to me , can you please tell me why ?

 

Thank you.

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Windows.Forms;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: ExtensionApplication(typeof(ExtensionApplicationTest.TestApp))]

namespace ExtensionApplicationTest
{
    public class TestApp : IExtensionApplication
    {
        public void Initialize()
        {
            MessageBox.Show("Initialize", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
            AcAp.Idle += OnIdle;
            AcAp.EnterModal += AnotherMessage;
        }
         void OnIdle(object sender, EventArgs e)
        {
            AcAp.Idle -= OnIdle;
            MessageBox.Show("Idle", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
         void AnotherMessage(object s, EventArgs e)
         {
             AcAp.EnterModal -= AnotherMessage;
             MessageBox.Show("Another test is here .", "2nd");
         }
         
         public void Terminate()
         {
             MessageBox.Show("Terminate", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }

         [CommandMethod("Test")]
         public void justTest()
         {
             MessageBox.Show("Just a simple test.");
         }
    }
}
0 Likes
Message 12 of 13

_gile
Consultant
Consultant
Accepted solution

The Terminate() method is run after AutoCAD closed (that's why I said it "never occurs in AutoCAD").

As dgorsman said, it can be use to release external resources, close a connection to a database...

 

I cannot say why the TEST command is unknown for you (it works fine here).

You can try to remove the ExtensionApplication attribute after the using directives ("This line is not mandatory, but improves loading performances").

Or add a CommandClass one pointing on a Commands class which may contain the commands methods (this can be prefereably done in a separate file or in the same one as below):

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Windows.Forms;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

// These lines are not mandatory, but improve loading performances [assembly: ExtensionApplication(typeof(ExtensionApplicationTest.TestApp))] [assembly: CommandClass(typeof(ExtensionApplicationTest.Commands))] namespace ExtensionApplicationTest { public class TestApp : IExtensionApplication { public void Initialize() { MessageBox.Show("Initialize", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); AcAp.Idle += OnIdle; } void OnIdle(object sender, EventArgs e) { AcAp.Idle -= OnIdle; MessageBox.Show("Idle", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } public void Terminate() { MessageBox.Show("Terminate", "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } } public class Commands { [CommandMethod("Test")] public void justTest() { MessageBox.Show("Just a simple test."); } } }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 13

J-Rocks
Collaborator
Collaborator

Thank you gile that works like a charm and your explanations with codes turns the shade into light .Smiley Wink 

0 Likes