/// <summary> /// Insert an existing rung under an anchor rung /// </summary> /// <param name="rung">Rung to be inserted</param> /// <param name="anchor">Anchor rung</param> public void InsertUnder(Rung rung, Rung anchor) { Trace.WriteLine("Insert under called -> ", "Diagram"); if (rung == null) { throw new ArgumentException("Null rung", "rung"); } if (anchor == null) { throw new ArgumentException("Null rung", "anchor"); } if (anchor == rung) { throw new ArgumentException("Anchor rung and new rung are the same", "anchor"); } int anchorIndex = _Rungs.IndexOf(anchor); if (anchorIndex != -1) { Trace.Indent(); _Rungs.Insert(anchorIndex + 1, rung); Trace.WriteLine("Rung inserted", "Diagram"); rung.DataTable = DataTable; Trace.Unindent(); } else { throw new ArgumentException("Anchor rung is not inserted in current diagram", "anchor"); } }
/// <summary> /// Add an existing rung in the bottom of the diagram /// </summary> /// <param name="rung">Rung to be inserted</param> public void Add(Rung rung) { Trace.WriteLine("Auto insert called -> ", "Diagram"); if (rung == null) { throw new ArgumentException("Null rung", "rung"); } Trace.Indent(); _Rungs.Add(rung); Trace.WriteLine("Rung inserted", "Diagram"); rung.DataTable = DataTable; Trace.Unindent(); }
/// <summary> /// Remove a rung from the ladder diagram /// </summary> /// <param name="rung">Rung to be removed</param> public void Remove(Rung rung) { Trace.WriteLine("Remove called", "Diagram"); if (rung == null) { throw new ArgumentException("Null rung", "rung"); } if (_Rungs.Contains(rung)) { Trace.Indent(); _Rungs.Remove(rung); Trace.WriteLine("Rung removed", "Diagram"); GC.Collect(); // Call gabarge collector Trace.Unindent(); } else { throw new ArgumentException("Rung is not inserted in current diagram", "rung"); } }
/// <summary> /// Insert a new rung under the anchor rung /// </summary> /// <param name="anchor">Anchor rung</param> public void InsertUnder(Rung anchor) { InsertUnder(new Rung(), anchor); }
/// <summary> /// Insert a new rung above the anchor rung /// </summary> /// <param name="anchor">Anchor rung</param> public void InsertAbove(Rung anchor) { InsertAbove(new Rung(), anchor); }
/// <summary> /// Build Circuit structure from a rung /// </summary> /// <param name="rung"></param> /// <returns></returns> public Circuit(Rung rung) { this.Components = new List <ComponentBase>(); this.Mode = CircuitMode.Serial; this.Parent = null; List <NodeConnections> nodes = new List <NodeConnections>().RunAnalysis(rung); Stack <int> CircuitModeStack = new Stack <int>(); CircuitModeStack.Push(-1); Stack <Circuit> Circuits = new Stack <Circuit>(); this.LeftLide = rung.Components.First().LeftLide; this.RightLide = rung.Components.Last().RightLide; Circuits.Push(this); foreach (ComponentBase component in rung.Components) { NodeConnections NodeA = nodes.GetNodeConnections(component.LeftLide); NodeConnections NodeB = nodes.GetNodeConnections(component.RightLide); //Decide when add a parallel sub-circuit if (NodeA.OutComponents.Count > 1) { if (CircuitModeStack.Peek() == -1) { CircuitModeStack.Push(NodeA.OutComponents.Count - 1); Circuit parent = Circuits.Peek(); Circuit son = new Circuit(CircuitMode.Parallel, parent); son.LeftLide = component.LeftLide; parent.Components.Add(son); Circuits.Push(son); } else { CircuitModeStack.Push(CircuitModeStack.Pop() - 1); } } // Decide when add a serial sub-circuit if (NodeB.InComponents.Count == 1 && CircuitModeStack.Peek() != -1) { CircuitModeStack.Push(-1); Circuit parent = Circuits.Peek(); Circuit son = new Circuit(CircuitMode.Serial, parent); son.LeftLide = component.LeftLide; parent.Components.Add(son); Circuits.Push(son); } Circuits.Peek().Components.Add(component); //Close sub-circuit if ((NodeB.InComponents.Count > 1 || NodeA.InComponents.Count > 1) && CircuitModeStack.Count > 1 && CircuitModeStack.Peek() <= 0) { CircuitModeStack.Pop(); Circuits.Pop().RightLide = component.RightLide; } } }