I find all commands of my assembly and create a button for each of them. I want to do it through cycle. I can create a ribbon tab and a ribbon tab panel:
// The uic_app is UIControlledApplication instance uic_app.CreateRibbonTab(tab_name); var panel = uic_app.CreateRibbonPanel(tab_name, panel_name); ... var button_data = new PushButtonData(cmd_name, cmd_text, this_assembly_path, cmd_type.FullName); var push_button = panel.AddItem(button_data) as PushButton; ...
But I need to check the existing of them and get them if they already exist. How can I do it?
Solved! Go to Solution.
Hi @Anonymous,
While there are other ways to find some of this info, here's the easiest to get all info:
Imports adWin = Autodesk.Windows ... Private MyRibbon As adWin.RibbonControl ... MyRibbon = Autodesk.Windows.ComponentManager.Ribbon For Each tab As adWin.RibbonTab In MyRibbon.Tabs ... For Each panel As adWin.RibbonPanel In tab.Panels ... etc...
There are .FindTab ,.FindPanel , and .FindItem functions also.
(Note that this method is unsupported by Autodesk, but I've never had any trouble with it when using Revit 2011-2017.)
Cheers,
-Matt
Thank you, Matt.
I try to use your advice, but I have a problem. This is my code (the comment shows where I get exception):
public static void AddButton( UIControlledApplication uic_app, Type cmd_type, Type aviability_type) { try { ResourceManager base_res_mng = new ResourceManager( typeof(Resources)); ResourceManager res_mng = new ResourceManager( cmd_type); /* if command hasn't own resources then use their * default values which are defined inside of * Resources class. */ if (res_mng == null) { res_mng = new ResourceManager(typeof(Resources) ); } string tab_name = base_res_mng.GetString( "_Ribbon_tab_name"); var ribbon = adWin.ComponentManager.Ribbon; var tab = ribbon.FindTab(tab_name); if (tab == null) { tab = new adWin.RibbonTab(); tab.Name = tab_name; tab.Id = tab_name; ribbon.Tabs.Add(tab); } string panel_name = res_mng.GetString( "_Ribbon_panel_name"); var panel = tab.FindPanel(panel_name); if (panel == null) { panel = new adWin.RibbonPanel(); panel.Id = panel_name; tab.Panels.Add(panel); } string this_assembly_path = Assembly .GetExecutingAssembly().Location; // ********************************** // Get localized help file name string help_file = Path.Combine(Path .GetDirectoryName(this_assembly_path), base_res_mng.GetString("_Help_file_name")); string help_topic_id = res_mng.GetString( "_Help_topic_Id"); string cmd_name = cmd_type.Name; string cmd_text = res_mng.GetString("_Button_text"); string cmd_tooltip = res_mng.GetString( "_Button_tooltip_text"); string long_description = res_mng.GetString( "_Button_long_description"); // the button image key name string button_img = "_Button_image"; // the button tooltip image key name string tooltip_img = "_Button_tooltip_image"; Bitmap btn_image = (Bitmap)res_mng.GetObject( button_img); BitmapSource btn_src_img = Imaging .CreateBitmapSourceFromHBitmap( btn_image.GetHbitmap(), IntPtr.Zero, WPF.Int32Rect.Empty, BitmapSizeOptions .FromEmptyOptions()); Bitmap ttp_image = (Bitmap)res_mng.GetObject( tooltip_img); BitmapSource ttp_bitmap_src=Imaging .CreateBitmapSourceFromHBitmap( ttp_image.GetHbitmap(), IntPtr.Zero, WPF.Int32Rect.Empty, BitmapSizeOptions .FromEmptyOptions()); // -------------------------------------------- adWin.RibbonButton button = new adWin.RibbonButton(); button.Name = cmd_name; button.Image = btn_src_img; button.LargeImage = ttp_bitmap_src; button.Id = cmd_name; button.AllowInStatusBar = true; button.AllowInToolBar = true; button.GroupLocation = Autodesk.Private .Windows.RibbonItemGroupLocation.Middle; button.IsEnabled = true; button.IsToolTipEnabled = true; button.IsVisible = true; button.ShowImage = true; button.ShowText = true; button.ShowToolTipOnDisabled = true; button.Text = cmd_text; button.ToolTip = cmd_tooltip; button.MinHeight = 0; button.MinWidth = 0; button.Size = adWin.RibbonItemSize.Large; button.ResizeStyle = adWin .RibbonItemResizeStyles.HideText; button.IsCheckable = true; button.Orientation = System.Windows .Controls.Orientation.Vertical; button.KeyTip = "TBC"; button.Description = long_description; // -------------------------------------------- // I GET THE EXCEPTION HERE: // Object reference not set to an instance of // an object. panel.Source.Items.Add(button); res_mng.ReleaseAllResources(); base_res_mng.ReleaseAllResources(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
I don't understand why I get this exception. I call this method inside of my `OnStartup` method. Did you get such problem?
Thank you.
Hi @Anonymous,
You asked how to find if they exist!
I wouldn't create them that way, considering the Revit API does that already.
Create them using the *fully supported* Revit API as you showed in your initial question.
Cheers,
-Matt
static class Tools { /* Through the using of the tabs_dict * dictionary I try to minimize the exceptions * count. */ static Dictionary<string, string> tabs_dict = new Dictionary<string, string>(); public static void AddButton( UIControlledApplication uic_app, Type cmd_type, Type aviability_type) { ResourceManager base_res_mng = new ResourceManager( typeof(Resources)); ResourceManager res_mng = new ResourceManager( cmd_type); /* if command hasn't own resources then use their * default values which are defined inside of * Resources class. */ if (res_mng == null) { res_mng = new ResourceManager(typeof(Resources) ); } string tab_name = base_res_mng.GetString( "_Ribbon_tab_name"); try { /* Through the using of the tabs_dict * dictionary I try to minimize the exceptions * count. */ if (!tabs_dict.ContainsKey(tab_name)) { uic_app.CreateRibbonTab(tab_name); tabs_dict.Add(tab_name, tab_name); } } catch { } string panel_name = res_mng.GetString( "_Ribbon_panel_name"); RibbonPanel panel = uic_app.GetRibbonPanels( tab_name).FirstOrDefault( n => n.Name.Equals(panel_name, StringComparison .InvariantCulture)); if (panel == null) { panel = uic_app.CreateRibbonPanel(tab_name, panel_name); } string this_assembly_path = Assembly .GetExecutingAssembly().Location; // ********************************** // Get localized help file name string help_file = Path.Combine(Path .GetDirectoryName(this_assembly_path), base_res_mng.GetString("_Help_file_name")); ContextualHelp help = new ContextualHelp( ContextualHelpType.ChmFile, help_file); help.HelpTopicUrl = res_mng.GetString( "_Help_topic_Id"); string cmd_name = cmd_type.Name; string cmd_text = res_mng.GetString("_Button_text"); string cmd_tooltip = res_mng.GetString( "_Button_tooltip_text"); string long_description = res_mng.GetString( "_Button_long_description"); // the button image key name string button_img = "_Button_image"; // the button tooltip image key name string tooltip_img = "_Button_tooltip_image"; PushButtonData button_data = new PushButtonData( cmd_name, cmd_text, this_assembly_path, cmd_type.FullName); button_data.AvailabilityClassName = aviability_type.FullName; PushButton push_button = panel.AddItem( button_data) as PushButton; push_button.ToolTip = cmd_tooltip; Bitmap btn_image = (Bitmap)res_mng.GetObject( button_img); BitmapSource btn_src_img = Imaging .CreateBitmapSourceFromHBitmap( btn_image.GetHbitmap(), IntPtr.Zero, WPF.Int32Rect.Empty, BitmapSizeOptions .FromEmptyOptions()); push_button.LargeImage = btn_src_img; Bitmap ttp_image = (Bitmap)res_mng.GetObject( tooltip_img); BitmapSource ttp_bitmap_src=Imaging .CreateBitmapSourceFromHBitmap( ttp_image.GetHbitmap(), IntPtr.Zero, WPF.Int32Rect.Empty, BitmapSizeOptions .FromEmptyOptions()); push_button.ToolTipImage = ttp_bitmap_src; push_button.LongDescription = long_description; push_button.SetContextualHelp(help); res_mng.ReleaseAllResources(); base_res_mng.ReleaseAllResources(); } }
Err, @Anonymous
The item you marked as a solution didn't show how your tab dictionary was created, so it's only half a solution.
Cheers,
-Matt
This works for me
string tabName = "TabName"; string panelName = "PanelName"; try { a.CreateRibbonTab(tabName); } catch { } List<RibbonPanel> panelList = a.GetRibbonPanels(tabName); RibbonPanel panel = null; foreach (RibbonPanel rp in panelList) { if (rp.Name == panelName) { panel = rp; break; } } if (panel == null) { panel = a.CreateRibbonPanel(tabName, panelName); }
a.CreateRibbonPanel(tabName, panelName);
}
Can't find what you're looking for? Ask the community or share your knowledge.