Don't forget to reference the required namespaces:
using Autodesk.Max;
using Autodesk.Max.Plugins;
First lets define our action item:
internal class MyAction : ActionItem
{
const string ActionName = "My Action";
public override string ButtonText
{
get { return ActionName; }
}
public override string CategoryText
{
get { return Plugin.ProjectName; }
}
public override string DescriptionText
{
get { return ActionName + " " + Plugin.ProjectName; }
}
public override bool ExecuteAction()
{
// execute me
return true;
}
public override IMaxIcon Icon
{
get { return null; }
}
public override int Id
{
get { return 1; }
}
public override bool IsChecked
{
get { return false; }
}
public override bool IsEnabled
{
get { return true; }
}
public override bool IsItemVisible
{
get { return true; }
}
public override string MenuText
{
get { return "&" + ActionName + "..."; }
}
}
Then we declare some class variables that we will keep track of. These are stored inside out IPlugin-derived class.
IIMenu menu = null;
IIMenuItem menuItem = null;
IIMenuItem menuItemMyAction = null;
uint idActionTable = 0;
IActionTable actionTable = null;
IActionCallback actionCallback = null;
We need to declare one additional 'dummy' class to enable our action to execute:
public class ZKActionCallback : ActionCallback
{
public override bool ExecuteAction( int id )
{
// Do nothing...
return true;
}
}
Inside the IPlugin.Initialize() method we call MenusAndActionsRegister(), which is defined as follows:
void MenusAndActionsRegister()
{
IIActionManager actionManager = global.COREInterface.ActionManager;
IIMenuManager menuManager = global.COREInterface.MenuManager;
IActionTable actionTable = null;
// Set up global actions
{
idActionTable = (uint)actionManager.NumActionTables;
string actionTableName = ProjectName + " Actions";
actionTable = Global.ActionTable.Create( idActionTable, 0, ref actionTableName );
actionTable.AppendOperation( new ActionItems.MyAction() );
actionCallback = new MyActionCallback();
actionManager.RegisterActionTable( actionTable );
actionManager.ActivateActionTable( actionCallback as ActionCallback, idActionTable );
}
// Set up menu
{
this.menu = menuManager.FindMenu( ProjectName );
if( this.menu != null )
{
menuManager.UnRegisterMenu( menu );
global.ReleaseIMenu( menu );
this.menu = null;
}
this.menuItemZookeeper = global.IMenuItem;
this.menuItemZookeeper.Title = ProjectName;
// Main menu
{
this.menu = global.IMenu;
this.menu.Title = ProjectName;
menuManager.RegisterMenu( menu, 0 );
// Launch option
{
this.menuItemMyAction = global.IMenuItem;
this.menuItemMyAction.Title = "&MyAction";
this.menuItemMyAction.ActionItem = actionTable[0];
menu.AddItem( this.menuItemMyAction, -1 );
}
}
this.menuItem.SubMenu = menu;
menuManager.MainMenuBar.AddItem( this.menuItem, -1 );
global.COREInterface.MenuManager.UpdateMenuBar();
}
}
Finally, when unloading our plugin it is preferrable that we also unregister all the menus. We do this in the IPlugin.Cleanup() method by calling the following function:
void MenusAndActionsUnregister()
{
if( this.actionTable != null )
{
this.Global.COREInterface.ActionManager.DeactivateActionTable( actionCallback, idActionTable );
}
// Clean up menu
if( this.menu != null )
{
this.Global.COREInterface.MenuManager.UnRegisterMenu( menu );
this.Global.ReleaseIMenu( menu );
this.Global.ReleaseIMenuItem( menuItemMyAction );
this.Global.ReleaseIMenuItem( menuItem );
this.menu = null;
this.menuItem = null;
}
}
And we're done!