I am writing an add-in for Pro. This add-in contains a form with a button that activates a custom maptool, i.e.
await FrameworkApplication.SetCurrentToolAsync("MyTool");This works, and I can sketch a geometry on the map.
How do I get a reference to the geometry in the form? Do I need to fire a custom event from the maptool's OnSketchCompleteAsync method? Or can I somehow reuse the existing SketchCompletedEvent event?
It appeared to be easier than I thought.
I created a custom event, and in my maptool, I added these two lines in the OnSketchCompleted method:
var payload = new SketchFinishedEventArgs(geometry);
SketchFinishedEvent.Publish(payload);
In the form, I keep track of the current (and previous) tool, using the ActiveToolChangedEvent:
_currentToolId = args.CurrentID;
_previousToolId = args.PreviousID;
The form also listens to my custom event:
private async Task SketchFinished(SketchFinishedEventArgs args)
{
// Only act if MyTool published this event
if (_currentToolId == "MyTool")
{
_line = args.Sketch as Polyline;
// activate whatever the previous tool was
await FrameworkApplication.SetCurrentToolAsync(_previousToolId);
}
}
There are several options for this, one would be to create the graphics layers BEFORE you run the custom
the tool so you can grab a handle to the layer AFTER you return from await. Assumption here is you are using the same map. This would work with any specific layer in the map., I'm just assuming you are drawing on the graphics layer.
{
// get params
var map = MapView.Active.Map;
var layerParams = new LayerCreationParams("My New Graphics Layer");
// create graphics layer
GraphicsLayer newLayer = LayerFactory.Instance.CreateLayer(layerParams, map , LayerPosition.AddToTop);
Bool success=false;
success = await myTask(my variables);
// Find the first graphics layer in the map if we returned from the task without error
If (success==true)
{
var graphicsLayer = map.GetLayersAsFlattenedList().OfType().FirstOrDefault();
... do more stuff ...
or use newLayer
}
}
Of note, at ArcGIS Pro 3.4 maps started having a default graphics layer. This means you can grab that instead of creating your own layer (but since you are working with a custom tool, having your OWN graphics layer would have value).
// Get the first available GraphicsLayer in the map var graphicsLayer = MapView.Active.Map.GetLayersAsFlattenedList() .OfType<GraphicsLayer>().FirstOrDefault();