/// <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 Task <bool> OnSketchCompleteAsync(Geometry geometry) { // execute on the MCT return(QueuedTask.Run(() => { // find features under the sketch var features = MapView.Active.GetFeatures(geometry); if (features.Count == 0) { return false; } EditOperation op = null; var insp = new Inspector(); foreach (var annoLayer in features.ToDictionary().Keys.OfType <AnnotationLayer>()) { // are there features? var featOids = features[annoLayer]; if (featOids.Count == 0) { continue; } // for each feature foreach (var oid in featOids) { // load an inspector insp.Load(annoLayer, oid); // get the annotation properties var annoProperties = insp.GetAnnotationProperties(); // get the text graphic var cimTextGraphic = annoProperties.TextGraphic; if (cimTextGraphic != null) { // // add a leader point to the text graphic // // get the feature shape var textExtent = insp.Shape; // find the lower left of the text extent var extent = textExtent.Extent; var lowerLeft = MapPointBuilderEx.CreateMapPoint(extent.XMin, extent.YMin, textExtent.SpatialReference); // move it a little to the left and down var newPoint = GeometryEngine.Instance.Move(lowerLeft, -40, -40); // create a leader point CIMLeaderPoint leaderPt = new CIMLeaderPoint(); leaderPt.Point = newPoint as MapPoint; // add to a list List <CIMLeader> leaderArray = new List <CIMLeader>(); leaderArray.Add(leaderPt); // assign to the textGraphic cimTextGraphic.Leaders = leaderArray.ToArray(); // // add the balloon callout // // create the callout CIMBalloonCallout balloon = new CIMBalloonCallout(); // yellow background balloon.BackgroundSymbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.CreateRGBColor(255, 255, 0)); // set the balloon style balloon.BalloonStyle = BalloonCalloutStyle.RoundedRectangle; // add a text margin CIMTextMargin textMargin = new CIMTextMargin() { Left = 4, Right = 4, Bottom = 4, Top = 4 }; balloon.Margin = textMargin; // asign it to the textSymbol var symbol = cimTextGraphic.Symbol.Symbol; var textSymbol = symbol as CIMTextSymbol; textSymbol.Callout = balloon; // assign the text graphic back to the annotation properties annoProperties.LoadFromTextGraphic(cimTextGraphic); // assign the annotation properties back to the inspector insp.SetAnnotationProperties(annoProperties); } // create the edit operation if (op == null) { op = new EditOperation(); op.Name = "Alter symbol to balloon callout"; op.SelectModifiedFeatures = true; op.SelectNewFeatures = false; } // modify the feature op.Modify(insp); } } if ((op != null) && !op.IsEmpty) { bool result = op.Execute(); return result; } return false; })); }
protected override Task <bool> OnSketchCompleteAsync(Geometry geometry) { // execute on the MCT return(QueuedTask.Run(() => { // find features under the sketch var features = MapView.Active.GetFeatures(geometry); if (features.Count == 0) { return false; } EditOperation op = null; foreach (var layerKey in features.Keys) { // is it an anno layer? if (!(layerKey is AnnotationLayer)) { continue; } // are there features? var featOids = features[layerKey]; if (featOids.Count == 0) { continue; } // for each feature foreach (var oid in featOids) { // create the edit operation if (op == null) { op = new EditOperation(); op.Name = "Alter symbol to balloon callout"; op.SelectModifiedFeatures = true; op.SelectNewFeatures = false; } // use the callback method op.Callback(context => { // find the feature QueryFilter qf = new QueryFilter(); qf.WhereClause = "OBJECTID = " + oid.ToString(); // make sure you use a non-recycling cursor using (var rowCursor = layerKey.GetTable().Search(qf, false)) { rowCursor.MoveNext(); if (rowCursor.Current != null) { ArcGIS.Core.Data.Mapping.AnnotationFeature annoFeature = rowCursor.Current as ArcGIS.Core.Data.Mapping.AnnotationFeature; if (annoFeature != null) { // get the CIMTextGraphic var textGraphic = annoFeature.GetGraphic() as CIMTextGraphic; if (textGraphic != null) { // // add a leader point to the text graphic // // get the feature shape var feature = annoFeature as Feature; var textExtent = feature.GetShape(); // find the lower left of the text extent var extent = textExtent.Extent; var lowerLeft = MapPointBuilder.CreateMapPoint(extent.XMin, extent.YMin, textExtent.SpatialReference); // move it a little to the left and down var newPoint = GeometryEngine.Instance.Move(lowerLeft, -40, -40); // create a leader point CIMLeaderPoint leaderPt = new CIMLeaderPoint(); leaderPt.Point = newPoint as MapPoint; // add to a list List <CIMLeader> leaderArray = new List <CIMLeader>(); leaderArray.Add(leaderPt); // assign to the textGraphic textGraphic.Leaders = leaderArray.ToArray(); // // add the balloon callout // // create the callout CIMBalloonCallout balloon = new CIMBalloonCallout(); // yellow background balloon.BackgroundSymbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.CreateRGBColor(255, 255, 0)); // set the balloon style balloon.BalloonStyle = BalloonCalloutStyle.RoundedRectangle; // add a text margin CIMTextMargin textMargin = new CIMTextMargin() { Left = 4, Right = 4, Bottom = 4, Top = 4 }; balloon.Margin = textMargin; // asign it to the textSymbol var symbol = textGraphic.Symbol.Symbol; var textSymbol = symbol as CIMTextSymbol; textSymbol.Callout = balloon; try { // update the graphic annoFeature.SetGraphic(textGraphic); // store annoFeature.Store(); // refresh the cache context.Invalidate(annoFeature); } // SetGraphic can throw a GeodatabaseException if the AnnotationFeatureClassDefinition AreSymbolOverridesAllowed = false // or if IsSymbolIDRequired = true and the symbol edit you're making causes the symbol to be disconnected from the symbol collection. // see http://pro.arcgis.com/en/pro-app/sdk/api-reference/#topic17424.html // and http://pro.arcgis.com/en/pro-app/sdk/api-reference/#topic17432.html catch (GeodatabaseException ex) { } } } } } }, layerKey.GetTable()); } } if ((op != null) && !op.IsEmpty) { bool result = op.Execute(); return result; } return false; })); }