ArcGIS Pro: Arcpy View Layout (make viewable)

3053
10
03-16-2020 07:53 PM
Status: Implemented
Labels (1)
GeoNZ
by
Occasional Contributor

There is a method in arcpy to import a layout into a ArcPro document (aprx.importDocument('path to pagx').

This imports it into the project but does not show the layout by default so a user may not know that the layout had been added (i.e. it requires use of the Catalog view in the UI to navigate to that layout and then view it/open it).

There are also other project items which it would be useful to have arcpy control over their visibility status in the main stage (i.e. maps, scenes)

Please add a property on maps, layouts, scenes etc to control their visibility.

i.e.

aprx = arcpy.mp.ArcGISProject("Current")

aprx.importDocument("path to pagx file")

layout = aprx.listLayouts('pagx layout I just imported")[0]

layout.visible = True # This will show the layout in the main stage

layout.visible = False # This will remove the layout from the main stage, but still keep it in the project.

10 Comments
JeffBarrette
Status changed to: In Product Plan

This is addressed in Pro 3.0.  Similar to your idea, we have an OpenView() method on the Layout and Map objects.  We also have a ArcGISProject.CloseViews() method too.

So to address your scenario, before importing a pagx, you might want to also close all other map and layout views.

p = arcpy.mp.ArcGISProject('current')
p.importDocument(pagx_path)
p.closeViews() #Default is "MAPS_AND_LAYOUTS"  or "MAPS" or "LAYOUTS"
lyt = p.listLayouts('MyLayout')[0]
lyt.openView()

I hope you are excited by this new feature.

Jeff - Layout and arcpy.mp Teams

KoryKramer
Status changed to: Implemented

This functionality has been added in ArcGIS Pro 3.0. See Ideas in ArcGIS Pro 3.0  to see this and all of the other ideas you can look forward to when you upgrade.

Also be sure to check out the What's New documentation: https://pro.arcgis.com/en/pro-app/latest/get-started/whats-new-in-arcgis-pro.htm 

taylor
by

Hello there. I'm trying to use the openView() method, configured the same way as in your example above, and I get the following traceback:

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

Please advise.

JeffBarrette

@taylor can you please provide more information.  Can I see more of the script?  Are you running it from a script tool, the Python Window?

I can NOT reproduce your scenario, I'll need more information.

Jeff - Layout and arcpy.mp teams

taylor
by

Good morning @JeffBarrette , here's the section of my script that depends on openView() to be worth running:

#import arcpy, set variables
import arcpy
foo = "AOI"
dlDate = "10102022"
lOr = 'P'
#set workspace and project
arcpy.env.workspace = fr"C:\mypath\{AOI}_{dlDate}.gdb"
projAGP = arcpy.mp.ArcGISProject("CURRENT")
#import portrait or landscape template depending on lOr var value
if
lOr == 'P':
projAGP.importDocument(r"D:\localGISws_TPC\ESH21_mapbook_wkspc\layoutFiles\tempPortrait.pagx")
if lOr == 'L':
projAGP.importDocument(r"D:\localGISws_TPC\ESH21_mapbook_wkspc\layoutFiles\tempLandscape.pagx")

#rename layout using string stored in AOI var
allLayouts = projAGP.listLayouts()
for l in allLayouts:
if l.name == 'tempPortrait_new':
l.name = AOI
if l.name == 'tempLandscape_new':
l.name = AOI

#verify existence of layout named {AOI}
currLayoutNm = projAGP.listLayouts(AOI)[0]
print(f'Current Layout is {currLayoutNm.name}.')

#trying to pass Layout object to variable
#(this is just the latest approach I've tried; seems to me I could just pass currLayoutNm
#to openView as well. Here, I'm rebuilding a layout list, in case .listLayouts() is referencing
#some list that's stuck in memory (non-refreshed) or some other random thing

fullLayoutList = []
for l in projAGP.listLayouts():
fullLayoutList.append(l.name)
iPos = fullLayoutList.index(AOI)
print(iPos)

currLayout = projAGP.listLayouts()[iPos]
currLayout.openView()
#breaks with the RunTimeError referenced in previous post, 90+% of the time.
#Oddly (and infuriatingly) sometimes if I wait for a while and pass this command,
#it works and opens the layout.
taylor
by

@JeffBarrette I'm writing my script in PyCharm and pasting it to the Python Window.

The thing here that has really burned a lot of time and patience is that the openView() works sometimes. I'm sorry to say, but this behavior is consistent with everything in the ESRI universe.

Another example of what I'll generously call "quirkiness" is that setting the workspace environment wouldn't "take". Anytime I tried to reference a dataset, I would receive errors indicating it didn't exist, and trying to retrieve field lists would return []. Yet when I would run this part of my script in PyCharm (yes, referencing the same python interpreter as AGP and the Python Window), it would work just fine. What's the deal here? I've seen a lot of reports of this kind of behavior on many of the forums. Until the Python Window (or Notebook, which is even worse), can be made to behave reliably, is there any way to integrate PyCharm to the AGP interface?

Hoping there's something wrong with my script, so I can leverage arcpy to get work done efficiently, rather than use the AGP interface to edit the many layouts I'm trying to get done. Do I need to pre-configure the Python Window in some way? Thanks in advance

JeffBarrette

@taylor I was able to reproduce your error.  I've created a bug in our system and we are investigating it.

Thanks you!

Jeff - Layout and arcpy.mp teams

taylor
by

Thanks @JeffBarrette for your response. Let me know what you all come up with!

jdaniel

@JeffBarrette I'm having the same issue with openView(). I am able to use importDocument() successfully, but openView() will not display my imported Layout. 

JeffBarrette

@jdaniel this was addressed in 3.1 based on @taylor 's workflow and scripts.

Please test when ArcGIS Pro 3.1.  It is scheduled to be released in late Feb.

 

Jeff - Layout and arcpy.mp teams