应用程序名称:MeasureHeight

Revit平台:所有版本

Revit版本:2011.0

首次发布版本:2011.0

编程语言:C#

技能水平:中等

类别:几何

类型:外部命令

主题:测量天窗窗户的高度。

概要:这个示例演示了如何使用Revit API方法FindReferencesWithContextByDirection来找到天窗窗户相对于地面高度的方法。

类:

Autodesk.Revit.DB.FilteredElementCollector

Autodesk.Revit.DB.FamilyInstance

Autodesk.Revit.DB.ModelCurve

Autodesk.Revit.DB.BoundingBoxXYZ

Autodesk.Revit.DB.Document

Autodesk.Revit.UI.Selection.Selection

项目文件:MeasureHeight.cs

描述:

1. 找到一个3D视图以在FindReferencesWithContextByDirection中使用。

2. 检查是否选择了一个屋顶托管的窗户。

3. 计算窗户相对于一个楼层的高度(该样例中楼层的ID是硬编码的)。

4. 使用窗户边界框的中心点作为FindReferencesWithContextByDirection的起点。

5. 使用负Z方向运行FindReferencesWithContextByDirection

6. 找到属于该楼层的最近参考。

7. 创建一个模型曲线来显示FindReferencesWithContextByDirection找到的“射线”。

说明:

1. 打开MeasureSkylightHeight.rvt

2. 选择天窗窗户。

3. 运行外部命令。

4. 弹出一个任务对话框报告窗户的高度,一条线将显示测量距离的位置。

源代码

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

MeasureHeight.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Revit.SDK.Samples.MeasureHeight.CS
{
    /// <summary>
    /// Calculate the height above the ground floor of a selected skylight.
    /// </summary>
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    public class Command : IExternalCommand
    {
        #region Class Members
        /// <summary>
        /// Revit application
        /// </summary>
        private Autodesk.Revit.ApplicationServices.Application m_app;

        /// <summary>
        /// Revit active document
        /// </summary>
        private Document m_doc;

        /// <summary>
        /// a 3d view
        /// </summary>
        private View3D m_view3D;

        /// <summary>
        /// skylight family instance
        /// </summary>
        private FamilyInstance m_skylight;

        /// <summary>
        /// Floor element
        /// </summary>
        private Floor m_floor;
        #endregion

        #region Class Interface Implementation
        /// <summary>
        /// The top level command.
        /// </summary>
        /// <param name="revit">An object that is passed to the external application 
        /// which contains data related to the command, 
        /// such as the application object and active view.</param>
        /// <param name="message">A message that can be set by the external application 
        /// which will be displayed if a failure or cancellation is returned by 
        /// the external command.</param>
        /// <param name="elements">A set of elements to which the external application 
        /// can add elements that are to be highlighted in case of failure or cancellation.</param>
        /// <returns>Return the status of the external command. 
        /// A result of Succeeded means that the API external method functioned as expected. 
        /// Cancelled can be used to signify that the user cancelled the external operation 
        /// at some point. Failure should be returned if the application is unable to proceed with 
        /// the operation.</returns>
        public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
        {
            m_app = revit.Application.Application;
            m_doc = revit.Application.ActiveUIDocument.Document;

            Transaction trans = new Transaction(m_doc, "Revit.SDK.Samples.MeasureHeight");
            trans.Start();
            // Find a 3D view to use for the ray tracing operation
            FilteredElementCollector collector = new FilteredElementCollector(m_doc);
            Func<View3D, bool> isNotTemplate = v3 => !(v3.IsTemplate);
            m_view3D = collector.OfClass(typeof(View3D)).Cast<View3D>().First<View3D>(isNotTemplate);

            Autodesk.Revit.UI.Selection.Selection selection = revit.Application.ActiveUIDocument.Selection;

            // If skylight is selected, process it.
            m_skylight = null;
            if (selection.GetElementIds().Count == 1)
            {
                foreach (ElementId eId in selection.GetElementIds())
                {
                   Element e = revit.Application.ActiveUIDocument.Document.GetElement(eId);
                    if (e is FamilyInstance)
                    {
                        FamilyInstance instance = e as FamilyInstance;
                        bool isWindow = (instance.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Windows);
                        bool isHostedByRoof = (instance.Host.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Roofs);

                        if (isWindow && isHostedByRoof)
                            m_skylight = instance;
                    }
                }
            }

            if (m_skylight == null)
            {
                message = "This tool requires exactly one skylight to be selected.";
                trans.RollBack();
                return Result.Cancelled;
            }

            // Find the floor to use for the measurement (hardcoded)
            ElementId id = new ElementId(150314);
            m_floor = m_doc.GetElement(id) as Floor;

            // Calculate the height
            Line line = CalculateLineAboveFloor();

            // Create a model curve to show the distance
            Plane plane = Plane.CreateByNormalAndOrigin(new XYZ(1, 0, 0), line.GetEndPoint(0));
            SketchPlane sketchPlane = SketchPlane.Create(m_doc, plane);

            ModelCurve curve = m_doc.Create.NewModelCurve(line, sketchPlane);

            // Show a message with the length value
            TaskDialog.Show("Distance", "Distance to floor: " + String.Format("{0:f2}", line.Length));

            trans.Commit();
            return Result.Succeeded;
        }
        #endregion

        #region Class Implementations
        /// <summary>
        /// Determines the line segment that connects the skylight to the already obtained floor.
        /// </summary>
        /// <returns>The line segment.</returns>
        private Line CalculateLineAboveFloor()
        {
            // Use the center of the skylight bounding box as the start point.
            BoundingBoxXYZ box = m_skylight.get_BoundingBox(m_view3D);
            XYZ center = box.Min.Add(box.Max).Multiply(0.5);

            // Project in the negative Z direction down to the floor.
            XYZ rayDirection = new XYZ(0, 0, -1);

            // Look for references to faces where the element is the floor element id.
            ReferenceIntersector referenceIntersector = new ReferenceIntersector(m_floor.Id, FindReferenceTarget.Face, m_view3D);
            IList<ReferenceWithContext> references = referenceIntersector.Find(center, rayDirection);

            double distance = Double.PositiveInfinity;
            XYZ intersection = null;
            foreach (ReferenceWithContext referenceWithContext in references)
            {
                Reference reference = referenceWithContext.GetReference();
                // Keep the closest matching reference (using the proximity parameter to determine closeness).
                double proximity = referenceWithContext.Proximity;
                if (proximity < distance)
                {
                    distance = proximity;
                    intersection = reference.GlobalPoint;
                }
            }

            // Create line segment from the start point and intersection point.
            Line result = Line.CreateBound(center, intersection);
            return result;
        }
        #endregion
    }
}