<?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: Repeat External Command in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661265#M72130</link>
    <description>&lt;P&gt;I've actually figured it out myself. I wrapped that part I want to repeat in an endless while statement and I placed the transaction start after picking an element. This avoids the transacation being started but never finished, which causes everything created by the external command to be deleted when the escape key is hit. I also set the catch for the escape key to report back as Result.Succeeded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;public Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            m_application = commandData.Application;
            m_document = m_application.ActiveUIDocument;

            try
            {
                int linkedFileId = 0;
                int linkedElementId = 0;

                int testInt = -1;
                while(testInt &amp;lt;0)
                {
                    #region Extract Door Information
                    // Selectin filter so that you can only pick doors
                    ISelectionFilter selectionFilter = new SecurityDoorFilter();
                    //Selection. You can only select one linked element at a time
                    Reference linkedDoorRef = m_document.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.LinkedElement, "Please pick a door to tag.");
                    Transaction t = new Transaction(m_document.Document);
                    t.Start("Add Security Door");

                    //Do some stuff                  

                    t.Commit();
                }
                    #endregion
                return Result.Succeeded;
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                // Selection Cancelled.
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                // If any error, give error information and return failed
                            return Result.Failed;
            }&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Jun 2015 15:42:21 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2015-06-02T15:42:21Z</dc:date>
    <item>
      <title>Repeat External Command</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5659429#M72129</link>
      <description>&lt;P&gt;I am trying to figure out how to repeat an external command similar to placing elements. I have written an api that placing an object directly on top of door from a linked file. It extracts and stores some information from the door. Currently you have to click the button for the command after every door. I am looking to be able to just click on the doors one right after the other. I have tried a couple of things to keep the command active but as soon as you hit the escape key to stop the command everything disappears. Any ideas would be appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2015 13:18:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5659429#M72129</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-06-01T13:18:45Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat External Command</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661265#M72130</link>
      <description>&lt;P&gt;I've actually figured it out myself. I wrapped that part I want to repeat in an endless while statement and I placed the transaction start after picking an element. This avoids the transacation being started but never finished, which causes everything created by the external command to be deleted when the escape key is hit. I also set the catch for the escape key to report back as Result.Succeeded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;public Result Execute(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            m_application = commandData.Application;
            m_document = m_application.ActiveUIDocument;

            try
            {
                int linkedFileId = 0;
                int linkedElementId = 0;

                int testInt = -1;
                while(testInt &amp;lt;0)
                {
                    #region Extract Door Information
                    // Selectin filter so that you can only pick doors
                    ISelectionFilter selectionFilter = new SecurityDoorFilter();
                    //Selection. You can only select one linked element at a time
                    Reference linkedDoorRef = m_document.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.LinkedElement, "Please pick a door to tag.");
                    Transaction t = new Transaction(m_document.Document);
                    t.Start("Add Security Door");

                    //Do some stuff                  

                    t.Commit();
                }
                    #endregion
                return Result.Succeeded;
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                // Selection Cancelled.
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                // If any error, give error information and return failed
                            return Result.Failed;
            }&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jun 2015 15:42:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661265#M72130</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-06-02T15:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat External Command</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661314#M72131</link>
      <description>&lt;P&gt;Hello adeuley,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am afraid I have to tell you that yours is not an solution. Although it works for this particular case, it still suffers from the original problem which is still present in your code: transactions objects (including Transaction, TransactionGroup, and SubTransaction) need to be enclosed in a “using” scope that will guarantee the object are terminated properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, your original code would be implement properly as follows:&lt;/P&gt;
&lt;PRE&gt;using (Transaction trans = new Transaction(m_document.Document))
{
  trans.Start("Add Security Door");
  // Do some stuff
  trans.Commit();
}
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN style="line-height: 15px;"&gt;That will guarantee that even in the case of an exception from the “doing some stuff” the transaction will get finished properly and in time.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Naturally, since in your case the OperationCanceledException is anticipated (as it often is), you need to account for it in your implementation as well. Therefore, the following will be the correct implementation you need:&lt;/P&gt;
&lt;PRE&gt;using (Transaction trans = new Transaction(m_document.Document))
{
   trans.Start("Add Security Door");
   try
   { 
     // Do some stuff
   }
   catch (OperationCanceledException)
   {     
      // this is anticipated no need to do anything
   }
   trans.Commit();
   return (trans.GetStatus() == TransactionStatus.Committed)
      ? Succeeded
      : Failed;
}
&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN style="line-height: 15px; white-space: pre-wrap;"&gt;Again, please remember to always enclose your transactions into a "using" scope block. It&amp;nbsp;helps to avoid unexpected behavior due to unfinalized transactions phases.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2015 16:16:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661314#M72131</guid>
      <dc:creator>arnostlobel</dc:creator>
      <dc:date>2015-06-02T16:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat External Command</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661428#M72132</link>
      <description>&lt;P&gt;I've updated my code as you suggested and now I am back to my original problem. I can't add more than one object without having to click the button for the external command before placing each object. Do you have any suggestions on how I can make your code work for that?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2015 17:20:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661428#M72132</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-06-02T17:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat External Command</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661561#M72133</link>
      <description>&lt;P&gt;adeuley,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not completely understand why my change make your infifite loop to be interupted. However, I think I can suggest some implementation. Here is what I woudl probably use&amp;nbsp;if I were you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all, I'd separate the&amp;nbsp;door-tag-selecting procedure along the following approach:&lt;/P&gt;
&lt;PRE&gt;public ElementId selectDoorTag (Document doc)
{
    try
    {
        // invoke the selection here
        doc.Selection.PickObject(...)
        
        // analyse the selection here to see if any tags were picked
        // if so, return its Id,
        // otherwise return ElementId.InvalidElementId
    }
    catch (OperationCancelledException
    {
        // this is inticipated and indicates the end the iteraction session
    )
    
    return ElementId.InvalidElementId;  // indicates the user did not make a selection
}&lt;/PRE&gt;
&lt;P&gt;Then I would modify the main loop as follows:&lt;/P&gt;
&lt;PRE&gt;using (Transaction trans = new Transaction(document, "Added Security Door")
{
    ElementId tagId = selectDoorTag(document);
    while (tagId != ElementId.InvalidElementId)
    {
        trans.Start();
        // add a security door here
        trans.Commit();
        
        if (trans.GetStatus() != TransactionStatus.Committed)
        {
            // I assume we will not continue in case of a failure
            return Failed;
        }

        // invoke another selecting action
        tagId = selectDoorTag(document);
    }
}&lt;/PRE&gt;
&lt;P&gt;Does that make sense?&lt;/P&gt;
&lt;P&gt;Please keep in mind I have typed the code above in Notepad and I might have made a typo here and there. You need to make sure it compiles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2015 18:22:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5661561#M72133</guid>
      <dc:creator>arnostlobel</dc:creator>
      <dc:date>2015-06-02T18:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Repeat External Command</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5662415#M72134</link>
      <description>cool, glad to hear you've found the solution by yourself!</description>
      <pubDate>Wed, 03 Jun 2015 09:59:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/repeat-external-command/m-p/5662415#M72134</guid>
      <dc:creator>Aaron.Lu</dc:creator>
      <dc:date>2015-06-03T09:59:58Z</dc:date>
    </item>
  </channel>
</rss>

