/// <summary> /// Apply the selected symbol to the selected feature layers in the TOC whose geometry type match the symbol type. /// </summary> private Task ApplySelectedSymbol() { if (_selectedSymbol == null) { return(Task.FromResult(0)); } return(QueuedTask.Run(() => { //Get symbol from symbol style item. var symbol = _selectedSymbol.Symbol; var mapView = MapView.Active; if (mapView == null) { return; } //Get the feature layer currently selected in the Contents pane var selectedLayers = mapView.GetSelectedLayers().OfType <FeatureLayer>().Where(l => IsMatchingShapeType(l, symbol)); //Only one feature layer should be selected in the Contents pane if (selectedLayers.Count() == 0) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Select at least one feature layer in the TOC whose geometry type matches the symbol's geometry type.", "No Valid Layer Selected"); return; } var symbolReference = SymbolFactory.MakeSymbolReference(symbol); foreach (var layer in selectedLayers) { ////Create a simple renderer //var renderer = layer.CreateRenderer(new SimpleRendererDefinition(symbolReference)); //layer.SetRenderer(renderer); //Create a unique value renderer var fieldDescription = layer.GetFieldDescriptions().FirstOrDefault(f => f.Type == ArcGIS.Core.Data.FieldType.String); if (fieldDescription != null) { var uvr = layer.CreateRenderer(new UniqueValueRendererDefinition(new string[1] { fieldDescription.Name }, symbolReference)); layer.SetRenderer(uvr); } } })); }
/// <summary> /// Apply the selected symbol to the selected feature layers in the TOC whose geometry type match the symbol type. /// </summary> private Task ApplySelectedSymbol() { if (_selectedSymbol == null || string.IsNullOrEmpty(_selectedSymbol.Key)) { return(Task.FromResult(0)); } return(QueuedTask.Run(() => { //Get symbol from symbol style item. var symbol = _selectedSymbol.Symbol; var mapView = MapView.Active; if (mapView == null) { return; } //Get the feature layer currently selected in the Contents pane var selectedLayers = mapView.GetSelectedLayers().OfType <FeatureLayer>().Where(l => IsMatchingShapeType(l, symbol)); //Only one feature layer should be selected in the Contents pane if (selectedLayers.Count() == 0) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Select at least one feature layer in the TOC whose geometry type matches the symbol's geometry type.", "No Valid Layer Selected"); //Clear the current selection in gallery _selectedSymbol = null; NotifyPropertyChanged(() => SelectedSymbol); return; } var symbolReference = SymbolFactory.MakeSymbolReference(symbol); foreach (var layer in selectedLayers) { var renderer = layer.CreateRenderer(new SimpleRendererDefinition(symbolReference)); layer.SetRenderer(renderer); } })); }
//Create a CIMSymbol from style item selected in the results gallery list box and //apply this newly created symbol to the feature layer currently selected in Contents pane private async void ApplyTheSelectedSymbol(SymbolStyleItem selectedStyleItemToApply) { if (selectedStyleItemToApply == null || string.IsNullOrEmpty(selectedStyleItemToApply.Key)) { return; } await QueuedTask.Run(() => { //Get the feature layer currently selected in the Contents pane var selectedLayers = MapView.Active.GetSelectedLayers(); //Only one feature layer should be selected in the Contents pane if (selectedLayers.Count != 1) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Select the feature layer to which you want to apply the selected symbol. Only one feature layer should be selected."); //Clear the current selection in gallery _selectedStyleItem = null; NotifyPropertyChanged(() => SelectedStyleItem); return; } FeatureLayer ftrLayer = selectedLayers[0] as FeatureLayer; //The selected layer should be a feature layer if (ftrLayer == null) { return; } //Get symbol from symbol style item. CIMSymbol symbolFromStyleItem = selectedStyleItemToApply.Symbol; //Make sure there isn't a mismatch between the type of selected symbol in gallery and feature layer geometry type if ((symbolFromStyleItem is CIMPointSymbol && ftrLayer.ShapeType != esriGeometryType.esriGeometryPoint) || (symbolFromStyleItem is CIMLineSymbol && ftrLayer.ShapeType != esriGeometryType.esriGeometryPolyline) || (symbolFromStyleItem is CIMPolygonSymbol && ftrLayer.ShapeType != esriGeometryType.esriGeometryPolygon && ftrLayer.ShapeType != esriGeometryType.esriGeometryMultiPatch)) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("There is a mismatch between symbol type and feature layer geometry type."); //Clear the current selection in gallery _selectedStyleItem = null; NotifyPropertyChanged(() => SelectedStyleItem); return; } //Get simple renderer from feature layer CIMSimpleRenderer currentRenderer = ftrLayer.GetRenderer() as CIMSimpleRenderer; if (currentRenderer == null) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Select a feature layer symbolized with a simple renderer."); //Clear the current selection in gallery _selectedStyleItem = null; NotifyPropertyChanged(() => SelectedStyleItem); return; } //Set real world setting for created symbol = feature layer's setting //so that there isn't a mismatch between symbol and feature layer SymbolFactory.SetRealWorldUnits(symbolFromStyleItem, ftrLayer.UsesRealWorldSymbolSizes); //Set current renderer's symbol reference = symbol reference of the newly created symbol currentRenderer.Symbol = SymbolFactory.MakeSymbolReference(symbolFromStyleItem); //Update feature layer renderer with new symbol ftrLayer.SetRenderer(currentRenderer); }); }