Example #1
0
        private void ConnectEnds(LineBlock nodeContainerBlock)
        {
            System.Diagnostics.Debug.WriteLine($"***ConnectEnds***");


            HashSet <BlockPortTerminal> conduitEndTerminalAlreadyConnected = new HashSet <BlockPortTerminal>();

            HashSet <Guid> cableAlreadyConnected = new HashSet <Guid>();

            foreach (var terminalEndList in _terminalEndsByTerminalId.Values)
            {
                foreach (var terminalEnd in terminalEndList)
                {
                    if (!conduitEndTerminalAlreadyConnected.Contains(terminalEnd.DiagramTerminal))
                    {
                        // If connected conduit ends
                        if (terminalEnd.TerminalId != Guid.Empty && _terminalEndsByTerminalId[terminalEnd.TerminalId].Any(th => th.DiagramTerminal != terminalEnd.DiagramTerminal))
                        {
                            var otherDiagramTerminal = _terminalEndsByTerminalId[terminalEnd.TerminalId].First(th => th.DiagramTerminal != terminalEnd.DiagramTerminal);

                            if (!conduitEndTerminalAlreadyConnected.Contains(otherDiagramTerminal.DiagramTerminal))
                            {
                                System.Diagnostics.Debug.WriteLine($"Will connect conduit ends segmentId: {terminalEnd.SpanSegment.Id} to segmentId: {otherDiagramTerminal.SpanSegment.Id}");

                                // Conduit connection
                                var terminalConnection = nodeContainerBlock.AddTerminalConnection(
                                    fromSide: terminalEnd.DiagramTerminal.Port.Side,
                                    fromPortIndex: terminalEnd.DiagramTerminal.Port.Index,
                                    fromTerminalIndex: terminalEnd.DiagramTerminal.Index,
                                    toSide: otherDiagramTerminal.DiagramTerminal.Port.Side,
                                    toPortIndex: otherDiagramTerminal.DiagramTerminal.Port.Index,
                                    toTerminalIndex: otherDiagramTerminal.DiagramTerminal.Index,
                                    label: null,
                                    style: terminalEnd.Style,
                                    lineShapeType: LineShapeTypeEnum.Polygon
                                    );

                                terminalConnection.DrawingOrder = 550;

                                // This to make to show branched-out spans on top
                                if (terminalEnd.DiagramTerminal.Port.Side != otherDiagramTerminal.DiagramTerminal.Port.Side)
                                {
                                    terminalConnection.DrawingOrder = 560;
                                }

                                terminalConnection.SetReference(terminalEnd.SpanSegment.Id, "SpanSegment");

                                conduitEndTerminalAlreadyConnected.Add(terminalEnd.DiagramTerminal);
                                conduitEndTerminalAlreadyConnected.Add(otherDiagramTerminal.DiagramTerminal);


                                // Add eventually cable running through inner conduit
                                if (_nodeContainerViewModel.Data.ConduitSegmentToCableChildRelations.ContainsKey(terminalEnd.SpanSegment.Id))
                                {
                                    var cableId             = _nodeContainerViewModel.Data.ConduitSegmentToCableChildRelations[terminalEnd.SpanSegment.Id].First();
                                    var fiberCableLineLabel = _nodeContainerViewModel.Data.GetCableEquipmentLineLabel(cableId);

                                    System.Diagnostics.Debug.WriteLine($" Will connect cable: {cableId} {fiberCableLineLabel} through conduit connection");

                                    var cableTerminalConnection = nodeContainerBlock.AddTerminalConnection(
                                        fromSide: terminalEnd.DiagramTerminal.Port.Side,
                                        fromPortIndex: terminalEnd.DiagramTerminal.Port.Index,
                                        fromTerminalIndex: terminalEnd.DiagramTerminal.Index,
                                        toSide: otherDiagramTerminal.DiagramTerminal.Port.Side,
                                        toPortIndex: otherDiagramTerminal.DiagramTerminal.Port.Index,
                                        toTerminalIndex: otherDiagramTerminal.DiagramTerminal.Index,
                                        label: fiberCableLineLabel,
                                        style: "FiberCable",
                                        lineShapeType: LineShapeTypeEnum.Line
                                        );

                                    cableTerminalConnection.SetReference(cableId, "SpanSegment");

                                    cableTerminalConnection.DrawingOrder = 600;
                                }
                            }
                        }
                        // Not connected conduit ends
                        else
                        {
                            var test = _nodeContainerViewModel.Data.ConduitSegmentToCableChildRelations;


                            // Add eventually cable running through inner conduit
                            if (_nodeContainerViewModel.Data.ConduitSegmentToCableChildRelations.ContainsKey(terminalEnd.SpanSegment.Id))
                            {
                                // Check if cable is running from one port to another

                                var cableIds = _nodeContainerViewModel.Data.ConduitSegmentToCableChildRelations[terminalEnd.SpanSegment.Id];

                                foreach (var cableId in cableIds)
                                {
                                    var cableLineLabel = _nodeContainerViewModel.Data.GetCableEquipmentLineLabel(cableId);

                                    System.Diagnostics.Debug.WriteLine($"Not connected conduits ends. Try connecting cable: {cableId} {cableLineLabel} terminalEnd: {terminalEnd.TerminalId} terminalEnd segment id: {terminalEnd.SpanSegment.Id} port-to-port");

                                    // Only connect if cable goes through two span segments and terminal end it not the fake one (where terminal id equal the cable id)
                                    if (_nodeContainerViewModel.Data.CableToConduitSegmentParentRelations[cableId].Count == 2 && cableId != terminalEnd.TerminalId)
                                    {
                                        // get other segment id
                                        var otherEndSegmentId = _nodeContainerViewModel.Data.CableToConduitSegmentParentRelations[cableId].First(r => r != terminalEnd.SpanSegment.Id);

                                        TerminalEndHolder otherDiagramTerminal = null;

                                        if (_terminalEndsByTerminalId.ContainsKey(Guid.Empty) && _terminalEndsByTerminalId[Guid.Empty].Exists(s => s.SpanSegment.Id == otherEndSegmentId))
                                        {
                                            otherDiagramTerminal = _terminalEndsByTerminalId[Guid.Empty].First(s => s.SpanSegment.Id == otherEndSegmentId);
                                        }
                                        else if (_terminalEndsByTerminalId.ContainsKey(cableId))
                                        {
                                            otherDiagramTerminal = _terminalEndsByTerminalId[cableId].First(s => s.SpanSegment.Id == otherEndSegmentId);
                                        }

                                        foreach (var cableIdConnected in cableAlreadyConnected)
                                        {
                                            System.Diagnostics.Debug.WriteLine($"   Already connected cable: {cableIdConnected}");
                                        }

                                        if (otherDiagramTerminal != null && !cableAlreadyConnected.Contains(cableId))
                                        {
                                            System.Diagnostics.Debug.WriteLine($" Will connect cable: {cableId} {cableLineLabel} port-to-port");

                                            cableAlreadyConnected.Add(cableId);

                                            var cableTerminalConnection = nodeContainerBlock.AddTerminalConnection(
                                                fromSide: terminalEnd.DiagramTerminal.Port.Side,
                                                fromPortIndex: terminalEnd.DiagramTerminal.Port.Index,
                                                fromTerminalIndex: terminalEnd.DiagramTerminal.Index,
                                                toSide: otherDiagramTerminal.DiagramTerminal.Port.Side,
                                                toPortIndex: otherDiagramTerminal.DiagramTerminal.Port.Index,
                                                toTerminalIndex: otherDiagramTerminal.DiagramTerminal.Index,
                                                label: cableLineLabel,
                                                style: "FiberCable",
                                                lineShapeType: LineShapeTypeEnum.Line
                                                );

                                            cableTerminalConnection.SetReference(cableId, "SpanSegment");

                                            cableTerminalConnection.DrawingOrder = 600;
                                        }
                                    }
                                    else
                                    {
                                        var segmentIds = _nodeContainerViewModel.Data.CableToConduitSegmentParentRelations[cableId];

                                        // Only make half-connection if the cable is not running between two parent span segments
                                        if (segmentIds.Count == 1)
                                        {
                                            System.Diagnostics.Debug.WriteLine($"Will connect cable: {cableId} half connection. Number of segment relations: {segmentIds.Count}");

                                            var cableTerminalConnection = nodeContainerBlock.AddTerminalHalfConnection(
                                                fromSide: terminalEnd.DiagramTerminal.Port.Side,
                                                fromPortIndex: terminalEnd.DiagramTerminal.Port.Index,
                                                fromTerminalIndex: terminalEnd.DiagramTerminal.Index,
                                                label: cableLineLabel,
                                                style: "FiberCable",
                                                50
                                                );

                                            cableTerminalConnection.SetReference(cableId, "SpanSegment");

                                            cableTerminalConnection.DrawingOrder = 600;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }