using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.Windows;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(RibbonToggleButtonsTest.MyCommands))]
namespace RibbonToggleButtonsTest
{
public class MyCommands
{
[CommandMethod("AddTab", CommandFlags.Modal)]
public void AddTab()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
RibbonControl ribbonControl
= ComponentManager.Ribbon;
if (ribbonControl.FindTab("ID_TestRibbonTab") != null)
return;
RibbonTab tab = new RibbonTab();
tab.Title = "Test Ribbon";
tab.Id = "ID_TestRibbonTab";
ribbonControl.Tabs.Add(tab);
RibbonPanelSource srcPanel
= new RibbonPanelSource();
srcPanel.Title = "Panel1";
srcPanel.Id = "ID_SrcPanel";
RibbonPanel panel = new RibbonPanel();
panel.Source = srcPanel;
RibbonToggleButton button1 = new RibbonToggleButton
{
Text = "Button 1",
Id = "id_button1",
ShowText = true,
IsChecked = true,
CommandParameter = "",
CommandHandler = new ToggleButtonCmdHandler1()
};
srcPanel.Items.Add(button1);
RibbonToggleButton button2 = new RibbonToggleButton
{
Text = "Button 2",
Id = "id_button2",
ShowText = true,
IsChecked = false,
CommandParameter = "",
CommandHandler = new ToggleButtonCmdHandler2()
};
srcPanel.Items.Add(button2);
tab.Panels.Add(panel);
tab.IsActive = true;
}
}
public class ToggleButtonCmdHandler1 : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
RibbonToggleButton button1 = parameter as RibbonToggleButton;
RibbonControl ribbonControl = ComponentManager.Ribbon;
RibbonToggleButton button2 = ribbonControl.FindItem("id_button2", true) as RibbonToggleButton;
if (button2 != null)
{
button2.IsChecked = !button1.IsChecked;
}
}
}
public class ToggleButtonCmdHandler2 : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
RibbonToggleButton button2 = parameter as RibbonToggleButton;
RibbonControl ribbonControl = ComponentManager.Ribbon;
RibbonToggleButton button1 = ribbonControl.FindItem("id_button1", true) as RibbonToggleButton;
if (button1 != null)
{
button1.IsChecked = !button2.IsChecked;
}
}
}
}