This tutorial demonstrates how to build an add-in that can assign a renderer to your feature layer to change feature symbology.
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, such as the ability to interact with feature layers and their symbology, specific to their organization or industry. You can update layer symbology based upon an existing system style or a custom style that is loaded within your project.
In this tutorial you will create an add-in with two buttons that update the symbology of a point feature layer with either a new symbol or graduated colors.
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 solution
-
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 "LayerSymbology". Click OK to proceed.
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 matches the class name of the module.Name The ArcGIS templates group is located in Templates > Visual C# > ArcGIS. Confirm that the latest .NET Framework is selected.
Create new Pro buttons and update DAML
-
Right-click the project and choose Add > New Item and then from the ArcGIS Pro Add-ins group select ArcGIS Pro Button from the list of item templates.
-
When prompted, name the new class file
Single
and click Add to close the dialog box.Symbol.cs -
Repeat these steps to add a second button, naming this class
Graduated
.Color.cs -
Add the following code to the
On
method ofClick Single
:Symbol.cs Use dark colors for code blocks Copy Module1.ApplySymbol("single symbol");
-
Add the following code to the
On
method ofClick Graduated
:Color.cs Use dark colors for code blocks Copy Module1.ApplySymbol("graduated color");
-
Update the
Config.daml
fileAdd
section so that the DAML appears as follows. This updates the captions for each of the buttons, and the caption of the control group, which will appear on the Add-In tab.In Info Use dark colors for code blocks Copy </AddInInfo> <!-- *** Add *** --> <modules> <insertModule id="LayerSymbology_Module" className="Module1" autoLoad="false" caption="Module1"> <groups> <group id="LayerSymbology_Group1" caption="Point Layer Symbols" appearsOnAddInTab="true"> <!-- host controls within groups --> <button refID="LayerSymbology_SingleSymbol" size="large" /> <button refID="LayerSymbology_GraduatedColor" size="large" /> </group> </groups> <controls> <button id="LayerSymbology_SingleSymbol" caption="Apply Single Symbol" className="SingleSymbol" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png"> <tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip> </button> <button id="LayerSymbology_GraduatedColor" caption="Apply Graduated Color" className="GraduatedColor" loadOnClick="true" smallImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue16.png" largeImage="pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericButtonBlue32.png"> <tooltip heading="Tooltip Heading">Tooltip text<disabledText /></tooltip> </button> </controls> </insertModule> </modules>
Create a new method in the module file
-
Copy the following code to create a new
Apply
method, and paste it into the Module1.cs file below theSymbol Overrides
region.Use dark colors for code blocks Copy #region Business Logic public static void ApplySymbol(string symbolChoice) { QueuedTask.Run(() => { // Check for an active 2D mapview, if not, then prompt and exit. if (MapView.Active == null || (MapView.Active.ViewingMode != ArcGIS.Core.CIM.MapViewingMode.Map)) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("An active 2D MapView is required to use this tool. Exiting...", "Info"); return; } // Get the layer(s) selected in the Contents pane, if there is not just one, then prompt then exit. if (MapView.Active.GetSelectedLayers().Count != 1) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("One feature layer must be selected in the Contents pane. Exiting...", "Info"); return; } // Check to see if the selected layer is a feature layer, if not, then prompt and exit. var featLayer = MapView.Active.GetSelectedLayers().First() as FeatureLayer; if (featLayer == null) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("A feature layer must be selected in the Contents pane. Exiting...", "Info"); return; } // Check if the feature layer shape type is point, if not, then prompt and exit. else if (featLayer.ShapeType != esriGeometryType.esriGeometryPoint) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Selected feature layer must be shape type POINT. Exiting...", "Info"); return; } try { // If symbolChoice is a single symbol if (symbolChoice == "single symbol") { // Get all styles in the project var styles = Project.Current.GetItems<StyleProjectItem>(); // Get a specific style in the project StyleProjectItem style = styles.First(s => s.Name == "ArcGIS 2D"); // Get the Push Pin 1 symbol var pt_ssi = style.SearchSymbols(StyleItemType.PointSymbol, "Push Pin 1").FirstOrDefault(); // Create a new renderer definition and reference the symbol SimpleRendererDefinition srDef = new SimpleRendererDefinition(); srDef.SymbolTemplate = pt_ssi.Symbol.MakeSymbolReference(); // Create the renderer and apply the definition CIMSimpleRenderer ssRenderer = (CIMSimpleRenderer)featLayer.CreateRenderer(srDef); // Update the feature layer renderer featLayer.SetRenderer(ssRenderer); } else if (symbolChoice == "graduated color") { // Get a style and color ramp to apply to the renderer StyleProjectItem style = Project.Current.GetItems<StyleProjectItem>().First(s => s.Name == "ColorBrewer Schemes (RGB)"); IList<ColorRampStyleItem> colorRampList = style.SearchColorRamps("Greens (Continuous)"); ColorRampStyleItem aColorRamp = colorRampList[0]; // Create a graduated color renderer definition with 3 breaks, a populated numeric field named "Symbol" is required GraduatedColorsRendererDefinition gcDef = new GraduatedColorsRendererDefinition() { ClassificationField = "Symbol", ClassificationMethod = ArcGIS.Core.CIM.ClassificationMethod.EqualInterval, BreakCount = 4, ColorRamp = aColorRamp.ColorRamp, SymbolTemplate = SymbolFactory.Instance.ConstructPointSymbol().MakeSymbolReference(), }; // Create the renderer and apply the definition CIMClassBreaksRenderer cbRndr = (CIMClassBreaksRenderer)featLayer.CreateRenderer(gcDef); // Update the feature layer renderer featLayer.SetRenderer(cbRndr); } } catch (Exception exc) { // Catch any exception found and display in a message box ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught: " + exc.Message); return; } }); } #endregion
Update the dataset and test the add-in
To update layer symbology to graduated colors requires a numeric attribute named Symbol
on which to apply an equal interval classification. One method is to add a new field to the previously downloaded ArcGIS Pro SDK community sample dataset, FeatureTest, which is typically located in C
.
The following steps assume that you are using this dataset for testing.
-
Build your project and resolve any issues.
-
Click Start Debugging to run your project, which will launch ArcGIS Pro. Open the FeatureTest.aprx project.
-
In the map view, find the layer named TestPoints. Add a new integer attribute named Symbol to this layer and then calculate its values to match the values in the
Object
attribute.ID You can use the following Geoprocessing tools to add a new field and calculate values:
<https
://pro.arcgis.com/en/pro-app/tool-reference/data-management/add-field.htm > <https
://pro.arcgis.com/en/pro-app/tool-reference/data-management/calculate-field.htm >
-
Once the new Symbol attribute is available and populated, from the Add-In tab find your new control group with your two new buttons. Test both of your buttons with the TestPoints point feature layer to see it rendered with either a new single symbol or graduated colors.
What's Next?
Learn how to use additional ArcGIS Pro SDK for .NET features in these tutorials: