revit二次开发测量构件中心到楼板的距离

[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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值