Get and set layout element anchor points in ArcPy

2042
10
05-17-2011 12:13 PM
Status: Already Offered
Labels (1)
TannerSemerad1
New Contributor II
I need to be able to get and set the anchor points of layout elements in ArcPy. Currently, it is impossible for arcpy to know whether the layout elements of any given MapDocument are on or off the layout. You can access the XY positions of the elements, but these are in relation to their anchor point which could be different for each element. This would be very helpful for map automation scripts, as I currently have to make separate versions of my scripts for MXDs that have slightly different layout elements or page sizes.

10 Comments
ShannonDeArmond

Agreed. If I could even read which anchor point the layout item was using, even if I couldn't change it, that would be tremendously helpful.

HeshamOthman1

Hi,

Any update on this? did you find a solution? i'm facing the same issue

GrantHaynes

This would be very helpful, right now I have to validate the anchor point positions by hand which kind of defeats the purpose of automating with python

KennethEggering1

Completely agree with other commenters here, and here. This would make automation in Pro/Map tremendously easier. ESRI, can you please add an "elementAnchor" property to the different layout elements.

ArcGIS_ElementAnchorPropertyNeeded.jpg

RossVolkwein1

+1 for this idea

JeffBarrette

Starting with Pro 2.5, this can be accomplished using Python CIM Access

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

Here is a simple snippet:

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts()[0]
lyt_cim = lyt.getDefinition('V3')
for e in lyt_cim.elements:
   if e.name == 'Rectangle':
       print(elm.anchor)


###BottomLeftCorner

JeffBarrette
Status changed to: Already Offered

Starting with Pro 2.5, this can be accomplished using Python CIM Access

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

Here is a simple snippet:

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts()[0]
lyt_cim = lyt.getDefinition('V2')   #for 2.x and '3.0' for 3x
for e in lyt_cim.elements:
   if e.name == 'Rectangle':
       print(elm.anchor)


###BottomLeftCorner

 

RossVolkwein1

Cool! Thanks for the update.

I received an error saying I needed to use cim_version 'V2' but that seems to work okay.

Are you supposed to be able to set the anchor as well? It looks like it works, but the actual element properties in the layout don't seem to update.

Also, type, should be " print(e.anchor)"

JeffBarrette

@RossVolkwein1 Sorry about that.  I'm using Pro 3.0.  I'll update the script above.

Jeff

AK20221130

For anyone in the future wondering how to set the anchor point, heres a code snipped from the resources provided by esri (ArcGIS Pro 2.5 CIM Samples V1 - Overview)

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts('GreatLakes')[0]

lyt_cim = lyt.getDefinition("V2")     #Get the layout's CIM definition

#Iterate though all layout elements to find the PointText element
for elm in lyt_cim.elements:
  if elm.name == "Point Text":
    txt = elm
    elm.anchor = 'CenterPoint'        #Change anchor position

lyt.setDefinition(lyt_cim)            #Set the layout's CIM definition

 

 

The last line (12) is required for the anchor to change