public void Delete(CompositionInstance composition)
 {
     if (!composition.Name.Equals(CompositionInstancesList.MainCompositionName))
     {
         /* Delete all connections to the composition */
         PortsConnectionsList connections = composition.GetConnections();
         foreach (PortConnection connection in connections)
         {
             Delete(connection);
         }
         compositions.Remove(composition);
     }
 }
        public PortsConnectionsList GetPortConnections(PortPainter portPainter)
        {
            PortsConnectionsList portConnection = new PortsConnectionsList();

            foreach (CompositionInstance composition in Compositions)
            {
                foreach (PortConnection connection in composition.Connections)
                {
                    if (connection.PortPainter1Guid.Equals(portPainter.GUID) || connection.PortPainter2Guid.Equals(portPainter.GUID))
                    {
                        portConnection.Add(connection);
                    }
                }
            }
            return(portConnection);
        }
        public void GetOppositePortAndComponent(PortPainter portPainter, out ComponentInstance oppositeComponent, out PortPainter oppositePort)
        {
            oppositeComponent = null;
            oppositePort      = null;

            if (portPainter.PortDefenition.InterfaceName == "isrDesiredForce")
            {
            }

            PortsConnectionsList portConnections = GetPortConnections(portPainter);

            if (portConnections.Count != 0)
            {
                oppositePort = portConnections[0].GetOppositePort(portPainter);
                if (oppositePort != null)
                {
                    IElementWithPorts elem = FindComponentInstanceByPort(oppositePort);
                    if (elem is ComponentInstance)
                    {
                        /* No problem just return the component*/
                        oppositeComponent = elem as ComponentInstance;
                    }
                    /* the port belongs to the composition */
                    else if (elem is CompositionInstance)
                    {
                        CompositionInstance composition = elem as CompositionInstance;
                        PortPainter         middlePort;
                        /* Is it composition's external port?*/
                        int externalPortIndex = composition.Ports.IndexOf(oppositePort);
                        if (externalPortIndex >= 0)
                        {
                            /* get internal index */
                            middlePort = composition.InternalPortsInstances[externalPortIndex];
                        }
                        else /* It was internal port */
                        {
                            /* get external port */
                            int internalPortIndex = composition.InternalPortsInstances.IndexOf(oppositePort);
                            middlePort = composition.Ports[internalPortIndex];
                        }

                        /* Get middle port connections */
                        portConnections = GetPortConnections(middlePort);
                        if (portConnections.Count > 0)
                        {
                            PortConnection connection = portConnections[0];
                            oppositePort = connection.GetOppositePort(middlePort);
                            Object oppositeObject = FindComponentInstanceByPort(oppositePort);

                            /* Check if another port belongs to composition */
                            if (oppositeObject is CompositionInstance)
                            {
                                CompositionInstance oppositeComposition = oppositeObject as CompositionInstance;
                                PortPainter         middlePort2;

                                int externalPortIndex2 = oppositeComposition.Ports.IndexOf(oppositePort);

                                /* get internal index */
                                middlePort2 = oppositeComposition.InternalPortsInstances[externalPortIndex2];

                                /* Get middle port connections */
                                PortsConnectionsList portConnections2 = GetPortConnections(middlePort2);
                                if (portConnections2.Count > 0)
                                {
                                    PortConnection connection3 = portConnections2[0];
                                    oppositePort      = connection3.GetOppositePort(middlePort2);
                                    oppositeComponent = FindComponentInstanceByPort(oppositePort) as ComponentInstance;
                                }
                            }
                            else
                            {
                                oppositeComponent = oppositeObject as ComponentInstance;
                            }
                        }
                    }
                }
            }
        }