[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
[Journaling(JournalingMode.NoCommandData)]
internal class Class3 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uIDocument = commandData.Application.ActiveUIDocument;
Document revitDoc = uIDocument.Document;
FilteredElementCollector collector = new FilteredElementCollector(revitDoc);
View3DD = collector.OfClass(typeof(View3D)).Cast<View3D>().Where(item => item.Name == "{三维}").First();
Reference selection = uIDocument.Selection.PickObject(ObjectType.Element, "选择一个构件");
Element instance = revitDoc.GetElement(selection) as Element;
if (instance != null)
{
FamilyInstance = instance;
}
if (FamilyInstance == null)
{
message = "错误";
return Result.Failed;
}
using (Transaction tr = new Transaction(revitDoc, "cl"))
{
tr.Start();
Floor = revitDoc.GetElement(new ElementId(1597532L)) as Floor;
Line line = CalculateLineAboveFloor();
Plane plane = Plane.CreateByNormalAndOrigin(new XYZ(1, 0, 0), line.GetEndPoint(0));
SketchPlane sketchPlane = SketchPlane.Create(revitDoc, plane);
ModelCurve modelCurve = revitDoc.Create.NewModelCurve(line, sketchPlane);
TaskDialog.Show("提示", $"{line.Length * 304.8:f2}");
tr.Commit();
}
return Result.Succeeded;
}
private Element FamilyInstance { get; set; } = null;
private View3D View3DD { get; set; } = null;
private Floor Floor { get; set; } = null;
private Line CalculateLineAboveFloor()
{
BoundingBoxXYZ box = FamilyInstance.get_BoundingBox(View3DD);
XYZ centerPoint = box.Min.Add(box.Max).Multiply(0.5);
XYZ rayDirection = new XYZ(0, 0, -1);
ReferenceIntersector referenceIntersector = new ReferenceIntersector(Floor.Id, FindReferenceTarget.Face, View3DD);
IList<ReferenceWithContext> references = referenceIntersector.Find(centerPoint, rayDirection);
double distance = double.PositiveInfinity;
XYZ intersection = null;
foreach (ReferenceWithContext item in references)
{
Reference reference = item.GetReference();
double proximity = item.Proximity;
if (proximity < distance)
{
distance = proximity;
intersection = reference.GlobalPoint;
}
}
Line line = Line.CreateBound(centerPoint, intersection);
return line;
}
}
revit二次开发测量构件中心到楼板的距离
最新推荐文章于 2024-11-12 23:38:30 发布