2

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

Elliott Addi
  • 370
  • 4
  • 18

3 Answers3

1

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

PatH
  • 105
  • 1
  • 11
  • Indeed, there is no need for the program.cs file. It is the file that is automatically generated when you create a project using the windows form template. – Elliott Addi Jan 09 '17 at 12:49
1

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
Cflux
  • 1,423
  • 3
  • 19
  • 39
Matt
  • 1,043
  • 5
  • 8
1

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

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
  • I've used the Revit 2017 template provided on your blog. it works (almost) great. The problem now is managing multi-threading and events. – Elliott Addi Jan 09 '17 at 12:51
  • and btw, your blog is extremely helpful for this – Elliott Addi Jan 09 '17 at 14:35
  • thank you for your appreciation! the Revit API cannot be used with multi-threading. you can do other things there, of course, e.g. process data that you extracted from Revit, but the extraction itself (and all Revit communication) is limited to the main Revit thread and a valid Revit API context. – Jeremy Tammik Jan 10 '17 at 13:46