Select to view content in your preferred language

mapframe.zoomToAllLayers(True) return Traceback/Runtime Error

189
1
Jump to solution
06-21-2024 05:24 AM
Labels (1)
IlkaIllers1
Occasional Contributor III

I am trying to go through the features of a feature class one by one, selecting one feature class and zooming to it. 

aprx= arcpy.mp.ArcGISProject(aprx)
l = aprx.listLayouts("Layout")[0]
mf = l.listElements('MAPFRAME_ELEMENT','Map Frame')[0]
#make feature layer or points
f2 = fc
#make feature layer
fl = arcpy.management.MakeFeatureLayer(f2,f2)
#loop through features
with arcpy.da.SearchCursor(fl, ['OBJECTID']) as cursor:
    for row in cursor:
        # Find OBJECTID of the row
        oid = row[0]
        # Select the row
        where_clause = "{} = {}".format(arcpy.AddFieldDelimiters(fl, "OBJECTID"), oid)
        arcpy.management.SelectLayerByAttribute(fl, "NEW_SELECTION", where_clause)
        #zoom to selection
        mf.zoomToAllLayers(True)

 

When I run this in a completely new aprx, the code runs through without errors - but zooms to the extent of the basemap, even though it should zoom only to selected features. Once I remove the basemap, I get a Traceback:

Traceback (most recent call last):
File "<string>", line 17, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 5541, in zoomToAllLayers
return convertArcObjectToPythonObject(self._arc_object.zoomToAllLayers(*gp_fixargs((selection_only, symbolized_extent), True)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError

Even if I add the basemap again, this is all I get from that point on. What am I doing wrong here?

0 Kudos
1 Solution

Accepted Solutions
IlkaIllers1
Occasional Contributor III

I found the issue - Layout needs to be activated, then the code works.

View solution in original post

0 Kudos
1 Reply
IlkaIllers1
Occasional Contributor III

I found the issue - Layout needs to be activated, then the code works.

0 Kudos