public override void Execute(params string[] parameters) { try { var uniqueIndexes = new List <string>(); var shapeLocators = new List <ShapeLocator>(); if (parameters.Length < 1) { throw new ArgumentException("Отсуствует аргумент"); } foreach (var parameter in parameters) { if (uniqueIndexes.IndexOf(parameter) == -1) { uniqueIndexes.Add(parameter); shapeLocators.Add(ShapeLocator.Parse(parameter, picture)); } } foreach (var shapeLocator in shapeLocators) { var shape = shapeLocator.Shape; var parent = shapeLocator.Parent; if (parent != null) { parent.Shapes.Remove(shape); if (parent.Shapes.Count < 2) { var grandParent = shapeLocator.GrandParent; if (grandParent != null) { grandParent.Shapes.Add(parent.Shapes[0]); grandParent.Shapes.Remove(parent); } else { picture.Add(parent.Shapes[0]); picture.Remove(parent); } } } else { SelectionContainer.GetInstance().OnMainRemove(shape); picture.Remove(shape); } } UpdateHistory(); SelectionContainer.GetInstance().OnMainRemove(picture); } catch (ArgumentException e) { Console.WriteLine(e.Message); } }
public void Ungroup(string[] indexes) { var shape = ShapeLocator.Parse(indexes[0], this); SelectionContainer.GetInstance().OnUngroup(shape); if (shape.Shape is CompoundShape) { var compoundShape = shape.Shape as CompoundShape; if (shape.Parent != null) { shape.Parent.Shapes.Remove(shape.Shape); foreach (var currentShape in compoundShape.Shapes) { shape.Parent.Shapes.Add(currentShape); } } else { Remove(shape.Shape); foreach (var currentShape in compoundShape.Shapes) { Add(currentShape); } } } else { throw new ArgumentException($"Фигура с указанным индексом '{indexes[0]}' не составная"); } }
protected IEnumerable <IShape> GetShapes(string[] parameters, int shapeIndex) { if (parameters.Length < shapeIndex) { var shapes = SelectionContainer.GetInstance().Shapes; if (shapes.Count == 0) { throw new ArgumentException("Для команды необходимы аргументы либо выбор с помощью " + "команды 'select'"); } return(shapes); } var shape = ShapeLocator.Parse(parameters[shapeIndex - 1], picture).Shape; return(new List <IShape> { shape }); }
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); } }