/// <summary>
        /// Called when the sketch finishes. This is where we will create the edit operation and then execute it.
        /// </summary>
        /// <param name="geometry">The geometry created by the sketch.</param>
        /// <returns>A Task returning a Boolean indicating if the sketch complete event was successfully handled.</returns>
        protected override async Task <bool> OnSketchCompleteAsync(Geometry geometry)
        {
            if (CurrentTemplate == null || geometry == null)
            {
                return(false);
            }

            bool result = await QueuedTask.Run(() =>
            {
                // get the anno layer
                AnnotationLayer annoLayer = CurrentTemplate.Layer as AnnotationLayer;
                if (annoLayer == null)
                {
                    return(false);
                }

                // get the anno feature class
                var fc = annoLayer.GetFeatureClass() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClass;
                if (fc == null)
                {
                    return(false);
                }

                // get the featureclass CIM definition which contains the labels, symbols
                var cimDefinition = fc.GetDefinition() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClassDefinition;
                var labels        = cimDefinition.GetLabelClassCollection();
                var symbols       = cimDefinition.GetSymbolCollection();

                // make sure there are labels, symbols
                if ((labels.Count == 0) || (symbols.Count == 0))
                {
                    return(false);
                }


                // find the label class required
                //   typically you would use a subtype name or some other characteristic

                // use the first label class
                var label = labels[0];
                if (labels.Count > 1)
                {
                    // find a label class based on template name
                    foreach (var LabelClass in labels)
                    {
                        if (LabelClass.Name == CurrentTemplate.Name)
                        {
                            label = LabelClass;
                            break;
                        }
                    }
                }

                // each label has a textSymbol
                // the symbolName *should* be the symbolID to be used
                var symbolName = label.TextSymbol.SymbolName;
                int symbolID   = -1;
                if (!int.TryParse(symbolName, out symbolID))
                {
                    // int.TryParse fails - attempt to find the symbolName in the symbol collection
                    foreach (var symbol in symbols)
                    {
                        if (symbol.Name == symbolName)
                        {
                            symbolID = symbol.ID;
                            break;
                        }
                    }
                }
                // no symbol?
                if (symbolID == -1)
                {
                    return(false);
                }


                // use the template's inspector object
                var inspector = CurrentTemplate.Inspector;
                // get the annotation properties
                var annoProperties = inspector.GetAnnotationProperties();

                // AnnotationClassID, SymbolID and Shape are the bare minimum for an annotation feature

                // use the inspector[fieldName] to set the annotationClassid - this is allowed since annotationClassID is a guaranteed field in the annotation schema
                inspector["AnnotationClassID"] = label.ID;
                // set the symbolID too
                inspector["SymbolID"] = symbolID;

                // use the annotation properties to set the other attributes
                annoProperties.TextString        = "My annotation feature";
                annoProperties.Color             = ColorFactory.Instance.GreenRGB;
                annoProperties.VerticalAlignment = ArcGIS.Core.CIM.VerticalAlignment.Top;
                annoProperties.Underline         = true;

                // set the geometry to be the sketched line
                // when creating annotation features the shape to be passed in the create operation is the CIMTextGraphic shape
                annoProperties.Shape = geometry;

                // set the annotation properties back on the inspector
                inspector.SetAnnotationProperties(annoProperties);

                // Create an edit operation
                var createOperation  = new EditOperation();
                createOperation.Name = string.Format("Create {0}", CurrentTemplate.Layer.Name);
                createOperation.SelectNewFeatures = true;

                // create and execute using the inspector
                createOperation.Create(CurrentTemplate.Layer, inspector);
                return(createOperation.Execute());
            });

            return(result);
        }
        protected override async void OnClick()
        {
            // get an anno layer
            AnnotationLayer annoLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType <AnnotationLayer>().FirstOrDefault();

            if (annoLayer == null)
            {
                return;
            }

            Inspector insp   = null;
            bool      result = await QueuedTask.Run(() =>
            {
                // get the anno feature class
                var fc = annoLayer.GetFeatureClass() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClass;

                // get the featureclass CIM definition which contains the labels, symbols
                var cimDefinition = fc.GetDefinition() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClassDefinition;
                var labels        = cimDefinition.GetLabelClassCollection();
                var symbols       = cimDefinition.GetSymbolCollection();

                // make sure there are labels, symbols
                if ((labels.Count == 0) || (symbols.Count == 0))
                {
                    return(false);
                }

                // find the label class required
                //   typically you would use a subtype name or some other characteristic
                // in this case lets just use the first one

                // var label = labels[0];

                // find the label class required
                //   typically you would use a subtype name or some other characteristic

                // use the first label class
                var label = labels[0];
                if (labels.Count > 1)
                {
                    // find a label class based on template name
                    foreach (var LabelClass in labels)
                    {
                        if (LabelClass.Name == "Basic")
                        {
                            label = LabelClass;
                            break;
                        }
                    }
                }

                // each label has a textSymbol
                // the symbolName *should* be the symbolID to be used
                var symbolName = label.TextSymbol.SymbolName;
                int symbolID   = -1;
                if (!int.TryParse(symbolName, out symbolID))
                {
                    // int.TryParse fails - attempt to find the symbolName in the symbol collection
                    foreach (var symbol in symbols)
                    {
                        if (symbol.Name == symbolName)
                        {
                            symbolID = symbol.ID;
                            break;
                        }
                    }
                }
                // no symbol?
                if (symbolID == -1)
                {
                    return(false);
                }

                // load the schema
                insp = new Inspector();
                insp.LoadSchema(annoLayer);

                // ok to access AnnotationClassID, SymbolID this way - it is guaranteed to exist
                insp["AnnotationClassID"] = label.ID;
                insp["SymbolID"]          = symbolID;

                // set up some text properties
                AnnotationProperties annoProperties = insp.GetAnnotationProperties();
                annoProperties.FontSize             = 36;
                annoProperties.TextString           = "My Annotation feature";
                annoProperties.VerticalAlignment    = VerticalAlignment.Top;
                annoProperties.HorizontalAlignment  = HorizontalAlignment.Justify;

                // assign the properties back to the inspector
                insp.SetAnnotationProperties(annoProperties);

                // set up tags
                var tags = new[] { "Annotation", "tag1", "tag2" };

                // set up default tool - use daml-id rather than guid
                string defaultTool = "esri_editing_SketchStraightAnnoTool";

                // tool filter is the tools to filter OUT
                var toolFilter = new[] { "esri_editing_SketchCurvedAnnoTool" };

                // create a new CIM template  - new extension method
                var newTemplate = annoLayer.CreateTemplate("My new template", "sample template description", insp, defaultTool, tags, toolFilter);

                return(newTemplate != null);
            });
        }
Exemple #3
0
        public void CreateTemplate()
        {
            string value1 = "";
            string value2 = "";
            string value3 = "";

            #region Create New Template using layer.CreateTemplate

            var layer = MapView.Active.Map.GetLayersAsFlattenedList().OfType <FeatureLayer>().FirstOrDefault();
            if (layer == null)
            {
                return;
            }
            QueuedTask.Run(() =>
            {
                var insp = new Inspector();
                insp.LoadSchema(layer);

                insp["Field1"] = value1;
                insp["Field2"] = value2;
                insp["Field3"] = value3;

                var tags = new[] { "Polygon", "tag1", "tag2" };

                // set defaultTool using a daml-id
                string defaultTool = "esri_editing_SketchCirclePolygonTool";

                // tool filter is the tools to filter OUT
                var toolFilter = new[] { "esri_editing_SketchTracePolygonTool" };

                // create a new template
                var newTemplate = layer.CreateTemplate("My new template", "description", insp, defaultTool, tags, toolFilter);
            });
            #endregion

            #region Create Annotation Template

            // get an anno layer
            AnnotationLayer annoLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType <AnnotationLayer>().FirstOrDefault();
            if (annoLayer == null)
            {
                return;
            }

            QueuedTask.Run(() =>
            {
                Inspector insp = null;
                // get the anno feature class
                var fc = annoLayer.GetFeatureClass() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClass;

                // get the featureclass CIM definition which contains the labels, symbols
                var cimDefinition = fc.GetDefinition() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClassDefinition;
                var labels        = cimDefinition.GetLabelClassCollection();
                var symbols       = cimDefinition.GetSymbolCollection();

                // make sure there are labels, symbols
                if ((labels.Count == 0) || (symbols.Count == 0))
                {
                    return;
                }

                // find the label class required
                //   typically you would use a subtype name or some other characteristic
                // in this case lets just use the first one

                var label = labels[0];

                // each label has a textSymbol
                // the symbolName *should* be the symbolID to be used
                var symbolName = label.TextSymbol.SymbolName;
                int symbolID   = -1;
                if (!int.TryParse(symbolName, out symbolID))
                {
                    // int.TryParse fails - attempt to find the symbolName in the symbol collection
                    foreach (var symbol in symbols)
                    {
                        if (symbol.Name == symbolName)
                        {
                            symbolID = symbol.ID;
                            break;
                        }
                    }
                }
                // no symbol?
                if (symbolID == -1)
                {
                    return;
                }

                // load the schema
                insp = new Inspector();
                insp.LoadSchema(annoLayer);

                // ok to assign these fields using the inspector[fieldName] methodology
                //   these fields are guaranteed to exist in the annotation schema
                insp["AnnotationClassID"] = label.ID;
                insp["SymbolID"]          = symbolID;

                // set up some additional annotation properties
                AnnotationProperties annoProperties = insp.GetAnnotationProperties();
                annoProperties.FontSize             = 36;
                annoProperties.TextString           = "My Annotation feature";
                annoProperties.VerticalAlignment    = VerticalAlignment.Top;
                annoProperties.HorizontalAlignment  = HorizontalAlignment.Justify;

                insp.SetAnnotationProperties(annoProperties);

                var tags = new[] { "Annotation", "tag1", "tag2" };

                // use daml-id rather than guid
                string defaultTool = "esri_editing_SketchStraightAnnoTool";

                // tool filter is the tools to filter OUT
                var toolFilter = new[] { "esri_editing_SketchCurvedAnnoTool" };

                // create a new template
                var newTemplate = annoLayer.CreateTemplate("new anno template", "description", insp, defaultTool, tags, toolFilter);
            });

            #endregion
        }
        /// <summary>
        /// Called when the sketch finishes. This is where we will create the edit operation and then execute it.
        /// </summary>
        /// <param name="geometry">The geometry created by the sketch.</param>
        /// <returns>A Task returning a Boolean indicating if the sketch complete event was successfully handled.</returns>
        protected override async Task <bool> OnSketchCompleteAsync(Geometry geometry)
        {
            if (CurrentTemplate == null || geometry == null)
            {
                return(false);
            }

            bool result = await QueuedTask.Run(() =>
            {
                // get the anno layer
                AnnotationLayer annoLayer = CurrentTemplate.Layer as AnnotationLayer;
                if (annoLayer == null)
                {
                    return(false);
                }

                // get the anno feature class
                var fc = annoLayer.GetFeatureClass() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClass;
                if (fc == null)
                {
                    return(false);
                }

                // get the featureclass CIM definition which contains the labels, symbols
                var cimDefinition = fc.GetDefinition() as ArcGIS.Core.Data.Mapping.AnnotationFeatureClassDefinition;
                var labels        = cimDefinition.GetLabelClassCollection();
                var symbols       = cimDefinition.GetSymbolCollection();

                // make sure there are labels, symbols
                if ((labels.Count == 0) || (symbols.Count == 0))
                {
                    return(false);
                }


                // find the label class required
                //   typically you would use a subtype name or some other characteristic

                // use the first label class
                var label = labels[0];
                if (labels.Count > 1)
                {
                    // find a label class based on template name
                    foreach (var LabelClass in labels)
                    {
                        if (LabelClass.Name == CurrentTemplate.Name)
                        {
                            label = LabelClass;
                            break;
                        }
                    }
                }

                // each label has a textSymbol
                // the symbolName *should* be the symbolID to be used
                var symbolName = label.TextSymbol.SymbolName;
                int symbolID   = -1;
                if (!int.TryParse(symbolName, out symbolID))
                {
                    // int.TryParse fails - attempt to find the symbolName in the symbol collection
                    foreach (var symbol in symbols)
                    {
                        if (symbol.Name == symbolName)
                        {
                            symbolID = symbol.ID;
                            break;
                        }
                    }
                }
                // no symbol?
                if (symbolID == -1)
                {
                    return(false);
                }

                // use a dictionary to set values
                Dictionary <string, object> values = new Dictionary <string, object>();

                // AnnotationClassID, SymbolID and Shape are the bare minimum for an annotation feature

                // set the SymbolID
                values.Add("SymbolID", symbolID);
                // set the AnnotationClassID
                values.Add("AnnotationClassID", label.ID);

                // you can also add text (if the field exists)
                // if you don't set text then the feature has the default value of 'Text'
                int idxField = cimDefinition.FindField("TextString");
                if (idxField != -1)
                {
                    values.Add("TextString", "My annotation feature");
                }

                // set the geometry to be the sketched line
                // when creating annotation features the shape to be passed in the create operation is the CIMTextGraphic shape
                values["SHAPE"] = geometry;


                // Create an edit operation
                var createOperation  = new EditOperation();
                createOperation.Name = string.Format("Create {0}", CurrentTemplate.Layer.Name);
                createOperation.SelectNewFeatures = true;

                // create and execute
                createOperation.Create(CurrentTemplate.Layer, values);
                return(createOperation.Execute());
            });

            return(result);
        }