This tutorial demonstrates how to create a custom map layout using the ArcGIS Pro SDK for .NET.
With the ArcGIS Pro SDK for .NET, you can extend ArcGIS Pro with your own unique tools and workflows. Using Microsoft Visual Studio and the Pro SDK, developers can build Pro add-ins and solution configurations that provide users with custom functionality specific to their organization or industry.
The ArcGIS Pro SDK for .NET includes a Map Layout API, which provides classes and members to support managing layouts, layout elements, and working with layout views. You can create new layouts and layout elements, modify existing elements, and manage selections, layout view control, and navigation.
In this tutorial, you will create a new map layout, add layout elements including a map frame and text elements, and then open the layout in a new layout view.
Create a new ArcGIS Pro Add-In Visual Studio Project
Start Visual Studio.
Choose File > New > Project and then from the ArcGIS templates group, select ArcGIS Pro Module Add-in. Name the add-in project "MapLayout".
By default, the Config.daml file opens in Visual Studio. The Module1.cs file contains add-in module code.
Note also in the Config.daml file that the id attribute of the insertModule tag matches the ID within the Module1.cs file and that the className attribute also matches the class name of the module.
To find the ArcGIS templates group, see Templates > Visual C# > ArcGIS. Also confirm that the latest .NET Framework is selected.
Create a new Pro button and edit DAML
Right-click the project and choose Add > New Item. From the ArcGIS Pro Add-ins group > list of templates, select ArcGIS Pro Button.
Name the new class file "BuildNewLayout.cs" and press Add to close the dialog box.
Once added, the BuildNewLayout.cs file should open by default. The Button template also provides the contents of the class file, as well as corresponding code in the Config.daml file for the Pro UI, so the tool can be activated. The Config.daml is updated first.
Open the Config.daml file and modify the button item as follows:
When you update the Config.daml, you change how the tool displays in the Create Features pane UI, providing helpful information to the user about the tool's name and purpose.
Change caption to Build New Layout.
Change the tooltip heading to "Build New Map Layout" and the ToolTip text to "Create a new map layout with layout elements."
Change the group caption to “Layout Tools”.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
<groupid="MapLayout_Group1"caption="Layout Tools"appearsOnAddInTab="true"><!-- host controls within groups --><buttonrefID="MapLayout_BuildNewLayout"size="large" /></group></groups><controls><!-- add your controls here --><buttonid="MapLayout_BuildNewLayout"caption="Build New Layout"className="BuildNewLayout"loadOnClick="true"smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png"largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png"><tooltipheading="Build New Layout">Create a new map layout with layout elements.<disabledText /></tooltip></button>
Create and set up a new layout page
Open the BuildNewLayout.cs file and add the following using directives:
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
using ArcGIS.Core.CIM;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Layouts;
using ArcGIS.Desktop.Mapping;
In the BuildNewLayout.cs file, update the code to match the following for the OnClick method. This creates a new layout and CIM page and applies it to the layout:
Creating a new layout generates a new layout project item in the Layouts folder on the Contents pane. The next logical steps are to create new layout elements. See the ElementFactory for adding new elements to the layout. A new layout project item is not automatically opened in a layout view pane. Use methods from the LayoutFrameworkExtender to open a layout project item in a pane. See also LayoutFactory class.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Create a layout which will be returned from the QueuedTask
Layout newLayout = await QueuedTask.Run<Layout>(()=>
{ // Create a new CIM page
CIMPage newPage = new CIMPage();
// Add properties
newPage.Width =17;
newPage.Height =11;
newPage.Units = LinearUnit.Inches;
// Add rulers
newPage.ShowRulers =true;
newPage.SmallestRulerDivision =0.5;
// Apply the CIM page to a new layout and set name
newLayout = LayoutFactory.Instance.CreateLayout(newPage);
newLayout.SetName("Census Data");
Create a map and map elements and add to the layout
You will continue adding code to the OnClick method.
In this step you create a map and add a layer through code, and then set the MapFrame and then the camera. The MapFrame class primarily manages the placement of maps and scenes on a page layout. You can use Map and SetMap members to get and set the Map associated with a MapFrame. To change the geographic extent within a map frame, use the Camera and SetCamera members to get and set the Camera associated with a MapFrame.
Next you will add code to create a map with census data, add it to the layout, and set the camera. Update the code to match the following:
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Create a new map with an ArcGIS Online basemapMap newMap = MapFactory.Instance.CreateMap("Census Map", MapType.Map, MapViewingMode.Map, Basemap.NationalGeographic);
stringurl = @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer";
Uri uri = new Uri(url);
LayerFactory.Instance.CreateLayer(uri, newMap);
// Build a map frame geometry / envelope Coordinate2D ll = new Coordinate2D(1, 0.5);
Coordinate2D ur = new Coordinate2D(13, 9);
Envelope mapEnv = EnvelopeBuilderEx.CreateEnvelope(ll, ur);
// Create a map frame and add it to the layout MapFrame newMapframe = ElementFactory.Instance.CreateMapFrameElement(newLayout, mapEnv, newMap);
newMapframe.SetName("Map Frame");
// Create and set the camera Camera camera = newMapframe.Camera;
camera.X = -118.465;
camera.Y = 33.988;
camera.Scale = 30000;
newMapframe.SetCamera(camera);
Continue adding code to the OnClick method. This code creates a title and North arrow:
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Add text for title
Coordinate2D titleTxt_ll = new Coordinate2D(4.5, 9.5);
CIMTextSymbol arial36bold = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlueRGB, 80, "Arial", "Bold");
GraphicElement titleTxtElm = ElementFactory.Instance.CreateTextGraphicElement
(newLayout, TextType.PointText, titleTxt_ll.ToMapPoint(), arial36bold, "Census Data", "Title");
titleTxtElm.SetName("Title");
// Add north arrow
// Reference a North Arrow in a style
StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name =="ArcGIS 2D");
NorthArrowStyleItem naStyleItm = stylePrjItm.SearchNorthArrows("ArcGIS North 8")[0];
// Set the center coordinate and create the arrow on the layout
Coordinate2D center = new Coordinate2D(15, 7);
var naInfo = new NorthArrowInfo(){ MapFrameName = newMapframe.Name, NorthArrowStyleItem = naStyleItm };
var arrowElm = ElementFactory.Instance.CreateMapSurroundElement(newLayout, center.ToMapPoint(), naInfo);
arrowElm.SetName("New North Arrow");
arrowElm.SetHeight(2);
Finalize code and test
In this step, you continue to use the ElementFactory to create and add new map elements to your layout.
Finalize the OnClick method with additional code creating a map legend and then opening the layout pane.
After creating a legend, return the layout, close the QueuedTask, and then open the layout with CreateLayoutPaneAsync.