Ejemplo n.º 1
0
        public void Group(string[] indexes)
        {
            var positions = new List <string>();

            foreach (var index in indexes)
            {
                if (positions.IndexOf(index) == -1)
                {
                    bool correct = true;
                    foreach (var position in positions)
                    {
                        if (index.Length > position.Length && index.StartsWith(position))
                        {
                            correct = false;
                            break;
                        }
                        else if (index.Length < position.Length && position.StartsWith(index))
                        {
                            positions.Remove(position);
                        }
                    }
                    if (correct)
                    {
                        positions.Add(index);
                    }
                }
            }

            if (positions.Count < 2)
            {
                throw new ArgumentException("Для группировки необходимы минимум 2 фигуры");
            }

            lock (lockObject)
            {
                int j      = 0;
                var shapes = new ShapeLocator[positions.Count];
                foreach (var position in positions)
                {
                    shapes[j] = ShapeLocator.Parse(position, this);

                    j++;
                }

                CompoundShape compoundShape = new CompoundShape();
                foreach (var shape in shapes)
                {
                    compoundShape.Shapes.Add(shape.Shape);
                    if (shape.Parent != null)
                    {
                        shape.Parent.Shapes.Remove(shape.Shape);
                    }
                    else
                    {
                        Remove(shape.Shape);
                    }
                }

                foreach (var shape in shapes)
                {
                    if (shape.Parent != null && shape.Parent.Shapes.Count < 2)
                    {
                        if (shape.GrandParent != null)
                        {
                            if (shape.Parent.Shapes.Count == 1)
                            {
                                shape.GrandParent.Shapes.Add(shape.Parent.Shapes[0]);
                            }
                            shape.GrandParent.Shapes.Remove(shape.Parent);
                        }
                        else
                        {
                            if (shape.Parent.Shapes.Count == 1)
                            {
                                Add(shape.Parent.Shapes[0]);
                            }
                            Remove(shape.Parent);
                        }
                    }
                }
                Add(compoundShape);
            }
        }
Ejemplo n.º 2
0
 private ShapeLocator()
 {
     Parent      = null;
     GrandParent = null;
 }