public void AlignSelected(object parameter) { string alignType = parameter.ToString(); if (DynamoSelection.Instance.Selection.Count <= 1) { return; } // All the models in the selection will be modified, // record their current states before anything gets changed. SmartCollection <ISelectable> selection = DynamoSelection.Instance.Selection; IEnumerable <ModelBase> models = selection.OfType <ModelBase>(); WorkspaceModel.RecordModelsForModification(models.ToList(), Model.UndoRecorder); var toAlign = DynamoSelection.Instance.Selection.OfType <ILocatable>().ToList(); switch (alignType) { case "HorizontalCenter": { var xAll = GetSelectionAverageX(); toAlign.ForEach((x) => { x.CenterX = xAll; }); } break; case "HorizontalLeft": { var xAll = GetSelectionMinX(); toAlign.ForEach((x) => { x.X = xAll; }); } break; case "HorizontalRight": { var xAll = GetSelectionMaxX(); toAlign.ForEach((x) => { x.X = xAll - x.Width; }); } break; case "VerticalCenter": { var yAll = GetSelectionAverageY(); toAlign.ForEach((x) => { x.CenterY = yAll; }); } break; case "VerticalTop": { var yAll = GetSelectionMinY(); toAlign.ForEach((x) => { x.Y = yAll; }); } break; case "VerticalBottom": { var yAll = GetSelectionMaxY(); toAlign.ForEach((x) => { x.Y = yAll - x.Height; }); } break; case "VerticalDistribute": { if (DynamoSelection.Instance.Selection.Count <= 2) { return; } var yMin = GetSelectionMinY(); var yMax = GetSelectionMaxY(); var spacing = 0.0; var span = yMax - yMin; var nodeHeightSum = DynamoSelection.Instance.Selection.Where(y => y is ILocatable) .Cast <ILocatable>() .Sum((y) => y.Height); if (span > nodeHeightSum) { spacing = (span - nodeHeightSum) / (DynamoSelection.Instance.Selection.Count - 1); } var cursor = yMin; foreach (var node in toAlign.OrderBy(y => y.Y)) { node.Y = cursor; cursor += node.Height + spacing; } } break; case "HorizontalDistribute": { if (DynamoSelection.Instance.Selection.Count <= 2) { return; } var xMin = GetSelectionMinX(); var xMax = GetSelectionMaxX(); var spacing = 0.0; var span = xMax - xMin; var nodeWidthSum = DynamoSelection.Instance.Selection.Where((x) => x is ILocatable) .Cast <ILocatable>() .Sum((x) => x.Width); // If there is more span than total node width, // distribute the nodes with a gap. If not, leave // the spacing at 0 and the nodes will distribute // up against each other. if (span > nodeWidthSum) { spacing = (span - nodeWidthSum) / (DynamoSelection.Instance.Selection.Count - 1); } var cursor = xMin; foreach (var node in toAlign.OrderBy(x => x.X)) { node.X = cursor; cursor += node.Width + spacing; } } break; } toAlign.ForEach(x => x.ReportPosition()); }
public void AlignSelected(object parameter) { string alignType = parameter.ToString(); if (DynamoSelection.Instance.Selection.Count <= 1) { return; } // All the models in the selection will be modified, // record their current states before anything gets changed. SmartCollection <ISelectable> selection = DynamoSelection.Instance.Selection; IEnumerable <ModelBase> models = selection.OfType <ModelBase>(); _model.RecordModelsForModification(models.ToList()); var toAlign = DynamoSelection.Instance.Selection.Where((x) => x is ILocatable) .Cast <ILocatable>() .ToList(); if (alignType == "HorizontalCenter") // make vertial line of elements { var xAll = GetSelectionAverageX(); toAlign.ForEach((x) => { x.CenterX = xAll; }); } else if (alignType == "HorizontalLeft") { var xAll = GetSelectionMinX(); toAlign.ForEach((x) => { x.X = xAll; }); } else if (alignType == "HorizontalRight") { var xAll = GetSelectionMaxX(); toAlign.ForEach((x) => { x.X = xAll - x.Width; }); } else if (alignType == "VerticalCenter") { var yAll = GetSelectionAverageY(); toAlign.ForEach((x) => { x.CenterY = yAll; }); } else if (alignType == "VerticalTop") { var yAll = GetSelectionMinY(); toAlign.ForEach((x) => { x.Y = yAll; }); } else if (alignType == "VerticalBottom") { var yAll = GetSelectionMaxY(); toAlign.ForEach((x) => { x.Y = yAll - x.Height; }); } else if (alignType == "VerticalDistribute") { if (DynamoSelection.Instance.Selection.Count <= 2) { return; } var yMin = GetSelectionMinY(); var yMax = GetSelectionMaxTopY(); var spacing = (yMax - yMin) / (DynamoSelection.Instance.Selection.Count - 1); int count = 0; toAlign.OrderBy((x) => x.Y) .ToList() .ForEach((x) => x.Y = yMin + spacing * count++); } else if (alignType == "HorizontalDistribute") { if (DynamoSelection.Instance.Selection.Count <= 2) { return; } var xMin = GetSelectionMinX(); var xMax = GetSelectionMaxLeftX(); var spacing = (xMax - xMin) / (DynamoSelection.Instance.Selection.Count - 1); int count = 0; toAlign.OrderBy((x) => x.X) .ToList() .ForEach((x) => x.X = xMin + spacing * count++); } toAlign.ForEach(x => x.ReportPosition()); }