Add new PushButton when I click pushbutton

Add new PushButton when I click pushbutton

qoreodlf37
Enthusiast Enthusiast
277 Views
2 Replies
Message 1 of 3

Add new PushButton when I click pushbutton

qoreodlf37
Enthusiast
Enthusiast

Hi, I have a question.

I created tab, pannel and some buttons.

qoreodlf37_0-1720402158218.png

 

Is it possible to create new pushbutton (Button3, Button4...) when I click  my "Create" button?

 

0 Likes
Accepted solutions (1)
278 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

Yes, almost certainly. You just need access to the RibbonPanel object, create new PushButtonData objects, and call AddPushButton. Why would you want to do this? Is your goal to confuse your end user? Another possible approach would be to create all required buttons up front and only enable them when they are needed.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

Mohamed_Arshad
Advisor
Advisor
Accepted solution

Hi @qoreodlf37 

 

    There is no direct method to achieve that functionality, But we have a workaround to achieve that.

1. Create all Button in the IExtetnal Application
2. Add "AdWindows" Dll and Hide Button2 and Button3
3. In Create Command UnHide the Button2 and Button3


Reference Video
Button Creation.gif

Reference Code (IExternalApplication)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;

using WD = Autodesk.Windows;

namespace PushButtonCreation
{
    public class App : IExternalApplication
    {
        #region Global Variables

        private static readonly string TabName = "Demo";
        private static readonly string PanelName = "My Ribbon Panel";

        #endregion

        public Result OnShutdown(UIControlledApplication application) => Result.Succeeded;

        public Result OnStartup(UIControlledApplication application)
        {

            string dllLoc = Assembly.GetExecutingAssembly().Location;

            ///Create Tab
            application.CreateRibbonTab(TabName);

            ///Create Panel
            RibbonPanel panel = application.CreateRibbonPanel(TabName, PanelName);

            /// Create PushButtonData
            PushButtonData createButtonData = new PushButtonData(
                "createBtn",
                "Create",
                dllLoc,
                "PushButtonCreation.CreateCmd");

            PushButton pushButton = panel.AddItem(createButtonData) as PushButton;

            PushButtonData button_2_Data = new PushButtonData(
               "Btn_2",
               "Button_2",
               dllLoc,
               "PushButtonCreation.Button_2");

            PushButton pushButton_2 = panel.AddItem(button_2_Data) as PushButton;

            PushButtonData button_3_Data = new PushButtonData(
               "Btn_3",
               "Button_3",
               dllLoc,
               "PushButtonCreation.Button_3");

            PushButton pushButton_3 = panel.AddItem(button_3_Data) as PushButton;

            HideButtons();

            return Result.Succeeded;
        }

        public void HideButtons()
        {
            foreach (WD.RibbonTab tab in WD.ComponentManager.Ribbon.Tabs)
            {
                if (tab.Name == TabName)
                {
                    foreach (WD.RibbonPanel panel in tab.Panels)
                    {
                        if (panel.Source.Name == PanelName)
                        {
                            foreach (WD.RibbonItem item in panel.Source.Items)
                            {
                                if (item.Text == "Button_2" || item.Text == "Button_3")
                                {
                                    item.IsVisible = false;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


Create Command (IExternalCommand)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using WD = Autodesk.Windows;
using Autodesk.Windows;
using System.Reflection;

namespace PushButtonCreation
{
    [Transaction(TransactionMode.Manual)]
    public class CreateCmd : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            ShowButtons();
            return Result.Succeeded;
        }

        public void ShowButtons()
        {
            foreach (RibbonTab tab in WD.ComponentManager.Ribbon.Tabs)
            {

                if (tab.Name == "Demo")
                {
                    foreach (WD.RibbonPanel panel in tab.Panels)
                    {
                        if (panel.Source.Name == "My Ribbon Panel")
                        {
                            foreach (WD.RibbonItem item in panel.Source.Items)
                            {
                                if (item.Text == "Button_2" || item.Text == "Button_3")
                                {
                                    item.IsVisible = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Kindly check the attached project for additional reference.

 

 


Mohamed Arshad K
Software Developer (CAD & BIM)