I'm currently trying to create a tool and need to be able to manipulate the snapping settings on individual layers so that the snapping environment only snaps to a desired set of feature layers. I was using the SetSnappable method on FeatureLayer originally but the operation is slow for the number of layers I need to manipulate (200+ layers on a map takes over 10 seconds to run flayer.SetSnappable(true) on all of them).
I'm attempting to use Snapping.SetLayerModes(SetLayerSnapModes(IDictionary<Layer,LayerSnapModes>,Boolean) but unfortunately, anything I enter in here seems to get ignored and I haven't been able to get something similar to the current code snippet working.
The outcome I expect is that the layers I feed become the only layers the user can snap to, but in practice, it seems that the global Snapping environment's options I set (point, edge, end, vertex) take priority and all layers in the map can still be snapped to, even if I specify in the dictionary object that all snap modes for certain layers should be false.
Am I doing something wrong here? It seems because the layers still appear with snapping enabled in the Snap window and I've selected certain things to snap to (point, vertex, etc.), the options I set don't matter, but it sounds like LayerSnapModes are supposed to let me set different snapping rules from those. Do I have the wrong idea, or am I writing this incorrectly possibly
Here was my attempt:
public static void UpdateMapSnapping(List<FeatureLayer> layersToSnap)
{
Snapping.IsEnabled = true
// Reset snap settings to the options I want
Snapping.SetSnapMode();
Snapping.SetSnapMode(SnapMode.Point, true);
Snapping.SetSnapMode(SnapMode.Edge, true);
Snapping.SetSnapMode(SnapMode.End, true);
Snapping.SetSnapMode(SnapMode.Vertex, true)
var mapLayers = MapView.Active.map.GetLayersAsFlattenedList().OfType<FeatureLayer>().ToList();
var snapModes = new Dictionary<Layer, LayerSnapModes>();
foreach (var flayer in mapLayers)
{
// This was done previously; all layers appear as snappable in the Snap table of contents
// flayer.SetSnappable(true);
if layersToSnap.Contains(flayer)
{
// Want to snap to this layer
snapModes.Add(flayer, new LayerSnapModes(true));
}
else
{
// Do not want to snap to this layer
snapModes.Add(flayer, new LayerSnapModes(false));
}
}
// Set all layers to either snap or not snap
// Setting the boolean to true or false doesn't appear to change anything, all layers are still snappable.
Snapping.SetLayerSnapModes(snapModes, false);
}