<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Keep Windows Form Open after running plugin! in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/keep-windows-form-open-after-running-plugin/m-p/10397136#M25361</link>
    <description>&lt;P&gt;The answer is easy and has been discussed many times, both here and by The Building Coder:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A modal form displayed by &lt;U&gt;ShowDialog&lt;/U&gt; must be closed before the thread can continue and the external command complete.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.showdialog?view=net-5.0" target="_blank"&gt;https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.showdialog?view=net-5.0&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A modeless form displayed by the &lt;U&gt;Show&lt;/U&gt; method runs in a separate thread and can continue running even after&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the external command completes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.show?view=net-5.0" target="_blank"&gt;https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.show?view=net-5.0&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The modeless form, being in a separate thread, has no access to the single-threaded Revit API.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, it is easy to keep your form open, but you will have to change the way you interact with the Revit API.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The most common&amp;nbsp;(and maybe the only clean) way to achieve that is to use an external event.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots more on that topic:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Jun 2021 06:26:58 GMT</pubDate>
    <dc:creator>jeremy_tammik</dc:creator>
    <dc:date>2021-06-17T06:26:58Z</dc:date>
    <item>
      <title>Keep Windows Form Open after running plugin!</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/keep-windows-form-open-after-running-plugin/m-p/10396379#M25360</link>
      <description>&lt;P&gt;Hey Everyone! I looked around on the forum and I couldnt find a solution for this.... So how do I keep my windows form up after running the plugin? My work around before was to copy the body of my code into the&amp;nbsp; form itself, but i figured there has to be a better way... I want the form to stay open so I can run it multiple times on different selections. I think it has something to do with it being a dialog result but not sure. Can anyone give me some guidance on this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my FormCode:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Plumbing;

namespace DynamoToRevitPlugin
{
    public partial class PipeCreationForm : System.Windows.Forms.Form
    {
        public PipeCreationForm(Autodesk.Revit.UI.ExternalCommandData commandData)
        {
            InitializeComponent();

            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            //levels
            FilteredElementCollector levels = new FilteredElementCollector(uidoc.Document);
            List&amp;lt;Level&amp;gt; levelCol = levels.OfClass(typeof(Level)).OfType&amp;lt;Level&amp;gt;().OrderBy(lev =&amp;gt; lev.Elevation).ToList();
            sLevel.DataSource = new List&amp;lt;Level&amp;gt;(levelCol);
            sLevel.DisplayMember = "Name";
            if (levelCol.Any())
            {
                sLevel.SelectedIndex = 0;
            }

            //System Type
            FilteredElementCollector mepSystem = new FilteredElementCollector(uidoc.Document);
            List&amp;lt;PipingSystemType&amp;gt; systemTypeCol = mepSystem.OfClass(typeof(PipingSystemType)).OfType&amp;lt;PipingSystemType&amp;gt;().OrderBy(x =&amp;gt; x.Name).ToList();
            pSysType.DataSource = new List&amp;lt;PipingSystemType&amp;gt;(systemTypeCol);
            pSysType.DisplayMember = "Name";
            if (systemTypeCol.Any())
            {
                pSysType.SelectedIndex = 0;
            }

            //pipe types
            FilteredElementCollector pipeType = new FilteredElementCollector(uidoc.Document);
            List&amp;lt;PipeType&amp;gt; pipeTypeCol = pipeType.OfClass(typeof(PipeType)).OfType&amp;lt;PipeType&amp;gt;().ToList();
            pType.DataSource = new List&amp;lt;PipeType&amp;gt;(pipeTypeCol);
            pType.DisplayMember = "Name";
            if (pipeTypeCol.Any())
            {
                pType.SelectedIndex = 0;
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {

        }

        private void PipeCreationForm_Load(object sender, EventArgs e)
        {

        }

        private void pType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //find pipe segment sizes
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is code for the plugin:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB.Plumbing;

namespace DynamoToRevitPlugin
{
    [Transaction(TransactionMode.Manual)]
    class PipeCreation : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //need to select elements before

            // Get the handle of current document
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Autodesk.Revit.ApplicationServices.Application app = doc.Application;

            // reference the form
            PipeCreationForm form = new PipeCreationForm(commandData);


            


            // Get the element selection of current document.
            Selection selection = uidoc.Selection;
            ICollection&amp;lt;ElementId&amp;gt; selectedIds = uidoc.Selection.GetElementIds();

            //check if selction is empty
            if (0 == selectedIds.Count)
            {
                // If no elements selected.
                TaskDialog.Show("Revit", "Select Columns Before Running Plugin.");
            }
            else
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    //gets the level from form
                    ElementId slvlId = null;
                    if (form.sLevel.SelectedIndex &amp;gt; -1)
                    {
                        slvlId = (form.sLevel.SelectedItem as Level).Id;
                    }


                    //gets the system type from form
                    ElementId sysTypeId = null;
                    if (form.pSysType.SelectedIndex &amp;gt; -1)
                    {
                        sysTypeId = (form.pSysType.SelectedItem as PipingSystemType).Id;
                    }


                    //gets the pipe type from form
                    ElementId pTypeId = null;
                    if (form.pType.SelectedIndex &amp;gt; -1)
                    {
                        pTypeId = (form.pType.SelectedItem as PipeType).Id;
                    }

                    using (Transaction tx = new Transaction(doc))
                    {
                        tx.Start("Create Pipes");

                        List&amp;lt;XYZ&amp;gt; points = new List&amp;lt;XYZ&amp;gt;();
                        List&amp;lt;ElementId&amp;gt; elemLi = new List&amp;lt;ElementId&amp;gt;();
                        foreach (ElementId elemId in selectedIds)
                        {
                            Element elem = uidoc.Document.GetElement(elemId);

                            //bounding box around solid
                            BoundingBoxXYZ elemBB = elem.get_BoundingBox(doc.ActiveView);
                            //gets center of bounding box
                            XYZ bbCentroid = (elemBB.Max + elemBB.Min) / 2;
                            //add centroid points to list
                            points.Add(bbCentroid);
                            elemLi.Add(elemId);
                        }

                        
                        foreach (XYZ pt in points)
                        {
                            XYZ closestPoint = null;
                            foreach (XYZ point2 in points) //loop to find closest point
                            {
                                if(pt != point2)
                                {
                                    if (closestPoint == null)
                                    {
                                        closestPoint = point2;
                                    }
                                    if (pt.DistanceTo(point2) &amp;lt; pt.DistanceTo(closestPoint))
                                    {
                                        closestPoint = point2;
                                    }
                                }  
                            }
                            Pipe pipe = Pipe.Create(doc, sysTypeId, pTypeId, slvlId, pt, closestPoint);
                        }
                        tx.Commit();
                    }
                }
            }
            return Result.Succeeded;
        }
    }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Jun 2021 23:15:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/keep-windows-form-open-after-running-plugin/m-p/10396379#M25360</guid>
      <dc:creator>zrodgersTSSSU</dc:creator>
      <dc:date>2021-06-16T23:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Windows Form Open after running plugin!</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/keep-windows-form-open-after-running-plugin/m-p/10397136#M25361</link>
      <description>&lt;P&gt;The answer is easy and has been discussed many times, both here and by The Building Coder:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A modal form displayed by &lt;U&gt;ShowDialog&lt;/U&gt; must be closed before the thread can continue and the external command complete.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.showdialog?view=net-5.0" target="_blank"&gt;https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.showdialog?view=net-5.0&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A modeless form displayed by the &lt;U&gt;Show&lt;/U&gt; method runs in a separate thread and can continue running even after&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the external command completes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.show?view=net-5.0" target="_blank"&gt;https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.show?view=net-5.0&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The modeless form, being in a separate thread, has no access to the single-threaded Revit API.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, it is easy to keep your form open, but you will have to change the way you interact with the Revit API.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The most common&amp;nbsp;(and maybe the only clean) way to achieve that is to use an external event.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots more on that topic:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jun 2021 06:26:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/keep-windows-form-open-after-running-plugin/m-p/10397136#M25361</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2021-06-17T06:26:58Z</dc:date>
    </item>
  </channel>
</rss>

