Create Dockable Window

Github Address

Address

Reference

The Building Coder CSDN Revit Secondary Development Dockable Window Revit Secondary Development - Non-modal Window and Revit Window Focus Switching Rain Flower Pick - Revit Secondary Development ExternalEvent Implement Non-modal Form Revit Secondary Development Entry: Chapter 9 Events

Create WPF Window

  • After adding WPF window, modify usercontrol to Page, F7 cut into background also modify to Page. Also need to reference Autodesk.Revit.UI.IDockablePaneProvider page position use Dockpositon enumeration value to control, select position. Page is created at this time, add UI layout according to needs.
public partial class UserControl1 : Page,Autodesk.Revit.UI.IDockablePaneProvider
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public void SetupDockablePane(DockablePaneProviderData data)
        {
            data.FrameworkElement = this as FrameworkElement;
            data.InitialState = new DockablePaneState
            {
                DockPosition = DockPosition.Bottom
            };
            
        }
  
        
    }

Create Panel (CreatPanel)

 public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
        
        public Result OnStartup(UIControlledApplication application)
        {
            thisApp = this;
            apiUntil = new APIUntil();
            string tabName = "DockPane";

            string PanelName = "DockPane";
            string RegisterName = "Register";
            application.CreateRibbonTab(tabName);
            RibbonPanel architecturePanel = application.CreateRibbonPanel(tabName, PanelName);
            PushButtonData buttonData = new PushButtonData("DockPane", "DockPane", Assembly.GetExecutingAssembly().Location, typeof(ShowPage).FullName);
            PushButton ShowPage = architecturePanel.AddItem(buttonData) as PushButton;
            ShowPage.AvailabilityClassName = typeof(ShowPage).FullName;

            RibbonPanel RegisterPanel = application.CreateRibbonPanel(tabName, RegisterName);
            PushButtonData buttonDataRegisterPage = new PushButtonData("RegisterDockPane", "RegisterDockPane", Assembly.GetExecutingAssembly().Location, typeof(RegisterPage).FullName);
            PushButton RegisterPage = RegisterPanel.AddItem(buttonDataRegisterPage) as PushButton;
            RegisterPage.AvailabilityClassName = typeof(RegisterPage).FullName;
            
            return Result.Succeeded;
        }

Register Page (RegisterPage)

It is best to register the page when the project starts. I follow SDKSmple method to divide into registration window and display window.

ThisApplication.thisApp.RegisterPage(app);
public void RegisterPage(Autodesk.Revit.UI.UIApplication uiapp)
        {
            DockablePaneId dockablePaneId = new DockablePaneId(APIUntil.m_GUID);
            uiapp.RegisterDockablePane(dockablePaneId, "Pane", ThisApplication.thisApp.GetMainWindow() as IDockablePaneProvider);
        }

In this step we need a GUID for registration. Finally create a separate class to store and place to avoid code confusion due to too much code later.

Show Window

Window display is controlled by Show(), hide is Hide()

public void SetWindowAvibilable(Autodesk.Revit.UI.UIApplication uiApp)
        {
            DockablePane pane = uiApp.GetDockablePane(new DockablePaneId(APIUntil.m_GUID));
            pane.Show();
           
        }

Up to this step we have created all dockable panels. Just register the Addin file and open revit to see our panel.


After creation, we need to complete selecting component in main interface by clicking button on page and pop up corresponding ID and Name. So we need to create an idling event for (Select) operation.

 public static void IdlingHandler(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs args)
        {
            UIApplication uiapp = sender as UIApplication;
            if (uiapp != null)
            {
                Autodesk.Revit.UI.UIDocument uidoc = uiapp.ActiveUIDocument;
                Autodesk.Revit.DB.Document doc = uidoc.Document;
                if (uidoc != null && doc != null)
                {
                    Reference re = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "");
                    Element e = doc.GetElement(re);

                    System.Windows.Forms.MessageBox.Show(e.Id.ToString() + e.Name);
                    uiapp.Idling -= IdlingHandler;
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Must Get should Idling!");
            }
        }

There is a problem here: At this time WPF form as an independent process cannot interact directly with Revit. If directly apply idling event, system will report API cannot be called. This troubled me for a long time, and I also went to stackflow to ask questions. Question Link Image Description This time jimmy replied to me. Commands can be called and data transmitted through external events and idling events.

External Event (ExternalEvent)

There are many articles on external events on blogs. Here I will write code directly.

public class ExternalHander : IExternalEventHandler
        {
            public void Execute(UIApplication app)
            {
                
               ThisApplication.thisApp.AddIdlingEvent(app);
                
            }

            public string GetName()
            {
                return "ExternalHander.Excute()";
            }
        }

Regarding this interface, you can refer to thebuildingcoder which has explanation for this interface and Idling event. After that we just need to nest idling event into external event to execute command. Image Description