Can I set starting geometry for SketchViewModel?

1489
6
10-08-2021 10:06 AM
GaryLSheppardJr
New Contributor III

Question

When activating SketchViewModel by calling sketchViewModel.create('polyline'), is it possible to give it a starting geometry programmatically so that the sketch starts with one or more vertices before the user clicks the map or scene? If it is possible, how is it done?

Use case

The use case is that I'm adding SketchViewModel to an existing application that allows the user to switch between 2D mode (MapView) and 3D mode (SceneView). It's working really well for allowing the user to draw graphics in both 2D mode and 3D mode and see the graphics after switching the mode. When the mode changes, I recreate the SketchViewModel object with the new view and call sketchViewModel.create('polyline') to resume drawing. That works, but the current in-progress sketch disappears, of course. I would like to let the user continue the sketch after the 2D/3D switch. That's why I need to know if I can give SketchViewModel a starting geometry before turning it over to the user.

0 Kudos
6 Replies
BlakeTerhune
MVP Regular Contributor

This should work. See this sample app that creates a point, line, and polygon graphic. Just create the geometry and add it to the GraphicsLayer the SketchViewModel is using.

Add Graphics to a SceneView | ArcGIS API for JavaScript 4.21 | ArcGIS Developer

0 Kudos
GaryLSheppardJr
New Contributor III

It's true that you can add any graphic to the layer, including the one that was just being drawn. But then it's a finished graphic like any other, which is not really what I need. But thank you for the idea and the graphics sample link.

0 Kudos
JohnGrayson
Esri Regular Contributor

Gary, for this use-case, I would take the Graphic that is currently being edited in the previous view and re-created it in the GraphicsLayer being used by the SketchViewModel of the new view, then instead of 'create', call 'update' using the new Graphic you just re-created in this new view.

GaryLSheppardJr
New Contributor III

Thanks, John. Your idea might be as good as it gets. The result is that it's a finished graphic but it is selected for updating. It saves the user a click anyway. It's not exactly what I wanted--it would be great if the user could just keep drawing the unfinished graphic--but it's not bad. I appreciate the idea.

0 Kudos
ArnoFiva
Esri Contributor

Interesting scenario Gary! As John already pointed out, your best option at the moment is to add the starting geometry as a new Graphic to the SketchViewModel.layer and call SketchViewModel.update().

We realize that updating a geometry is different to creating a new one where you continuously place vertices. In the future we would like bring these two user experiences closer together, providing a way to update existing features using a create-like mode. No concrete timeline for that yet though.

Are you able to share more about the different 2D/3D modes? In what way do they differ from each other? Would you be able to achieve the same changing the camera to a top-down view, without interrupting the SketchViewModel workflow?

GaryLSheppardJr
New Contributor III

Thanks for the follow-up, Arno. That's an interesting idea to stick with SceneView and move the camera to give a top-down view. Our app switches between the 2D MapView and the 3D SceneView, and it would take some major rework to remove the 2D MapView, so I don't think we're going to do that just to get the ability to keep drawing the existing graphic when switching between 2D and 3D. But this is small stuff that we can live without. Frankly I'm impressed at how well SketchViewModel works in 3D as well as 2D. It has saved me a lot of time.