I have been trying to automate the map production in ArcGIS Pro 2.9.1
There is a point in the production where it has to turn on and off some of the elements in the layout and I figured how to do it as well.
this is the code I'm using:
am_lyt=lyt.listElements(wildcard="Name_of_the_table")[0]
print(am_lyt.visible)
am_lyt.visible=True
print(am_lyt.visible)
This gives the result:
False
True
But the table is not turning on and there's no error either. Surpisingly, when I executed the below code, I could see the action on the layout, its just the visibility not working
mf.camera.setExtent(mf.getLayerExtent(sitelyr, False, True))
print ("x")
I tried using the CIM:
cim_lyt=lyt.getCIM()
but this line of code is not executing, so I tried the below:
cim_lyt=lyt.getDefinition('V2')
Sadly getDefinition kept giving me it has to attribute error.
Does anyone know how to resolve this?
Hmmm... This code works for me. No matter if the layout is opened or not, no matter the element (eg north arrow works too), and no matter whether I execute in a notebook or in the Python Window.
lyt = arcpy.mp.ArcGISProject("current").listLayouts()[0]
print(f"Layout name: {lyt.name}")
e = lyt.listElements(wildcard="Table Frame")[0]
print(f"Element name: {e.name}")
print(f"{e.name} is visible: {e.visible}")
e.visible = not e.visible
print(f"{e.name} is visible: {e.visible}")
cim = lyt.getDefinition("V2")
print(f"CIM: {cim}")
Element name: Table Frame Table Frame is visible: False Table Frame is visible: True CIM: <arcpy.cim.CIMLayout.CIMLayout object at 0x00000265A2EA4CC8>
Are you sure you're manipulating the correct layout?
Can you share the whole code?
Thank you for waiting. I believe I'm manipulating the correct layout. Here's the code:
import arcpy
import pandas as pd
import os
abspath=os.path.abspath
abspath=os.path.abspath("__file__")
dname=os.path.dirname(abspath)
os.chdir(dname)
ProjectFolder=dname
print(dname)
arcpy.env.workspace=ProjectFolder
project=ProjectFolder +"\ContamCertificates.aprx"
aprx=arcpy.mp.ArcGISProject(project)
mapName= "Map"
layoutName="CTXX"
site= "siteboundary"
m=aprx.listMaps(mapName)[0]
sitelyr=m.listLayers(site)[0]
lyt=aprx.listLayouts(layoutName)[0]
mf = lyt.listElements("mapframe_element", "Map")[0]
mf.camera.setExtent(mf.getLayerExtent(sitelyr, False, True))
print ("x")
arcpy.env.overwriteOutput = True
NSW_CLR_STATUS_IN_Current = "NSW_CLR_STATUS_IN_Current"
siteboundary = "siteboundary"
# Process: Select Layer By Location (Select Layer By Location) (management)
NSW_CLR_STATUS_IN_Current, Output_Layer_Names, Count = arcpy.management.SelectLayerByLocation(in_layer=[NSW_CLR_STATUS_IN_Current], overlap_type="INTERSECT", select_features=siteboundary, search_distance="500 Meters", selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT")
print (arcpy.GetCount_management(NSW_CLR_STATUS_IN_Current))
if arcpy.GetCount_management(NSW_CLR_STATUS_IN_Current) != 0:
print("the site is medium risk")
#Results = lyt.listElements("GRAPHIC_ELEMENT", "Results")[0]
#Print(results)
rslt_within500=lyt.listElements("TEXT_ELEMENT", "Note_within_500m")[0]
rslt_within500.visible=True
print(rslt_within500.visible)
am_lyt=lyt.listElements(wildcard="Name_of_the_table")[0]
print (am_lyt.visible)
am_lyt.visible=True
print(am_lyt.visible)
fn = ProjectFolder+"\ContaminationCertificate.pdf"
lyt.exportToPDF(fn)
I tried your style
am_lyt.visible = not am_lyt.visible
This isn't working either.
Thank you for looking at this
Funny enough, I tried running the same code as a python script and the visibility feature works but not the zoom to the feature extents, in fact, it zooms out to the entire world with this line
mf.camera.setExtent(mf.getLayerExtent(sitelyr, False, True))