public ShortBranchNode(List <TNodeUI> uis, ShortBranchNode parent, string endName)
 {
     this.uis     = uis;
     this.parent  = parent;
     this.endName = endName;
     this.nodes   = new HashSet <ShortBranchNode>();
 }
        public BranchView getVccToGndBranch()
        {
            #region 将树状结构的节点放到一维集合中
            BranchView             rst   = getNodes(vccNodes[0].Equal, label => label == "GND");
            ISet <ShortBranchNode> brs   = rst.Nodes;
            ShortBranchNode        head  = brs.First().Parent;
            ISet <ShortBranchNode> total = expandBranch(brs);
            rst.Type = true;
            #endregion

            #region 过滤出所有总正到总负的通路
            filterNodes(total, name => name != "GND");
            #endregion

            #region 为短支路树计算位置信息
            ISet <ShortBranchNode> data = new HashSet <ShortBranchNode>(total.Where(p => p.Parent == head).ToList());
            CalculatePosition(data, 0, 0);
            if (rst.Type)
            {
                alignBranchNode(total, rst);
            }
            rst.Nodes = data;
            #endregion
            rst.GraphType = GraphType.VccToGndGraph;
            rst.GraphName = "总正至总负布线图";
            return(rst);
        }
 private void alignBranchNode(ISet <ShortBranchNode> expandNodes, BranchView view)
 {
     foreach (ShortBranchNode node in view.LabelToBranch.Values)
     {
         if (node == null)
         {
             continue;
         }
         var filterNodes = expandNodes.Where(p => node.EndName == p.EndName).ToList();
         if (filterNodes.Count == 0 || filterNodes.Count == 1)
         {
             continue;
         }
         double maxWidth = double.MinValue;
         foreach (ShortBranchNode br in filterNodes)
         {
             if (br.Uis[br.Uis.Count - 1].Pos.X > maxWidth)
             {
                 maxWidth = br.Uis[br.Uis.Count - 1].Pos.X;
             }
         }
         foreach (ShortBranchNode br in filterNodes)
         {
             double start = br.Uis[br.Uis.Count - 1].Pos.X;
             TNode  head  = br.Uis[br.Uis.Count - 1].Info.getHeadNode();
             TNode  tail  = br.Uis[br.Uis.Count - 1].Info.getTailNode();
             for (double x = start; x < maxWidth; x++)
             {
                 INotifyComponentChanged info = new ComponentViewModel(Tuple.Create <TNode, TNode>(head, tail), ComponentType.Blank);
                 TNodeUI ui = new TNodeUI(info, false, view.Observer);
                 ui.Pos = new Point(x + 1, br.Uis[br.Uis.Count - 1].Pos.Y);
                 br.Uis.Add(ui);
             }
             moveRight(br.Nodes, maxWidth - start);
         }
         double maxy = filterNodes.Max(p => p.Uis[p.Uis.Count - 1].Pos.Y);
         double miny = filterNodes.Min(p => p.Uis[p.Uis.Count - 1].Pos.Y);
         TNode  tm   = AppProject.GetInstance().Equals[node.EndName].First();
         INotifyComponentChanged _info = new ComponentViewModel(Tuple.Create <TNode, TNode>(tm, null), ComponentType.Blank);
         TNodeUI nui = new TNodeUI(_info, false, view.Observer);
         nui.Pos = new Point(node.Uis[node.Uis.Count - 1].Pos.X + 1, node.Uis[node.Uis.Count - 1].Pos.Y);
         List <TNodeUI> uis = new List <TNodeUI>()
         {
             nui
         };
         ShortBranchNode nnode = new ShortBranchNode(uis, node, node.EndName);
         foreach (ShortBranchNode nd in node.Nodes)
         {
             nnode.Nodes.Add(nd);
         }
         moveRight(node.Nodes, 1);
         node.Nodes.Clear();
         node.Nodes.Add(nnode);
     }
 }
        /// <summary>
        /// 过滤出节点
        /// </summary>
        private void filterNodes(ISet <ShortBranchNode> total, Func <string, bool> filter)
        {
            ISet <string> remain = new HashSet <string>();

            foreach (ShortBranchNode node in total)
            {
                if (node.EndName == "GND")
                {
                    ShortBranchNode temp = node;
                    while (temp != null)
                    {
                        remain.Add(temp.EndName);
                        temp = temp.Parent;
                    }
                }
            }


            bool next = true;

            while (next)
            {
                next = false;
                List <ShortBranchNode> temp = new List <ShortBranchNode>();
                foreach (ShortBranchNode node in total)
                {
                    if (node.Nodes.Count == 0 && filter(node.EndName))//node.EndName != "GND"
                    {
                        if (!remain.Contains(node.EndName))
                        {
                            node.remove();
                            temp.Add(node);
                            next = true;
                        }
                        else
                        {
                            ShortBranchNode tp = node;
                            while (tp != null)
                            {
                                remain.Add(tp.EndName);
                                tp = tp.Parent;
                            }
                        }
                    }
                }
                foreach (ShortBranchNode node in temp)
                {
                    total.Remove(node);
                }
            }
        }
        public BranchView getCFToGndBranch()
        {
            #region 将树状结构的节点放到一维集合中
            BranchView             rst   = getNodes(gndNodes[0].Equal, label => label == "DC110V");
            ISet <ShortBranchNode> brs   = rst.Nodes;
            ShortBranchNode        head  = brs.First().Parent;
            ISet <ShortBranchNode> total = expandBranch(brs);
            #endregion

            #region 过滤出所有总负到测试点的通路
            AppProject    pro    = AppProject.GetInstance();
            ISet <string> remain = new HashSet <string>();
            foreach (ShortBranchNode node in total)
            {
                if (node.Nodes.Count == 0)
                {
                    bool mark = false;
                    foreach (TNode tnode in pro.Equals[node.EndName])
                    {
                        if (tnode.PartType.StartsWith("接口连接器"))
                        {
                            mark = true;
                            break;
                        }
                    }
                    if (mark)
                    {
                        ShortBranchNode tp = node;
                        while (tp != null)
                        {
                            remain.Add(tp.EndName);
                            tp = tp.Parent;
                        }
                    }
                }
            }
            //先把叶节点中末端是测试点的标注出来
            bool next = true;
            while (next)
            {
                next = false;
                List <ShortBranchNode> temp = new List <ShortBranchNode>();
                foreach (ShortBranchNode node in total)
                {
                    if (node.Nodes.Count == 0)
                    {
                        bool mark = false;
                        foreach (TNode tnode in pro.Equals[node.EndName])
                        {
                            if (tnode.PartType.StartsWith("接口连接器"))
                            {
                                mark = true;
                                if (!node.Uis[node.Uis.Count - 1].Info.isCFNode())
                                {
                                    INotifyComponentChanged info = new ComponentViewModel(Tuple.Create <TNode, TNode>(tnode, null), ComponentType.Terminal);
                                    node.Uis.Add(new TNodeUI(info, false, rst.Observer));
                                }
                                break;
                            }
                        }
                        if (!mark)
                        {
                            if (!remain.Contains(node.EndName))
                            {
                                node.remove();
                                temp.Add(node);
                                next = true;
                            }
                            else
                            {
                                ShortBranchNode tp = node;
                                while (tp != null)
                                {
                                    remain.Add(tp.EndName);
                                    tp = tp.Parent;
                                }
                            }
                        }
                    }
                }
                foreach (ShortBranchNode node in temp)
                {
                    total.Remove(node);
                }
            }
            rst.Nodes = removeRedundentRoute(rst.Nodes, total);//去除冗余的支路
            #endregion

            #region 为短支路树计算位置信息
            ISet <ShortBranchNode> data = new HashSet <ShortBranchNode>(total.Where(p => p.Parent == head).ToList());
            CalculatePosition(data, 0, 0);
            if (rst.Type)
            {
                alignBranchNode(total, rst);
            }
            rst.Nodes = data;
            #endregion
            rst.GraphType = GraphType.CFToGndGraph;
            rst.GraphName = "测试点至总负布线图";
            return(rst);
        }
        /// <summary>
        /// 获得从总正出发,总负截止的所有拼接支路
        /// </summary>
        private BranchView getNodes(string headEqual, Func <string, bool> filter)
        {
            ComponentVMObserver _observer = new ComponentVMObserver();
            AppProject          pro       = AppProject.GetInstance();

            ((List <ShortBranch>)pro.Shorts).ForEach(p => p.HasIncluded = false);
            LinkedList <string> labels = new LinkedList <string>();
            IDictionary <string, ShortBranchNode> allLabels = new Dictionary <string, ShortBranchNode>();

            labels.AddLast(headEqual);

            ShortBranchNode head = new ShortBranchNode(null, null, headEqual);

            allLabels.Add(headEqual, head);

            #region 广度遍历得到短支路的树结构
            while (labels.First != null)
            {
                string label = labels.First.Value;
                labels.RemoveFirst();
                if (filter(label))
                {
                    continue;
                }
                TNodeUI item1 = null, item2 = null;
                foreach (ShortBranch br in pro.Shorts)
                {
                    if (!br.HasIncluded)
                    {
                        if (br.HeadEqualNum == label || br.TailEqualNum == label)
                        {
//                             if (br.HeadEqualNum == "Net203" || br.TailEqualNum == "Net203")
//                             {
//
//                             }
                            List <TNodeUI> uis     = new List <TNodeUI>();
                            List <TNode>   brNodes = br.Branch;
                            if (br.TailEqualNum == label)
                            {
                                brNodes.Reverse();
                            }
                            int i = 0;
                            while (i < brNodes.Count)
                            {
                                TNodeUI ui = null;
                                if (i + 1 < brNodes.Count && brNodes[i].Brother == brNodes[i + 1])
                                {
                                    ui = BranchFactory.convert(brNodes[i], brNodes[i + 1], _observer);
                                    if (i == 0)
                                    {
                                        item2 = ui;
                                    }
                                    uis.Add(ui);
                                    i += 2;
                                }
                                else
                                {
                                    ui = BranchFactory.convert(brNodes[i], null, _observer);
                                    if (i == 0)
                                    {
                                        item2 = ui;
                                    }
                                    uis.Add(ui);
                                    i++;
                                }
                                if (ui != null && item1 == null)
                                {
                                    item1 = ui;
                                }
                            }
                            ShortBranchNode bnode = new ShortBranchNode(uis, allLabels[label], brNodes[brNodes.Count - 1].Equal);
                            allLabels[label].Nodes.Add(bnode);
                            if (!allLabels.ContainsKey(brNodes[brNodes.Count - 1].Equal))
                            {
                                allLabels.Add(brNodes[brNodes.Count - 1].Equal, bnode);
                                labels.AddLast(brNodes[brNodes.Count - 1].Equal);
                            }
                            br.HasIncluded = true;
                        }
                    }
                }
            }
            #endregion

            #region 返回顶节点
//             ISet<ShortBranchNode> branchNodes = new HashSet<ShortBranchNode>();
//             foreach (ShortBranchNode brNode in allLabels.Values)
//             {
//                 if (brNode.Parent == head)
//                     branchNodes.Add(brNode);
//             }
            return(new BranchView(allLabels, head.Nodes, _observer));

            #endregion
        }