Message 1 of 3
Run IExternalCommand by pressing custom RibbonButton
Not applicable
01-28-2021
12:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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:
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.
Developer Advocacy and Support +