I'm using this Visual Studio Extension
Thankfully the starting template is very small and simple. But I wanted to ask some clarification on these attributes.
[assembly: CommandClass(typeof(WarmBoard.NET.MyCommands))]
[assembly: ExtensionApplication(typeof(WarmBoard.NET.PluginExtension))]
They are at the top of the command file like so:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(WarmBoard.NET.MyCommands))]
[assembly: ExtensionApplication(typeof(WarmBoard.NET.PluginExtension))]
namespace WarmBoard.NET
{
public class MyCommands
{
// The CommandMethod attribute can be applied to any public member
// function of any public class.
// The function should take no arguments and return nothing.
// If the method is an intance member then the enclosing class is
// intantiated for each document. If the member is a static member then
// the enclosing class is NOT intantiated.
//
// NOTE: CommandMethod has overloads where you can provide helpid and
// context menu.
// Modal Command with localized name
[CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
public void MyCommand() // This method can have any name
{
// Put your command code here
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Autodesk.AutoCAD.EditorInput.Editor ed;
if (doc != null)
{
ed = doc.Editor;
ed.WriteMessage("Hello, this is your first command.");
}
}
// Modal Command with pickfirst selection
[CommandMethod("MyGroup", "MyPickFirst", "MyPickFirstLocal", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void MyPickFirst() // This method can have any name
{
PromptSelectionResult result = AcadApp.DocumentManager.MdiActiveDocument.Editor.GetSelection();
if (result.Status == PromptStatus.OK)
{
// There are selected entities
// Put your command using pickfirst set code here
}
else
{
// There are no selected entities
// Put your command code here
}
}
// Application Session Command with localized name
[CommandMethod("MyGroup", "MySessionCmd", "MySessionCmdLocal", CommandFlags.Modal | CommandFlags.Session)]
public void MySessionCmd() // This method can have any name
{
// Put your command code here
}
// LispFunction is similar to CommandMethod but it creates a lisp
// callable function. Many return types are supported not just string
// or integer.
[LispFunction("MyLispFunction", "MyLispFunctionLocal")]
public int MyLispFunction(ResultBuffer args) // This method can have any name
{
// Put your command code here
// Return a value to the AutoCAD Lisp Interpreter
return 1;
}
}
}
My questions are
1. What exactly is the CommandClass Attribute doing? I use CommandMethod for all my command methods, and everything seems to work fine. Does CommandClass do something important or special?
2. Why is the ExtensionApplication attribute in this commands file and not here:
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmBoard.NET
{
public class PluginExtension : IExtensionApplication
{
public void Initialize()
{
// Add your initialization code here
}
public void Terminate()
{
// Add your termination code here
}
}
}
This is a seperate file that actually creates the plugin.
Thanks for any clarification and insight, I'm just trying to learn what I can and get a holistic understanding for the "official" code.
Solved! Go to Solution.
Solved by Ed.Jobe. Go to Solution.
Solved by ActivistInvestor. Go to Solution.
CommandClass and ExtensionApplication are both assembly attributes, which means they can go anywhere. However, as you noted, the ExtensionApplication attribute probably should be in the same file where the IExtensionApplication is.
CommandClass provides a way to speed up the loading process, because when you use it, you are basically saying that the class(es) you apply it to contain commands, and there are no commands in any other classes that do not have the CommandClass attribute applied to them.
If you don't use the CommandClass attribute, AutoCAD's managed runtime will search through every compatible method of every class in your assembly looking for CommandMethod attributes, which depending on how many classes/methods you have, can take a lot longer and slow down the loading and/or startup process.
When you use the CommandClass attribute, AutoCAD only searches through the types that you apply it to. That means, if you use the CommandClass attribute, you must apply it to every class that contains CommandMethods.
You may see solutions where the ExtensionApplication attribute is in a separate class file that implements iExtensionApplication. Usually, it just implements the Initialize and Terminate methods.
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly: ExtensionApplication(typeof(TID_.MyPlugin))]
namespace TID_
{
public class MyPlugin : IExtensionApplication
{
void IExtensionApplication.Initialize()
{
// Do plug-in application initialization here
}
void IExtensionApplication.Terminate()
{
// Do plug-in application clean up here
}
}
}
Can't find what you're looking for? Ask the community or share your knowledge.