Create Revit Plugin With Windows Form

6.9k views Asked by At

I've been trying to create a plugin for Revit 2017 with Visual Studio 2015 with windows Form. Unfortunately I've not found any documentation online for doing so (if you have links, i'll be happy to give them a look)

I've built a simple form using a Listbox and a select button

  • The Listbox shows all the doors in a Revit project
  • The select button selects all the selected doors from the Listbox and selects them in the Revit project (that's a lot of selects ...)

It's a test solution, to see how it all works.

WeWillSee class is the class implementing the main RevitAPI function Execute:

 using System; 
 using Autodesk.Revit.UI; 
 using Autodesk.Revit.Attributes; 
 using Autodesk.Revit.DB;

 namespace Test2 {

 [Transaction(TransactionMode.Manual)]
 class WeWillSee : IExternalCommand
 {
     public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
     {
         UIApplication uiapp = commandData.Application;
         /*UIDocument uidoc = uiapp.ActiveUIDocument;
         Document doc = uidoc.Document;*/

         try
         {
             System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
             System.Windows.Forms.Application.Run(new Form(commandData));
             //System.Windows.Forms.Form wf = new Form1(uiapp);
         }
         catch (Exception e)
         {
             TaskDialog.Show("Error", e.ToString());
             return Result.Failed;
         }

         return Result.Succeeded;
     }
 } 
 }

The Form I want to open (the rest in not important):

namespace Test2
{
    public partial class Form : System.Windows.Forms.Form
    {
        private UIApplication uiapp;
        private UIDocument uidoc;
        private Document doc;

        public Form(ExternalCommandData commandData)
        {
            InitializeComponent();

            uiapp = commandData.Application;
            uidoc = uiapp.ActiveUIDocument;
            doc = uidoc.Document;
        }

And finally the Program.cs file (the one causing me problems):

namespace Test2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(/*Can't call ExternalCommandData on static class*/));
        }
    }
}

Thanks for any help you can offer! :)

3

There are 3 answers

3
Jeremy Tammik On

Here is a simple Revit add-in implementing an external command that creates and displays a Windows form on the fly:

http://thebuildingcoder.typepad.com/blog/2012/05/the-schedule-api-and-access-to-schedule-data.html

1
PatH On

I don't think you even need the Program.cs class file in your project the way you have it written.

0
Matt On

You don't need to do the Application.Run kind of things (that's only for standalone windows applications). You don't need the Program.cs thing at all.

You can just do as you started to:

 Form1 wf = new Form1(uiapp);
 if (wf.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
 return Result.Success