I'm running from the ArcMap Python window.
My code is:
import arcpy, comtypes, Snippets
Snippets.ArcMap_GetSelectedGeometry(bStandalone=False)
The error message:
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "V:\Projects\Shared\kalley\PythonScripts\Snippets.py", line 258, in ArcMap_GetSelectedGeometry
pApp = GetCurrentApp()
File "V:\Projects\Shared\kalley\PythonScripts\Snippets.py", line 126, in GetCurrentApp
return NewObj(esriFramework.AppRef, esriFramework.IApplication)
AttributeError: 'module' object has no attribute 'AppRef'
The entire Snippets.py file is in the link in my original post, but the snippets involved in my line of code are:
def GetLibPath():
"""Return location of ArcGIS type libraries as string"""
# This will still work on 64-bit machines because Python runs in 32 bit mode
import _winreg
keyESRI = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\ESRI\\Desktop10.2")
return _winreg.QueryValueEx(keyESRI, "InstallDir")[0] + "com\\"
def GetModule(sModuleName):
"""Import ArcGIS module"""
from comtypes.client import GetModule
sLibPath = GetLibPath()
GetModule(sLibPath + sModuleName)
def GetDesktopModules():
"""Import basic ArcGIS Desktop libraries"""
GetModule("esriFramework.olb")
GetModule("esriArcMapUI.olb")
GetModule("esriArcCatalogUI.olb")
#**** Helper Functions ****
def NewObj(MyClass, MyInterface):
"""Creates a new comtypes POINTER object where\n\
MyClass is the class to be instantiated,\n\
MyInterface is the interface to be assigned"""
from comtypes.client import CreateObject
try:
ptr = CreateObject(MyClass, interface=MyInterface)
return ptr
except:
return None
def GetCurrentApp():
"""Gets an IApplication handle to the current app.\n\
Must be run inside the app's Python window.\n\
Execute GetDesktopModules() first"""
import comtypes.gen.esriFramework as esriFramework
return NewObj(esriFramework.AppRef, esriFramework.IApplication)
def ArcMap_GetSelectedGeometry(bStandalone=False):
GetDesktopModules()
if bStandalone:
InitStandalone()
pApp = GetApp()
else:
pApp = GetCurrentApp()
if not pApp:
print "We found this spoon, sir."
return
import comtypes.gen.esriFramework as esriFramework
import comtypes.gen.esriArcMapUI as esriArcMapUI
import comtypes.gen.esriSystem as esriSystem
import comtypes.gen.esriCarto as esriCarto
import comtypes.gen.esriGeoDatabase as esriGeoDatabase
import comtypes.gen.esriGeometry as esriGeometry
# Get selected feature geometry
pDoc = pApp.Document
pMxDoc = CType(pDoc, esriArcMapUI.IMxDocument)
pMap = pMxDoc.FocusMap
pFeatSel = pMap.FeatureSelection
pEnumFeat = CType(pFeatSel, esriGeoDatabase.IEnumFeature)
pEnumFeat.Reset()
pFeat = pEnumFeat.Next()
if not pFeat:
print "No selection found."
return
pShape = pFeat.ShapeCopy
eType = pShape.GeometryType
if eType == esriGeometry.esriGeometryPoint:
print "Geometry type = Point"
elif eType == esriGeometry.esriGeometryPolyline:
print "Geometry type = Line"
elif eType == esriGeometry.esriGeometryPolygon:
print "Geometry type = Poly"
else:
print "Geometry type = Other"
return pShape