I am attempting (and failing) to add an image to a map layout using ArcObjects via Python comtypes. I do need to run this outside of ArcMap (i.e. not in the ArcMap Python window).
The basic idea is: open mxd, place jpg in layout, save/close mxd.
Python solution preferred, but I would also accept a similar workflow in VB.NET.
Using ArcGIS 10.2.2, Python 2.7.5
Script:
from comtypes.client import GetModule, CreateObject
from Snippets import GetLibPath, InitStandalone
def CType(obj, interface):
"""Casts obj to interface and returns comtypes POINTER or None"""
try:
newobj = obj.QueryInterface(interface)
return newobj
except:
return None
def main():
InitStandalone()
modCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriCarto.olb")
modGeom = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriGeometry.olb")
modArcMapUI = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriArcMapUI.olb")
modFramework = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriFramework.olb")
app = CreateObject(modFramework.AppROT, interface=modFramework.IAppROT)
mxDoc = CType(app.Item(0).Document,modArcMapUI.IMxDocument)
layout = mxDoc.PageLayout
graphicContainer = CType(layout, modCarto.IGraphicsContainer)
pictureElement = CreateObject(modCarto.JpgPictureElement, interface=modCarto.IPictureElement)
envelope = CreateObject(modGeom.Envelope, interface=modGeom.IEnvelope)
envelope.PutCoords(0, 0, 5, 5)
pictureElement.Geometry = envelope
pictureElement.ImportPictureFromFile(r'C:\junk\happy.jpg')
iElement = CType(pictureElement,modCarto.IElement)
graphicContainer.AddElement(pictureElement,0) # ERROR HERE
#graphicContainer.AddElement(iElement,0)
app.Item(0).SaveDocument()
if __name__ == '__main__':
main()
Traceback (whether attempting to add pictureElement or iElement):
Traceback (most recent call last):
File "C:\Scripts\arcobjects_add_jpeg.py", line 54, in <module>
main()
File "C:\Scripts\arcobjects_add_jpeg.py", line 47, in main
graphicContainer.AddElement(pictureElement,0)
COMError: (-2147024809, 'The parameter is incorrect.', (None, None, None, 0, None))
Full disclosure: I am absolutely new with ArcObjects, so whether or not I am handling/creating/juggling objects between interfaces correctly, I am not sure, but I think I'm close.



