Exemplo n.º 1
0
        /// <summary>
        /// Traverses a given path of groups, starting at the last (picked) item through a given destination group, for the pin
        /// that corresponds to the given group pin in the last group in the path. The last (innermost) group of "hitPath" is the picked item.</summary>
        /// <typeparam name="TEdgeRoute">Pin</typeparam>
        /// <param name="hitPath">Path of groups to traverse</param>
        /// <param name="destNode">Destination group (last group to search) which contains the pin searched for</param>
        /// <param name="hitRoute">Pin in innermost group where search begins</param>
        /// <returns>Group pin in destination group "destNode" corresponding to "hitRoute" group pin in innermost group in path</returns>
        static public TEdgeRoute EdgeRouteTraverser <TEdgeRoute>(AdaptablePath <object> hitPath, object destNode, TEdgeRoute hitRoute)
            where TEdgeRoute : class, ICircuitPin
        {
            if (!hitPath.Last.Is <Element>())
            {
                return(null);
            }
            int fromIndex = hitPath.Count - 1; //start from the hit sub-item

            int toIndex = hitPath.IndexOf(destNode);

            if (toIndex < 0 || toIndex > fromIndex)
            {
                return(null);
            }


            var circuitPin     = hitRoute;
            var currentElement = hitPath.Last;

            for (int i = fromIndex - 1; i >= toIndex; --i)
            {
                ICircuitPin matchedPin = null;
                var         parent     = hitPath[i];
                if (parent.Is <Group>())
                {
                    var group = parent.Cast <Group>();
                    foreach (var pin in group.AllInputGroupPins)
                    {
                        var grpPin = pin.Cast <GroupPin>();
                        if (grpPin.InternalElement.Equals(currentElement) &&
                            grpPin.InternalElement.InputPin(grpPin.InternalPinName) == circuitPin)
                        {
                            matchedPin = grpPin;
                            break;
                        }
                    }
                    if (matchedPin == null)
                    {
                        foreach (var pin in group.OutputGroupPins)
                        {
                            var grpPin = pin.Cast <GroupPin>();
                            if (grpPin.InternalElement.Equals(currentElement) &&
                                grpPin.InternalElement.OutputPin(grpPin.InternalPinName) == circuitPin)
                            {
                                matchedPin = grpPin;
                                break;
                            }
                        }
                    }
                }
                if (matchedPin == null)
                {
                    return(null);
                }
                circuitPin     = matchedPin.Cast <TEdgeRoute>();
                currentElement = parent;
            }
            return(circuitPin);
        }