private void AlignAnnotationElements(List <AnnotationElement> annotationElements, AlignType alignType, Document document) { View currentView = document.ActiveView; XYZ displacementDirection = currentView.UpDirection; switch (alignType) { case AlignType.Left: AnnotationElement farthestAnnotation = annotationElements.OrderBy(x => x.UpLeft.X).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(farthestAnnotation.UpLeft.X, annotationElement.UpLeft.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Left); } break; case AlignType.Right: farthestAnnotation = annotationElements.OrderByDescending(x => x.UpRight.X).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(farthestAnnotation.UpRight.X, annotationElement.UpRight.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Right); } break; case AlignType.Up: farthestAnnotation = annotationElements.OrderByDescending(x => x.UpRight.Y).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(annotationElement.UpRight.X, farthestAnnotation.UpRight.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Up); } break; case AlignType.Down: farthestAnnotation = annotationElements.OrderBy(x => x.DownRight.Y).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(annotationElement.DownRight.X, farthestAnnotation.DownRight.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Down); } break; case AlignType.Center: //On the same vertical axe List <AnnotationElement> sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.X).ToList(); AnnotationElement rightAnnotation = sortedAnnotationElements.LastOrDefault(); AnnotationElement leftAnnotation = sortedAnnotationElements.FirstOrDefault(); double XCoord = (rightAnnotation.Center.X + leftAnnotation.Center.X) / 2; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(XCoord, annotationElement.Center.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Center); } break; case AlignType.Middle: //On the same horizontal axe sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.Y).ToList(); AnnotationElement upperAnnotation = sortedAnnotationElements.LastOrDefault(); AnnotationElement lowerAnnotation = sortedAnnotationElements.FirstOrDefault(); double YCoord = (upperAnnotation.Center.Y + lowerAnnotation.Center.Y) / 2; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(annotationElement.Center.X, YCoord, 0); annotationElement.MoveTo(resultingPoint, AlignType.Middle); } break; case AlignType.Vertically: sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.Y).ToList(); upperAnnotation = sortedAnnotationElements.LastOrDefault(); lowerAnnotation = sortedAnnotationElements.FirstOrDefault(); double spacing = (upperAnnotation.Center.Y - lowerAnnotation.Center.Y) / (annotationElements.Count - 1); int i = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(annotationElement.Center.X, lowerAnnotation.Center.Y + i * spacing, 0); annotationElement.MoveTo(resultingPoint, AlignType.Vertically); i++; } break; case AlignType.Horizontally: sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.X).ToList(); rightAnnotation = sortedAnnotationElements.LastOrDefault(); leftAnnotation = sortedAnnotationElements.FirstOrDefault(); spacing = (rightAnnotation.Center.X - leftAnnotation.Center.X) / (annotationElements.Count - 1); i = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(leftAnnotation.Center.X + i * spacing, annotationElement.Center.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Horizontally); i++; } break; case AlignType.UntangleVertically: sortedAnnotationElements = annotationElements.OrderBy(y => y.GetLeaderEnd().Y).ToList(); upperAnnotation = sortedAnnotationElements.FirstOrDefault(); spacing = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(annotationElement.UpLeft.X, upperAnnotation.UpLeft.Y + spacing, 0); annotationElement.MoveTo(resultingPoint, AlignType.UntangleVertically); spacing = spacing + (annotationElement.UpLeft.Y - annotationElement.DownLeft.Y); } break; case AlignType.UntangleHorizontally: sortedAnnotationElements = annotationElements.OrderBy(x => x.GetLeaderEnd().X).ToList(); leftAnnotation = sortedAnnotationElements.FirstOrDefault(); spacing = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(leftAnnotation.UpLeft.X + spacing, annotationElement.UpLeft.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.UntangleHorizontally); spacing = spacing + (annotationElement.UpRight.X - annotationElement.UpLeft.X); } break; default: break; } }
private void AlignTag(UIDocument UIDoc, AlignType alignType, TransactionGroup txg) { Document document = UIDoc.Document(); ICollection <ElementId> elementIds = UIDoc.Selection().GetElementIds(); bool flag = false; using (Transaction tx = new Transaction(document)) { txg.Start("Align Tag"); if (elementIds.Count == 0) { flag = true; IList <Reference> selectedReferences = UIDoc.get_Selection().PickObjects((ObjectType)1, "Pick elements to be aligned"); elementIds = Tools.RevitReferencesToElementIds(document, selectedReferences); UIDoc.get_Selection().SetElementIds(elementIds); } List <AnnotationElement> source = this.RetriveAnnotationElementsFromSelection(UIDoc, tx); txg.RollBack(); txg.Start("Align Tag"); tx.Start("Align Tags"); if (source.Count > 1) { document.get_ActiveView().get_UpDirection(); switch (alignType) { case AlignType.Left: AnnotationElement annotationElement1 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpLeft.X())).FirstOrDefault <AnnotationElement>(); using (List <AnnotationElement> .Enumerator enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(annotationElement1.UpLeft.X(), current.UpLeft.Y(), 0.0); current.MoveTo(point, AlignType.Left); } break; } case AlignType.Right: AnnotationElement annotationElement2 = source.OrderByDescending <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpRight.get_X())).FirstOrDefault <AnnotationElement>(); using (List <AnnotationElement> .Enumerator enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(annotationElement2.UpRight.get_X(), current.UpRight.get_Y(), 0.0); current.MoveTo(point, AlignType.Right); } break; } case AlignType.Up: AnnotationElement annotationElement3 = source.OrderByDescending <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpRight.get_Y())).FirstOrDefault <AnnotationElement>(); using (List <AnnotationElement> .Enumerator enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(current.UpRight.get_X(), annotationElement3.UpRight.get_Y(), 0.0); current.MoveTo(point, AlignType.Up); } break; } case AlignType.Down: AnnotationElement annotationElement4 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.DownRight.get_Y())).FirstOrDefault <AnnotationElement>(); using (List <AnnotationElement> .Enumerator enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(current.DownRight.get_X(), annotationElement4.DownRight.get_Y(), 0.0); current.MoveTo(point, AlignType.Down); } break; } case AlignType.Center: List <AnnotationElement> list1 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpRight.get_X())).ToList <AnnotationElement>(); double num1 = (list1.LastOrDefault <AnnotationElement>().Center.get_X() + list1.FirstOrDefault <AnnotationElement>().Center.get_X()) / 2.0; using (List <AnnotationElement> .Enumerator enumerator = list1.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(num1, current.Center.get_Y(), 0.0); current.MoveTo(point, AlignType.Center); } break; } case AlignType.Middle: List <AnnotationElement> list2 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpRight.get_Y())).ToList <AnnotationElement>(); double num2 = (list2.LastOrDefault <AnnotationElement>().Center.get_Y() + list2.FirstOrDefault <AnnotationElement>().Center.get_Y()) / 2.0; using (List <AnnotationElement> .Enumerator enumerator = list2.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(current.Center.get_X(), num2, 0.0); current.MoveTo(point, AlignType.Middle); } break; } case AlignType.Vertically: List <AnnotationElement> list3 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpRight.get_Y())).ToList <AnnotationElement>(); AnnotationElement annotationElement5 = list3.LastOrDefault <AnnotationElement>(); AnnotationElement annotationElement6 = list3.FirstOrDefault <AnnotationElement>(); double num3 = (annotationElement5.Center.get_Y() - annotationElement6.Center.get_Y()) / (double)(source.Count - 1); int num4 = 0; using (List <AnnotationElement> .Enumerator enumerator = list3.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(current.Center.get_X(), annotationElement6.Center.get_Y() + (double)num4 * num3, 0.0); current.MoveTo(point, AlignType.Vertically); ++num4; } break; } case AlignType.Horizontally: List <AnnotationElement> list4 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.UpRight.get_X())).ToList <AnnotationElement>(); AnnotationElement annotationElement7 = list4.LastOrDefault <AnnotationElement>(); AnnotationElement annotationElement8 = list4.FirstOrDefault <AnnotationElement>(); double num5 = (annotationElement7.Center.get_X() - annotationElement8.Center.get_X()) / (double)(source.Count - 1); int num6 = 0; using (List <AnnotationElement> .Enumerator enumerator = list4.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(annotationElement8.Center.get_X() + (double)num6 * num5, current.Center.get_Y(), 0.0); current.MoveTo(point, AlignType.Horizontally); ++num6; } break; } case AlignType.UntangleVertically: List <AnnotationElement> list5 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(y => y.GetLeaderEnd().get_Y())).ToList <AnnotationElement>(); AnnotationElement annotationElement9 = list5.FirstOrDefault <AnnotationElement>(); double num7 = 0.0; using (List <AnnotationElement> .Enumerator enumerator = list5.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(current.UpLeft.get_X(), annotationElement9.UpLeft.Y() + num7, 0.0); current.MoveTo(point, AlignType.UntangleVertically); num7 += current.UpLeft.get_Y() - current.DownLeft.get_Y(); } break; } case AlignType.UntangleHorizontally: List <AnnotationElement> list6 = source.OrderBy <AnnotationElement, double>((Func <AnnotationElement, double>)(x => x.GetLeaderEnd().get_X())).ToList <AnnotationElement>(); AnnotationElement annotationElement10 = list6.FirstOrDefault <AnnotationElement>(); double num8 = 0.0; using (List <AnnotationElement> .Enumerator enumerator = list6.GetEnumerator()) { while (enumerator.MoveNext()) { AnnotationElement current = enumerator.Current; XYZ point = new XYZ(annotationElement10.UpLeft.get_X() + num8, current.UpLeft.Y(), 0.0); current.MoveTo(point, AlignType.UntangleHorizontally); num8 += current.UpRight.get_X() - current.UpLeft.get_X(); } break; } } } tx.Commit(); txg.Assimilate(); } if (flag) { List <ElementId> elementIdList = new List <ElementId>(); elementIdList.Add(ElementId.get_InvalidElementId()); elementIds = (ICollection <ElementId>)elementIdList; } UIDoc.get_Selection().SetElementIds(elementIds); }
private void AlignTag(UIDocument UIDoc, AlignType alignType, TransactionGroup txg) { // Get the handle of current document. Document doc = UIDoc.Document; ICollection <ElementId> selectedIds = UIDoc.Selection.GetElementIds(); // 1. First Proposed Change // First check if there is something that's been seleted, and if so - operate on that // However, if the Uidoc.Slection is empty, prompt for selection. // This allows you to stay on the 'Add-ins' Tab and keep using the 'Align' tab without going back and forth to 'Modify' // TO-DO: Should we disselect after we are done? (delete the boolean stuff if you don't think it's worth disselecting) bool empty = false; using (Transaction tx = new Transaction(doc)) { txg.Start("Align Tag"); if (selectedIds.Count == 0) { empty = true; IList <Reference> selectedReferences = UIDoc.Selection.PickObjects(ObjectType.Element, "Pick elements to be aligned"); selectedIds = Tools.RevitReferencesToElementIds(doc, selectedReferences); UIDoc.Selection.SetElementIds(selectedIds); } List <AnnotationElement> annotationElements = RetriveAnnotationElementsFromSelection(UIDoc, tx); txg.RollBack(); txg.Start("Align Tag"); tx.Start("Align Tags"); if (annotationElements.Count > 1) { View currentView = doc.ActiveView; XYZ displacementDirection = currentView.UpDirection; switch (alignType) { case AlignType.Left: AnnotationElement farthestAnnotation = annotationElements.OrderBy(x => x.UpLeft.X).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(farthestAnnotation.UpLeft.X, annotationElement.UpLeft.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Left); } break; case AlignType.Right: farthestAnnotation = annotationElements.OrderByDescending(x => x.UpRight.X).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(farthestAnnotation.UpRight.X, annotationElement.UpRight.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Right); } break; case AlignType.Up: farthestAnnotation = annotationElements.OrderByDescending(x => x.UpRight.Y).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(annotationElement.UpRight.X, farthestAnnotation.UpRight.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Up); } break; case AlignType.Down: farthestAnnotation = annotationElements.OrderBy(x => x.DownRight.Y).FirstOrDefault(); foreach (AnnotationElement annotationElement in annotationElements) { XYZ resultingPoint = new XYZ(annotationElement.DownRight.X, farthestAnnotation.DownRight.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Down); } break; case AlignType.Center: //On the same vertical axe List <AnnotationElement> sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.X).ToList(); AnnotationElement rightAnnotation = sortedAnnotationElements.LastOrDefault(); AnnotationElement leftAnnotation = sortedAnnotationElements.FirstOrDefault(); double XCoord = (rightAnnotation.Center.X + leftAnnotation.Center.X) / 2; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(XCoord, annotationElement.Center.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Center); } break; case AlignType.Middle: //On the same horizontal axe sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.Y).ToList(); AnnotationElement upperAnnotation = sortedAnnotationElements.LastOrDefault(); AnnotationElement lowerAnnotation = sortedAnnotationElements.FirstOrDefault(); double YCoord = (upperAnnotation.Center.Y + lowerAnnotation.Center.Y) / 2; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(annotationElement.Center.X, YCoord, 0); annotationElement.MoveTo(resultingPoint, AlignType.Middle); } break; case AlignType.Vertically: sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.Y).ToList(); upperAnnotation = sortedAnnotationElements.LastOrDefault(); lowerAnnotation = sortedAnnotationElements.FirstOrDefault(); double spacing = (upperAnnotation.Center.Y - lowerAnnotation.Center.Y) / (annotationElements.Count - 1); int i = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(annotationElement.Center.X, lowerAnnotation.Center.Y + i * spacing, 0); annotationElement.MoveTo(resultingPoint, AlignType.Vertically); i++; } break; case AlignType.Horizontally: sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.X).ToList(); rightAnnotation = sortedAnnotationElements.LastOrDefault(); leftAnnotation = sortedAnnotationElements.FirstOrDefault(); spacing = (rightAnnotation.Center.X - leftAnnotation.Center.X) / (annotationElements.Count - 1); i = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(leftAnnotation.Center.X + i * spacing, annotationElement.Center.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.Horizontally); i++; } break; case AlignType.UntangleVertically: sortedAnnotationElements = annotationElements.OrderBy(y => y.GetLeaderEnd().Y).ToList(); upperAnnotation = sortedAnnotationElements.FirstOrDefault(); spacing = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(annotationElement.UpLeft.X, upperAnnotation.UpLeft.Y + spacing, 0); annotationElement.MoveTo(resultingPoint, AlignType.UntangleVertically); spacing = spacing + (annotationElement.UpLeft.Y - annotationElement.DownLeft.Y); } break; case AlignType.UntangleHorizontally: sortedAnnotationElements = annotationElements.OrderBy(x => x.GetLeaderEnd().X).ToList(); leftAnnotation = sortedAnnotationElements.FirstOrDefault(); spacing = 0; foreach (AnnotationElement annotationElement in sortedAnnotationElements) { XYZ resultingPoint = new XYZ(leftAnnotation.UpLeft.X + spacing, annotationElement.UpLeft.Y, 0); annotationElement.MoveTo(resultingPoint, AlignType.UntangleHorizontally); spacing = spacing + (annotationElement.UpRight.X - annotationElement.UpLeft.X); } break; default: break; } } tx.Commit(); txg.Assimilate(); } // Disselect if the selection was empty to begin with if (empty) { selectedIds = new List <ElementId> { ElementId.InvalidElementId } } ; UIDoc.Selection.SetElementIds(selectedIds); } }