UI Top Forms, Buttons, Web, etc.

Several user interface related topics are being discussed in the Revit API discussion forum:

Window handle

Keep my Form on Top of Revit!

This topic came up twice in the past few days, in the Revit API discussion forum thread on modeless dialog stays on top and dialog / form visibility:

Question: I created an addon that runs along with the normal user use of Revit and allows the user to open certain views by displaying a modeless dialog.

This works well, but I've set the TopMost property of the dialog to True; otherwise, I keep losing the dialog behind other windows.

The issue is, that with this set, the dialog then stays on top and annoyingly covers other windows.

I'd like the window to follow Revit's window, minimise with it and stay the level with it, so when a window covers Revit, it covers the dialog too.

Answer: This can be easily fixed by making the Revit main window the owner window of your modeless dialogue, and your modeless dialogue a child of the Revit main window.

I implemented the JtWindowHandle class to help achieve this in the past, so you can search The Building Coder for JtWindowHandle to find a number of solutions.

Please note that the Revit API nowadays provides official access to the main Revit window via the UIApplication class MainWindowHandle property.

The JtWindowHandle class is defined like this:

  /// <summary>
  /// Wrapper class for converting 
  /// IntPtr to IWin32Window.
  /// </summary>
  public class JtWindowHandle : IWin32Window
  {
    IntPtr _hwnd;

    public JtWindowHandle( IntPtr h )
    {
      Debug.Assert( IntPtr.Zero != h,
        "expected non-null window handle" );

      _hwnd = h;
    }

    public IntPtr Handle
    {
      get
      {
        return _hwnd;
      }
    }
  }

You instantiate it from the Revit main window handle like this:

  JtWindowHandle owner_window_handle 
    = new JtWindowHandle( 
      uiapp.MainWindowHandle );

Then, you can parent your form with it using either Show or ShowDialog for a modeless or modal form, respectively:

  MyForm form = new MyForm();

  DialogResult rslt = form.ShowDialog( 
    owner_window_handle );

Creating Buttons and Getting Started with an Add-In UI

Another thread was misunderstood and brought up a bunch of answers on how to get started creating buttons and a user interface in general, even though it was actually asking something completely different, how to modify Revit button picture?

Here are the answers, once again, anyway:

Integrating a Web-Based UI

Next, Balint raised a discussion on the implementation of an external application with web UI:

Question: I would like to create an external application which opens a dialog and do some stuff after it. My dialog would be a WebView or something like that. Is it possible? Could someone recommend a good example? Which direction is worth following? I searched on the Autodesk AppStore and found BimObject or ProdLib plugins, which have really nice UIs. Are they using WPF or some web framework?

Answer: We recently discussed WPF versus Winforms.

Even though most of the Revit SDK samples demonstrate use of WinForms, this is hardly recommended anymore nowadays.

The Revit SDK DockableDialogs example is WPF. It's good in that it shows the various API's necessary to build an external app, but it was not designed to be a great WPF reference.

You can see some other samples searching for 'Revit API WPF sample'.

The Building Coder also shares some WPF related topics.

Have you checked out Ali Asad's Visual Studio WPF MVVM Revit add-in template?

This StackOverflow thread discusses how to create a WPF Revit add-in starting with the DockableDialogs SDK sample.

Oh, and finally we must not forget the recent solution using IPC to disentangle and connect with CefSharp.

Response: I hope I found the solution. Using DotNetBrowser, I can integrate a Chromium-based browser into Revit app. I tried it and it works fine.

Many thanks to Balint for sharing this!