Search code examples
c#winformsrevit-api

Is it possible to open my winForm through Revit API button I have created?


I would like to run my winForm through the button I have created in Revit API, but I am new in this field and I am a bit stuck at the moment.

Here in my Command.cs I am stating what button does after clicking on it. Where instead of displaying "Hello World" I would like it to open my winForm.

Is there any way how can I do that? Do I need to somehow link my winForm application to this one?

namespace SetElevation
{
    [Transaction(TransactionMode.Manual)]
    class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            TaskDialog.Show("SetElevation", "Hello World!");
            return Result.Succeeded;
        }
    }
}

Here is my winForm Program.cs application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormTest
{
    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


Solution

  • welcome to Revit addin development. You’re close. I presume if you made your addin work above, that it is compiling as a DLL, not a standalone EXE. Your Winform app appears to be a separate EXE application.

    To make this work, you’ll want to add your Form1 to the DLL project. Once you’ve got it in there, you can change TaskDialog.Show to instead these two lines:

    var myForm - new Form1(); myForm.ShowDialog();

    With that, you’re on your way.