<?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 do you refresh view in ArcMap from Python? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522844#M40995</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is indeed a workaround. You can refresh the screen using ArcObjects, which fortunately can be accessed in Python by using comtypes. It's a bit of work but for those of us still mired in 9.3 it can be a godsend.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can create a script tool from the code below (1st parameter = Layer, 2nd parameter = SQL Expression). It will select features from your layer and then refresh the active view. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One note here is the comtypes code isn't directly hooked into the current ArcMap session, so it has to loop through all open ArcGIS sessions to find ArcMap. If you have multiple ArcMap sessions open, this code will actually refresh the view in all of them. You could adjust the script to find a specific session by comparing the MXD name with the first part of objApplication.Caption and then break from the loop when it matches.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Comtypes can be downloaded from here: &lt;/SPAN&gt;&lt;A href="http://sourceforge.net/projects/comtypes/files/comtypes/0.6.2/comtypes-0.6.2.win32.exe/download" rel="nofollow noopener noreferrer" target="_blank"&gt;http://sourceforge.net/projects/comtypes/files/comtypes/0.6.2/comtypes-0.6.2.win32.exe/download&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcgisscripting
gp = arcgisscripting.create()

strLayer = gp.GetParameterAsText(0)
strExpression = gp.GetParameterAsText(1)

gp.SelectLayerByAttribute_management(strLayer, "NEW_SELECTION", strExpression)

import comtypes.client

# Load the required COM libraries
esriFramework = comtypes.client.GetModule(r'C:\Program Files\ArcGIS\com\esriFramework.olb')
esriArcMapUI = comtypes.client.GetModule(r'C:\Program Files\ArcGIS\com\esriArcMapUI.olb')

# Create the AppROT object which contains all open ArcGIS applications
objAppROT = comtypes.client.CreateObject(esriFramework.AppROT, interface=esriFramework.IAppROT)

# Loop through each open ArcGIS application
for i in range(objAppROT.Count):

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Query interface (convert) the current item in the list to something we can work with.
&amp;nbsp;&amp;nbsp;&amp;nbsp; objApplication = objAppROT.Item(i).QueryInterface(esriFramework.IApplication)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check to see if the current ArcGIS application is ArcMap.
&amp;nbsp;&amp;nbsp;&amp;nbsp; if objApplication.Name == "ArcMap":

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the current document in the ArcMap application.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objMxDocument = objApplication.Document.QueryInterface(esriArcMapUI.IMxDocument)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Refresh the active view.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objMxDocument.ActiveView.Refresh()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("View refreshed...")

del objAppROT 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:47:51 GMT</pubDate>
    <dc:creator>BradPosthumus</dc:creator>
    <dc:date>2021-12-11T22:47:51Z</dc:date>
    <item>
      <title>How do you refresh view in ArcMap from Python?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522843#M40994</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(originally posted on &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/21287-How-do-you-refresh-view-in-ArcMap-from-Python?p=69370#post69370"&gt;ArcGIS Desktop - General&lt;/A&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I've written a Python script in 9.3.1 which as its result selects a set of features (SelectLayerByAttribute) for a layer visible in ArcMap. However, the selected features are only visible once the user performs an action that results in a refresh. Is there some way (a work around of some type) to force a view refresh from within Python (in 9.3.1 &amp;amp; not 10)?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Darren&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Jan 2011 02:48:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522843#M40994</guid>
      <dc:creator>DarrenSmith</dc:creator>
      <dc:date>2011-01-18T02:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do you refresh view in ArcMap from Python?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522844#M40995</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is indeed a workaround. You can refresh the screen using ArcObjects, which fortunately can be accessed in Python by using comtypes. It's a bit of work but for those of us still mired in 9.3 it can be a godsend.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can create a script tool from the code below (1st parameter = Layer, 2nd parameter = SQL Expression). It will select features from your layer and then refresh the active view. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One note here is the comtypes code isn't directly hooked into the current ArcMap session, so it has to loop through all open ArcGIS sessions to find ArcMap. If you have multiple ArcMap sessions open, this code will actually refresh the view in all of them. You could adjust the script to find a specific session by comparing the MXD name with the first part of objApplication.Caption and then break from the loop when it matches.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Comtypes can be downloaded from here: &lt;/SPAN&gt;&lt;A href="http://sourceforge.net/projects/comtypes/files/comtypes/0.6.2/comtypes-0.6.2.win32.exe/download" rel="nofollow noopener noreferrer" target="_blank"&gt;http://sourceforge.net/projects/comtypes/files/comtypes/0.6.2/comtypes-0.6.2.win32.exe/download&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcgisscripting
gp = arcgisscripting.create()

strLayer = gp.GetParameterAsText(0)
strExpression = gp.GetParameterAsText(1)

gp.SelectLayerByAttribute_management(strLayer, "NEW_SELECTION", strExpression)

import comtypes.client

# Load the required COM libraries
esriFramework = comtypes.client.GetModule(r'C:\Program Files\ArcGIS\com\esriFramework.olb')
esriArcMapUI = comtypes.client.GetModule(r'C:\Program Files\ArcGIS\com\esriArcMapUI.olb')

# Create the AppROT object which contains all open ArcGIS applications
objAppROT = comtypes.client.CreateObject(esriFramework.AppROT, interface=esriFramework.IAppROT)

# Loop through each open ArcGIS application
for i in range(objAppROT.Count):

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Query interface (convert) the current item in the list to something we can work with.
&amp;nbsp;&amp;nbsp;&amp;nbsp; objApplication = objAppROT.Item(i).QueryInterface(esriFramework.IApplication)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check to see if the current ArcGIS application is ArcMap.
&amp;nbsp;&amp;nbsp;&amp;nbsp; if objApplication.Name == "ArcMap":

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the current document in the ArcMap application.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objMxDocument = objApplication.Document.QueryInterface(esriArcMapUI.IMxDocument)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Refresh the active view.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objMxDocument.ActiveView.Refresh()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.addmessage("View refreshed...")

del objAppROT 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:47:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522844#M40995</guid>
      <dc:creator>BradPosthumus</dc:creator>
      <dc:date>2021-12-11T22:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: How do you refresh view in ArcMap from Python?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522845#M40996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's a better way that accesses the current ArcMap session directly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcgisscripting
gp = arcgisscripting.create()

strLayer = gp.GetParameterAsText(0)
strExpression = gp.GetParameterAsText(1)

gp.SelectLayerByAttribute_management(strLayer, "NEW_SELECTION", strExpression)

import comtypes.client
# Load the required COM libraries
esriFramework = comtypes.client.GetModule(r'C:\Program Files\ArcGIS\com\esriFramework.olb')
esriArcMapUI = comtypes.client.GetModule(r'C:\Program Files\ArcGIS\com\esriArcMapUI.olb')

# Get the current ArcMap session
objApplication = comtypes.client.CreateObject(esriFramework.AppRef, interface=esriFramework.IApplication)
objMxDocument = objApplication.Document.QueryInterface(esriArcMapUI.IMxDocument)
objMxDocument.ActiveView.Refresh()
gp.addmessage("View refreshed...")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I found this solution, as well as some excellent wrapper code and discussion, at this link:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://gis.stackexchange.com/questions/5017/python-comtypes-and-arcobjects-error-creating-approt-object/5082#5082" rel="nofollow noopener noreferrer" target="_blank"&gt;http://gis.stackexchange.com/questions/5017/python-comtypes-and-arcobjects-error-creating-approt-object/5082#5082&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:47:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522845#M40996</guid>
      <dc:creator>BradPosthumus</dc:creator>
      <dc:date>2021-12-11T22:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do you refresh view in ArcMap from Python?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522846#M40997</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for both of your replies Brad.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've come up with the following modified function based on code found in the link you gave. However, as I'm working on a distributed geoprocessing tool I still have two problems with my current solution:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) I'm guessing the registry keys change for each release? i.e. the typelib key in the example given for 10 from the link (for the esriFramework OLB) was actually the key for esriGlobeCore.Globe on my install (9.3.1).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) This approach will only work for Windows.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def refreshArcMap():
&amp;nbsp;&amp;nbsp;&amp;nbsp; """
&amp;nbsp;&amp;nbsp;&amp;nbsp; Refreshes view in current ArcMap session.
&amp;nbsp;&amp;nbsp;&amp;nbsp; Assumptions: Python comtypes framework installed &amp;amp; comtypes.client already imported 
&amp;nbsp;&amp;nbsp;&amp;nbsp; Limitations: Only works on Windows &amp;amp; only tested for ArcGIS 9.3.1 (32-bit).
&amp;nbsp;&amp;nbsp;&amp;nbsp; """
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import _winreg

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Look in registry for the TypeLib entry for the esriFramework OLB 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; keyESRI = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, \
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r"TypeLib\{866AE5D3-530C-11D2-A2BD-0000F8774FB5}\1.0\HELPDIR") 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get path of ArcGIS type libraries as string from registry e.g "C:\Program Files\ArcGIS\com" 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comDir =_winreg.QueryValueEx(keyESRI, None)[0] 

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Load the required COM libraries
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; esriFramework = comtypes.client.GetModule(comDir + r'\esriFramework.olb')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; esriArcMapUI = comtypes.client.GetModule(comDir + r'\esriArcMapUI.olb')

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the current ArcMap session &amp;amp; refresh view
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objApplication = comtypes.client.CreateObject(esriFramework.AppRef, interface=esriFramework.IApplication)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objMxDocument = objApplication.Document.QueryInterface(esriArcMapUI.IMxDocument)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objMxDocument.ActiveView.Refresh()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("View refreshed...")

&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception, ErrorDesc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # If an error occurred while refreshing instruct user to manually refresh
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddWarning("A problem occured when trying to automatically refresh ArcMap!")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddWarning("Perform a manual refresh of map window (i.e. click 'Refresh View' button at the bottom of the map window) to view results")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:47:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522846#M40997</guid>
      <dc:creator>DarrenSmith</dc:creator>
      <dc:date>2021-12-11T22:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do you refresh view in ArcMap from Python?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522847#M40998</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks for both of your replies Brad.&lt;BR /&gt;&lt;BR /&gt;1) I'm guessing the registry keys change for each release? i.e. the typelib key in the example given for 10 from the link (for the esriFramework OLB) was actually the key for esriGlobeCore.Globe on my install (9.3.1).&lt;BR /&gt;&lt;BR /&gt;2) This approach will only work for Windows.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(Delayed response.....)&lt;/SPAN&gt;&lt;BR /&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;I suppose you would need to roll out a new version of your tool when ArcGIS 10 comes online for your users (though it would be a pain to maintain two versions if some are still in 9.3). Once in 10 you may want to rewrite it using ArcPy, though it may not be robust enough for your requirements.&lt;/LI&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;LI&gt;If it's for ArcGIS desktop you're limited to Windows anyway.&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Feb 2011 12:14:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522847#M40998</guid>
      <dc:creator>BradPosthumus</dc:creator>
      <dc:date>2011-02-03T12:14:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do you refresh view in ArcMap from Python?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522848#M40999</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks again Brad, am sure looking forward to moving to 10 &amp;amp; ArcPy.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 13 Feb 2011 20:38:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-you-refresh-view-in-arcmap-from-python/m-p/522848#M40999</guid>
      <dc:creator>DarrenSmith</dc:creator>
      <dc:date>2011-02-13T20:38:48Z</dc:date>
    </item>
  </channel>
</rss>

