<?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 ArcGIS Pro SDK automatically execute &amp;quot;Fit To Display&amp;quot; command of Georeferencing in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-automatically-execute-quot-fit-to/m-p/1672910#M13309</link>
    <description>&lt;P&gt;I am trying load an jpg file as a raster layer, start georeferencing and then fit to display the raster.&lt;BR /&gt;This part of a larger ArcGIS Pro 3.5 project, where the image file name is found using user input from a wpf form. It is written in C#.&lt;BR /&gt;This is a rejig of an existing ArcMap 10.8 ArcObjects project writen in VB.Net&lt;/P&gt;&lt;P&gt;The idea is to place the image roughly in the right place ready for the user to georeferencing workflow.&lt;BR /&gt;Previously I have acquired the image and added it to the table of contents. This all works fine.&lt;/P&gt;&lt;P&gt;I have separated out the execution of esri commands into its own function. There is a bool parameter the writes a Messagebox if true saying if the command is executed or not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The behavior I find:&lt;BR /&gt;if all three steps (attempt to close georeference session, start georeferencing, execute "fit to display") report their status then the image is displayed in the center of the current extent i.e "fit to display" is executed. After manually stopping georeferencing and removing the raster image, I can repeat the commands and the image is again displayed in the center of the current extent.&lt;/P&gt;&lt;P&gt;My code does the following: related commands:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;adds an jpg image as a raster to the TOC and selects it&lt;/LI&gt;&lt;LI&gt;execute command: esri_datasourcesraster_georefCloseSessionButton&amp;nbsp; close a Georeferencing session&lt;/LI&gt;&lt;LI&gt;execute command: FrameworkApplication.ActivateTab("esri_datasourcesraster_GeoreferenceTab" activate the Georeferencing tab&lt;/LI&gt;&lt;LI&gt;execute command: esri_datasourcesraster_georefStartButton start a georeferencing session&lt;/LI&gt;&lt;LI&gt;execute command: esri_datasourcesraster_georefFitToDisplayButton fit the selected raster to current display&lt;/LI&gt;&lt;LI&gt;loop through raster layers and make them visible if not already visible&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The step&amp;nbsp;esri_datasourcesraster_georefCloseSessionButton cannot be executed. I believe this is because there is a user dialog required to save/stop a Georeferencing session.&lt;/P&gt;&lt;P&gt;I use a separate function to execute the esri commands. This has an option to report if the command was executed or not via a Messagebox.&lt;/P&gt;&lt;P&gt;This all works repeatedly if and only I switch on reporting i.e. the user responds to a total of 3 messages when i execute the georefCloseSessionButton ,&amp;nbsp;georefStartButton and&amp;nbsp;georefFitToDisplayButton.&lt;/P&gt;&lt;P&gt;However if I switch off reporting for even one step it will not "fit to display" for many combinations. It will work for the first load when the georeferencing button reports on its status. It only works once if I do not attempt to stop the geoprocessing but report the other 2 steps.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similar behavior occurs if I do not include the initial&amp;nbsp;esri_datasourcesraster_georefCloseSessionButton step.&lt;/P&gt;&lt;P&gt;Also it will "fit to display" just the first time if I do not do any reporting but I add a delay after the start georeferencing button is activated.&lt;/P&gt;&lt;P&gt;I have tested this in all sorts of ways .&lt;/P&gt;&lt;P&gt;Questions:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;Is there a delay in executing any of the three commands?&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;Have I made some mistake with synchronous asynchronous calls?&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;It there a way to programmatically stop georeferencing?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;Stefan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code is placed in a utility class. The functions are called from within button bound to an ICoomand i.e.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;       public ICommand cmdLoad
       {
           get
           {
               return new RelayCommand(async () =&amp;gt;
               {
		 // Get image ame from 
                    await clsUtils.AddImageToMap(imagepath);
                    await clsUtils.MoveImageToDisplay(ImageName);
                }, true);
            }
        }
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The utility class functions:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static class clsUtils ...

		
       public static async Task MoveImageToDisplay(string layerName)
       {
           string procName = nameof(MoveImageToDisplay);

           // MoveImageToDisplay
           try
           {
               // to use Geroeferenceing need a raster layer  to be selected
               MapView mapView = MapView.Active;
               var myMap = mapView?.Map;
               RasterLayer targetlayer = myMap.GetLayersAsFlattenedList().OfType&amp;lt;RasterLayer&amp;gt;().FirstOrDefault(l =&amp;gt; l.Name.Equals(layerName, System.StringComparison.OrdinalIgnoreCase));
               List&amp;lt;RasterLayer&amp;gt; layersToSelect = new List&amp;lt;RasterLayer&amp;gt;();
               layersToSelect.Add(targetlayer);
               MapView.Active.SelectLayers(layersToSelect);


                bool report = true; 

               // always attempt to close down any georeferencing
		 // Does not work. Probably because it requires user input. The dialog box does not appear.
		// Does no seem to matter if on or not
               clsUtils.ExecuteESRICommand("esri_datasourcesraster_georefCloseSessionButton",report);

		// Activate Georeferencing Tab
		// I found that  i didn't need to activate the Imagry Tab first as is needed when working manually.
               string activeTabId = FrameworkApplication.ActiveTab;
               if (activeTabId != "esri_mapping_georeference_GeoreferenceTab")
               {
                   //FrameworkApplication.ActivateTab("esri_datasourcesraster_imageryCoreTab");
                   FrameworkApplication.ActivateTab("esri_datasourcesraster_GeoreferenceTab");
               }


		// Start georeferencing 
               clsUtils.ExecuteESRICommand("esri_datasourcesraster_georefStartButton",report);

		// Add a delay
               //await System.Threading.Tasks.Task.Delay(1000);  

		// execute the "FitToDisplay" georeferenciong command
               clsUtils.ExecuteESRICommand("esri_datasourcesraster_georefFitToDisplayButton",report);

               //Switch on seleceted layers if not aleady on

               foreach (RasterLayer layer in layersToSelect)
               {
                   if (!layer.IsVisible)
                   {
                       await QueuedTask.Run(() =&amp;gt;
                       {
                           layer.SetVisibility(true);                           // on MCT
                       });
                   }
               }


           }
           catch (Exception ex)
           {
               MessageBox.Show("Error ( " + procName + " ) " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);

           }


       }

        public static void ExecuteESRICommand(string commandId, bool report = false )
        {
            //var commandId = "esri_datasourcesraster_georefFitToDisplayButton";
            // get the ICommand interface from the ArcGIS Pro Button
            // using command's plug-in wrapper
            var iCommand = FrameworkApplication.GetPlugInWrapper(commandId) as ICommand;
            if (iCommand != null)
            {
                // Let ArcGIS Pro do the work for us
                if (iCommand.CanExecute(null))
                {
                    iCommand.Execute(null);
                    if(report) MessageBox.Show(commandId + " executed");
                }
                else
                {
                    if(report) MessageBox.Show("Cannot exexcute " + commandId);
                }
            }
            else
            {
                if(report) MessageBox.Show( commandId + " is null");

            }

        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use an asynchronous version of the ExecuteESRICommand, but this didn't work.&lt;BR /&gt;If all three commands are executed "Fit to Display" is done but then Pro crashes.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        public static async Task ExecuteESRICommandAsync(string commandId, bool report = false)
        {
            // get the ICommand interface from the ArcGIS Pro Button
            // using command's plug-in wrapper
            var iCommand = FrameworkApplication.GetPlugInWrapper(commandId) as ICommand;
            if (iCommand != null)
            {
                // Let ArcGIS Pro do the work for us
                if (iCommand.CanExecute(null))
                {
                    await QueuedTask.Run(() =&amp;gt; 
                    {
                        iCommand.Execute(null);
                    });
                    
                    if (report) MessageBox.Show(commandId + " executed");
                }
                else
                {
                    if (report) MessageBox.Show("Cannot exexcute " + commandId);
                }
            }
            else
            {
                if (report) MessageBox.Show(commandId + " is null");

            }

        }&lt;/LI-CODE&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;</description>
    <pubDate>Mon, 15 Dec 2025 04:11:25 GMT</pubDate>
    <dc:creator>StefanDieters1</dc:creator>
    <dc:date>2025-12-15T04:11:25Z</dc:date>
    <item>
      <title>ArcGIS Pro SDK automatically execute "Fit To Display" command of Georeferencing</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-automatically-execute-quot-fit-to/m-p/1672910#M13309</link>
      <description>&lt;P&gt;I am trying load an jpg file as a raster layer, start georeferencing and then fit to display the raster.&lt;BR /&gt;This part of a larger ArcGIS Pro 3.5 project, where the image file name is found using user input from a wpf form. It is written in C#.&lt;BR /&gt;This is a rejig of an existing ArcMap 10.8 ArcObjects project writen in VB.Net&lt;/P&gt;&lt;P&gt;The idea is to place the image roughly in the right place ready for the user to georeferencing workflow.&lt;BR /&gt;Previously I have acquired the image and added it to the table of contents. This all works fine.&lt;/P&gt;&lt;P&gt;I have separated out the execution of esri commands into its own function. There is a bool parameter the writes a Messagebox if true saying if the command is executed or not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The behavior I find:&lt;BR /&gt;if all three steps (attempt to close georeference session, start georeferencing, execute "fit to display") report their status then the image is displayed in the center of the current extent i.e "fit to display" is executed. After manually stopping georeferencing and removing the raster image, I can repeat the commands and the image is again displayed in the center of the current extent.&lt;/P&gt;&lt;P&gt;My code does the following: related commands:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;adds an jpg image as a raster to the TOC and selects it&lt;/LI&gt;&lt;LI&gt;execute command: esri_datasourcesraster_georefCloseSessionButton&amp;nbsp; close a Georeferencing session&lt;/LI&gt;&lt;LI&gt;execute command: FrameworkApplication.ActivateTab("esri_datasourcesraster_GeoreferenceTab" activate the Georeferencing tab&lt;/LI&gt;&lt;LI&gt;execute command: esri_datasourcesraster_georefStartButton start a georeferencing session&lt;/LI&gt;&lt;LI&gt;execute command: esri_datasourcesraster_georefFitToDisplayButton fit the selected raster to current display&lt;/LI&gt;&lt;LI&gt;loop through raster layers and make them visible if not already visible&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The step&amp;nbsp;esri_datasourcesraster_georefCloseSessionButton cannot be executed. I believe this is because there is a user dialog required to save/stop a Georeferencing session.&lt;/P&gt;&lt;P&gt;I use a separate function to execute the esri commands. This has an option to report if the command was executed or not via a Messagebox.&lt;/P&gt;&lt;P&gt;This all works repeatedly if and only I switch on reporting i.e. the user responds to a total of 3 messages when i execute the georefCloseSessionButton ,&amp;nbsp;georefStartButton and&amp;nbsp;georefFitToDisplayButton.&lt;/P&gt;&lt;P&gt;However if I switch off reporting for even one step it will not "fit to display" for many combinations. It will work for the first load when the georeferencing button reports on its status. It only works once if I do not attempt to stop the geoprocessing but report the other 2 steps.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similar behavior occurs if I do not include the initial&amp;nbsp;esri_datasourcesraster_georefCloseSessionButton step.&lt;/P&gt;&lt;P&gt;Also it will "fit to display" just the first time if I do not do any reporting but I add a delay after the start georeferencing button is activated.&lt;/P&gt;&lt;P&gt;I have tested this in all sorts of ways .&lt;/P&gt;&lt;P&gt;Questions:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;Is there a delay in executing any of the three commands?&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;Have I made some mistake with synchronous asynchronous calls?&lt;/LI&gt;&lt;LI&gt;&amp;nbsp;It there a way to programmatically stop georeferencing?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;Stefan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code is placed in a utility class. The functions are called from within button bound to an ICoomand i.e.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;       public ICommand cmdLoad
       {
           get
           {
               return new RelayCommand(async () =&amp;gt;
               {
		 // Get image ame from 
                    await clsUtils.AddImageToMap(imagepath);
                    await clsUtils.MoveImageToDisplay(ImageName);
                }, true);
            }
        }
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The utility class functions:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static class clsUtils ...

		
       public static async Task MoveImageToDisplay(string layerName)
       {
           string procName = nameof(MoveImageToDisplay);

           // MoveImageToDisplay
           try
           {
               // to use Geroeferenceing need a raster layer  to be selected
               MapView mapView = MapView.Active;
               var myMap = mapView?.Map;
               RasterLayer targetlayer = myMap.GetLayersAsFlattenedList().OfType&amp;lt;RasterLayer&amp;gt;().FirstOrDefault(l =&amp;gt; l.Name.Equals(layerName, System.StringComparison.OrdinalIgnoreCase));
               List&amp;lt;RasterLayer&amp;gt; layersToSelect = new List&amp;lt;RasterLayer&amp;gt;();
               layersToSelect.Add(targetlayer);
               MapView.Active.SelectLayers(layersToSelect);


                bool report = true; 

               // always attempt to close down any georeferencing
		 // Does not work. Probably because it requires user input. The dialog box does not appear.
		// Does no seem to matter if on or not
               clsUtils.ExecuteESRICommand("esri_datasourcesraster_georefCloseSessionButton",report);

		// Activate Georeferencing Tab
		// I found that  i didn't need to activate the Imagry Tab first as is needed when working manually.
               string activeTabId = FrameworkApplication.ActiveTab;
               if (activeTabId != "esri_mapping_georeference_GeoreferenceTab")
               {
                   //FrameworkApplication.ActivateTab("esri_datasourcesraster_imageryCoreTab");
                   FrameworkApplication.ActivateTab("esri_datasourcesraster_GeoreferenceTab");
               }


		// Start georeferencing 
               clsUtils.ExecuteESRICommand("esri_datasourcesraster_georefStartButton",report);

		// Add a delay
               //await System.Threading.Tasks.Task.Delay(1000);  

		// execute the "FitToDisplay" georeferenciong command
               clsUtils.ExecuteESRICommand("esri_datasourcesraster_georefFitToDisplayButton",report);

               //Switch on seleceted layers if not aleady on

               foreach (RasterLayer layer in layersToSelect)
               {
                   if (!layer.IsVisible)
                   {
                       await QueuedTask.Run(() =&amp;gt;
                       {
                           layer.SetVisibility(true);                           // on MCT
                       });
                   }
               }


           }
           catch (Exception ex)
           {
               MessageBox.Show("Error ( " + procName + " ) " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);

           }


       }

        public static void ExecuteESRICommand(string commandId, bool report = false )
        {
            //var commandId = "esri_datasourcesraster_georefFitToDisplayButton";
            // get the ICommand interface from the ArcGIS Pro Button
            // using command's plug-in wrapper
            var iCommand = FrameworkApplication.GetPlugInWrapper(commandId) as ICommand;
            if (iCommand != null)
            {
                // Let ArcGIS Pro do the work for us
                if (iCommand.CanExecute(null))
                {
                    iCommand.Execute(null);
                    if(report) MessageBox.Show(commandId + " executed");
                }
                else
                {
                    if(report) MessageBox.Show("Cannot exexcute " + commandId);
                }
            }
            else
            {
                if(report) MessageBox.Show( commandId + " is null");

            }

        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use an asynchronous version of the ExecuteESRICommand, but this didn't work.&lt;BR /&gt;If all three commands are executed "Fit to Display" is done but then Pro crashes.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        public static async Task ExecuteESRICommandAsync(string commandId, bool report = false)
        {
            // get the ICommand interface from the ArcGIS Pro Button
            // using command's plug-in wrapper
            var iCommand = FrameworkApplication.GetPlugInWrapper(commandId) as ICommand;
            if (iCommand != null)
            {
                // Let ArcGIS Pro do the work for us
                if (iCommand.CanExecute(null))
                {
                    await QueuedTask.Run(() =&amp;gt; 
                    {
                        iCommand.Execute(null);
                    });
                    
                    if (report) MessageBox.Show(commandId + " executed");
                }
                else
                {
                    if (report) MessageBox.Show("Cannot exexcute " + commandId);
                }
            }
            else
            {
                if (report) MessageBox.Show(commandId + " is null");

            }

        }&lt;/LI-CODE&gt;&lt;OL&gt;&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Mon, 15 Dec 2025 04:11:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-automatically-execute-quot-fit-to/m-p/1672910#M13309</guid>
      <dc:creator>StefanDieters1</dc:creator>
      <dc:date>2025-12-15T04:11:25Z</dc:date>
    </item>
  </channel>
</rss>

