Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Last active October 23, 2017 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexArcPy/bf0f1702d6b0d3840f43442a2d5ef5c6 to your computer and use it in GitHub Desktop.
Save AlexArcPy/bf0f1702d6b0d3840f43442a2d5ef5c6 to your computer and use it in GitHub Desktop.
Execute a tool on an ArcMap toolbar from Python script tool in open ArcMap session
#based on
#https://gis.stackexchange.com/questions/252950/how-can-i-call-a-net-c-tool-i-e-a-dll-file-from-a-model-workflow-or-a-py
import arcpy
import sys
from comtypes.client import GetModule, CreateObject
#path where snippets102.py module is stored
sys.path.append(r'C:\GIS\arcobjects')
import snippets102
esriFramework = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.5\com\esriFramework.olb")
esriSystem = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.5\com\esriSystem.olb")
#access the current application and the map document open
pApp = CreateObject(esriFramework.AppROT, interface=esriFramework.IAppROT)
pDoc = pApp.Item(0).Document.QueryInterface(esriFramework.IDocument)
#access the commands interface
pCmdBars = pDoc.CommandBars.QueryInterface(esriFramework.ICommandBars)
#get uid of Full extent button
uid = CreateObject(esriSystem.UID, interface=esriSystem.IUID)
#full list of tools UIDs
#http://edndoc.esri.com/arcobjects/9.2/NET/858a508c-b771-467c-81e6-c6aa72d88f9c.htm
uid.Value = "{0830FB35-7EE6-11D0-87EC-080009EC732A}"
#find and execute the tool
cmdItem = pCmdBars.Find(uid)
cmdItem.Execute()
#https://gis.stackexchange.com/questions/252950/how-can-i-call-a-net-c-tool-i-e-a-dll-file-from-a-model-workflow-or-a-py
import arcpy
import sys
from comtypes.client import GetModule, CreateObject
#path where snippets102.py module is stored
sys.path.append(r'C:\GIS\arcobjects')
import snippets102
esriFramework = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.5\com\esriFramework.olb")
esriSystem = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.5\com\esriSystem.olb")
#access the current application and the map document open
pApp = CreateObject(esriFramework.AppROT, interface=esriFramework.IAppROT)
pDoc = pApp.Item(0).Document.QueryInterface(esriFramework.IDocument)
#access the commands interface
pCmdBars = pDoc.CommandBars.QueryInterface(esriFramework.ICommandBars)
#custom add-in
cmdItem = pCmdBars.Find("ESRI_CopyParallel_Button1") #can be taken from Config.Designer.cs
cmdItem.Command.OnClick()
@dazzasmith
Copy link

dazzasmith commented Oct 20, 2017

Thanks Alexey for this great pair of examples. In order to complete the set, how about an example of calling a tool on custom Python add-in in ArcMap (from within the code behind a second custom Python add-in that is)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment