应用程序:RayTraceBounce

Revit平台:所有

Revit版本:2011.0

首次发布于:2010.0

编程语言:C#

技能水平:中等

类别:几何

类型:ExternalCommand

主题:射线追踪反弹

摘要

这个示例展示了如何通过Revit API方法FindReferencesWithContextByDirection,找到射线与面之间的交点并创建连接线。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.Document

Autodesk.Revit.DB.ElementSet

Autodesk.Revit.DB.GeometryObject

项目文件:

Command.cs

这个文件包含了从IExternalCommand继承的Command类。该类实现了Execute方法,用于显示RayTraceBounceForm

RayTraceBounceForm.cs

该窗体允许输入模型内的坐标位置(XYZ)和坐标方向(ijk)。

描述:

该示例主要使用FindReferencesWithContextByDirection方法来实现以下功能:

1. 从指定的位置按指定的方向启动射线,找到与面的第一个交点。

2. 计算从面反射的射线角度,并启动另一个射线以找到下一个交点。

3. 对于每个射线/交点,创建一个连接两个点的模型线。最终结果应该是从物品到物品反弹的一系列模型线。

4. 提供一个硬性限制,比如,100个交点,以防止在封闭空间内无限反射。

5. 编写记录文件,包含相交面的元素类型、ID和材料。

说明:

1. 打开文档bounce.rvt,该文档应该具有3D视图和文档中创建的线样式“bounce”。

2. 运行该外部命令以显示对话框。

3. 输入适当的起点和方向,然后单击“确定”按钮,将创建线。

源代码:

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

RayTraceBounce.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.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Xml;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.RayTraceBounce.CS
{
   /// <summary>
   /// A class inherits IExternalCommand interface.
   /// This class shows how to find intersection between ray and face and create  
   /// connecting lines by Revit API method FindReferencesByDirection.
   /// </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 Memeber Variables
      /// <summary>
      /// revit application
      /// </summary>
      Autodesk.Revit.UI.UIApplication m_app;
      /// <summary>
      /// a 3D View
      /// </summary>
      Autodesk.Revit.DB.View3D m_view = null;
      #endregion
      #region Class Interface Implementation
      /// <summary>
      /// Implement this method as an external command for Revit.
      /// </summary>
      /// <param name="commandData">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 Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
      {
         try
         {
            // should have a line style "bounce" created in the document before running this
            m_app = commandData.Application;
            Get3DView();
            if (m_view == null)
            {
               TaskDialog.Show("Revit", "A default 3D view (named {3D}) must exist before running this command");
               return Autodesk.Revit.UI.Result.Cancelled;
            }
            else
            {
               RayTraceBounceForm form = new RayTraceBounceForm(commandData, m_view);
               form.ShowDialog();
            }
            return Autodesk.Revit.UI.Result.Succeeded;
         }
         catch (Exception e)
         {
            message = e.ToString();
            return Autodesk.Revit.UI.Result.Failed;
         }
      }
      #endregion
      #region Class Implementation
      /// <summary>
      /// Get a 3D view from active document
      /// </summary>
      public void Get3DView()
      {
         List<Autodesk.Revit.DB.Element> list = new List<Autodesk.Revit.DB.Element>();
         FilteredElementCollector collector = new FilteredElementCollector(m_app.ActiveUIDocument.Document);
         list.AddRange(collector.OfClass(typeof(View3D)).ToElements());
         foreach (Autodesk.Revit.DB.View3D v in list)
         {
             // skip view template here because view templates are invisible in project browsers
             if (v != null && !v.IsTemplate && v.Name == "{3D}")
            {
               m_view = v as Autodesk.Revit.DB.View3D;
               break;
            }
         }
      }
      #endregion
   }
}