应用程序:Custom2DExporter

Revit平台:所有版本

Revit版本:2019.0

首次发布时间:2019.0

编程语言:C#

技能水平:中级

类别:基础知识,几何,视图

类型:外部命令

主题:使用CustomExporter导出2D视图

摘要:使用CustomExporter导出2D视图,使用导出器的不同选项。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.CustomExporter

Autodesk.Revit.DB.IExportContext2D

项目文件:

Command.cs

包含了类 Command,该类继承自接口 IExternalCommand 并实现了 Execute 方法。执行将显示在 Export2DForm.cs 中定义的对话框,您可以在其中选择导出器选项并导出当前视图。

Export2DForm.Designer.cs / Export2DForm.cs / Export2DForm.resx

定义用于显示导出选项的对话框。

TessellatedGeomAndText2DExportContext.cs

实现了用于 2D 导出的 IExportContext2D。这种特定的实现将 2D 曲线导出为细分为线段,还进行了一些基本的文本导出。

描述:

此示例允许用户从 Revit 模型的 2D 视图中导出和可视化模型和注释几何。

备注:

•        导出时使用当前视图。

•        所有导出的几何都被细分为线段。

•        然后通过隐藏视图中的所有元素并使用详细线来显示所有导出的线来可视化导出的几何形状。

•        要查看原始的未导出的 2D 视图,请撤消一次以删除绘制的导出。

此示例还证明了可以导出文本。最少量的文本被导出,并用新行分隔。然而,OnText 中有所有关于文本位置、字体、大小和格式的必要信息,所以 API 用户可以完整重构它。

一些重要的细节:

•        只有可见几何被导出。

•        不支持网格。

已知问题:

•        有些注释不会自动导出到此示例中。已知的例子有:剖面、参考平面、作用域框和计划区域。建议编写额外的代码,在 OnElementBegin 中检测这些元素并编写自定义代码来导出任何必要的几何。

说明:

打开 Revit 应用程序并执行以下操作:

1.导出模型几何。

a. 创建一个包含模型和注释元素的 Revit 模型。

b. 在平面图、立面图或剖面图上执行“使用 CustomExporter 导出 2D 视图”命令。在执行之前,在对话框中不要选择任何额外选项(注释或图案)。

预期结果:视图中所有元素都被隐藏,代替它们的是使用详细线绘制的模型几何形状;注释被隐藏。任何非线形模型曲线都会被细分为线段。关闭结果对话框,撤消一次以返回命令前的模型状态。

 

2.导出模型和注释。

a. 创建一个包含模型和注释元素的 Revit 模型。

b. 在平面图、立面图或剖面图上执行“使用 CustomExporter 导出 2D 视图”命令。在执行之前,选择“导出注释和文本”。

预期结果:视图中所有元素都被隐藏,代替它们的是使用详细线绘制的模型和注释几何形状。任何非线形模型或注释曲线都会被细分为线段。在结果对话框中,您可以获得在视图中找到的所有文本的转储。关闭结果对话框,撤消一次以返回命令前的模型状态。

 

3.导出图案线条。

a. 如果您的模型包含在其面上应用了图案的元素,请选择“导出图案线条”以导出并在屏幕上显示那些图案。

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

Command.cs

//
// (C) Copyright 2003-2016 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.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

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


namespace Revit.SDK.Samples.Custom2DExporter.CS
{
   /// <summary>
   /// Implements the Revit add-in interface IExternalCommand
   /// </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 ExportViewUtils
      public static ICollection<ViewType> GetExportableViewTypes()
      {
         return new ViewType[]
         {
            ViewType.FloorPlan,
            ViewType.CeilingPlan,
            ViewType.Section,
            ViewType.Elevation,
            ViewType.Detail,
            ViewType.AreaPlan,
            ViewType.EngineeringPlan
         };
      }

      bool isExportableView(Autodesk.Revit.DB.View view)
      {
         if (!view.CanBePrinted || view.IsTemplate)
            return false;

         ICollection<ViewType> exportableTypes = GetExportableViewTypes();
         if (!exportableTypes.Contains(view.ViewType))
            return false;

         return true;
      }

      private static void ExportView(Autodesk.Revit.DB.View exportableView,
                                     DisplayStyle displayStyle,
                                     bool includeGeometricObjects,
                                     bool export2DIncludingAnnotationObjects,
                                     bool export2DGeometricObjectsIncludingPatternLines,
                                     out IList<XYZ> points,
                                     out ResultsSummary resultsSummary)
      {
         TessellatedGeomAndText2DExportContext context = new TessellatedGeomAndText2DExportContext(out points);
         CustomExporter exporter = new CustomExporter(exportableView.Document, context);
         exporter.IncludeGeometricObjects = includeGeometricObjects;
         exporter.Export2DIncludingAnnotationObjects = export2DIncludingAnnotationObjects;
         exporter.Export2DGeometricObjectsIncludingPatternLines = export2DGeometricObjectsIncludingPatternLines;
         exporter.ShouldStopOnError = true;
         exporter.Export(exportableView);
         exporter.Dispose();

         resultsSummary = new ResultsSummary();
         resultsSummary.numElements = context.NumElements;
         resultsSummary.numTexts = context.NumTexts;
         resultsSummary.texts = context.Texts;
      }
      #endregion

      #region ResultsUtils
      /// <summary>
      /// Class that aggregates the results of the export.
      /// </summary>
      class ResultsSummary
      {
         public int numElements { get; set; }
         public int numTexts { get; set; }
         public string texts { get; set; }

         public ResultsSummary()
         {
         }
      }

      /// <summary>
      /// Displays the results from a run of path of travel creation using a TaskDialog.
      /// </summary>
      /// <param name="resultsSummary"></param>
      private static void ShowResults(ResultsSummary resultsSummary)
      {
         TaskDialog td = new TaskDialog("Results of 2D export");
         td.MainInstruction = String.Format("2D exporter exported {0} elements", resultsSummary.numElements);
         String details = String.Format("There were {0} text nodes exported.\n\n",
                                         resultsSummary.numTexts);

         if (resultsSummary.numTexts > 0 && resultsSummary.texts.Length > 0)
            details += "Exported text nodes:\n" + resultsSummary.texts;

         td.MainContent = details;

         td.Show();


      }
      #endregion

      /// <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 virtual Result Execute(ExternalCommandData commandData
          , ref string message, ElementSet elements)
      {
         try
         {
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Autodesk.Revit.DB.View activeView = uiDoc.ActiveView;
            if (!isExportableView(activeView))
            {
               TaskDialog td = new TaskDialog("Cannot export view.");
               td.MainInstruction = String.Format("Only plans, elevations and sections can be exported.");

               td.Show();

               return Result.Succeeded;
            }

            using (Export2DView exportForm = new Export2DView())
            {
               if (DialogResult.OK == exportForm.ShowDialog())
               {
                  IList<XYZ> points = null;
                  ResultsSummary resSummary = null;
                  ExportView(activeView,
                             activeView.DisplayStyle /*display with current display style*/,
                             true /* always export some geometry */,
                             exportForm.ViewExportOptions.ExportAnnotationObjects,
                             exportForm.ViewExportOptions.ExportPatternLines,
                             out points,
                             out resSummary);

                  Utilities.displayExport(activeView, points);

                  ShowResults(resSummary);
               }
            }            

            return Result.Succeeded;
         }
         catch (Exception ex)
         {
            message = ex.Message;
            return Result.Failed;
         }
      }
   }
}

TessellatedGeomAndText2DExportContext.cs

namespace Revit.SDK.Samples.Custom2DExporter.CS
{
    partial class Export2DView
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
         this.checkBox2 = new System.Windows.Forms.CheckBox();
         this.checkBox3 = new System.Windows.Forms.CheckBox();
         this.optionGroupBox = new System.Windows.Forms.GroupBox();
         this.textBox1 = new System.Windows.Forms.TextBox();
         this.buttonCancel = new System.Windows.Forms.Button();
         this.buttonOK = new System.Windows.Forms.Button();
         this.textBox2 = new System.Windows.Forms.TextBox();
         this.optionGroupBox.SuspendLayout();
         this.SuspendLayout();
         // 
         // checkBox2
         // 
         this.checkBox2.AutoSize = true;
         this.checkBox2.Location = new System.Drawing.Point(17, 58);
         this.checkBox2.Name = "checkBox2";
         this.checkBox2.Size = new System.Drawing.Size(196, 17);
         this.checkBox2.TabIndex = 1;
         this.checkBox2.Text = "Export annotation geometry and text";
         this.checkBox2.UseVisualStyleBackColor = true;
         this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
         // 
         // checkBox3
         // 
         this.checkBox3.AutoSize = true;
         this.checkBox3.Location = new System.Drawing.Point(17, 82);
         this.checkBox3.Name = "checkBox3";
         this.checkBox3.Size = new System.Drawing.Size(224, 17);
         this.checkBox3.TabIndex = 2;
         this.checkBox3.Text = "Export patterns (non-wireframe views only)";
         this.checkBox3.UseVisualStyleBackColor = true;
         this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged);
         // 
         // optionGroupBox
         // 
         this.optionGroupBox.Controls.Add(this.textBox1);
         this.optionGroupBox.Controls.Add(this.checkBox3);
         this.optionGroupBox.Controls.Add(this.checkBox2);
         this.optionGroupBox.Location = new System.Drawing.Point(24, 44);
         this.optionGroupBox.Name = "optionGroupBox";
         this.optionGroupBox.Size = new System.Drawing.Size(313, 100);
         this.optionGroupBox.TabIndex = 3;
         this.optionGroupBox.TabStop = false;
         this.optionGroupBox.Text = "Export Options";
         // 
         // textBox1
         // 
         this.textBox1.BackColor = System.Drawing.SystemColors.Menu;
         this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
         this.textBox1.Location = new System.Drawing.Point(17, 32);
         this.textBox1.Name = "textBox1";
         this.textBox1.Size = new System.Drawing.Size(262, 13);
         this.textBox1.TabIndex = 7;
         this.textBox1.Text = "Model objects are always exported, but you can also:";
         // 
         // buttonCancel
         // 
         this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
         this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
         this.buttonCancel.Location = new System.Drawing.Point(262, 166);
         this.buttonCancel.Name = "buttonCancel";
         this.buttonCancel.Size = new System.Drawing.Size(75, 23);
         this.buttonCancel.TabIndex = 6;
         this.buttonCancel.Text = "&Cancel";
         this.buttonCancel.UseVisualStyleBackColor = true;
         // 
         // buttonOK
         // 
         this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
         this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
         this.buttonOK.Location = new System.Drawing.Point(181, 166);
         this.buttonOK.Name = "buttonOK";
         this.buttonOK.Size = new System.Drawing.Size(75, 23);
         this.buttonOK.TabIndex = 5;
         this.buttonOK.Text = "&OK";
         this.buttonOK.UseVisualStyleBackColor = true;
         // 
         // textBox2
         // 
         this.textBox2.BackColor = System.Drawing.SystemColors.Menu;
         this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
         this.textBox2.Location = new System.Drawing.Point(30, 12);
         this.textBox2.Multiline = true;
         this.textBox2.Name = "textBox2";
         this.textBox2.Size = new System.Drawing.Size(307, 26);
         this.textBox2.TabIndex = 8;
         this.textBox2.Text = "Exports lines from the 2D view and draws them on the screen";
         // 
         // Export2DView
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(374, 210);
         this.Controls.Add(this.textBox2);
         this.Controls.Add(this.buttonCancel);
         this.Controls.Add(this.buttonOK);
         this.Controls.Add(this.optionGroupBox);
         this.Name = "Export2DView";
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Export 2D View";
         this.optionGroupBox.ResumeLayout(false);
         this.optionGroupBox.PerformLayout();
         this.ResumeLayout(false);
         this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.CheckBox checkBox2;
        private System.Windows.Forms.CheckBox checkBox3;
        private System.Windows.Forms.GroupBox optionGroupBox;
        private System.Windows.Forms.Button buttonCancel;
        private System.Windows.Forms.Button buttonOK;
      private System.Windows.Forms.TextBox textBox1;
      private System.Windows.Forms.TextBox textBox2;
   }
}