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.
Prerequisites
- Microsoft Visual Studio is required. See the ArcGIS Pro system requirements for latest version and compatibility.
- Install ArcGIS Pro version 3.3 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".
By default, the
Config.daml
file opens in Visual Studio. TheModule1.cs
file contains add-in module code.Note also in the
Config.daml
file that theid
attribute of theinsert
tag matches the ID within theModule Module1.cs
file and that theclass
attribute also matches the class name of the module.Name 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
Build
file should open by default. The Button template also provides the contents of the class file, as well as corresponding code in theNew Layout.cs Config.daml
file for the Pro UI, so the tool can be activated. TheConfig.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
toBuild New Layout
. -
Change the
tooltip heading
to"
and the ToolTip text toBuild New Map Layout" "
Create a new map layout with layout elements." -
Change the group
caption
to“
.Layout Tools”
Use dark colors for code blocks Copy <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
Build
file and add the following using directives:New Layout.cs Use dark colors for code blocks Copy 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
Build
file, update the code to match the following for theNew Layout.cs On
method. This creates a new layout and CIM page and applies it to the layout:Click 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 blocks Copy // 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 On
method.
In this step you create a map and add a layer through code, and then set the Map
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 Map
. To change the geographic extent within a map frame, use the Camera
and Set
members to get and set the Camera associated with a Map
.
- 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 = @"https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/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
On
method. This code creates a title and North arrow:Click Use dark colors for code blocks Copy // 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
On
method with additional code creating a map legend and then opening the layout pane.Click After creating a legend, return the layout, close the
Queued
, and then open the layout withTask Create
.Layout Pane Async Use dark colors for code blocks Copy // 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.
-
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: