Run IExternalCommand by pressing custom RibbonButton

Run IExternalCommand by pressing custom RibbonButton

Anonymous
Not applicable
719 Views
2 Replies
Message 1 of 3

Run IExternalCommand by pressing custom RibbonButton

Anonymous
Not applicable

Hello!

I apologize in advance for the question, which had already been raised, but I can't solve my problem.
I want make custom tab with button to run my external command:

 

public class CopySheets : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
         UIApplication uiapp = commandData.Application;
         UIDocument uidoc = uiapp.ActiveUIDocument;
         Document doc = uidoc.Document;

         using (CopySheetsForm csf = new CopySheetsForm(doc, uiapp))
         {
              csf.ShowDialog();
         }

         return Result.Succeeded;
    }
}

 


I was following this post https://thebuildingcoder.typepad.com/blog/2009/12/custom-ribbon-tab.html to write my IExternalApplication:

 

class App : IExternalApplication
{
    public Result OnStartup(UIControlledApplication a)
    {
        adWin.RibbonControl ribbon = adWin.ComponentManager.Ribbon;
        RibbonControl ribbonControl = adWin.ComponentManager.Ribbon;

        RibbonTab tab = new RibbonTab();
        tab.Title = "MyPlugins";
        tab.Id = "MYPLUGINS_TAB_ID";
        ribbon.Tabs.Add(tab);

        Autodesk.Windows.RibbonPanelSource srcPanel = new Autodesk.Windows.RibbonPanelSource();
        srcPanel.Title = "Functions";
        Autodesk.Windows.RibbonPanel panel = new Autodesk.Windows.RibbonPanel();
        panel.Source = srcPanel;
        tab.Panels.Add(panel);

        adWin.RibbonButton button = new adWin.RibbonButton();
        button.CommandHandler = new AdskCommandHandler("ScriptsV01.dll", "ScriptsV01.CopySheets");
        adWin.ComponentManager.UIElementActivated += new EventHandler<adWin.UIElementActivatedEventArgs>(ComponentManager_UIElementActivated);
        panel.Source.Items.Add(button);

        return Result.Succeeded;

    }

    public Result OnShutdown(UIControlledApplication a)
    {
         return Result.Succeeded;
    }
}

 

Then I created class, as in the above example, to be able to run my command:

 

public class AdskCommandHandler : System.Windows.Input.ICommand
{
    string AssemblyName
    {
        get;
        set;
    }

    string ClassName
    {
        get;
        set;
    }

    public AdskCommandHandler(string assemblyName, string className)
    {
        AssemblyName = assemblyName;
        ClassName = className;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object a)
    {
        return true;
    }

    public void Execute(object a)
    {
        System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(
                AssemblyName);

        IExternalCommand command
              = assembly.CreateInstance(
                ClassName) as IExternalCommand;

        ExternalCommandData commandData = null;
        string message = string.Empty;
        ElementSet elements = null;

        command.Execute(commandData,
                ref message, elements);
    }
}

 

The promblem is, when I press button, it returns an error:

123.png
I saw a similar problem here: https://forums.autodesk.com/t5/revit-api-forum/implement-icommand-with-actual-externalcommanddata-no...
but the solution didn't help.
So how can I run my command by pressing custom button? Any help you could provide would be much appreciated.

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

jeremy_tammik
Alumni
Alumni

Welcome to the Revit API.

 

Please simply work through the standard getting started material and all your questions will be answered:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#2

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

TripleM-Dev.net
Advisor
Advisor

Hi,

 

Download the Revit SDK, it has loads of sample also for making you're own RibbonTab with command buttons.

See the sample "Ribbon". About the Revit SDK 

 

- Michel