public void DiagramElementQueryResultAndNetworkDiagramSubsetClasses(Geodatabase geodatabase, DiagramManager diagramManager, string diagramName) { // Retrieve a diagram using (NetworkDiagram diagramTest = diagramManager.GetNetworkDiagram(diagramName)) { // Create a DiagramElementQueryByElementTypes query object to get the diagram elements we want to work with DiagramElementQueryByElementTypes query = new DiagramElementQueryByElementTypes(); query.QueryDiagramJunctionElement = true; query.QueryDiagramEdgeElement = true; query.QueryDiagramContainerElement = true; // Retrieve those diagram elements DiagramElementQueryResult elements = diagramTest.QueryDiagramElements(query); // Create a NetworkDiagramSubset object to edit this set of diagram elements NetworkDiagramSubset subset = new NetworkDiagramSubset(); subset.DiagramJunctionElements = elements.DiagramJunctionElements; subset.DiagramEdgeElements = elements.DiagramEdgeElements; subset.DiagramContainerElements = elements.DiagramContainerElements; // Edit the shapes of the diagram elements - left as an exercise for the student TranslateDiagramElements(subset); // Save the new layout of the diagram elements diagramTest.SaveLayout(subset, true); } }
/// <summary> /// Save diagram in geodatabase /// </summary> /// <param name="Diagram">Diagram to save</param> internal static void SaveDiagram(NetworkDiagram Diagram) { if (g_JunctionsToSave.Count + g_EdgesToSave.Count > 0) { NetworkDiagramSubset nds = new NetworkDiagramSubset { DiagramContainerElements = null, DiagramEdgeElements = g_EdgesToSave, DiagramJunctionElements = g_JunctionsToSave }; Diagram.SaveLayout(nds, true); MapView.Active.Redraw(true); } }
void TranslateDiagramElements(NetworkDiagramSubset subset) { }
/// <summary> /// Run the rotate selected junctions /// </summary> /// <param name="rotation">Rotation Angle</param> private void RotateSelectedJunctions(double rotation) { if (MapView.Active != null) { // Get the Network Diagram Layer DiagramLayer diagramLayer = GetDiagramLayerFromMap(MapView.Active.Map); if (diagramLayer != null) { QueuedTask.Run(() => { // Get the Network Diagram NetworkDiagram diagram = diagramLayer.GetNetworkDiagram(); if (diagram != null) { try { List <long> junctionObjectIDs = new List <long>(); // get the selection by Layer SelectionSet selection = MapView.Active.Map.GetSelection(); // Get the selection only for junctions foreach (var v in selection.ToDictionary()) { FeatureLayer featureLayer = v.Key as FeatureLayer; if (featureLayer != null) { if (featureLayer.ShapeType != esriGeometryType.esriGeometryPoint) { continue; } junctionObjectIDs.AddRange(v.Value); } } // if no junction selected, work on all diagram junctions DiagramElementQueryResult result; if (junctionObjectIDs.Count == 0) { DiagramElementQueryByElementTypes query = new DiagramElementQueryByElementTypes { QueryDiagramContainerElement = false, QueryDiagramEdgeElement = false, QueryDiagramJunctionElement = true }; result = diagram.QueryDiagramElements(query); } else { DiagramElementQueryByObjectIDs query = new DiagramElementQueryByObjectIDs { AddConnected = false, AddContents = false, JunctionObjectIDs = junctionObjectIDs }; result = diagram.QueryDiagramElements(query); } List <DiagramJunctionElement> junctionsToSave = new List <DiagramJunctionElement>(); // Set the new Rotation Value foreach (var junction in result.DiagramJunctionElements) { if (_isRelative) { junction.Rotation += rotation; } else { junction.Rotation = rotation; } junctionsToSave.Add(junction); } // Save junctions if needed if (junctionsToSave.Count() > 0) { NetworkDiagramSubset nds = new NetworkDiagramSubset { DiagramEdgeElements = null, DiagramContainerElements = null, DiagramJunctionElements = junctionsToSave }; diagram.SaveLayout(nds, true); MapView.Active.Redraw(true); // re set the selection if (selection.Count > 0) { MapView.Active.Map.SetSelection(selection, SelectionCombinationMethod.New); } } } catch (GeodatabaseException e) { MessageBox.Show(e.Message, "Failed to Rotate Junctions "); } } }); } } }
/// <summary> /// Execute the layout on the Diagram /// </summary> /// <param name="diagram"></param> internal void Execute(NetworkDiagram diagram) { _graphModel.Initialize(diagram); var junctions = _graphModel.Junctions; if (!junctions.Any()) { return; } int maxColorCount = 12; var containerIDs = new HashSet <int>(); UtilityNetwork un = diagram.DiagramManager?.GetNetwork <UtilityNetwork>(); IReadOnlyList <NetworkSource> theSources = un.GetDefinition().GetNetworkSources(); if (!_graphModel.Containers.Any() || !_graphModel.Edges.Any()) { return; } Dictionary <int, Dictionary <int, Dictionary <string, string> > > attributesBySourceID = new Dictionary <int, Dictionary <int, Dictionary <string, string> > >(); foreach (var jsonContainer in _graphModel.Containers) { int sourceID = jsonContainer.Element.AssociatedSourceID; if (sourceID != 9) // Communications Device source ID { continue; } if (!attributesBySourceID.TryGetValue(sourceID, out Dictionary <int, Dictionary <string, string> > AttributesByEID)) { AttributesByEID = FillSources(Diagram: diagram, theSources: theSources, SourceID: sourceID); attributesBySourceID.Add(sourceID, AttributesByEID); } if (AttributesByEID.TryGetValue(jsonContainer.ID, out Dictionary <string, string> IdProperties)) { jsonContainer.AssetGroup = IdProperties["Asset group"]; jsonContainer.AssetType = IdProperties["Asset type"]; } } foreach (var jsonEdge in _graphModel.Edges) { int sourceID = jsonEdge.Element.AssociatedSourceID; if (sourceID != 15) // Communications Edge Object source ID { continue; } if (!attributesBySourceID.TryGetValue(sourceID, out Dictionary <int, Dictionary <string, string> > AttributesByEID)) { AttributesByEID = FillSources(Diagram: diagram, theSources: theSources, SourceID: sourceID, IsEdge: true); attributesBySourceID.Add(sourceID, AttributesByEID); } if (AttributesByEID.TryGetValue(jsonEdge.ID, out Dictionary <string, string> IdProperties)) { string assetGroup = IdProperties["Asset group"]; string assetType = IdProperties["Asset type"]; if (assetGroup != "Strand" || assetType != "Fiber") { continue; } string groupColor = IdProperties["Strand Group Color"].ToString(); string color = IdProperties["Strand Color"].ToString(); int groupColorRank = GetColorRank(groupColor); int colorRank = GetColorRank(color); if (groupColorRank < 0 || colorRank < 0) { continue; } int rank = maxColorCount * groupColorRank + colorRank; int fromID = jsonEdge.Element.FromID; int toID = jsonEdge.Element.ToID; int fromContainerID = 0; int toContainerID = 0; var fromJunction = _graphModel.GetJunction(fromID); fromJunction.Rank = rank; fromContainerID = fromJunction.Element.ContainerID; if (fromContainerID > 0) { containerIDs.Add(fromContainerID); } var toJunction = _graphModel.GetJunction(toID); toJunction.Rank = rank; toContainerID = toJunction.Element.ContainerID; if (toContainerID > 0) { containerIDs.Add(toContainerID); } if (fromContainerID > 0 && toContainerID > 0) { var fromContainer = _graphModel.GetContainer(fromContainerID); fromContainer.ToContainers.Add(toContainerID); fromContainer.HasOutputFibers = true; var toContainer = _graphModel.GetContainer(toContainerID); toContainer.FromContainers.Add(fromContainerID); toContainer.HasInputFibers = true; if (fromJunction.Rank < toContainer.FromJunctionRankMin) { toContainer.FromJunctionRankMin = fromJunction.Rank; } } } } attributesBySourceID = null; foreach (var junction in junctions) { if (junction.Element.ContainerID < 1) { continue; } if (junction.Rank >= 0) { continue; } _graphModel.GetConnectedJunctions(junction.Element.ID, out IList <int> connectedJunctions); int fromRank = -1; int toRank = -1; CustomContainer fromContainer = null; CustomContainer toContainer = null; int count = Math.Min(connectedJunctions.Count, 2); for (int i = 0; i < count; i++) { var connectedJunction = _graphModel.GetJunction(connectedJunctions[i]); var container = _graphModel.GetContainer(connectedJunction.Element.ContainerID); if (container.HasInputFibers) { fromRank = connectedJunction.Rank; fromContainer = container; } else if (container.HasOutputFibers) { toRank = connectedJunction.Rank; toContainer = container; } } if (fromRank >= 0 || toRank >= 0) { junction.Rank = fromRank >= 0 ? fromRank : toRank; var container = _graphModel.GetContainer(junction.Element.ContainerID); containerIDs.Add(container.Element.ID); if (fromContainer != null) { container.FromContainers.Add(fromContainer.Element.ID); fromContainer.ToContainers.Add(container.Element.ID); } if (toContainer != null) { container.ToContainers.Add(toContainer.Element.ID); toContainer.FromContainers.Add(container.Element.ID); if (junction.Rank < toContainer.FromJunctionRankMin) { toContainer.FromJunctionRankMin = junction.Rank; } } } } var diagramInfo = diagram.GetDiagramInfo(); var diagramExtent = diagramInfo.DiagramExtent; double ySpacing = diagramInfo.ContainerMargin; double xSpacingMin = diagramInfo.ContainerMargin * 15; var startContainers = new List <CustomContainer>(); foreach (var containerID in containerIDs) { var container = _graphModel.GetContainer(containerID); if (!(container is null) && (container.FromContainers.Count > 0 || container.ToContainers.Count < 1)) { continue; } if (container.Element is null) { continue; } var parent = _graphModel.GetContainer(container.Element.ContainerID); if (parent == null) { continue; } if (parent.AssetType != "Hub Terminator" && parent.AssetType != "Mid Cable Splice Enclosure") { continue; } startContainers.Add(container); } double startY = diagramExtent.YMax; foreach (var startContainer in startContainers) { if (startContainer.FromContainerOrder != Int32.MaxValue) { continue; } var toContainerIDs = new HashSet <int>(); foreach (var toContainerID in startContainer.ToContainers) { toContainerIDs.Add(toContainerID); } double startX = diagramExtent.XMin; startContainer.X = startX; startContainer.Y = startY; while (toContainerIDs.Count > 0) { double y = startY; bool first = true; var toContainers = new List <CustomContainer>(); foreach (var containerID in toContainerIDs) { var toContainer = _graphModel.GetContainer(containerID); if (startContainer.FromContainerOrder == Int32.MaxValue) { toContainers.Add(toContainer); } } var sortedContainers = toContainers.OrderBy(cntr => cntr.FromContainerOrder + 0.001 * cntr.FromJunctionRankMin); int vertivalOrder = 0; foreach (var container in sortedContainers) { int containerID = container.Element.ID; container.Y = y; y -= (_graphModel.GetContainedElements(containerID).Count() * ySpacing + 7 * diagramInfo.ContainerMargin); if (first) { first = false; container.X = startX + xSpacingMin; startX = container.X; } else { container.X = startX; } foreach (var toContainerID in container.ToContainers) { var toContainer = _graphModel.GetContainer(toContainerID); if (toContainer.FromContainerOrder == Int32.MaxValue) { toContainer.FromContainerOrder = vertivalOrder; toContainerIDs.Add(toContainerID); } } vertivalOrder++; toContainerIDs.Remove(containerID); } } startY -= (_graphModel.GetContainedElements(startContainer.Element.ID).Count() * ySpacing + 4 * diagramInfo.ContainerMargin); } IList <DiagramJunctionElement> junctionsToSave = new List <DiagramJunctionElement>(); SpatialReference spatialRef = diagramInfo.DiagramExtent.SpatialReference; MapPointBuilder mapPointBuilder = new MapPointBuilder(spatialRef) { Z = 0 }; foreach (var containerID in containerIDs) { var container = _graphModel.GetContainer(containerID); if (container == null) { continue; } int rankCount = maxColorCount * maxColorCount; BitArray isEmpty = new BitArray(rankCount, true); double yTop = container.Y; var containedJunctions = _graphModel.GetContainedElements(containerID); var unconnectedJunctions = new Stack <int>(); foreach (var junctionID in containedJunctions) { var junction = _graphModel.GetJunction(junctionID); if (junction == null) { continue; } if (junction.Rank < 0) { unconnectedJunctions.Push(junction.Element.ID); continue; } isEmpty[junction.Rank] = false; mapPointBuilder.X = container.X; mapPointBuilder.Y = yTop - junction.Rank * ySpacing; junction.Element.Shape = mapPointBuilder.ToGeometry(); junctionsToSave.Add(junction.Element); } int rank = 0; while (unconnectedJunctions.Count > 0 && rank < rankCount) { if (isEmpty[rank]) { var junction = _graphModel.GetJunction(unconnectedJunctions.Pop()); if (junction != null) { mapPointBuilder.X = container.X; mapPointBuilder.Y = yTop - rank * ySpacing; junction.Element.Shape = mapPointBuilder.ToGeometry(); junctionsToSave.Add(junction.Element); } } rank++; } } if (junctionsToSave.Count > 0) { NetworkDiagramSubset nds = new NetworkDiagramSubset { DiagramJunctionElements = junctionsToSave }; diagram.SaveLayout(nds, true); } }