<?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: PickObject from Windows Form C# in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8021257#M49843</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Dear Amar,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for your query.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Yes, I think the problem is the single threading requirement in the Revit API.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The safest way to achieve this is to terminate the Windows form for the duration of the pick operation, and then reopen it again afterwards.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I suggest you search the Revit SDK samples for all occurrences of ShowDialog and PickObject.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The samples that make use of both of these calls will probably show you how you can easily solve your problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Another important sample that you should be aware of is ModelessDialog/ModelessForm_ExternalEvent.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It demonstrates how show a modeless dialogue, close it to select elements, and set up an external event to pass information from an external modeless context back into the Revit API.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, here is an example of a loop switching back and forth between selecting elements and displaying a modal form that I recently worked on:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;  public class Command : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIDocument uidoc = commandData.Application.ActiveUIDocument;
      ElementId id;

      while( true )
      {
        id = null;

        Form form = new Form();

        DialogResult rc = form.ShowDialog();

        if( DialogResult.Cancel == rc)
        {
          break;
        }
        if( DialogResult.OK == rc )
        {
          TreeItem item = form.Selecteditem;

          if( null == item )
          {
            TaskDialog.Show( "Nothing to Select",
              "Please select a valid tree node." );
          }
          else
          {
            FilteredElementCollector a
              = new FilteredElementCollector( uidoc.Document )
                .Where...();

            int n = a.GetElementCount();

            if( 0 == n )
            {
              TaskDialog.Show( "Nothing to Select",
                string.Format( "No {0} elements found.",
                bic.Description() ) );
            }
            else if( 1 == n )
            {
              id = a.FirstElementId();
            }
            else
            {
              try
              {
                Pick element ...
              }
              catch operation cancelled
              {
                TaskDialog.Show( "Nothing Selected",
                  "Nothing Selected." );
              }
            }
            if( null != id &amp;amp;&amp;amp; ElementId.InvalidElementId != id )
            {
              Debug.Print(
                "Selected element id {0} for tree node {1}",
                id.IntegerValue, item.Name );
            }
          }
        }
      }
      return Result.Succeeded;
    }
  }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I hope this helps.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best regards,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Jeremy&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 23 May 2018 15:41:41 GMT</pubDate>
    <dc:creator>jeremytammik</dc:creator>
    <dc:date>2018-05-23T15:41:41Z</dc:date>
    <item>
      <title>PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8021010#M49842</link>
      <description>&lt;P&gt;Hi, I've been messing around with the following code the past few hours, and I cant seem to get it to work. What I'm trying to achieve is to use a button on a windows form to select an object with the mouse in revit (to later store by id in a JSON file)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem I'm getting is that when it hits the selection.pickobject(...) command, the program just hangs and does not let you click in Revit. I think it has to do with Revit API being single thread, but I'm not sure.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code within the Windows Form.&lt;/P&gt;&lt;PRE&gt;  public partial class AddToJson : System.Windows.Forms.Form
    {
        ExternalCommandData commandData;
        UIDocument uidoc;
        Selection selection;

        public AddToJson(string optionName, ExternalCommandData cData)
        {
            commandData = cData;

            InitializeComponent();
            comboBox1.Text = optionName;
            label6.Text = optionName;

            populateForm();
            refreshData();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //this.Hide();
            foreach (System.Windows.Forms.Form f in Application.OpenForms)
            {
                f.Hide();
            }

            UIApplication app = commandData.Application;
            uidoc = app.ActiveUIDocument;
            Document doc = uidoc.Document;
            selection = uidoc.Selection;
            Reference ref1 = selection.PickObject(ObjectType.Element, "Pick element");        
            ElementId elemid = ref1.ElementId;
            this.Show();
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 14:17:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8021010#M49842</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-23T14:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8021257#M49843</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Dear Amar,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for your query.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Yes, I think the problem is the single threading requirement in the Revit API.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The safest way to achieve this is to terminate the Windows form for the duration of the pick operation, and then reopen it again afterwards.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I suggest you search the Revit SDK samples for all occurrences of ShowDialog and PickObject.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The samples that make use of both of these calls will probably show you how you can easily solve your problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Another important sample that you should be aware of is ModelessDialog/ModelessForm_ExternalEvent.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It demonstrates how show a modeless dialogue, close it to select elements, and set up an external event to pass information from an external modeless context back into the Revit API.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, here is an example of a loop switching back and forth between selecting elements and displaying a modal form that I recently worked on:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;  public class Command : IExternalCommand
  {
    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIDocument uidoc = commandData.Application.ActiveUIDocument;
      ElementId id;

      while( true )
      {
        id = null;

        Form form = new Form();

        DialogResult rc = form.ShowDialog();

        if( DialogResult.Cancel == rc)
        {
          break;
        }
        if( DialogResult.OK == rc )
        {
          TreeItem item = form.Selecteditem;

          if( null == item )
          {
            TaskDialog.Show( "Nothing to Select",
              "Please select a valid tree node." );
          }
          else
          {
            FilteredElementCollector a
              = new FilteredElementCollector( uidoc.Document )
                .Where...();

            int n = a.GetElementCount();

            if( 0 == n )
            {
              TaskDialog.Show( "Nothing to Select",
                string.Format( "No {0} elements found.",
                bic.Description() ) );
            }
            else if( 1 == n )
            {
              id = a.FirstElementId();
            }
            else
            {
              try
              {
                Pick element ...
              }
              catch operation cancelled
              {
                TaskDialog.Show( "Nothing Selected",
                  "Nothing Selected." );
              }
            }
            if( null != id &amp;amp;&amp;amp; ElementId.InvalidElementId != id )
            {
              Debug.Print(
                "Selected element id {0} for tree node {1}",
                id.IntegerValue, item.Name );
            }
          }
        }
      }
      return Result.Succeeded;
    }
  }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I hope this helps.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best regards,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Jeremy&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 15:41:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8021257#M49843</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2018-05-23T15:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8035477#M49844</link>
      <description>&lt;P&gt;Thanks for your answer, I'll look into it!&lt;/P&gt;</description>
      <pubDate>Wed, 30 May 2018 14:35:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/8035477#M49844</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-30T14:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/10020608#M49845</link>
      <description>&lt;P&gt;Hello Jeremy,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Three years later, is this answer still the best solution? How about using a Dockable Panel? I am trying my best at creating a floating tool panel outside of the ribbon, with lists and drop downs populated with external data, and I find managing the state of it pretty slow andd combersome if I have to redraw the form after each picking sessions. I also want this panel to hold buttons that call methods in my class. I tried a modeless form, but without something to block the UI Thread (like a pickobject session) it will just open and close, obviously. Is there another approach?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your BuildingCoder&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 19:46:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/10020608#M49845</guid>
      <dc:creator>Hugo.Bergeron</dc:creator>
      <dc:date>2021-01-21T19:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/10020971#M49846</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Dear Hugo,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you for your appreciation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Modal form, modeless form, Windows Forms, WPF, dockable panel, stand-alone external executable and many more are all viable approaches.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The best solution depends completely on your specific requirements.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Jeremy&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 22:27:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/10020971#M49846</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2021-01-21T22:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/10022678#M49847</link>
      <description>&lt;LI-CODE lang="csharp"&gt;using (var p = new ToolPalette(CSVFile.CSVFileChange(uiapp)))
{
    p.Show(_hWndRevit);
    System.Windows.Forms.Application.Run(p);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what i ended up using.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 14:50:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/10022678#M49847</guid>
      <dc:creator>Hugo.Bergeron</dc:creator>
      <dc:date>2021-01-22T14:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: PickObject from Windows Form C#</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/12407606#M49848</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6877865"&gt;@Hugo.Bergeron&lt;/a&gt;, thanks this worked for me!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 08:44:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/pickobject-from-windows-form-c/m-p/12407606#M49848</guid>
      <dc:creator>ShambhaveeP2ZMB</dc:creator>
      <dc:date>2023-11-29T08:44:20Z</dc:date>
    </item>
  </channel>
</rss>

