Exemple #1
0
        /// <summary>
        /// Queries all direct routes between two blocks.
        /// </summary>
        /// <param name="block0"></param>
        /// <param name="block1"></param>
        /// <returns></returns>
        public RouteList Get(PlanItem block0, PlanItem block1)
        {
            var res = new RouteList();

            if (block0 == null)
            {
                return(res);
            }
            if (block1 == null)
            {
                return(res);
            }
            foreach (var it in this)
            {
                if (it == null)
                {
                    continue;
                }
                var s = it.Start;
                var e = it.Target;
                if (s == null || e == null)
                {
                    continue;
                }

                if (!s.identifier.Equals(block0.identifier) && !s.identifier.Equals(block1.identifier))
                {
                    continue;
                }
                if (!e.identifier.Equals(block0.identifier) && !e.identifier.Equals(block1.identifier))
                {
                    continue;
                }

                // start of final checks
                if (s.identifier.Equals(block0.identifier) && e.identifier.Equals(block1.identifier))
                {
                    res.Add(it);
                }
                else if (e.identifier.Equals(block0.identifier) && s.identifier.Equals(block1.identifier))
                {
                    res.Add(it);
                }
                else if (s.identifier.Equals(block1.identifier) && e.identifier.Equals(block0.identifier))
                {
                    res.Add(it);
                }
                else if (e.identifier.Equals(block1.identifier) && s.identifier.Equals(block0.identifier))
                {
                    res.Add(it);
                }
            }

            return(res);
        }
        /// <summary>
        /// connectors[0] := start connector
        /// connectors[1] := target connector
        /// </summary>
        /// <param name="connectors"></param>
        /// <returns></returns>
        public List <Path> GetAllowedPath(out PlanItem[] connectors)
        {
            connectors = null;

            var ways = GetAllPossibleWays();

            var res = new List <Path>();

            foreach (var w in ways)
            {
                if (string.IsNullOrEmpty(w))
                {
                    continue;
                }
                var containsInt = w.Any(char.IsDigit);
                if (containsInt)
                {
                    var parts = w.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length != 2)
                    {
                        continue; // incorrect format
                    }
                    var from = parts[0].Trim();
                    var to   = parts[1].Trim();
                    if (from.Equals(to, StringComparison.OrdinalIgnoreCase))
                    {
                        continue; // incorrect format
                    }
                    try
                    {
                        var fromSide   = from[0];
                        var fromOffset = int.Parse(from.Substring(1));
                        if (!(fromSide >= 'a' && fromSide <= 'd'))
                        {
                            continue; // incorrect format
                        }
                        var toSide   = to[0];
                        var toOffset = int.Parse(to.Substring(1));
                        if (!(toSide >= 'a' && toSide <= 'd'))
                        {
                            continue; // incorrect format
                        }
                        var fromSideCoord = GetSideXy(fromSide, fromOffset);
                        var toSideCoord   = GetSideXy(toSide, toOffset);

                        var fromPreviousItem = GetBorderItem(fromSide, fromSideCoord);
                        var toNextItem       = GetBorderItem(toSide, toSideCoord);

                        if (this.IsBlock)
                        {
                            if (toNextItem == null)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (fromPreviousItem == null)
                            {
                                continue;
                            }
                            if (toNextItem == null)
                            {
                                continue;
                            }
                        }

                        var pp = new Path
                        {
                            From = fromPreviousItem,
                            To   = toNextItem
                        };

                        if (IsSwitch)
                        {
                            ApplySideInformation(ref pp, fromSide, toSide);
                        }

                        res.Add(pp);

                        // NOTICE(1) -- check previous note
                        if (this.IsSwitch)
                        {
                            var ppReverse = new Path
                            {
                                From = toNextItem,
                                To   = fromPreviousItem
                            };

                            ApplySideInformation(ref ppReverse, toSide, fromSide);

                            res.Add(ppReverse);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("GetAllowedPath(): " + ex.Message);
                    }
                }
                else
                {
                    if (w.Length < 2)
                    {
                        continue; // single target/source not supported
                    }
                    if (w.EndsWith("+", StringComparison.OrdinalIgnoreCase))
                    {
                        if (editor.connectorId == 1)
                        {
                            Trace.WriteLine("Connector id with 1 are ignored.");
                            continue;
                        }

                        var connectorItems = Ctx.GetConnectors(editor.connectorId);
                        if (connectorItems.Length != 2)
                        {
                            continue; // we need two elements with the same connectorId
                        }
                        PlanItem startConnector  = null;
                        PlanItem targetConnector = null;
                        if (identifier.Equals(connectorItems[0].identifier))
                        {
                            startConnector  = connectorItems[0];
                            targetConnector = connectorItems[1];
                        }
                        else if (identifier.Equals(connectorItems[1].identifier))
                        {
                            startConnector  = connectorItems[1];
                            targetConnector = connectorItems[0];
                        }
                        if (startConnector == null)
                        {
                            continue; // wrong connectors found
                        }
                        if (targetConnector == null)
                        {
                            continue; // invalid configuration
                        }
                        // from looks like "A+"
                        // to looks like "B+"

                        var from = w[0];

                        // get target themeDimIdx
                        var targetRoutes = targetConnector.GetDimensionRoutes();
                        if (targetRoutes.Count != 1)
                        {
                            continue; // invalid configuration; only one route for connector allowed
                        }
                        var targetMainRoute = targetRoutes.First().ToLower();
                        var to = targetMainRoute[0] == '+' ? targetMainRoute[1] : targetMainRoute[0];

                        var fromPreviousItem = GetBorderItem(from, new Position
                        {
                            x = coord.x,
                            y = coord.y
                        });

                        var toNextItem = GetBorderItem(to, new Position
                        {
                            x = targetConnector.coord.x,
                            y = targetConnector.coord.y
                        });

                        if (fromPreviousItem == null)
                        {
                            continue;
                        }
                        if (toNextItem == null)
                        {
                            continue;
                        }

                        connectors = new[] { startConnector, targetConnector };

                        var pp0 = new Path {
                            From = fromPreviousItem, To = toNextItem
                        };
                        var pp1 = new Path {
                            From = toNextItem, To = fromPreviousItem
                        };

                        res.Add(pp0);
                        res.Add(pp1);
                    }
                    else
                    {
                        var from = w[0];
                        var to   = w[1];

                        if (from == to)
                        {
                            continue; // incorrect format
                        }
                        var fromPreviousItem = GetBorderItem(from, new Position
                        {
                            x = coord.x,
                            y = coord.y
                        });

                        var toNextItem = GetBorderItem(to, new Position
                        {
                            x = coord.x,
                            y = coord.y
                        });

                        if (fromPreviousItem == null)
                        {
                            continue;
                        }
                        if (toNextItem == null)
                        {
                            continue;
                        }

                        var pp = new Path
                        {
                            From = fromPreviousItem,
                            To   = toNextItem
                        };

                        if (IsSwitch)
                        {
                            ApplySideInformation(ref pp, from, to);
                        }

                        res.Add(pp);
                    }
                }
            }

            return(res);
        }