Python Arc Objects
Python Arc Objects
Python
Mark Cederholm
UniSource Energy Services
Why Python?
def GetModule(sModuleName):
import comtypes
from comtypes.client import GetModule
sLibPath = GetLibPath()
GetModule(sLibPath + sModuleName)
GetModule("esriGeometry.olb")
import comtypes.gen.esriGeometry as esriGeometry
[or]
from comtypes.gen.esriGeometry import Point, IPoint
[import * is not recommended]
Creating and casting objects
def CLSID(MyClass):
return str(MyClass._reg_clsid_)
Standalone licensing
pInit = NewObj(esriSystem.AoInitialize, \
esriSystem.IAoInitialize)
eProduct = esriSystem.esriLicenseProductCodeArcEditor
licenseStatus = pInit.IsProductCodeAvailable(eProduct)
if licenseStatus == esriSystem.esriLicenseAvailable:
licenseStatus = pInit.Initialize(eProduct)
return (licenseStatus == esriSystem.esriLicenseCheckedOut)
import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.setproduct("ArcEditor")
Demo: Manipulating an existing
ArcMap or ArcCatalog session
Retrieving an existing session from
outside the application boundary
pApp = GetApp()
. . .
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"
. . .
Creating session objects with
IObjectFactory
If manipulating a session from outside the application boundary,
use IObjectFactory to create new session objects:
pApp = GetApp()
pFact = CType(pApp, esriFramework.IObjectFactory)
pUnk = pFact.Create(CLSID(esriCarto.TextElement))
pTextElement = CType(pUnk, esriCarto.ITextElement)
TIP: At 10.0, you can run a script within the session's Python shell
and create objects normally; use AppRef to get the app handle
http://www.pierssen.com/arcgis/misc.htm