/// <summary> /// Inset component with auto select position /// </summary> /// <param name="component">Component to be added</param> public Rung Add(ComponentBase component) { Trace.WriteLine("Auto insert called", "Rung"); if (component == null) { throw new ArgumentNullException("Null component", "component"); } Trace.Indent(); if (component.Class == ComponentBase.ComponentClass.Output) { if (_Components.Count == 0) { SC SC = new SC(PowerRail); SC.RightLide = new Node(SC); component.RightLide = GroundRail; component.LeftLide = SC.RightLide; _Components.Add(SC); _Components.Add(component); Trace.WriteLine(component.GetType() + " inserted at the empty rung", "Rung"); } else { ComponentBase LC = _Components[_Components.Count - 1]; component.LeftLide = (LC.Class != ComponentBase.ComponentClass.Output) ? LC.RightLide : LC.LeftLide; component.RightLide = GroundRail; _Components.Add(component); Trace.WriteLine(component.GetType() + " inserted", "Rung"); } } else { if (_Components.Count == 0) { component.LeftLide = PowerRail; component.RightLide.Root = component; _Components.Add(component); Trace.WriteLine(component.GetType() + " inserted at the empty rung", "Rung"); } else { ComponentBase FC = _Components[0]; component.LeftLide = PowerRail; if (FC is SC) { component.RightLide = FC.RightLide; component.RightLide.Root = component; _Components.Remove(FC); Trace.Write(" Short Circuit removed -> ", "Rung"); } else { component.RightLide.Root = component; FC.LeftLide = component.RightLide; } _Components.Insert(0, component); Trace.WriteLine(component.GetType() + " inserted", "Rung"); } } component.DataTable = DataTable; Trace.Unindent(); return(this); }
public Tuple <Node, Node> FindInterception(ComponentBase componentA, ComponentBase componentB) { if (componentA == componentB) { throw new Exception("Components A and B are the same"); } int indexA = _Components.IndexOf(componentA); int indexB = _Components.IndexOf(componentB); if (indexA == -1) { throw new ArgumentException("Component not inserted in current Rung", "componentA"); } if (indexB == -1) { throw new ArgumentException("Component not inserted in current Rung", "componentB"); } if (indexA > indexB) { int temp = indexA; indexA = indexB; indexB = temp; } #region Search Node B bool foundB = false; int rni = indexA - 1; while (!foundB && ++rni < indexB) { for (int rnj = indexB; rnj < _Components.Count; rnj++) { if (_Components[rnj].RightLide == _Components[rni].RightLide) { foundB = true; break; } } } if (!foundB) { return(null); } #endregion #region Search Node A bool foundA = false; int lni = indexB + 1; while (!foundA && --lni > indexA) { for (int lnj = indexA; lnj >= 0; lnj--) { if (_Components[lnj].LeftLide == _Components[lni].LeftLide) { foundA = true; break; } } } if (!foundA) { return(null); } #endregion return(new Tuple <Node, Node>(_Components[lni].LeftLide, _Components[rni].RightLide)); }