I was planning to write a script which would save a copy of aprx from the template and perform the zoom on all Layouts. Just to simplify things I will give an example of one Layout. On Layout called Elevation, zoom should be performed to Site Boundary (always different size, geometry, and location). See below what I have prepared, but it is not performing the zoom. I think I don't call correctly newly created aprx, but I can not figure out where is the issue. If I create and aprx and run the zoom on the opened aprx (using "CURRENT") it works very well, but I would like to do the same without needing to open it. Below is the script and I would really appreciate any feedback
import arcpy, os
import math
def path_file(path):
path_split = path.split(os.sep)
site_name = arcpy.GetParameterAsText(0)
out_path = arcpy.GetParameterAsText (1)
aprx = arcpy.mp.ArcGISProject(r"C:\...\Template.aprx")
aprx.saveACopy(out_path + os.sep + site_name + ".aprx")
new_aprx = arcpy.mp.ArcGISProject(out_path + os.sep + site_name + ".aprx")
m = new_aprx.listMaps("Elevation")[0]
lyr = m.listLayers("Site Boundary")[0]
lyt = new_aprx.listLayouts("Elevation")[0]
mf = lyt.listElements("mapframe_element", "Map" )[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True ))
if mf.camera.scale < 1000:
mf.camera.scale = 1000
else: mf.camera.scale = mf.camera.scale * 1.15
mf.camera.scale = math.ceil(mf.camera.scale/2000)*2000
Hi, I think the issue is when you define mf variable
it should be
Don't forget to delete variable aprx and new aprx using del
Hopefully it helps you
Cheers.
HI Alexander Daniel_Pratama, thanks for getting back to me. I have tried your suggestion, but sorry it doesn't work. Anyway I will keep trying
That's spirit!
Actually I ran your code several times, and it perfectly works. But I modified the map_frame string as MAPFRAME_ELEMENT", "Map Frame* as I suggest.
You can see the difference of this two images below
scale 6000 with mf.camera.scale * 1.15
scale 16000 with mf.camera.scale * 3
Here's your code. NOTES: please do not mind of arcpy.GetParameterAsText be removed, I just use the string to main code is fine or not.
import arcpy
import os
import math
site_name = 'test'
out_path = 'C:\..\TEMPLATE_COPY'
aprx = arcpy.mp.ArcGISProject(r"C:\..\TEMPLATE\LAYOUT_1.aprx")
aprx.saveACopy(out_path + os.sep + site_name + ".aprx")
new_aprx = arcpy.mp.ArcGISProject(out_path + os.sep + site_name + ".aprx")
m = new_aprx.listMaps("First Page Map")[0]
lyr = m.listLayers("POI")[0]
lyt = new_aprx.listLayouts("Layout")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map Frame*")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True ))
if mf.camera.scale < 1000:
mf.camera.scale = 1000
else: mf.camera.scale = mf.camera.scale * 1.15
mf.camera.scale = math.ceil(mf.camera.scale/2000)*2000
lyt.exportToJPEG(os.path.join(out_path, 'Sample.jpeg'), resolution=200)
del new_aprx
del aprx
I did not open the aprx also. I am using ArcGIS Pro 3.2. However, I believe the Arcgis pro version does not affect the code. If everything does not work, I suggest updating the ArcGIS Pro if you do not have other concerns.
thanks again AlexanderDaniel_Pratama, still not working I am getting error on
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map Frame*")[0]
possible asterisk*? . Using ArcGIS Pro version 2.9.5. Anyway I have figure out the way, to export every Layout after being zoomed in. so added a line at the end of my scritpt
lyt.exportToPDF(os.path.join(out_path, lyt.name + ".pdf"), resolution=300)
which is an idea I used from you answer. All works fine now, so thanks a bunch