2

I am attempting to add a new MapFrame to an existing Layout Object in an ArcGIS Pro Project;

import arcpy

aprx = arcpy.mp.ArcGISProject("CURRENT")
layout = aprx.listLayouts("LayoutOne")[0]
layout_cim = layout.getDefinition("V2")

new_mapframe = arcpy.cim.CreateCIMObjectFromClassName("CIMMapFrame", "V2")

layout_cim.mapFrame = new_mapframe
layout.mapFramename = "Map"

layout.setDefinition(layout_cim)

When this code is executed, I see a "flash" in the Layout Tab, but no new MapFrame.

I have also tried omitting the line layout.mapFramename = "Map", the same thing happened.

1 Answer 1

2

A layout can have more than one MapFrame so when you realise that then you realise that it's a collection object that you need to be inserting your MapFrames into, typically in python its a list. In the code below I get a handle on the elements of the layout and append a new MapFrame. But I think you need to setup a lot of properties for it to function correctly. What will definitely help you when accessing the CIM object model is to have the CIM viewer AddIn installed as it allows you to navigate the object model and see how the various properties are set.

import arcpy

aprx = arcpy.mp.ArcGISProject("CURRENT")
layout = aprx.listLayouts("Funky")[0]
layout_cim = layout.getDefinition("V2")

# Get layout elements and append frame to it
elements = layout_cim.elements
new_mapframe = arcpy.cim.CreateCIMObjectFromClassName("CIMMapFrame", "V2")
new_mapframe.name = "my mapframe 4"
elements.append(new_mapframe)

layout.setDefinition(layout_cim)
5
  • Thank you for the heads up, I was able to execute the above code with layout_cim.elements.append(new_mapframe) instead - I still wasn't able to see the new MapFrame in layout. Is this potentially due to an incomplete CIM and in need of further investigation with the CIM viewer SDK add-in?
    – Rose
    Commented Apr 29, 2022 at 10:33
  • Did you look at the contents pane, rather than the layout page? It should have appeared, but think about what you have not done. You have not set its size or contents so why would you see anything? A MapFrame needs to be linked to a map with an extent and spatial reference, you have not done that too. That's what I was hinting at about all the other properties that need setting.
    – Hornbydd
    Commented Apr 29, 2022 at 10:46
  • I looked at the contents page, was asking for further clarification. Makes sense to me now why it won't be showing up, thank you again. Will have a look at the CIM Viewer Add In to see if I can do this task.
    – Rose
    Commented Apr 29, 2022 at 10:54
  • 1
    Another approach you could investigate is to clone an existing MapFrame and then tweak the properties. I know you can clone say text elements but I have not tried it with MapFrames. Food for thought.
    – Hornbydd
    Commented Apr 29, 2022 at 11:53
  • Unfortunately I tried to do this with an .pagx file with a MapFrame containing just a Map (has to be a map in there - otherwise would need to delete MapFrame) using aprx.importDocument() + arcpy.mp.ConvertLayoutFileToLayout() with no luck
    – Rose
    Commented May 1, 2022 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.