// This routine obtains a utility network from a FeatureLayer, SubtypeGroupLayer, or UtilityNetworkLayer
public static UtilityNetwork GetUtilityNetworkFromLayer(Layer layer)
{
UtilityNetwork utilityNetwork = null;
if (layer is UtilityNetworkLayer)
{
UtilityNetworkLayer utilityNetworkLayer = layer as UtilityNetworkLayer;
utilityNetwork = utilityNetworkLayer.GetUtilityNetwork();
}
else if (layer is SubtypeGroupLayer)
{
CompositeLayer compositeLayer = layer as CompositeLayer;
utilityNetwork = GetUtilityNetworkFromLayer(compositeLayer.Layers.First());
}
else if (layer is FeatureLayer)
{
FeatureLayer featureLayer = layer as FeatureLayer;
using (FeatureClass featureClass = featureLayer.GetFeatureClass())
{
if (featureClass.IsControllerDatasetSupported())
{
IReadOnlyList<Dataset> controllerDatasets = new List<Dataset>();
controllerDatasets = featureClass.GetControllerDatasets();
foreach (Dataset controllerDataset in controllerDatasets)
{
if (controllerDataset is UtilityNetwork)
{
utilityNetwork = controllerDataset as UtilityNetwork;
}
else
{
controllerDataset.Dispose();
}
}
}
}
}
return utilityNetwork;
}