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.
Tip
For more information, see the ArcGIS Pro wiki layouts document, and review layout code snippets, or see the ArcGIS Pro SDK API reference. You can also review the ArcGIS Pro user community's layout samples.
Prerequisites
- Microsoft Visual Studio is required. See the ArcGIS Pro system requirements for latest version and compatibility.
- Install ArcGIS Pro version 3.0 or higher.
- Install the ArcGIS Pro SDK for .NET. See the Installation ProGuide for more information.
- This tutorial uses Pro SDK Community sample data.
Steps
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".More info
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.More info
- Open the
Config.daml
file and modify the button item as follows:More info
<group id="MapLayout_Group1" caption="Layout Tools" appearsOnAddInTab="true">
<!-- host controls within groups -->
<button refID="MapLayout_BuildNewLayout" size="large" />
</group>
</groups>
<controls>
<!-- add your controls here -->
<button id="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">
<tooltip heading="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:
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 theOnClick
method. This creates a new layout and CIM page and applies it to the layout:More info
// 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.
More info
- 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:
// Create a new map with an ArcGIS Online basemap
Map newMap = MapFactory.Instance.CreateMap("Census Map", MapType.Map, MapViewingMode.Map, Basemap.NationalGeographic);
string url = @"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:
// 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.More info
// Add legend - first build 2D envelope geometry
Coordinate2D leg_ll = new Coordinate2D(13.5, 0.5);
Coordinate2D leg_ur = new Coordinate2D(16.5, 4.0);
Envelope leg_env = EnvelopeBuilderEx.CreateEnvelope(leg_ll, leg_ur);
// Create legend
var legInfo = new LegendInfo() { MapFrameName = newMapframe.Name };
var legendElm = ElementFactory.Instance.CreateMapSurroundElement(newLayout, leg_env, legInfo);
legendElm.SetVisible(true);
legendElm.SetName("New Legend");
return newLayout;
});
var layoutPane = await ProApp.Panes.CreateLayoutPaneAsync(newLayout);
- Once your code is completed, build your project and debug any issues. Start the project, which will launch ArcGIS Pro.
Note:
You must be connected to the internet while running this add-in in order to access the ArcGIS Online dataset referenced in the code.
- Create a new project when the ArcGIS Pro start page appears. From the Add-in tab and find your new button.
- Press your new button to run the code and open the new layout.
What's Next?
Learn how to use additional ArcGIS Pro SDK for .NET features in these tutorials:
- Build your first configuration
- Manage add-in loading
- Edit attribute data