.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to arrange custom buttons in custom ribbon?

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
luckyboy82292
221 Views, 4 Replies

How to arrange custom buttons in custom ribbon?

Hey there!

I have created a custom ribbon but now I need to arrange the buttons as I need but it doesn't fit well. So is there any way to fix the size and arrange it as we need. I want to arrange the buttons as shown in the below SS:

Screenshot 2024-12-27 101124.png

 

 

 

 

 

 

 

 

 

But mine look like this:

Screenshot 2024-12-27 101624.png

 

Edit:

I forgot to share the code, so here is the code:

private void CreateRibbon()
{
    try
    {
        RibbonControl ribCntrl = Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
        if (ribCntrl == null) return;

        var existingTab = ribCntrl.Tabs.FirstOrDefault(t => t.Id == MY_TAB_ID);
        if (existingTab != null && existingTab.IsActive) return;

        if (existingTab != null)
        {
            ribCntrl.Tabs.Remove(existingTab);
        }

        RibbonTab ribTab = new RibbonTab { Title = "Advanced Tools", Id = MY_TAB_ID };
        ribCntrl.Tabs.Add(ribTab);

        AddSimplePanel(ribTab, "CAD Tools");

        if (wasActive)
            ribTab.IsActive = true;

        ribTab.Activated += RibTab_Activated;
        ribTab.Deactivated += RibTab_Deactivated;
    }
    catch (Exception ex)
    {
        Application.DocumentManager.MdiActiveDocument?.Editor.WriteMessage($"\nError creating ribbon tab: {ex.Message}");
    }
}

private void AddSimplePanel(RibbonTab tab, string panelTitle)
{
    RibbonRowBreak rbrk = new RibbonRowBreak();
    RibbonSubPanelSource rspl = new RibbonSubPanelSource();
    RibbonPanelSource panelSource = new RibbonPanelSource { Title = panelTitle };
    RibbonPanel ribPanel = new RibbonPanel { Source = panelSource };
    tab.Panels.Add(ribPanel);

    RibbonRowPanel rowPanel = new RibbonRowPanel();
    panelSource.Items.Add(rowPanel);

    var buttons = new[]
    {
        ("Command 1", "COMMAND1 "),
        ("Command 2", "COMMAND1 "),
        ("Command 3", "COMMAND1 "),

    };
    int count = 0;
    int index = 0;
    foreach (var (text, command) in buttons)
    {
        var button = new RibbonButton
        {
            Text = text,
            ShowText = true,
            Orientation = System.Windows.Controls.Orientation.Horizontal,
            Size = RibbonItemSize.Standard,
            CommandParameter = command,

        };
        button.CommandHandler = new RelayCommand();
        rowPanel.Items.Add(button);
    }
}
4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: luckyboy82292

You need to use RibbonRowBreak, RibbonRowPanel... properly. For example, if I want to have a Panel hold 9 command buttons in 3 rows, and each row with 3 buttons, I can have code as shown below:

using Autodesk.AutoCAD.Ribbon;
using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AcadMicsTests
{
    public class MyRibbon
    {
        private readonly RibbonControl _ribbonControl;
        private const string TAB_ID = "MY_CUSTOM_RIBBON_TAB";

        public MyRibbon()
        {
            _ribbonControl = ComponentManager.Ribbon;
        }

        public void CreateMyTab()
        {
            if (_ribbonControl==null)
            {
                throw new InvalidOperationException(
                    "Ribbon menu is not available.");
            }

            if (_ribbonControl.FindTab(TAB_ID) != null) return;

            BuildTab();
        }

        private void BuildTab()
        {
            var tab=new RibbonTab();
            tab.Title = "My Custom Tab";
            tab.Id= TAB_ID;
            AddRibbonPanelToTab(tab);
            _ribbonControl.Tabs.Add(tab);
            _ribbonControl.ActiveTab = tab;
        }

        private void AddRibbonPanelToTab(RibbonTab tab)
        {
            var source = new RibbonPanelSource { Title = "Custom Commands", Id = "MY_CUSTOM_COMMANDS" };
            var panel = new RibbonPanel { Source = source };

            RibbonRowPanel rowPanel;

            rowPanel = CreateRibbonRowPanel(
                new []
                {
                    new RibbonCommandButton{Text="Xxxxxx1", ShowText=true },
                    new RibbonCommandButton{Text="Xxxxxx2", ShowText=true },
                    new RibbonCommandButton{Text="Xxxxxx3", ShowText=true },
                });
            source.Items.Add(rowPanel);

            source.Items.Add(new RibbonRowBreak());

            rowPanel = CreateRibbonRowPanel(
                new []
                {
                    new RibbonCommandButton{Text="Yyyyyy1", ShowText = true},
                    new RibbonCommandButton{Text="Yyyyyy2", ShowText=true },
                    new RibbonCommandButton{Text="Yyyyyy3", ShowText = true},
                });
            source.Items.Add(rowPanel);

            source.Items.Add(new RibbonRowBreak());

            rowPanel = CreateRibbonRowPanel(
                new[]
                {
                    new RibbonCommandButton{Text="Zzzzzz1", ShowText=true },
                    new RibbonCommandButton{Text="Zzzzzz2", ShowText=true },
                    new RibbonCommandButton{Text="Zzzzzz3", ShowText=true },
                });
            source.Items.Add(rowPanel);

            tab.Panels.Add(panel);
        }

        private RibbonRowPanel CreateRibbonRowPanel(IEnumerable<RibbonCommandButton> buttons)
        {
            var rowPanel=new RibbonRowPanel();
            foreach (var btn in buttons)
            {
                rowPanel.Items.Add(btn);
            }
            return rowPanel;
        }
    }
}

 

        [CommandMethod("MyRibbonTab")]
        public static void AddMyCustomRibbonTab()
        {
            try
            {
                var myRibbon= new MyRibbon();
                myRibbon.CreateMyTab();
            }
            catch (System.Exception ex)
            {
                CadApp.ShowAlertDialog($"Error:\n{ex.Message}");
            }
        }

 

Here is the picture showing the created ribbon tab:

normanyuan_0-1735318220759.png

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5
luckyboy82292
in reply to: norman.yuan

This worked but what I need is to set the property manually like Height, Width and position etc. But change in these properties doesn't effect at all.

Message 4 of 5

Check out the official format for Autodesk Plugins (.bundle ).  You can build the UI for your custom tools right in CAD with the CUI command and use the .cuix file for your UI, keeping it separate from your C#.  Super convenient and easy to manage.  There's nice extras, like you can assign Extended Help File custom .xaml to the buttons (in the cuix file) for fancy dropdown tooltips on hover that can include images, etc.  It's pretty nice, really.

Message 5 of 5

 


@luckyboy82292 wrote:

This worked but what I need is to set the property manually like Height, Width and position etc. But change in these properties doesn't effect at all.


The ribbon only supports predefined button sizes, but not custom sizes, so setting the height and width properties of a RibbonItem will not work. That applies to both custom ribbon content you create and add yourself via code, as well as ribbon content created using the CUI.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report