private void LinkPortTo(ISimulationAccess sim, Port port, Node nDesired)
        {
            Node        nCurrent = null;
            List <Node> nRemove  = null;

            foreach (Node n in port.Neighbors)
            {
                if (n is Port)
                {
                    // ignore - this is a link to the submodule's circuit
                }
                else if (nCurrent == null)
                {
                    nCurrent = n;
                }
                else
                {
                    if (nRemove == null)
                    {
                        nRemove = new List <Node>();
                    }
                    nRemove.Add(n);
                }
            }
            if (nRemove != null)
            {
                foreach (Node n in nRemove)
                {
                    if (Debug)
                    {
                        Console.WriteLine("    remove link {0}-{0}", port, n);
                    }
                    sim.RemoveLink(new Link(port, n));
                }
            }
            if (nDesired != nCurrent)
            {
                if (nCurrent != null)
                {
                    if (Debug)
                    {
                        Console.WriteLine("    remove link {0}-{1}", port, nCurrent);
                    }
                    sim.RemoveLink(new Link(port, nCurrent));
                }
                if (nDesired != null)
                {
                    if (Debug)
                    {
                        Console.WriteLine("    add link {0}-{1}", port, nDesired);
                    }
                    sim.AddLink(new Link(port, nDesired));
                }
            }
        }
        private void RenewPortLinks(ILayoutAccess subLayout, ISimulationAccess sim)
        {
            IEnumerator <Port>      ports = this.Ports.GetEnumerator();
            IEnumerator <Component> pins  = subLayout.Pins.GetEnumerator();

            while (ports.MoveNext() && pins.MoveNext())
            {
                Port      compPort    = ports.Current;
                Component pin         = pins.Current;
                Instance  pinInstance = subSimulation.GetInstance(subLayout, pin);
                if (pinInstance != null)
                {
                    Port pinPort = null;
                    foreach (Port tempPinPort in pinInstance.Ports)
                    {
                        pinPort = tempPinPort;
                        break;
                    }
                    List <Link> toRemove = new List <Link>(2);
                    foreach (Node nbr in pinPort.Neighbors)
                    {
                        if (nbr is Port && nbr != compPort)
                        {
                            toRemove.Add(new Link(pinPort, nbr));
                        }
                    }
                    foreach (Node nbr in compPort.Neighbors)
                    {
                        if (nbr is Port && nbr != pinPort)
                        {
                            toRemove.Add(new Link(compPort, nbr));
                        }
                    }
                    foreach (Link outLink in toRemove)
                    {
                        sim.RemoveLink(outLink);
                    }
                    sim.AddLink(new Link(compPort, pinPort));
                }
            }
        }