Files
Shrlalgo.RvKits/RvAddinTest/InstancesCreator.cs

89 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Nice3point.Revit.Toolkit.External;
using Nice3point.Revit.Toolkit.Options;
using Nice3point.Revit.Toolkit.Utils;
namespace RvAddinTest
{
[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();
}
}
public class InsertInfo
{
public string Name { get; set; }
public XYZ InsertPoint { get; set; }
public double Radian { get; set; }
public double Length { get; set; }
}
}