private static void ResetLabels()
        {
            QueuedTask.Run(async() =>
            {
                //Get the line feature we want to clear labels for
                var featureLayer = MapView.Active.Map.GetLayersAsFlattenedList().Where(f => f.Name == "U.S. Rivers (Generalized)").FirstOrDefault() as FeatureLayer;
                if (featureLayer == null)
                {
                    return;
                }

                // create an instance of the inspector class
                var inspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();

                //Clear the previous "Yes" values for the LabelOn field.
                //Create a QueryFilter to search for all features that have the LabelOn field value set at Yes.
                var qf = new QueryFilter
                {
                    WhereClause = "LabelOn = 'Yes'"
                };
                var selectionLabelOnYes = featureLayer.Select(qf);
                var featuresToClear     = selectionLabelOnYes.GetObjectIDs()?.ToList();
                if (featuresToClear.Count > 0)
                {
                    // load the features into the inspector using a list of object IDs
                    await inspector.LoadAsync(featureLayer, featuresToClear);

                    // assign the attribute value of "No" to the field "LabelOn"
                    // if more than one features are loaded, the change applies to all features
                    inspector["LabelOn"] = "No";
                    // apply the changes as an edit operation
                    await inspector.ApplyAsync();
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// The features are labeled using a Field in the feature layer. The field LabelOn has values Yes or No.
        /// If the value is "Yes", that feature will be labeled. If value is No, the feature will not be labeled.
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        protected override Task <bool> OnSketchCompleteAsync(Geometry geometry)
        {
            QueuedTask.Run(async() =>
            {
                //select features that intersect the sketch geometry
                var selection = MapView.Active.SelectFeatures(geometry, SelectionCombinationMethod.New);
                //Get the line feature we want to label
                var featureLayer = selection.Where(f => f.Key.Name == "U.S. Rivers (Generalized)").FirstOrDefault().Key as FeatureLayer;
                if (featureLayer == null)
                {
                    return;
                }
                //Get the list of line features that are selected
                var oidsOfSelectedFeature = selection[featureLayer];
                if (oidsOfSelectedFeature.Count == 0)
                {
                    return;
                }

                //We want to only label the selected features. So..
                //Clear the previous "Yes" values for the LabelOn field.
                //Create a QueryFilter to search for all features that have the LabelOn field value set at Yes.
                // create an instance of the inspector class
                var inspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();

                var qf = new QueryFilter
                {
                    WhereClause = "LabelOn = 'Yes'"
                };
                var selectionLabelOnYes = featureLayer.Select(qf);
                var featuresToClear     = selectionLabelOnYes.GetObjectIDs()?.ToList();
                if (featuresToClear.Count > 0)
                {
                    // load the features into the inspector using a list of object IDs
                    await inspector.LoadAsync(featureLayer, featuresToClear);

                    // assign the attribute value of "No" to the field "LabelOn"
                    // if more than one features are loaded, the change applies to all features
                    inspector["LabelOn"] = "No";
                    // apply the changes as an edit operation
                    await inspector.ApplyAsync();
                }

                //Now modify the features that are selected by the tool to have the value Yes
                await inspector.LoadAsync(featureLayer, oidsOfSelectedFeature);
                // assign the attribute value of "Yes" to the field "LabelOn" for the features selected
                // if more than one features are loaded, the change applies to all features
                inspector["LabelOn"] = "Yes";
                // apply the changes as an edit operation
                await inspector.ApplyAsync();
                //Get the layer's definition
                var lyrDefn = featureLayer.GetDefinition() as CIMFeatureLayer;
                //Get the label classes - and check if the correct label class we need to label the line features with exists
                var listLabelClasses = lyrDefn.LabelClasses.ToList();
                var theLabelClass    = listLabelClasses.Where(l => l.Name == "LabelSelectedManyFeaturesWithLength").FirstOrDefault();
                if (theLabelClass == null) //create label class and add to the collection
                {
                    theLabelClass = await CreateAndApplyLabelClassAsync(featureLayer);
                    listLabelClasses.Add(theLabelClass);
                }

                //Turn off all the label classes except this one
                foreach (var lc in listLabelClasses)
                {
                    lc.Visibility = lc.Name == "LabelSelectedManyFeaturesWithLength" ? true : false;
                }
                //Apply the label classes back to the layer definition
                lyrDefn.LabelClasses = listLabelClasses.ToArray();
                //Set the layer definition
                featureLayer.SetDefinition(lyrDefn);
                //set the label's visiblity
                featureLayer.SetLabelVisibility(true);
            });
            return(base.OnSketchCompleteAsync(geometry));
        }