using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Nice3point.Revit.Toolkit.External; namespace ShrlAlgo.Addin.Test; [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class InstancesCreator : ExternalCommand { public override void Execute() { var doc = Document; var uidoc = UiDocument; //var uidoc = uiApplication.ActiveUIDocument; //var doc = uidoc.Document; var ids = uidoc.Selection.GetElementIds(); using (Transaction ts = new(doc, "旋转")) { ts.Start(); var collector = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(TextNote)); foreach (var id in ids) { var elem = doc.GetElement(id); var loc = elem.Location as LocationPoint; var point = loc.Point; XYZ flattenXY = new(loc.Point.X, loc.Point.Y, 0); var tempDistance = double.MaxValue; TextNote textNoteNearest = null; var vector = XYZ.BasisX; foreach (var element in collector) { var textNote = element as TextNote; XYZ flattenCoord = new(textNote.Coord.X, textNote.Coord.Y, 0); var currentDistance = flattenCoord.DistanceTo(flattenXY); if (currentDistance < tempDistance) { tempDistance = currentDistance; textNoteNearest = textNote; vector = textNote.BaseDirection; } } if (textNoteNearest == null) { continue; } var radian = XYZ.BasisX.AngleTo(vector); var crossProduct = XYZ.BasisX.CrossProduct(textNoteNearest.BaseDirection).Normalize(); if (Math.Abs(radian - Math.PI) < 10E-6) { ElementTransformUtils.RotateElement(doc, id, Line.CreateUnbound(point, XYZ.BasisZ), Math.PI); } if (crossProduct.IsAlmostEqualTo(XYZ.BasisZ)) { ElementTransformUtils.RotateElement(doc, id, Line.CreateUnbound(point, XYZ.BasisZ), radian); } if (crossProduct.IsAlmostEqualTo(-XYZ.BasisZ)) { ElementTransformUtils.RotateElement(doc, id, Line.CreateUnbound(point, XYZ.BasisZ), 2 * Math.PI - radian); } } ts.Commit(); } //MainWindow window = new MainWindow(Document); //window.ShowDialog(); } }