Elliott Addi
Elliott Addi

Reputation: 380

Create Revit Plugin With Windows Form

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

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! :)

Upvotes: 2

Views: 7152

Answers (3)

Matt
Matt

Reputation: 1096

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

Upvotes: 1

Jeremy Tammik
Jeremy Tammik

Reputation: 8294

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

Upvotes: 1

PatH
PatH

Reputation: 105

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

Upvotes: 1

Related Questions