应用程序:FamilyParametersOrder

Revit 平台:所有版本

Revit 版本:2015.0

首次发布版本:2015.0

编程语言:C#

技能级别:中等

类别:参数,族

类型:外部命令

主题:对家族参数的顺序进行排序。

摘要:本示例演示如何对家族参数的顺序进行排序。该家族可以是磁盘文件或已加载到项目中。

相关类:

Autodesk.Revit.DB

Autodesk.Revit.UI.IExternalCommand;

Autodesk.Revit.UI.IExternalApplication;

Autodesk.Revit.DB.FilteredElementCollector;

Autodesk.Revit.DB.Family;

Autodesk.Revit.DB.FamilyManager;

Autodesk.Revit.DB.IFamilyLoadOptions;

Autodesk.Revit.DB.FamilySource;

Autodesk.Revit.DB.Document;

Autodesk.Revit.ApplicationServices.Application;

项目文件:

Command.cs

该文件包含类 Command,该类继承自 IExternalCommand 接口并实现 Execute 方法。

 

ExternalApplication.cs

该文件包含类 ExternalApplication,该类继承自 IExternalApplication 接口并将打开的文档事件绑定到弹出对话框。

 

SortFamilyFilesParamsForm.cs

该文件包含类 SortFamilyFilesParamsForm,该类将弹出对话框以对位于指定文件夹中的家族文件的参数进行排序(不包括子文件夹)。

 

SortLoadedFamiliesParamsForm.cs

该文件包含类 SortLoadedFamiliesParamsForm,该类将弹出对话框以对加载到文档中的家族的参数进行排序。

描述:

该示例提供以下功能。

- 对位于文件夹中的家族文件的批量排序参数顺序。

- 对加载到项目或家族中的参数进行排序。

使用说明:

打开 Revit 应用程序并执行命令。

1. 对位于文件夹中的家族文件的批量排序参数顺序。

a. 执行命令。

b. 通过“浏览”按钮选择包含某些家族文件的文件夹,或输入包含某些家族文件的文件夹(绝对或相对)

c. A->Z”按钮将更新位于特定文件夹中每个家族文件的参数以字母顺序排列。

d. Z->A”按钮将更新位于特定文件夹中每个家族文件的参数以相反的字母顺序排列。

e. “关闭”按钮将关闭整个对话框。

 

预期结果:家族文件中的参数将按字母顺序或相反的字母顺序排序。

 

2. 对加载到项目或家族中的参数进行排序。

a. 打开一个 revit 文档,在文档打开后将会弹出一个对话框。

b. A->Z”按钮将更新每个加载的家族的参数以字母顺序排列,然后重新加载家族到项目中。

c. Z->A”按钮将更新每个加载的家族的参数以相反的字母顺序排列,然后重新加载家族到项目中。

d. “关闭”按钮将关闭整个对话框。

 

预期结果:在已打开的文档中加载和可编辑的家族中的参数将按字母顺序或相反的字母顺序排序。

源代码:

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

ExternalApplication.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.Linq;
using System.Text;

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

using Autodesk.Revit.DB.Events;

namespace Revit.SDK.Samples.FamilyParametersOrder.CS
{
   /// <summary>
   /// A class inherits IExternalApplication interface and provide an entry of the sample.
   /// This class controls other function class and plays the bridge role in this sample.
   /// It create a custom menu "Track Revit Events" of which the corresponding 
   /// external command is the command in this project.
   /// </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 ExternalApplication : IExternalApplication
   {
      #region IExternalApplication Members

      /// <summary>
      /// Implement this method to implement the external application which should be called when 
      /// Revit starts before a file or default template is actually loaded.
      /// </summary>
      /// <param name="application">An object that is passed to the external application 
      /// which contains the controlled application.</param> 
      /// <returns>Return the status of the external application. 
      /// A result of Succeeded means that the external application successfully started. 
      /// Cancelled can be used to signify that the user cancelled the external operation at 
      /// some point.
      /// If false is returned then Revit should inform the user that the external application 
      /// failed to load and the release the internal reference.</returns>
      public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
      {
         try
         {
            application.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(SortLoadedFamiliesParams);
         }
         catch (Exception)
         {
            return Autodesk.Revit.UI.Result.Failed;
         }

         return Autodesk.Revit.UI.Result.Succeeded;
      }

      /// <summary>
      /// Implement this method to implement the external application which should be called when 
      /// Revit is about to exit,Any documents must have been closed before this method is called.
      /// </summary>
      /// <param name="application">An object that is passed to the external application 
      /// which contains the controlled application.</param>
      /// <returns>Return the status of the external application. 
      /// A result of Succeeded means that the external application successfully shutdown. 
      /// Cancelled can be used to signify that the user cancelled the external operation at 
      /// some point.
      /// If false is returned then the Revit user should be warned of the failure of the external 
      /// application to shut down correctly.</returns>
      public Autodesk.Revit.UI.Result OnShutdown(UIControlledApplication application)
      {
         return Autodesk.Revit.UI.Result.Succeeded;
      }
      #endregion

      #region Class Methods
      /// <summary>
      /// Generic event handler can be subscribed to any events.
      /// It will dump events information(sender and EventArgs) to log window and log file
      /// </summary>
      /// <param name="obj"></param>
      /// <param name="args"></param>
      public void SortLoadedFamiliesParams(Object obj, DocumentOpenedEventArgs args)
      {
         if (!Command.m_SortDialogIsOpened)
            return;

         using (SortLoadedFamiliesParamsForm sortForm = new SortLoadedFamiliesParamsForm(args.Document))
         {
            sortForm.ShowDialog();
         }
      }
      #endregion
   }
}

SortFamilyFilesParamsForm.cs

namespace Revit.SDK.Samples.FamilyParametersOrder.CS
{
   partial class SortFamilyFilesParamsForm
   {
      /// <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.label1 = new System.Windows.Forms.Label();
         this.directoryTxt = new System.Windows.Forms.TextBox();
         this.browseBtn = new System.Windows.Forms.Button();
         this.Z_ABtn = new System.Windows.Forms.Button();
         this.closeBtn = new System.Windows.Forms.Button();
         this.A_ZBtn = new System.Windows.Forms.Button();
         this.groupBox1 = new System.Windows.Forms.GroupBox();
         this.groupBox2 = new System.Windows.Forms.GroupBox();
         this.groupBox1.SuspendLayout();
         this.groupBox2.SuspendLayout();
         this.SuspendLayout();
         // 
         // label1
         // 
         this.label1.AutoSize = true;
         this.label1.Location = new System.Drawing.Point(18, 30);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(49, 13);
         this.label1.TabIndex = 0;
         this.label1.Text = "Directory";
         // 
         // directoryTxt
         // 
         this.directoryTxt.Location = new System.Drawing.Point(75, 26);
         this.directoryTxt.Name = "directoryTxt";
         this.directoryTxt.Size = new System.Drawing.Size(214, 20);
         this.directoryTxt.TabIndex = 1;
         // 
         // browseBtn
         // 
         this.browseBtn.Location = new System.Drawing.Point(295, 25);
         this.browseBtn.Name = "browseBtn";
         this.browseBtn.Size = new System.Drawing.Size(75, 23);
         this.browseBtn.TabIndex = 2;
         this.browseBtn.Text = "&Browse";
         this.browseBtn.UseVisualStyleBackColor = true;
         this.browseBtn.Click += new System.EventHandler(this.browseBtn_Click);
         // 
         // Z_ABtn
         // 
         this.Z_ABtn.Location = new System.Drawing.Point(158, 36);
         this.Z_ABtn.Name = "Z_ABtn";
         this.Z_ABtn.Size = new System.Drawing.Size(75, 23);
         this.Z_ABtn.TabIndex = 2;
         this.Z_ABtn.Text = "&Z-->A";
         this.Z_ABtn.UseVisualStyleBackColor = true;
         this.Z_ABtn.Click += new System.EventHandler(this.Z_ABtn_Click);
         // 
         // closeBtn
         // 
         this.closeBtn.Location = new System.Drawing.Point(295, 36);
         this.closeBtn.Name = "closeBtn";
         this.closeBtn.Size = new System.Drawing.Size(75, 23);
         this.closeBtn.TabIndex = 2;
         this.closeBtn.Text = "&Close";
         this.closeBtn.UseVisualStyleBackColor = true;
         this.closeBtn.Click += new System.EventHandler(this.closeBtn_Click);
         // 
         // A_ZBtn
         // 
         this.A_ZBtn.Location = new System.Drawing.Point(21, 36);
         this.A_ZBtn.Name = "A_ZBtn";
         this.A_ZBtn.Size = new System.Drawing.Size(75, 23);
         this.A_ZBtn.TabIndex = 3;
         this.A_ZBtn.Text = "&A-->Z";
         this.A_ZBtn.UseVisualStyleBackColor = true;
         this.A_ZBtn.Click += new System.EventHandler(this.A_ZBtn_Click);
         // 
         // groupBox1
         // 
         this.groupBox1.Controls.Add(this.label1);
         this.groupBox1.Controls.Add(this.browseBtn);
         this.groupBox1.Controls.Add(this.directoryTxt);
         this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
         this.groupBox1.Location = new System.Drawing.Point(0, 0);
         this.groupBox1.Name = "groupBox1";
         this.groupBox1.Size = new System.Drawing.Size(403, 72);
         this.groupBox1.TabIndex = 4;
         this.groupBox1.TabStop = false;
         this.groupBox1.Text = "Family Files Path";
         // 
         // groupBox2
         // 
         this.groupBox2.Controls.Add(this.A_ZBtn);
         this.groupBox2.Controls.Add(this.Z_ABtn);
         this.groupBox2.Controls.Add(this.closeBtn);
         this.groupBox2.Dock = System.Windows.Forms.DockStyle.Bottom;
         this.groupBox2.Location = new System.Drawing.Point(0, 66);
         this.groupBox2.Name = "groupBox2";
         this.groupBox2.Size = new System.Drawing.Size(403, 87);
         this.groupBox2.TabIndex = 5;
         this.groupBox2.TabStop = false;
         this.groupBox2.Text = "Parameters Order";
         // 
         // SortFamilyFilesParamsForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(403, 153);
         this.Controls.Add(this.groupBox2);
         this.Controls.Add(this.groupBox1);
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "SortFamilyFilesParamsForm";
         this.ShowIcon = false;
         this.Text = "Sort Family Files Parameters";
         this.groupBox1.ResumeLayout(false);
         this.groupBox1.PerformLayout();
         this.groupBox2.ResumeLayout(false);
         this.ResumeLayout(false);

      }

      #endregion

      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.TextBox directoryTxt;
      private System.Windows.Forms.Button browseBtn;
      private System.Windows.Forms.Button Z_ABtn;
      private System.Windows.Forms.Button closeBtn;
      private System.Windows.Forms.Button A_ZBtn;
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.GroupBox groupBox2;
   }
}

SortLoadedFamiliesParamsForm.cs

namespace Revit.SDK.Samples.FamilyParametersOrder.CS
{
   partial class SortLoadedFamiliesParamsForm
   {
      /// <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.A_ZBtn = new System.Windows.Forms.Button();
         this.CloseBtn = new System.Windows.Forms.Button();
         this.Z_ABtn = new System.Windows.Forms.Button();
         this.groupBox1 = new System.Windows.Forms.GroupBox();
         this.groupBox1.SuspendLayout();
         this.SuspendLayout();
         // 
         // A_ZBtn
         // 
         this.A_ZBtn.Location = new System.Drawing.Point(21, 29);
         this.A_ZBtn.Name = "A_ZBtn";
         this.A_ZBtn.Size = new System.Drawing.Size(75, 23);
         this.A_ZBtn.TabIndex = 6;
         this.A_ZBtn.Text = "&A-->Z";
         this.A_ZBtn.UseVisualStyleBackColor = true;
         this.A_ZBtn.Click += new System.EventHandler(this.A_ZBtn_Click);
         // 
         // CloseBtn
         // 
         this.CloseBtn.Location = new System.Drawing.Point(279, 29);
         this.CloseBtn.Name = "CloseBtn";
         this.CloseBtn.Size = new System.Drawing.Size(75, 23);
         this.CloseBtn.TabIndex = 4;
         this.CloseBtn.Text = "&Close";
         this.CloseBtn.UseVisualStyleBackColor = true;
         this.CloseBtn.Click += new System.EventHandler(this.closeBtn_Click);
         // 
         // Z_ABtn
         // 
         this.Z_ABtn.Location = new System.Drawing.Point(150, 29);
         this.Z_ABtn.Name = "Z_ABtn";
         this.Z_ABtn.Size = new System.Drawing.Size(75, 23);
         this.Z_ABtn.TabIndex = 5;
         this.Z_ABtn.Text = "&Z-->A";
         this.Z_ABtn.UseVisualStyleBackColor = true;
         this.Z_ABtn.Click += new System.EventHandler(this.Z_ABtn_Click);
         // 
         // groupBox1
         // 
         this.groupBox1.Controls.Add(this.A_ZBtn);
         this.groupBox1.Controls.Add(this.Z_ABtn);
         this.groupBox1.Controls.Add(this.CloseBtn);
         this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
         this.groupBox1.Location = new System.Drawing.Point(0, 0);
         this.groupBox1.Name = "groupBox1";
         this.groupBox1.Size = new System.Drawing.Size(372, 75);
         this.groupBox1.TabIndex = 7;
         this.groupBox1.TabStop = false;
         this.groupBox1.Text = "Parameters Order";
         // 
         // SortLoadedFamiliesParamsForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(372, 75);
         this.Controls.Add(this.groupBox1);
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "SortLoadedFamiliesParamsForm";
         this.ShowIcon = false;
         this.Text = "Sort Loaded Families Parameters";
         this.groupBox1.ResumeLayout(false);
         this.ResumeLayout(false);

      }

      #endregion

      private System.Windows.Forms.Button A_ZBtn;
      private System.Windows.Forms.Button CloseBtn;
      private System.Windows.Forms.Button Z_ABtn;
      private System.Windows.Forms.GroupBox groupBox1;
   }
}