应用程序:AnalyticalSupportData_Info

 Revit平台:Structure

 Revit版本:2011.0

首次发布用于:9.0

编程语言:C#

技能等级:中等

类别:结构

类型:外部命令

主题:显示元素的分析支持信息。

摘要

此示例显示元素支持的信息(包括元素id、元素类型和元素支持的类型)。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.Structure.AnalyticalModel

Autodesk.Revit.DB.Structure.AnalyticalModelSupport

Autodesk.Revit.DB.Structure.AnalyticalSupportType

项目文件:

AnalyticalSupportDataInfo.cs

它是此示例中最重要的文件。它包括一个继承IExternalCommand接口的类。元素id、类型信息和支持的信息可以在这个类中存储和访问。

AnalyticalSupportDataInfoForm.cs

此文件包含AnalyticalSupportData_InfoForm类。这个类有一个网格,向用户显示元素id、类型信息和支持的信息。

功能:

-如果支持所选图元,则报告其符号类型名称(例如,UB通用梁:254×102×28UB)、图元id和其他图元提供的支撑类型(点/线/表面)。

-“支持”标志表示元素是否完全受支持。分析支持信息中的支持类型可跟踪所有支持。

-使用Element.Id属性获取元素Id。

-通过Element.ObjectType属性获取其类型信息。

-通过分析模型获取结构元素的分析模型支持信息。GetAnalyticalModelSupports方法。检查IsElementFullySupported方法是否支持该元素。InfoArray属性提供了支持信息数组。

-以下结构图元具有分析模型:柱、梁、板、支撑、墙和某些类型的基础。

实施:

1.确保Revit文件是使用结构模板创建的。

2.确保选中“结构设置”对话框的“分析模型”选项卡上的“构件支撑”单选按钮。

3.绘制一些有无支撑的结构构件。

[提示]示例文件夹中有一个示例项目文件AnalyticalSupportData_Info.rvt。

4.选择一些结构构件,包括梁、墙等。

5.运行此示例。

6.预期结果:将出现一个带有网格的表单,向用户显示所选元素的id、类型信息和支持的信息。

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

AnalyticalSupportDataInfo.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.Data;
using System.Collections.Generic;
using System.Text;

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

namespace Revit.SDK.Samples.AnalyticalSupportData_Info.CS
{
    /// <summary>
    /// get element's id and type information and its supported information.
    /// </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
    {

        ExternalCommandData m_revit    = null;  // application of Revit
        DataTable m_elementInformation = null;  // store all required information

        /// <summary>
        /// property to get private member variable m_elementInformation.
        /// </summary>
        public DataTable ElementInformation
        {
            get
            {
                return m_elementInformation;
            }
        }

        /// <summary>
        /// Implement this method as an external command for Revit.
        /// </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 Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData revit,
                                                              ref string message,
                                                              ElementSet elements)
        {
            // Set currently executable application to private variable m_revit
            m_revit = revit;

            ElementSet selectedElements = new ElementSet();
            foreach (ElementId elementId in m_revit.Application.ActiveUIDocument.Selection.GetElementIds())
            {
               selectedElements.Insert(m_revit.Application.ActiveUIDocument.Document.GetElement(elementId));
            }

            // get all the required information of selected elements and store them in a data table.
            m_elementInformation = StoreInformationInDataTable(selectedElements);

            // show UI
            AnalyticalSupportData_InfoForm displayForm = new AnalyticalSupportData_InfoForm(this);
            displayForm.ShowDialog();

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

        /// <summary>
        /// get all the required information of selected elements and store them in a data table
        /// </summary>
        /// <param name="selectedElements">
        /// all selected elements in Revit main program
        /// </param>
        /// <returns>
        /// a data table which store all the required information
        /// </returns>
        private DataTable StoreInformationInDataTable(ElementSet selectedElements)
        {
            DataTable informationTable = CreatDataTable();
            
            foreach (Element element in selectedElements)
            {
                // Get  
                AnalyticalModel analyticalModel = element.GetAnalyticalModel();
                if (null == analyticalModel) // skip no AnalyticalModel element
                {
                    continue;
                }

                DataRow newRow              = informationTable.NewRow(); 
                string idValue              = element.Id.IntegerValue.ToString();// store element Id value             
                string typeName             = "";                      // store element type name
                string[] supportInformation = GetSupportInformation(analyticalModel);// store support information
   
                // get element type information
                switch (element.GetType().Name)
                {
                case "WallFoundation":
                    WallFoundation wallFound = element as WallFoundation;  
                    ElementType wallFootSymbol =m_revit.Application.ActiveUIDocument.Document.GetElement(wallFound.GetTypeId()) as ElementType;// get element Type
                    typeName              = wallFootSymbol.Category.Name + ": " + wallFootSymbol.Name;
                    break;

                case "FamilyInstance":
                    FamilyInstance familyInstance = element as FamilyInstance;
                    FamilySymbol symbol = m_revit.Application.ActiveUIDocument.Document.GetElement(familyInstance.GetTypeId()) as FamilySymbol;
                    typeName                      = symbol.Family.Name + ": " + symbol.Name;
                    break;

                case "Floor":
                    Floor slab         = element as Floor;                    
                    FloorType slabType = m_revit.Application.ActiveUIDocument.Document.GetElement(slab.GetTypeId()) as FloorType; // get element type
                    typeName           = slabType.Category.Name + ": " + slabType.Name;
                    break;

                case "Wall":
                    Wall wall         = element as Wall;  
                    WallType wallType = m_revit.Application.ActiveUIDocument.Document.GetElement(wall.GetTypeId()) as WallType; // get element type
                    typeName          = wallType.Kind.ToString() + ": " + wallType.Name;
                    break;

                default:
                    break;
                }

                // set the relative information of current element into the table.
                newRow["Id"] = idValue;
                newRow["Element Type"] = typeName;
                newRow["Support Type"] = supportInformation[0];
                newRow["Remark"] = supportInformation[1];
                informationTable.Rows.Add(newRow);
            }

            return informationTable;
        }

        /// <summary>
        /// create a empty DataTable
        /// </summary>
        /// <returns></returns>
        private DataTable CreatDataTable()
        {
            // Create a new DataTable.
            DataTable elementInformationTable = new DataTable("ElementInformationTable");

            // Create element id column and add to the DataTable.
            DataColumn idColumn = new DataColumn();
            idColumn.DataType   = typeof(System.String);
            idColumn.ColumnName = "Id";
            idColumn.Caption    = "Id";
            idColumn.ReadOnly   = true;
            elementInformationTable.Columns.Add(idColumn);

            // Create element type column and add to the DataTable.
            DataColumn typeColumn = new DataColumn();
            typeColumn.DataType   = typeof(System.String);
            typeColumn.ColumnName = "Element Type";
            typeColumn.Caption    = "Element Type";
            typeColumn.ReadOnly   = true;
            elementInformationTable.Columns.Add(typeColumn);

            // Create support column and add to the DataTable.
            DataColumn supportColumn = new DataColumn();
            supportColumn.DataType   = typeof(System.String);
            supportColumn.ColumnName = "Support Type";
            supportColumn.Caption    = "Support Type";
            supportColumn.ReadOnly   = true;
            elementInformationTable.Columns.Add(supportColumn);

            // Create a column which can note others information
            DataColumn remarkColumn = new DataColumn();
            remarkColumn.DataType   = typeof(System.String);
            remarkColumn.ColumnName = "Remark";
            remarkColumn.Caption    = "Remark";
            remarkColumn.ReadOnly   = true;
            elementInformationTable.Columns.Add(remarkColumn);
          
            return elementInformationTable;
        }

        /// <summary>
        /// get element's support information
        /// </summary>
        /// <param name="analyticalModel"> element's analytical model</param>
        /// <returns></returns>
        private string[] GetSupportInformation(AnalyticalModel analyticalModel)
        {
            // supportInformation[0] store supportType
            // supportInformation[1] store other informations
            string[] supportInformations = new string[2] { "", "" };

            IList<AnalyticalModelSupport> supports = analyticalModel.GetAnalyticalModelSupports();

            // "Supported" flag indicates if the Element is completely supported.
            // AnalyticalModel Support list keeps track of all supports.
            if (!analyticalModel.IsElementFullySupported())// judge if supported
            {
                if (0 == supports.Count)
                {
                    supportInformations[0] = "not supported";
                }
                else
                {
                    foreach (AnalyticalModelSupport support in supports)
                    {
                        supportInformations[0] = supportInformations[0] +
                                                             support.GetSupportType().ToString() + ", ";
                    }
               }
            }
            else
            {
                if (0 == supports.Count)
                {
                    supportInformations[1] = "supported but no more information";
                }
                else
                {
                    foreach (AnalyticalModelSupport support in supports)
                    {
                        supportInformations[0] = supportInformations[0] +
                                                 support.GetSupportType().ToString() + ", ";
                    }
                }
            }

            return supportInformations;
        }
    }
}

AnalyticalSupportDataInfoForm.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.
//


namespace Revit.SDK.Samples.AnalyticalSupportData_Info.CS
{
    partial class AnalyticalSupportData_InfoForm
    {
        /// <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.closeButton = new System.Windows.Forms.Button();
           this.elementInfoDataGridView = new System.Windows.Forms.DataGridView();
           this.id = new System.Windows.Forms.DataGridViewTextBoxColumn();
           this.typeName = new System.Windows.Forms.DataGridViewTextBoxColumn();
           this.support = new System.Windows.Forms.DataGridViewTextBoxColumn();
           this.remark = new System.Windows.Forms.DataGridViewTextBoxColumn();
           ((System.ComponentModel.ISupportInitialize)(this.elementInfoDataGridView)).BeginInit();
           this.SuspendLayout();
           // 
           // closeButton
           // 
           this.closeButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
           this.closeButton.Location = new System.Drawing.Point(690, 342);
           this.closeButton.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
           this.closeButton.Name = "closeButton";
           this.closeButton.Size = new System.Drawing.Size(68, 24);
           this.closeButton.TabIndex = 0;
           this.closeButton.Text = "&Close";
           this.closeButton.UseVisualStyleBackColor = true;
           this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
           // 
           // elementInfoDataGridView
           // 
           this.elementInfoDataGridView.AllowUserToAddRows = false;
           this.elementInfoDataGridView.AllowUserToDeleteRows = false;
           this.elementInfoDataGridView.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
           this.elementInfoDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
           this.elementInfoDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.id,
            this.typeName,
            this.support,
            this.remark});
           this.elementInfoDataGridView.Location = new System.Drawing.Point(15, 10);
           this.elementInfoDataGridView.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
           this.elementInfoDataGridView.Name = "elementInfoDataGridView";
           this.elementInfoDataGridView.ReadOnly = true;
           this.elementInfoDataGridView.RowHeadersVisible = false;
           this.elementInfoDataGridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
           this.elementInfoDataGridView.RowTemplate.Height = 24;
           this.elementInfoDataGridView.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;
           this.elementInfoDataGridView.Size = new System.Drawing.Size(743, 324);
           this.elementInfoDataGridView.TabIndex = 1;
           // 
           // id
           // 
           this.id.HeaderText = "Element ID";
           this.id.Name = "id";
           this.id.ReadOnly = true;
           this.id.Width = 90;
           // 
           // typeName
           // 
           this.typeName.HeaderText = "Element Type";
           this.typeName.Name = "typeName";
           this.typeName.ReadOnly = true;
           this.typeName.Width = 250;
           // 
           // support
           // 
           this.support.HeaderText = "Support Type";
           this.support.Name = "support";
           this.support.ReadOnly = true;
           this.support.Width = 200;
           // 
           // remark
           // 
           this.remark.HeaderText = "Remark";
           this.remark.Name = "remark";
           this.remark.ReadOnly = true;
           this.remark.Width = 200;
           // 
           // AnalyticalSupportData_InfoForm
           // 
           this.AcceptButton = this.closeButton;
           this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.CancelButton = this.closeButton;
           this.ClientSize = new System.Drawing.Size(772, 370);
           this.Controls.Add(this.elementInfoDataGridView);
           this.Controls.Add(this.closeButton);
           this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
           this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
           this.MaximizeBox = false;
           this.MinimizeBox = false;
           this.Name = "AnalyticalSupportData_InfoForm";
           this.ShowInTaskbar = false;
           this.Text = "Analytical Support Data";
           ((System.ComponentModel.ISupportInitialize)(this.elementInfoDataGridView)).EndInit();
           this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button closeButton;
       private System.Windows.Forms.DataGridView elementInfoDataGridView;
       private System.Windows.Forms.DataGridViewTextBoxColumn id;
       private System.Windows.Forms.DataGridViewTextBoxColumn typeName;
       private System.Windows.Forms.DataGridViewTextBoxColumn support;
       private System.Windows.Forms.DataGridViewTextBoxColumn remark;
    }
}