public static void Disable(Node nd)
 {
     if (ConnectedSectionsInLoop(nd))
     {
         Node strtNode = GetCommonLoopParent(nd);
     }
 }
 //public static Node EnergizedNodeFromLoop(Node nd)
 //{
 //    Queue<Node> q = new Queue<Node>();
 //    q.Enqueue(root);
 //    if (root != null)
 //    {
 //        while (q.Count > 0)
 //        {
 //            Node current = q.Dequeue();
 //            if (current != null && !StatusExtensions.IsNetJunction(current.status))
 //                list.Add(current); //add node
 //            if (current == null || StatusExtensions.IsDisconnected(current.status))
 //                continue;
 //            if (ContainsAdjNode(current))
 //            {
 //                q.Enqueue(current.adjacentNode);
 //            }
 //            parentPhase = current.phaseCode;
 //            if (StatusExtensions.IsUnenergized(current.status) || StatusExtensions.IsDisabled(current.status))
 //            {
 //                foreach (var item in current.childList.Values)
 //                {
 //                    q.Enqueue(item.childNode);
 //                }
 //                foreach (var item in current.parentList.Values)
 //                {
 //                    q.Enqueue(item.childNode);
 //                }
 //            }
 //            else
 //            {
 //                foreach (var item in current.childList.Values)
 //                {
 //                    q.Enqueue(item.childNode);
 //                }
 //            }
 //        }
 //    }
 //}
 public static bool ConnectedSectionsInLoop(Node nd)
 {
     bool inLoop = false;
     foreach (Section sec in nd.parentList.Values)
     {
         if (StatusExtensions.IsLoop(sec.status))
             inLoop = true;
     }
     foreach (Section sec in nd.childList.Values)
     {
         if (StatusExtensions.IsLoop(sec.status))
             inLoop = true;
     }
     return inLoop;
 }
 public void AddNode(EditObject eObj)
 {
     Node snappednd = null;
     Node ndNew = null;
     if (eObj.Connectivity.snappedNodes.Count > 1)
     {
         snappednd = eObj.Connectivity.snappedNodes.First(nd => StatusExtensions.IsNetJunction(nd.status));
     }
     snappednd = eObj.Connectivity.snappedNodes != null ? eObj.Connectivity.snappedNodes[0] : null;
     if (StatusExtensions.IsNetJunction(snappednd.status) && ClassIDRules.IsAdjJunction(snappednd) && ClassIDRules.IsAdjGroupNode(eObj.CLSID))
     {
         ndNew = new Node();
         ndNew.status = eObj.Connectivity.status;
         ndNew.uid = eObj.UID;
         snappednd.adjacentNode = ndNew;
         ndNew.classID = eObj.CLSID;
         ndNew.x = snappednd.x;
         ndNew.y = snappednd.y;
         ndNew.phaseCode = eObj.Connectivity.phaseCode;
         ndNew.parentList = snappednd.parentList;
         AddSecondaryToAdjNode(snappednd, ndNew);
     }
     else if (StatusExtensions.IsNetJunction(snappednd.status))
     {
         ndNew = new Node();
         ndNew.status = eObj.Connectivity.status;
         ndNew.uid = eObj.UID;
         ndNew.x = snappednd.x;
         ndNew.y = snappednd.y;
         ndNew.classID = eObj.CLSID;
         ndNew.phaseCode = eObj.Connectivity.phaseCode;
         ndNew.parentList = snappednd.parentList;
         ndNew.childList = snappednd.childList;
         AdjustParentChild(snappednd, ndNew);
         //now delete net junction *****
         if (StaticStuff._nodeList.ContainsKey(snappednd.uid)) StaticStuff._nodeList.Remove(snappednd.uid);
     }
     if (ndNew != null)
     {
         StaticStuff._nodeList.Add(ndNew.uid, ndNew);
         ExtensionInfo.checkFlow.AddPointsToPointList(ndNew);
     }
 }
 public static bool IsAdjJunction(Node nd)
 {
     if (nd != null && nd.adjacentNode != null && AdjGroup.Contains(nd.adjacentNode.classID))
         return true;
     return false;
 }
 public void AdjustParentChild(Node nd, Node newNd)
 {
     foreach (Section sec in nd.childList.Values)
     {
         sec.parentNode = newNd;
     }
     foreach (Section sec in nd.parentList.Values)
     {
         sec.childNode = newNd;
     }
 }
 public Section SelectParentFromParentSections(Node snappedNode)
 {
     foreach (Section sec in snappedNode.parentList.Values)
     {
         if (PhaseCodeUtil.IsPhaseCodePresent(sec.phaseCode, snappedNode.phaseCode))
             return sec;
     }
     return null;
 }
 public bool HasParent(Node fromNode)
 {
     bool hasParent = false;
     foreach (Section sec in fromNode.parentList.Values)
     {
         hasParent = true;
     }
     return hasParent;
 }
 public int GetPhaseCodeFromChildSections(Node node)
 {
     int childPC = 128;
     foreach (Section sec in node.childList.Values)
     {
         childPC = PhaseCodeUtil.AddPhaseCodes(childPC, sec.phaseCode);
     }
     return childPC;
 }
 public List<Section> GetConnectedEdges(Node node)
 {
     int xFloor = Convert.ToInt32(Math.Floor(node.x));
     int yFloor = Convert.ToInt32(Math.Floor(node.y));
     Point Pt1 = new Point(xFloor, yFloor);
     var nearestSections = new List<Section>();
     if (lineList.ContainsKey(Pt1)) nearestSections = lineList[Pt1];
     var connectedEdges = from row in nearestSections
                          where (ConnectionBuffer(row.x, row.y, node.x, node.y, row.tox, row.toy))
                          select row;
     return connectedEdges.ToList();
 }
 public static void SetFlowUnenergize(Node nd, HashSet<Section> visited)
 {
     List<Node> disabledList = new List<Node>();
     if (nd != null)
     {
         bool first = true;
         Queue<Node> q = new Queue<Node>();
         q.Enqueue(nd);
         while (q.Count > 0)
         {
             Node current = q.Dequeue();
             StatusExtensions.UnLoop(current.status);
             if (current == null)
                 continue;
             if (StatusExtensions.IsDisabled(current.status) && first)
             {
                 current.phaseCode = 128;
             }
             else if (StatusExtensions.IsDisabled(current.status) && !first)
             {
                 disabledList.Add(current);
                 continue;
             }
             //downstream
             foreach (var item in current.childList.Values)
             {
                 if (!visited.Contains(item))
                 {
                     visited.Add(item);
                     q.Enqueue(item.childNode);
                     NetworkUtil.DownsectionPCAdjustment(current, item);
                 }
             }
         }
     }
     for (int i = 0; i < disabledList.Count; i++)
     {
         SetFlowUnenergize(disabledList[i], visited);
     }
 }
 public static void SetFlowEnergizeLoop(Node nd, HashSet<Section> visited)
 {
     List<Node> disabledList = new List<Node>();
     if (nd != null)
     {
         Queue<Node> q = new Queue<Node>();
         q.Enqueue(nd);
         while (q.Count > 0)
         {
             Node current = q.Dequeue();
             StatusExtensions.UnLoop(current.status);
             if (current == null)
                 continue;
             if (StatusExtensions.IsDisabled(current.status))
             {
                 disabledList.Add(current);
             }
             else
             {
                 Dictionary<string, Section> connSecs = GetConnectedLoopSections(current, visited);
                 foreach (var item in connSecs.Values)
                 {
                     if (!visited.Contains(item) && StatusExtensions.IsLoop(item.status))
                     {
                         visited.Add(item);
                         StatusExtensions.UnLoop(item.status);
                         if (current == item.childNode)
                         {
                             FlipSection(item);
                             FlipDigitized(item);
                             q.Enqueue(item.childNode);
                         }
                         q.Enqueue(item.childNode);
                         NetworkUtil.DownsectionPCAdjustment(current, item);
                     }
                 }
                 //downstream
                 foreach (var item in current.childList.Values)
                 {
                     if (!visited.Contains(item) && !connSecs.ContainsKey(item.uid))
                     {
                         visited.Add(item);
                         q.Enqueue(item.childNode);
                         NetworkUtil.DownsectionPCAdjustment(current, item);
                     }
                 }
             }
         }
     }
     for (int i = 0; i < disabledList.Count; i++)
     {
         SetFlowUnenergize(disabledList[i], visited);
     }
 }
 public static void SetFlow(Node nd)
 {
     HashSet<Section> visited = new HashSet<Section>();
     SetFlowEnergize(nd, visited);
 }
 public static List<string> GuidsToRoot(Node leaf, HashSet<Section> visited)
 {
     List<string> path = new List<string>();
     if (leaf != null)
     {
         Queue<Node> q = new Queue<Node>();
         q.Enqueue(leaf);
         while (q.Count > 0)
         {
             Node current = q.Dequeue();
             if (current == null)
                 continue;
             path.Add(current.uid);
             Dictionary<string, Section> connSecs = GetConnectedLoopSections(current, visited);
             foreach (var item in connSecs.Values)
             {
                 if (!visited.Contains(item))
                 {
                     visited.Add(item);
                     if (current == item.parentNode) q.Enqueue(item.childNode);
                     if (current == item.childNode) q.Enqueue(item.parentNode);
                     //path.Add(item.uid);
                     break;
                 }
             }
         }
     }
     return path;
 }
 public static Dictionary<string, Section> GetConnectedUnenergizedSections(Node nd, HashSet<Section> visited)
 {
     Dictionary<string, Section> connSecs = new Dictionary<string, Section>();
     foreach (string secUID in nd.parentList.Keys)
     {
         if ((StatusExtensions.IsDisconnected(nd.parentList[secUID].status) || StatusExtensions.IsUnenergized(nd.parentList[secUID].status)) && !visited.Contains(nd.parentList[secUID]))
             connSecs.Add(secUID, nd.parentList[secUID]);
     }
     foreach (string secUID in nd.childList.Keys)
     {
         if ((StatusExtensions.IsDisconnected(nd.parentList[secUID].status) || StatusExtensions.IsUnenergized(nd.parentList[secUID].status)) && !visited.Contains(nd.childList[secUID]))
             connSecs.Add(secUID, nd.childList[secUID]);
     }
     return connSecs;
 }
 public static Node GetCommonLoopParent(Node nd)
 {
     Node intersectionNode = null;
     HashSet<Section> visited = new HashSet<Section>();
     List<Section> connSecs = GetConnectedLoopSections(nd, visited).Values.ToList();
     string intersectionPt = "";
     for (int i = 0; i < connSecs.Count - 1; i++)
     {
         for (int j = i + 1; j < connSecs.Count; j++)
         {
             Section item1 = connSecs[i];
             Section item2 = connSecs[j];
             List<string> lst1 = new List<string>();
             List<string> lst2 = new List<string>();
             visited.Add(connSecs[i]);
             visited.Add(connSecs[j]);
             if (nd == item1.parentNode) lst1 = GuidsToRoot(item1.childNode, visited);
             else if (nd == item1.childNode) lst1 = GuidsToRoot(item1.parentNode, visited);
             if (nd == item2.parentNode) lst2 = GuidsToRoot(item2.childNode, visited);
             else if (nd == item2.childNode) lst2 = GuidsToRoot(item2.parentNode, visited);
             List<string> intersections = lst1.Intersect(lst2).ToList<string>();
             if (intersections.Count != 0)
             {
                 intersectionPt = intersections[0];
                 int i1 = lst1.IndexOf(intersectionPt);
                 int i2 = lst2.IndexOf(intersectionPt);
             }
             if (StaticStuff._nodeList.ContainsKey(intersectionPt))
                 intersectionNode = StaticStuff._nodeList[intersectionPt];
             if (intersectionNode != null) break;
         }
     }
     return intersectionNode;
 }
        public bool FromToHasNoParent(Node fromNode, Node toNode)
        {
            bool noFromToParent = true;
            bool noFromParent = true;
            bool noToParent = true;
            foreach (Section sec in fromNode.parentList.Values)
            {
                noFromParent = false;
            }

            foreach (Section sec in toNode.parentList.Values)
            {
                noToParent = false;
            }

            noFromToParent = noFromParent && noToParent;
            return noFromToParent;
        }
        public bool FromToHasParents(Node fromNode, Node toNode)
        {
            bool fromToParent = false;
            bool fromParent = false;
            bool toParent = false;
            foreach (Section sec in fromNode.parentList.Values)
            {
                fromParent = true;
            }

            foreach (Section sec in toNode.parentList.Values)
            {
                toParent = true;
            }

            fromToParent = fromParent && toParent;
            return fromToParent;
        }
        public static void DownsectionPCCorrection(Node parentNode, Section childSection)
        {
            int parentPCAccumilated = 128;
            int parentPC = 128;
            int parentConstrPC = parentNode.constructedPhaseCode;
            int constrChildPC = childSection.constructedPhaseCode;
            foreach (Section sec in parentNode.parentList.Values)
            {
                parentPCAccumilated = PhaseCodeUtil.AddPhaseCodes(parentPC, sec.phaseCode);
            }
            if (!StatusExtensions.IsNetJunction(parentNode.status))
            {
                if (PhaseCodeUtil.IsPhaseCodePresent(parentConstrPC, parentPCAccumilated)) // may add phase here
                {
                    if (!StatusExtensions.IsYDTransformer(parentNode.status))
                    {
                        parentNode.phaseCode = parentPCAccumilated;
                    }
                }
                else if (PhaseCodeUtil.IsPhaseCodePresent(parentPCAccumilated, parentConstrPC))
                {
                    parentNode.phaseCode = parentConstrPC;
                }
                else if (!PhaseCodeUtil.HaveCommonPhaseCodes(parentPCAccumilated, parentConstrPC))
                {
                    parentNode.phaseCode = 128;
                }
            }
            if (StatusExtensions.IsNetJunction(parentNode.status))
            {
                parentPC = parentPCAccumilated;
            }
            else
                parentPC = parentNode.phaseCode;
            int existingChildPC = childSection.phaseCode;
            if (PhaseCodeUtil.IsPhaseCodePresent(constrChildPC, parentPC)) // may add phase here
            {
                childSection.phaseCode = parentPC;
            }
            else if (PhaseCodeUtil.IsPhaseCodePresent(parentPC, constrChildPC))
            {
                childSection.phaseCode = constrChildPC;
            }
            else if (!PhaseCodeUtil.HaveCommonPhaseCodes(parentPC, constrChildPC))
            {
                childSection.phaseCode = 128;
            }
            if (childSection.phaseCode != 128)
            {
                if (StatusExtensions.IsUnenergized(childSection.status))
                    StatusExtensions.Energize(childSection.status);

            }
            else
            {
                if (!StatusExtensions.IsUnenergized(childSection.status))
                {
                    StatusExtensions.Unenerize(childSection.status);
                    childSection.status += Constants.Unenergized;
                }
            }
            if (StatusExtensions.IsDisconnected(childSection.status))
                StatusExtensions.Connect(childSection.status);
        }
 public List<Section> GetConnectedSections(Node node)
 {
     List<Section> secs = null;
     if (_nodeList.ContainsKey(node.uid))
     {
         secs = new List<Section>();
         foreach (Section item in node.parentList.Values) secs.Add(item);
         foreach (Section item in node.childList.Values) secs.Add(item);
     }
     return secs;
 }
        public void AddPointsToPointList(Node dr)
        {
            int xFloor = Convert.ToInt32(Math.Floor(dr.x));
            int xCeiling = Convert.ToInt32(Math.Ceiling(dr.x));

            int yFloor = Convert.ToInt32(Math.Floor(dr.y));
            int yCeiling = Convert.ToInt32(Math.Ceiling(dr.y));

            //1,2 xfloor
            Point Pt = new Point(xFloor, yFloor);
            if (pointList.ContainsKey(Pt))
            {
                pointList[Pt].Add(dr);
            }
            else
            {
                pointList[Pt] = new List<Node>();
                pointList[Pt].Add(dr);
            }
            Pt = new Point(xFloor, yCeiling);
            if (pointList.ContainsKey(Pt))
            {
                pointList[Pt].Add(dr);
            }
            else
            {
                pointList[Pt] = new List<Node>();
                pointList[Pt].Add(dr);
            }
            //3, 4 xceiling
            Pt = new Point(xCeiling, yFloor);

            if (pointList.ContainsKey(Pt))
            {
                pointList[Pt].Add(dr);
            }
            else
            {
                pointList[Pt] = new List<Node>();
                pointList[Pt].Add(dr);
            }

            Pt = new Point(xCeiling, yCeiling);

            if (pointList.ContainsKey(Pt))
            {
                pointList[Pt].Add(dr);
            }
            else
            {
                pointList[Pt] = new List<Node>();
                pointList[Pt].Add(dr);
            }
        }
 public int GetPhaseCodeFromParentSections(Node node)
 {
     int parentPC = 128;
     foreach (Section sec in node.parentList.Values)
     {
         parentPC = PhaseCodeUtil.AddPhaseCodes(parentPC, sec.phaseCode);
     }
     return parentPC;
 }
 public bool ContainsEnergizedUnenergized(Node fromNode, Node toNode)
 {
     bool containsEnergizedUnenergizedSection = false;
     bool energized = false;
     bool unenergized = false;
     foreach (Section sec in fromNode.parentList.Values)
     {
         if (StatusExtensions.IsEnergized(sec.status))
             energized = true;
         if (StatusExtensions.IsUnenergized(sec.status))
             unenergized = true;
     }
     foreach (Section sec in fromNode.childList.Values)
     {
         if (StatusExtensions.IsEnergized(sec.status))
             energized = true;
         if (StatusExtensions.IsUnenergized(sec.status))
             unenergized = true;
     }
     foreach (Section sec in toNode.parentList.Values)
     {
         if (StatusExtensions.IsEnergized(sec.status))
             energized = true;
         if (StatusExtensions.IsUnenergized(sec.status))
             unenergized = true;
     }
     foreach (Section sec in toNode.childList.Values)
     {
         if (StatusExtensions.IsEnergized(sec.status))
             energized = true;
         if (StatusExtensions.IsUnenergized(sec.status))
             unenergized = true;
     }
     containsEnergizedUnenergizedSection = energized && unenergized;
     return containsEnergizedUnenergizedSection;
 }
 public bool OneSectionEnergized(Node fromNode)
 {
     bool energized = false;
     foreach (Section sec in fromNode.parentList.Values)
     {
         if (StatusExtensions.IsEnergized(sec.status))
             energized = true;
     }
     foreach (Section sec in fromNode.childList.Values)
     {
         if (StatusExtensions.IsEnergized(sec.status))
             energized = true;
     }
     return energized;
 }
 public void FillConnectivityInfoForEnergizedSections(ConnectivityInfo conInfo, Node fromNode, Node toNode)
 {
     conInfo = new ConnectivityInfo();
     conInfo.phaseCode = 128;
     conInfo.parentNode = fromNode;
     conInfo.childNode = toNode;
     conInfo.setFlow = false;
     if (StatusExtensions.IsSource(fromNode.status) || StatusExtensions.IsSource(toNode.status))
     {
         if(StatusExtensions.IsSource(fromNode.status))
         {
             conInfo.parentNode = fromNode;
             conInfo.childNode = toNode;
             conInfo.status = Constants.WithFlow + Constants.Unenergized;
             conInfo.setFlow = false;
             conInfo.phaseCode = -1;
         }
         if (StatusExtensions.IsSource(toNode.status))
         {
             conInfo.parentNode = toNode ;
             conInfo.childNode = fromNode;
             conInfo.status = Constants.AgainstFlow + Constants.Unenergized;
             conInfo.setFlow = false;
             conInfo.phaseCode = -1;
         }
     }
     else if ((HasParent(fromNode) && !HasChild(fromNode) && HasChild(toNode) && !HasParent(toNode)))
     {
             conInfo.parentNode = fromNode ;
             conInfo.childNode = toNode;
             conInfo.status = Constants.WithFlow ;
             conInfo.setFlow = false;
     }
     else if ((HasParent(toNode) && !HasChild(toNode) && HasChild(fromNode) && !HasParent(fromNode)))
     {
             conInfo.parentNode = toNode ;
             conInfo.childNode = fromNode;
             conInfo.status = Constants.AgainstFlow;
             conInfo.setFlow = false;
     }
         else
         {
             //int fromNodePC = GetPhaseCodeFromParentSections(fromNode);
             //int toNodePC = GetPhaseCodeFromParentSections(toNode);
             conInfo.parentNode = fromNode;
             conInfo.childNode = toNode;
             conInfo.status = Constants.WithFlow + Constants.Unenergized;
             conInfo.setFlow = false;
             conInfo.phaseCode = -1;
         }
 }
 public bool UnenergizedLocal(Node node)
 {
     if (StatusExtensions.IsDisabled(node.status))
     {
         return true;
     }
     if (StatusExtensions.IsSource(node.status)) return false;
     List<Section> connSections = GetConnectedSections(node);
     bool hasFlowOrLoop = true;
     foreach (Section sec in connSections)
     {
         if (StatusExtensions.IsEnergized(sec.status) || StatusExtensions.IsLoop(sec.status))
         {
             hasFlowOrLoop = false;
         }
     }
     return hasFlowOrLoop;
 }
        public bool FromToHasChildren(Node fromNode, Node toNode)
        {
            bool fromToChild = false;
            bool fromChild = false;
            bool toChild = false;
            foreach (Section sec in fromNode.childList.Values)
            {
                fromChild = true;
            }

            foreach (Section sec in toNode.childList.Values)
            {
                toChild = true;
            }

            fromToChild = fromChild && toChild;
            return fromToChild;
        }
 public Node CreateNetJn(EditObject eObj)
 {
     Node netJn = new Node();
     netJn.status = Constants.Energized + Constants.NetJunction;
     if (StatusExtensions.IsUnenergized(eObj.Connectivity.status))
     {
         netJn.status = Constants.Unenergized + Constants.NetJunction;
     }
     else if (StatusExtensions.IsEnergized(eObj.Connectivity.status))
     {
         netJn.status = Constants.Energized + Constants.NetJunction;
     }
     else if (StatusExtensions.IsLoop(eObj.Connectivity.status))
     {
         netJn.status = Constants.Loop + Constants.NetJunction;
     }
     if (StatusExtensions.IsDisconnected(eObj.Connectivity.status))
     {
         netJn.status = Constants.Disconnected + Constants.NetJunction;
     }
     netJn.uid = "{" + Guid.NewGuid().ToString() + "}";
     netJn.phaseCode = eObj.Connectivity.phaseCode;
     netJn.classID = -1;//ntInfo.networkClassIds[ClassIDRules.NetJunction];
     netJn.oid = -1;
     return netJn;
 }
        public bool FromToHasNoChildren(Node fromNode, Node toNode)
        {
            bool noFromToChild = true;
            bool noFromChild = true;
            bool noToChild = true;
            foreach (Section sec in fromNode.childList.Values)
            {
                noFromChild = false;
            }

            foreach (Section sec in toNode.childList.Values)
            {
                noToChild = false;
            }

            noFromToChild = noFromChild && noToChild;
            return noFromToChild;
        }
 public void AddSecondaryToAdjNode(Node nd, Node adjNd)
 {
     foreach (Section sec in nd.childList.Values)
     {
         adjNd.childList = new Dictionary<string, Section>();
         if (ClassIDRules.AdjRuleSucceeds(sec))
         {
             adjNd.childList.Add(sec.uid, sec);
         }
     }
 }
 public static bool IsAdjGroupNode(Node nd)
 {
     if (nd != null &&  AdjGroup.Contains(nd.classID))
         return true;
     return false;
 }