<?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: How to remove a layer from the undo stack, which has been removed from the map. in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202083#M8585</link>
    <description>&lt;P&gt;Regardless of knowing that a multi-layer removal included an 'allowed' layer, you can only remove the complete undo operation from OperationManager.&amp;nbsp; &amp;nbsp;It's not possible to just remove one part of the undo operation (i.e., the layer you don't want to have re-added again).&amp;nbsp; Since it's an all or nothing [removal from the Undo stack] scenario, I would suggest removing an undo operation if the&amp;nbsp;LayersRemovedEvent&amp;nbsp; contained at least one of the layers that cannot be re-added to the map.&amp;nbsp; If the&amp;nbsp;LayersRemovedEvent only contained layers that can be re-added don't do anything with the undo stack.&lt;/P&gt;</description>
    <pubDate>Thu, 11 Aug 2022 18:25:16 GMT</pubDate>
    <dc:creator>Wolf</dc:creator>
    <dc:date>2022-08-11T18:25:16Z</dc:date>
    <item>
      <title>How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201433#M8577</link>
      <description>&lt;P&gt;I am working in ArcGIS Pro 2.9 and I am attempting to remove a layer from the undo stack after the layer has been removed from the map.&amp;nbsp; I do not want the user to have the ability to undo the removal of the layer and bring it back into the map.&amp;nbsp; Once the layer has been removed then that is final.&amp;nbsp; With that being said, I am successfully using the &lt;STRONG&gt;&lt;EM&gt;OperationManager&lt;/EM&gt; &lt;/STRONG&gt;object to remove the layer from the undo stack if the layer is removed programmatically.&amp;nbsp; However, the same code does not work if the layer is removed from the GUI using the TOC.&amp;nbsp; This doesn't make any sense that I could do what I need programmatically, but not from the GUI.&amp;nbsp; I am using the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;LayersRemovedEvent&lt;/EM&gt; &lt;/STRONG&gt;to check for whether the layer has been added to the undo stack.&amp;nbsp; It does when I call "&lt;STRONG&gt;&lt;EM&gt;map.RemoveLaye&lt;/EM&gt;&lt;EM&gt;r&lt;/EM&gt;&lt;/STRONG&gt;," but not when the event fires from a GUI removal from the TOC.&amp;nbsp; Is this just the way that ArcGIS Pro is designed?&amp;nbsp; Does the GUI removal of the layer from the TOC not add the layer to the undo stack until after the &lt;EM&gt;&lt;STRONG&gt;LayersRemovedEvent &lt;/STRONG&gt;&lt;/EM&gt;fires?&amp;nbsp; If so, is there another way to get to the layer removal operation and remove it from the undo stack after the event?&lt;/P&gt;&lt;P&gt;The following is my method that removes operations from the undo stack:&lt;/P&gt;&lt;P&gt;public static void RemoveUndoOperations(string opName)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if (MapView.Active?.Map == null) { return; }&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; OperationManager opManager = MapView.Active.Map.OperationManager;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; List&amp;lt;Operation&amp;gt; ops = opManager.FindUndoOperations(o =&amp;gt; o.Name.Contains(opName));&lt;BR /&gt;&amp;nbsp; &amp;nbsp; foreach (Operation op in ops)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; opManager.RemoveUndoOperation(op);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; }&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 19:12:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201433#M8577</guid>
      <dc:creator>DaveLewis73</dc:creator>
      <dc:date>2022-08-10T19:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201533#M8578</link>
      <description>&lt;P&gt;I was able to duplicate the problem and you are correct in that the Undo operation doesn't appear to get added until after the LayersRemovedEvent fires.&amp;nbsp; &amp;nbsp;I am trying to find a workaround, but you will have to modify your .FindUndoOperations logic as well.&amp;nbsp; If you remove more than one layer at the same time the Operation Name doesn't contain the layer name.&amp;nbsp; Also, any other operation (like adding a layer) would be removed from the stack as well.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 19:50:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201533#M8578</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2022-08-10T19:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201621#M8579</link>
      <description>&lt;P&gt;Hi Dave,&lt;/P&gt;&lt;P&gt;Internally, "Do" or "DoAync" is called on the OperationManager to execute the given operation - in this case the removing of a layer. &lt;U&gt;Within the context of the Do or DoAsync&lt;/U&gt;, the operation is executed - i.e. removal of the layer(s) which, in turn, fires all relevant events - to include "Removing" and "Removed" events - &lt;EM&gt;_then_&lt;/EM&gt; , assuming completion was &lt;U&gt;successful&lt;/U&gt;, the operation is added to the undo stack to allow it to be undone. Do now returns and/or DoAsync completes. An unsuccessful operation, conversely, would not be added (to be undone).&lt;/P&gt;&lt;P&gt;The best u can do, as u r trying to catch the operation "as it/when it" is added to the stack is to try a slight delay. So, something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;//elsewhere - register for layers removed
ArcGIS.Desktop.Mapping.Events.LayersRemovedEvent.Subscribe((args) =&amp;gt; {
  DelayedListUndoOperations("LayersRemovedEvent",
    args.Layers.Select(x =&amp;gt; $"Remove layer: {x.Name}").ToList(),
    1000);
});

//delay before reading operations
private async void DelayedListUndoOperations(string when, 
                       List&amp;lt;string&amp;gt; opNames, int milliseconds) {
   //non-blocking wait
   await Task.Delay(milliseconds);
   ListUndoOperations($"delayed {milliseconds}, {when}", opNames);
}

private void ListUndoOperations(string when, List&amp;lt;string&amp;gt; opNames) {
  var opManager = MapView.Active?.Map?.OperationManager;
  var ops =
    opManager?.FindUndoOperations(o =&amp;gt; !string.IsNullOrEmpty(o.Name)) ??
        new List&amp;lt;Operation&amp;gt;();
  System.Diagnostics.Debug.WriteLine(when);
  if (ops.Count() == 0) {
    System.Diagnostics.Debug.WriteLine("No Undo operations");
  }
  foreach (var op in ops) {
    var match = opNames.Contains(op.Name) ? " (match)" : "";
    System.Diagnostics.Debug.WriteLine($"{op.Name}{match}");
  }
}
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is still an edge case however, where, depending on the delay, the user still has the opportunity - albeit brief - to execute the undo.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 21:47:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201621#M8579</guid>
      <dc:creator>CharlesMacleod</dc:creator>
      <dc:date>2022-08-10T21:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201935#M8580</link>
      <description>&lt;P&gt;I found that this works in my test code and doesn't require a timer, however, you have to try this in your environment in order to verify that it works for you.&amp;nbsp; Don't use a debugger and breakpoints when you debug this snippet it will change the thread execution sequence.&amp;nbsp; Also as mentioned above your&amp;nbsp;&lt;SPAN&gt;FindUndoOperations logic still requires some work.&amp;nbsp; Charlie mentioned that various async operations still have to complete before OperationManager's queue reflects the true state so i decided to try executing the OperationManager related code on the UI thread once the UI thread is idle again, clearly a workaround, but it seemed to work for me.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void RemoveUndoOperations(string opName)
{
  if (MapView.Active?.Map == null) { return; }
  QueuedTask.Run(async () =&amp;gt;
  {
    await System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =&amp;gt;
    {
      OperationManager opManager = MapView.Active.Map.OperationManager;
      List&amp;lt;Operation&amp;gt; ops = opManager.FindUndoOperations(o =&amp;gt; o.Name.Contains(opName));
      foreach (Operation op in ops)
      {
        var opDetail = $@"Operation: {op.Name} canundo: {op.CanUndo} cat: {op.Category} sub: {op.SubCategory}";
        System.Diagnostics.Trace.WriteLine(opDetail);
        opManager.RemoveUndoOperation(op);
      }
    }), DispatcherPriority.ApplicationIdle);
  });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 15:36:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201935#M8580</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2022-08-11T15:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201960#M8581</link>
      <description>&lt;P&gt;Charlie,&lt;/P&gt;&lt;P&gt;Thank you for the code snippet.&amp;nbsp; Everything works great!&amp;nbsp; This is just the kind of thing I was looking for (very straightforward).&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 15:55:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201960#M8581</guid>
      <dc:creator>DaveLewis73</dc:creator>
      <dc:date>2022-08-11T15:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201968#M8582</link>
      <description>&lt;P&gt;Wolf,&lt;/P&gt;&lt;P&gt;Thank you for your code snippet as well.&amp;nbsp; I am not sure which solution to use, your solution or Charlie's.&amp;nbsp; I wish I could accept both as a solution. Great work guys!&lt;/P&gt;&lt;P&gt;By the way, do you have any idea as to what I need to do to update my &lt;STRONG&gt;&lt;EM&gt;FindUndoOperations&lt;/EM&gt;&lt;/STRONG&gt; code to get around that problem of deleting multiple layers at the same time?&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 17:28:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1201968#M8582</guid>
      <dc:creator>DaveLewis73</dc:creator>
      <dc:date>2022-08-11T17:28:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202002#M8583</link>
      <description>&lt;P&gt;You have to check if the operation name starts with the string "Remove" and ends with your layer name or the string "layers".&amp;nbsp; &amp;nbsp;If you have multi-language support, you have to use the proper translation of those strings (best implemented through the corresponding language resource file).&lt;/P&gt;&lt;P&gt;This will support the single layer delete and deletion of multiple layers:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Wolf_0-1660235635034.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/48435i426C17AE2AD8C9E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Wolf_0-1660235635034.png" alt="Wolf_0-1660235635034.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 16:38:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202002#M8583</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2022-08-11T16:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202055#M8584</link>
      <description>&lt;P&gt;I am going to have to see if there is some other solution to removing multiple layers, because I only want to remove the undo capability on certain types of layers that have been removed from the map.&amp;nbsp; I am specifically dealing with certain image service layers that I don't want to have undo removal capability.&amp;nbsp; All other types of layers are ok to be removed and to then be able to undo the removal and restore them to the map.&amp;nbsp; Therefore, as you can see, having multiple layers removed at the same time is a rather serious concern.&amp;nbsp; I don't know what the user has selected from the TOC to remove.&amp;nbsp; I may want all the layers to have undo capability removed or just one/some layers depending on which ones are image service layers.&amp;nbsp; This is my dilemma.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 17:36:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202055#M8584</guid>
      <dc:creator>DaveLewis73</dc:creator>
      <dc:date>2022-08-11T17:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove a layer from the undo stack, which has been removed from the map.</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202083#M8585</link>
      <description>&lt;P&gt;Regardless of knowing that a multi-layer removal included an 'allowed' layer, you can only remove the complete undo operation from OperationManager.&amp;nbsp; &amp;nbsp;It's not possible to just remove one part of the undo operation (i.e., the layer you don't want to have re-added again).&amp;nbsp; Since it's an all or nothing [removal from the Undo stack] scenario, I would suggest removing an undo operation if the&amp;nbsp;LayersRemovedEvent&amp;nbsp; contained at least one of the layers that cannot be re-added to the map.&amp;nbsp; If the&amp;nbsp;LayersRemovedEvent only contained layers that can be re-added don't do anything with the undo stack.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 18:25:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-remove-a-layer-from-the-undo-stack-which/m-p/1202083#M8585</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2022-08-11T18:25:16Z</dc:date>
    </item>
  </channel>
</rss>

