Community
public void Initialize()
{
try
{
// Handle idle event for any setup functions, then remove it after
try
{
MessageBox.Show("Initializing Jacobs Tools");
Application.Idle -= LoadRibbonMenuOnIdle;
}
catch (System.Exception ex)
{
// No need to deal with error. Will only throw error if there was no event to remove;
}
Application.Idle += LoadRibbonMenuOnIdle;
// Further initialization tasks....
}
}
private void LoadRibbonMenuOnIdle(object sender, EventArgs e)
{
MessageBox.Show("Running LoadRibbonMenuOnIdle method");
var ribbonControl = Autodesk.Windows.ComponentManager.Ribbon;
if (ribbonControl != null && ribbonControl.Tabs != null)
{
// Load custom ribbon / menu
SetUpRibbon();
// Ribbon now loaded. Add in an event to check when the user has changed the worksapce
// If they have, we need to reload the ribbon again as it will
// have got removed when the ribbon was rebuilt during the workspace change
Application.SystemVariableChanged += (o, w) =>
{
// This is checking if the user changed the workspace
if (w.Name.ToUpper() == "WSCURRENT")
{
// Load Ribbon/Menu again
SetUpRibbon();
}
};
MessageBox.Show("Removing LoadRibbonMenuOnIdle from the Idle Event");
Application.Idle -= LoadRibbonMenuOnIdle;
}
}
private static void SetUpRibbon()
{
var ribbonControl = ComponentManager.Ribbon;
var tab = ribbonControl.FindTab("Jacobs Tools");
if (tab != null)
{
// Tab already exists. This function shouldnt be running again. Remove the event and exit function
MessageBox.Show("Tab already exists.");
//Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSetCreated -= LoadRibbonMenuOnIdle;
return;
}
MessageBox.Show("Starting to create ribbon");
// Create tab
tab = new RibbonTab();
tab.Title = "Jacobs Tools";
tab.Id = tab.Title;
ribbonControl.Tabs.Add(tab);
var panelSources = new Dictionary<string, RibbonPanelSource>();
var contentLabGroupName = "Content Lab";
var settingsGroupName = "Settings";
var helpGroupName = "Help";
var defaultHelpUrl = new Uri(Path.Combine(Web.HelpUrl, "autocad-addin"));
// Button data
var buttonDataList = new[]
{
new ButtonData {
Name = "Content Lab",
Image = Resources.browser,
PanelName = contentLabGroupName,
Handler = new ContentBrowserCommandHandler(),
HelpSource = defaultHelpUrl,
},
// More buttons created.....
};
foreach (var buttonData in buttonDataList)
{
// Get or create panel source
RibbonPanelSource panelSource;
if (!panelSources.ContainsKey(buttonData.PanelName))
{
panelSource = new RibbonPanelSource
{
Title = buttonData.PanelName
};
panelSources[buttonData.PanelName] = panelSource;
}
else
{
panelSource = panelSources[buttonData.PanelName];
}
// Create button
var button = new RibbonButton();
button.Name = buttonData.Name;
button.Text = button.Name;
button.Description = buttonData.Tooltip;
button.ShowText = true;
button.ShowImage = true;
button.HelpSource = buttonData.HelpSource;
if (buttonData.Image != null)
button.LargeImage = Misc.BitmapToImageSource(buttonData.Image);
button.Size = RibbonItemSize.Large;
button.CommandHandler = buttonData.Handler;
button.Orientation = System.Windows.Controls.Orientation.Vertical;
// Add button to panel
panelSource.Items.Add(button);
}
// Add panels to tab
foreach (var panelSource in panelSources.Values)
tab.Panels.Add(new RibbonPanel
{
Source = panelSource
});
MessageBox.Show("Ribbon Loaded.");
}