Is there a way to open an existing layout programmatically using ArcGIS Pro 3.1?

254
5
11-11-2024 06:20 PM
OscarYam
Occasional Contributor

I was asked to develop a function to export a PDF with a specific layout file that already exists but is not included in the project file. I came across this topic, which was posted 9 years ago. So, is there a way to open an existing layout programmatically using ArcGIS Pro 3.1?


Remark on 15Nov2024: I know the location of the specific layout file.

Tags (1)
0 Kudos
5 Replies
DiegoMCastellari
Emerging Contributor

I don't know if this is goint to help, because in your case the layout is not included in the project. In my case, I had to import the Arcmap layouts into de ArcGIS Pro project, but I don't know if that's an option for you. 

I once had to create a tool like this in ArcGIS Pro. The complete code is in the .py file attached. The tool opens the layout in the project, filters the printing area by a polygon, and exports several pdfs files (depending on the scale). 

0 Kudos
OscarYam
Occasional Contributor

Since users are free to import the layout as they desire. Therefore, I will never know if they imported the layout. But I know the location of the layout file I needed. It would be easier to open/import the layout programmatically. I will take some time to study your Python script to check if it suits my case.

Thank you for your reply.

0 Kudos
StephenRhea_NV5
Frequent Contributor

Code from the ItemFactory.Create, LayoutProjectItem, Layout.Export API pages. Note that there are several independent calls to QueuedTask that should probably be grouped into a single call. That's bitten me in the butt before. Also note that viewing the layout via CreateLayoutPaneAsync isn't necessary if you're doing a direct export.

//Import a pagx into a project.
//Create a layout project item from importing a pagx file
await QueuedTask.Run(() =>
{
  IProjectItem pagx = ItemFactory.Instance.Create(
                            @"C:\Temp\Layout.pagx") as IProjectItem;
  Project.Current.AddItem(pagx);
});

//Open a layout project item in a new view.

//Reference a layout project item by name
LayoutProjectItem someLytItem = Project.Current.GetItems<LayoutProjectItem>()
                          .FirstOrDefault(item => item.Name.Equals("MyLayout"));

//Get the layout associated with the layout project item
Layout layout = await QueuedTask.Run(() => someLytItem.GetLayout());  //Worker thread

//Create the new pane - call on UI
ILayoutPane iNewLayoutPane = await ProApp.Panes.CreateLayoutPaneAsync(layout); //GUI thread

//Export a layout to PDF
//Create a PDF export format
PDFFormat pdf = new PDFFormat()
{
  OutputFileName = filePath,
  Resolution = 300,
  DoCompressVectorGraphics = true,
  DoEmbedFonts = true,
  HasGeoRefInfo = true,
  ImageCompression = ImageCompression.Adaptive,
  ImageQuality = ImageQuality.Best,
  LayersAndAttributes = LayersAndAttributes.LayersAndAttributes
};

//Check to see if the path is valid and export
if (pdf.ValidateOutputFilePath())
{
  layout.Export(pdf);  //Export the PDF 
}

 

0 Kudos
OscarYam
Occasional Contributor

Thank you for your reply. I will take some time to test it out.

0 Kudos
OscarYam
Occasional Contributor

I am in the middle of testing and wondering how to export after the layout rendering is completed.

Also, after line 20 of your script, there will be two layout panes.

0 Kudos