Search code examples
c#winformsrevit-apirevit

Stuck in blue Spinner after Windows Form closed trying to pass value


I'm trying to pass a value back from a WinForms that I'm trying to set up however the once the form closes, Revit is stuck in the blue spinner after the continue button is clicked and doesn't return to running the original program.

I've checked around but haven't found anything that gets it to return to the program. Most(if not all) of the tutorials on WinForms are not related to dealing with Revit. Most that I have found/watched deal with passing a value from one form to another, although I found this one even though it's a bit different.

here is my code:

Form1.cs

public partial class Form1 : System.Windows.Forms.Form
    {
        private UIApplication uiapp;
        private UIDocument uidoc;
        private Autodesk.Revit.ApplicationServices.Application app;
        private Document doc;

        private string myVal;

        public string MyVal
        {
            get { return myVal; }
            set { myVal = value; }
        }

        public Form1(ExternalCommandData commandData)
        {
            InitializeComponent();

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

    public delegate void delPassData(System.Windows.Forms.ComboBox text);

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create a filter to get all the title block types.
            FilteredElementCollector colTitleBlocks = new FilteredElementCollector(doc);
            colTitleBlocks.OfCategory(BuiltInCategory.OST_TitleBlocks);
            colTitleBlocks.WhereElementIsElementType();

            foreach(Element x in colTitleBlocks)
            {
                comboBox1TitleBlockList.Items.Add(x.Name);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1Continue_Click(object sender, EventArgs e)
        {

            MyVal = comboBox1TitleBlockList.Text;  // is returning with Titleblock.Name

            button1Continue.DialogResult = DialogResult.OK;
            Debug.WriteLine("OK button was clicked.");
            Close();
            //Dispose();
            return;
        }

        private void button2Cancel_Click(object sender, EventArgs e)
        {
            button2Cancel.DialogResult = DialogResult.Cancel;
            Debug.WriteLine("Cancel button was clicked");
            //Dispose();

            Close();
            return;
        }

        private void comboBox1TitleBlockList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

Command.cs

 Form1 form1 = new Form1(commandData);

 System.Windows.Forms.Application.Run(new Form1(commandData)); // not sure if this line is required.

 form1.ShowDialog();

 String elementString = form1.MyVal; //<---- this is returning null. need to figure out how to pass value from form1 back to Command.cs

if (elementString != null)
   {
       elementString = form1.MyVal.ToString();
       Element eFromString = doc.GetElement(elementString);
       titleBlockId = eFromString.Id;                                
   }
   else
   {
        titleBlockId = collector.FirstElementId();
   }

Any and all help/direction is appreciated.


Solution

  • This will never work. you line saying not sure if this line is required attempts to ... I cannot even describe what it is trying to achieve.

    The Revit API is purely event driven and your Revit add-in can only make calls to the Revit API from a valid Revit API context. Such a context is only provided within event handlers that you hook up with Revit events.

    Your code does not subscribe to any such events. Hence, it is not a Revit add-in.

    Please work trough the Revit API getting started material and build you add-in conforming to the Revit add-in architectural requirements.