Select to view content in your preferred language

Accessing a user-drawn sketch created from a custom tool

224
4
05-09-2024 10:20 AM
TyroneLigon1
New Contributor III

In a previous post, I created a tool that allows a user to drag a rectangle on the map. The extent is then used as part of a URL to a web application query. Once the rectangle is drawn to the map, I'm unsure as to how I access the rectangle. I tried MapView.GetCurrentSketchAsync().Result, but that always returns null. Is the sketch in the map's graphic layer? Or somewhere else? Or am I missing a method?

0 Kudos
4 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

The reason why GetCurrentSketchAsync returns null is because you call the method after the sketching is already finished.  GetCurrentSketchAsync is meant to support modifying the sketch while sketching is in progress.     You can check-out the use in the Editing\RestartSketch community sample to see a use of changing a sketch while sketching is in progress. 
If you want to use a sketched polygon after the sketching is finished, you have to create your own construction tool and save the sketched geometry in a graphics layer (or any other layer).  The following sample contains the code to create a polygon (you have to change the SketchType to rectangle) and this MapTool will allow you to complete a sketch which is then stored in the graphics layer:  arcgis-pro-sdk-community-samples/Framework/CopyPaste/CreatePolygon.cs at master · Esri/arcgis-pro-sd...

Please note that the latest community samples are release 3.3 samples that require .NET 8.  If you want to build the latest community samples on releases 3.0 - 3.2 you have to change the project's target framework to .NET 6.

0 Kudos
TyroneLigon1
New Contributor III

Using the "Create Polygon" example, I was able to create the graphic layer with the user-drawn rectangle and add it to the map. In the section where I create the CIM polygon graphic, I add the following parameter: Name = "AOIPoly"

In a different class, when I need the extent of the shape, I run the code in the following snippet:

var mapView = MapView.Active;

GraphicsLayer gLyr = null;

GraphicElement gElem = null;

Geometry aoiGeom = null;

ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => {

  gLyr = mapView.Map.GetLayersAsFlattenedList().OfType<GraphicsLayer>().Where(e => e.Name == "AOI Graphic").FirstOrDefault();

gElem = gLyr.FindElement("AOIPoly") as GraphicElement

aoiGeom = gElem.GetGeometry();

});

The graphics layer is found, but the graphic is not. Am I doing something wrong?

0 Kudos
TyroneLigon1
New Contributor III

Update to yesterday's post - instead of performing a "FindElement" operation on the graphics layer, I performed "GetElementsAsFlattenedList", then executed "FirstOrDefault" on the element list. Now I have the element. However, what I noticed while running in debug mode is that I can't seem to find the geometry for the element. I see there's a graphic for the element, which has a null polygon.

I'm going to try to add the geometry's extent parameters as attributes to the graphic element in the OnSketchCompleteAsync method as a last resort sans a more elegant solution.

0 Kudos
TyroneLigon1
New Contributor III

This question is a follow-on to this post: https://community.esri.com/t5/arcgis-pro-sdk-questions/user-drawn-rectangle-doesn-t-persist-on-map-a...

Based on my experience with ArcObjects a few years ago, I suspected that I'd have to create a graphics layer and attach the shape as a graphic. I just thought that since I've been able to draw the shape on the map, and it's still visible, that there was some way to grab the geometry after the sketch was completed. I seem to remember there being a default graphics layer for the map (I might be wrong...it's been a few years), so I was hoping that was the case and I just hadn't found the reference in the SDK yet. 

0 Kudos