I am trying to build a simple plugin for AutoCAD 2020 using .NET.
I would like to run the HelloWorld() command by clicking on a ribbon on a custom tab. The command is the following:
[CommandMethod("HelloWorld")]
public void HelloWorld()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("Hello!");
}
I have the following code to create a new tab and a ribbon to run the command:
[CommandMethod("testmyRibbon", CommandFlags.Transparent)]
public void Testme()
{
RibbonControl ribbon = ComponentManager.Ribbon;
if (ribbon != null)
{
RibbonTab rtab = ribbon.FindTab("TESTME");
if (rtab != null)
{
ribbon.Tabs.Remove(rtab);
}
rtab = new RibbonTab();
rtab.Title = "TEST ME";
rtab.Id = "Testing";
//Add the Tab
ribbon.Tabs.Add(rtab);
addContent(rtab);
}
}
static void addContent(RibbonTab rtab)
{
rtab.Panels.Add(AddOnePanel());
}
static RibbonPanel AddOnePanel()
{
RibbonCommandButton rb;
RibbonPanelSource rps = new RibbonPanelSource();
rps.Title = "Test One";
RibbonPanel rp = new RibbonPanel();
rp.Source = rps;
//Create a Command Item that the Dialog Launcher can use,
// for this test it is just a place holder.
RibbonButton rci = new RibbonButton();
rci.Name = "TestCommand";
//assign the Command Item to the DialgLauncher which auto-enables
// the little button at the lower right of a Panel
rps.DialogLauncher = rci;
rb = new RibbonCommandButton();
rb.Name = "Hello World";
rb.ShowText = true;
rb.Text = "Hello World";
rb.Macro = "^C^CHELLOWORLD";
//Add the Button to the Tab
rps.Items.Add(rb);
return rp;
}
Running HELLOWORLD on the command line works. And after running TESTMYRIBBON, a new tab called "TEST ME" is created, and a button with the text "Hello World" appears on the "Test One" tab group. However, once I click the button, nothing happens. No message appears on the editor. Am I using RibbonCommandButton wrong? Is there another easy way for this?
I have read answers from a few years ago, but most of them do not use the RibbonCommandButton and resort to creating Macros, which I would like to avoid.
Thank you!
Solved! Go to Solution.
Solved by christian.hestnes. Go to Solution.
You could try to use a button with a commandhandler instead of a macro.
RibbonButton bt = new RibbonButton();
bt.Size = RibbonItemSize.Large;
bt.Text = "VA_Layout";
bt.Orientation = System.Windows.Controls.Orientation.Vertical;
bt.ShowText = true;
bt.CommandParameter = "NyVALayout "; //REMEMBER: space after commandname
bt.CommandHandler = new AdskCommandHandler();
ribPanelSource.Items.Add(bt);
class AdskCommandHandler : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
//is from Ribbon Button
RibbonButton ribBtn = parameter as RibbonButton;
if (ribBtn != null)
{
//execute the command
acApp.DocumentManager.MdiActiveDocument
.SendStringToExecute(
(string)ribBtn.CommandParameter, true, false, true);
}
}
}
Hello Christian. Your solution worked well, thank you very much. I'll stick to your solution instead of `RibbonCommandButton`.
Still, I could not understand why my code wouldn't work. I created a "Run Script..." button on my CUI and it works with "^C^Chelloworld", but for some reason `RibbonCommandButton` doesn't. It would be nice if Autodesk had some examples of this being used, because I feel that I am missing some step.
Thanks again Christian!
Can't find what you're looking for? Ask the community or share your knowledge.