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);
     }
 }
 public TNodeUIPair(TNodeUI item1, TNodeUI item2)
 {
     this.item1 = item1;
     this.item2 = item2;
 }
        /// <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
        }
        public static UIElement convert(TNodeUI node)
        {
            UIElement rst = null;

            switch (node.Info.CptType)
            {
            case ComponentType.Terminal:
            {
                rst = new Terminal(node.Info);
                break;
            }

            case ComponentType.Switch:
            {
                rst = new Switch(node.Info);
                break;
            }

            case ComponentType.Resistance:
            {
                rst = new Resistance(node.Info);
                break;
            }

            case ComponentType.Indicator:
            {
                rst = new Indicator(node.Info);
                break;
            }

            case ComponentType.Diode:
            {
                rst = new Diode(node.Info, node.Reverse);
                break;
            }

            case ComponentType.ContactClose:
            case ComponentType.ContactOpen:
            {
                rst = new StandardContact(node.Info);
                break;
            }

            case ComponentType.Coil:
            {
                rst = new Coil(node.Info);
                break;
            }

            case ComponentType.Capacitance:
            {
                rst = new Capacitance(node.Info);
                break;
            }

            case ComponentType.Breaker:
            {
                rst = new Breaker(node.Info);
                break;
            }

            case ComponentType.Blank:
            {
                rst = new BlankLine(node.Info);
                break;
            }
            }
            return(rst);
        }