应用程序: CreateWallsUnderBeams

Revit平台: 结构

Revit版本: 2011.0

首次发布版本: 9.0

编程语言: C#

技能水平: 中等

类别: 结构

类型: 外部命令

主题: 在所选梁下创建墙。

摘要:该示例演示了如何在选定的梁下方生成简单的矩形墙体。执行命令时,应该会出现一个对话框,其中包含一个下拉列表,其中包含所有墙的类型以及用于指定墙是否为结构墙的复选框。默认应该是选中的。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.FamilyInstance

Autodesk.Revit.DB.Document

项目文件:

CreateWallsUnderBeams.cs 是主 DLL 源文件,包含Command类,该类实现了IExternalCommand接口。在 Revit 中查找梁的选择;确保所有梁都具有水平分析线;为用户显示对话框以选择墙体样式,并创建沿梁路径的墙体以及梁下方。

CreateWallsUnderBeamsForm.cs 文件包含一个窗体类,包含一个下拉列表,其中包含所有墙的类型以及用于指定墙是否为结构墙的复选框。

功能:

该示例应该提供以下功能:

- 添加一个命令,它接受一个垂直轮廓的梁的选择,并使用梁生成的轮廓制作一个轮廓墙体。

- 此命令将获取梁的选择并绘制沿梁路径的简单矩形墙体和梁下方的矩形墙体。

- 该示例仅适用于水平梁对象。

- 当执行命令时,应该会出现一个对话框,其中包含以下内容:

- 一个下拉列表,其中包含所有墙体类型。

- 一个复选框,用于指定墙体是否为结构墙,应默认选中。

- 用户应该能够选择用于创建墙体的墙类型。并且用户也可以选择创建结构或建筑墙。

- 用户从这些框中选择所需的项目。单击“确定”后,对话框将关闭,并在所选梁沿路径方向和梁下方创建简单矩形墙。

实施:

1. 打开 Revit Structure 并绘制一些梁。

2. 选择这些梁并运行此命令。

3. 用户可以选择墙体类型并选择墙体是否为结构墙。

4. 单击“确定”按钮。

5. 应用程序将在梁下方创建简单矩形墙体。

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

CreateWallsUnderBeams.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.Collections;
using System.Text;
using System.Windows.Forms;
using System.Linq;

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

namespace Revit.SDK.Samples.CreateWallsUnderBeams.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 CreateWallsUnderBeams : IExternalCommand
    {
        // Private Members
        IList<WallType> m_wallTypeCollection;         // Store all the wall types in current document
        ArrayList m_beamCollection;             // Store the selection of beams in Revit
        WallType m_selectedWallType;            // Store the selected wall type
        Level m_level;                          // Store the level which wall create on
        Boolean m_isStructural;                 // Indicate whether create structural walls
        String m_errorInformation;              // Store the error information
        const Double PRECISION = 0.0000000001;  // Define a precision of double data

        // Properties
        /// <summary>
        /// Inform all the wall types can be created in current document
        /// </summary>
        public IList<WallType> WallTypes
        {
            get
            {
                return m_wallTypeCollection;
            }
        }

        /// <summary>
        /// Inform the wall type selected by the user
        /// </summary>
        public Object SelectedWallType
        {
            set
            {
                m_selectedWallType = value as WallType;
            }
        }

        /// <summary>
        /// Inform whether the user want to create structural or architecture walls
        /// </summary>
        public Boolean IsSturctual
        {
            get
            {
                return m_isStructural;
            }
            set
            {
                m_isStructural = value;
            }
        }

        // Methods
        /// <summary>
        /// Default constructor of CreateWallsUnderBeams
        /// </summary>
        public CreateWallsUnderBeams()
        {
            m_wallTypeCollection = new List<WallType>();
            m_beamCollection = new ArrayList();
            m_isStructural = true;
        }

        #region IExternalCommand Members 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)
        {
            Autodesk.Revit.UI.UIApplication revit = commandData.Application;
            UIDocument project = revit.ActiveUIDocument;

            // Find the selection of beams in Revit
            ElementSet selection = new ElementSet();
            foreach (ElementId elementId in project.Selection.GetElementIds())
            {
               selection.Insert(project.Document.GetElement(elementId));
            }
            foreach (Autodesk.Revit.DB.Element e in selection)
            {
                FamilyInstance m = e as FamilyInstance;
                if (null != m)
                {
                    if (StructuralType.Beam == m.StructuralType)
                    {
                        // Store all the beams the user selected in Revit
                        m_beamCollection.Add(e);
                    }
                }
            }
            if (0 == m_beamCollection.Count)
            {
                message = "Can not find any beams.";
                return Autodesk.Revit.UI.Result.Failed;
            }

            // Make sure all the beams have horizontal analytical line
            if (!CheckBeamHorizontal())
            {
                message = m_errorInformation;
                return Autodesk.Revit.UI.Result.Failed;
            }

            // Search all the wall types in the Revit
            FilteredElementCollector filteredElementCollector = new FilteredElementCollector(project.Document);
            filteredElementCollector.OfClass(typeof(WallType));
            m_wallTypeCollection = filteredElementCollector.Cast<WallType>().ToList<WallType>();

            // Show the dialog for the user select the wall style
            using (CreateWallsUnderBeamsForm displayForm = new CreateWallsUnderBeamsForm(this))
            {
                if (DialogResult.OK != displayForm.ShowDialog())
                {
                    return Autodesk.Revit.UI.Result.Failed;
                }
            }

            // Create the walls which along and under the path of the beams.
            if (!BeginCreate(project.Document))
            {
                message = m_errorInformation;
                return Autodesk.Revit.UI.Result.Failed;
            }

            // If everything goes right, return succeeded.
            return Autodesk.Revit.UI.Result.Succeeded;
        }
        #endregion IExternalCommand Members Implementation

        /// <summary>
        /// Create the walls which along and under the path of the selected beams
        /// </summary>
        /// <param name="project"> A reference of current document</param>
        /// <returns>true if there is no error in process; otherwise, false.</returns>
        Boolean BeginCreate(Autodesk.Revit.DB.Document project)
        {
            // Begin to create walls along and under each beam
            for (int i = 0; i < m_beamCollection.Count; i++)
            {
                // Get each selected beam.
                FamilyInstance m = m_beamCollection[i] as FamilyInstance;
                if (null == m)
                {
                    m_errorInformation = "The program should not go here.";
                    return false;
                }

                // Get the analytical model of the beam, 
                // the wall will be created using this model line as path.            
                AnalyticalModel model = m.GetAnalyticalModel();
                if (null == model)
                {
                    m_errorInformation = "The beam should have analytical model.";
                    return false;
                }

                // Get the level using the beam's reference level
                Autodesk.Revit.DB.ElementId levelId = m.get_Parameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM).AsElementId();
                m_level = project.GetElement(levelId) as Level;
                if (null == m_level)
                {
                    m_errorInformation = "The program should not go here.";
                    return false;
                }

                Transaction t = new Transaction(project, Guid.NewGuid().GetHashCode().ToString());
                t.Start();
                Wall createdWall = Wall.Create(project, model.GetCurve(), m_selectedWallType.Id,
                                                m_level.Id, 10, 0, true, m_isStructural);
                if (null == createdWall)
                {
                    m_errorInformation = "Can not create the walls";
                    return false;
                }

                // Modify some parameters of the created wall to make it look better.
                Double offset = model.GetCurve().GetEndPoint(0).Z - m_level.Elevation;
                createdWall.get_Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).Set(levelId);
                createdWall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET).Set(offset - 3000 / 304.8);
                createdWall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).Set(levelId);
                t.Commit();
            }
            return true;
        }


        /// <summary>
        /// Check whether all the beams have horizontal analytical line 
        /// </summary>
        /// <returns>true if each beam has horizontal analytical line; otherwise, false.</returns>
        Boolean CheckBeamHorizontal()
        {
            for (int i = 0; i < m_beamCollection.Count; i++)
            {
                // Get the analytical curve of each selected beam.
                // And check if Z coordinate of start point and end point of the curve are same.
                FamilyInstance m = m_beamCollection[i] as FamilyInstance;
                AnalyticalModel model = m.GetAnalyticalModel();
                if (null == model)
                {
                    m_errorInformation = "The beam should have analytical model.";
                    return false;
                }
                else if ((PRECISION <= model.GetCurve().GetEndPoint(0).Z - model.GetCurve().GetEndPoint(1).Z)
                    || (-PRECISION >= model.GetCurve().GetEndPoint(0).Z - model.GetCurve().GetEndPoint(1).Z))
                {
                    m_errorInformation = "Please only select horizontal beams.";
                    return false;
                }
            }
            return true;
        }
    }
}

CreateWallsUnderBeamsForm.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.CreateWallsUnderBeams.CS
{
    partial class CreateWallsUnderBeamsForm
    {
        /// <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.optionGroupBox = new System.Windows.Forms.GroupBox();
           this.structualCheckBox = new System.Windows.Forms.CheckBox();
           this.wallTypeLabel = new System.Windows.Forms.Label();
           this.wallTypeComboBox = new System.Windows.Forms.ComboBox();
           this.OKButton = new System.Windows.Forms.Button();
           this.cancelButton = new System.Windows.Forms.Button();
           this.optionGroupBox.SuspendLayout();
           this.SuspendLayout();
           // 
           // optionGroupBox
           // 
           this.optionGroupBox.Controls.Add(this.structualCheckBox);
           this.optionGroupBox.Controls.Add(this.wallTypeLabel);
           this.optionGroupBox.Controls.Add(this.wallTypeComboBox);
           this.optionGroupBox.Location = new System.Drawing.Point(12, 25);
           this.optionGroupBox.Name = "optionGroupBox";
           this.optionGroupBox.Size = new System.Drawing.Size(268, 100);
           this.optionGroupBox.TabIndex = 0;
           this.optionGroupBox.TabStop = false;
           this.optionGroupBox.Text = "Create Option";
           // 
           // structualCheckBox
           // 
           this.structualCheckBox.AutoSize = true;
           this.structualCheckBox.Location = new System.Drawing.Point(17, 65);
           this.structualCheckBox.Name = "structualCheckBox";
           this.structualCheckBox.Size = new System.Drawing.Size(92, 17);
           this.structualCheckBox.TabIndex = 2;
           this.structualCheckBox.Text = "Structual Wall";
           this.structualCheckBox.UseVisualStyleBackColor = true;
           // 
           // wallTypeLabel
           // 
           this.wallTypeLabel.AutoSize = true;
           this.wallTypeLabel.Location = new System.Drawing.Point(14, 32);
           this.wallTypeLabel.Name = "wallTypeLabel";
           this.wallTypeLabel.Size = new System.Drawing.Size(58, 13);
           this.wallTypeLabel.TabIndex = 0;
           this.wallTypeLabel.Text = "Wall Type:";
           // 
           // wallTypeComboBox
           // 
           this.wallTypeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
           this.wallTypeComboBox.FormattingEnabled = true;
           this.wallTypeComboBox.Location = new System.Drawing.Point(78, 29);
           this.wallTypeComboBox.Name = "wallTypeComboBox";
           this.wallTypeComboBox.Size = new System.Drawing.Size(184, 21);
           this.wallTypeComboBox.TabIndex = 1;
           // 
           // OKButton
           // 
           this.OKButton.DialogResult = System.Windows.Forms.DialogResult.OK;
           this.OKButton.Location = new System.Drawing.Point(50, 159);
           this.OKButton.Name = "OKButton";
           this.OKButton.Size = new System.Drawing.Size(57, 25);
           this.OKButton.TabIndex = 1;
           this.OKButton.Text = "&OK";
           this.OKButton.UseVisualStyleBackColor = true;
           this.OKButton.Click += new System.EventHandler(this.OKButton_Click);
           // 
           // cancelButton
           // 
           this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
           this.cancelButton.Location = new System.Drawing.Point(179, 159);
           this.cancelButton.Name = "cancelButton";
           this.cancelButton.Size = new System.Drawing.Size(57, 25);
           this.cancelButton.TabIndex = 2;
           this.cancelButton.Text = "&Cancel";
           this.cancelButton.UseVisualStyleBackColor = true;
           // 
           // CreateWallsUnderBeamsForm
           // 
           this.AcceptButton = this.OKButton;
           this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.CancelButton = this.cancelButton;
           this.ClientSize = new System.Drawing.Size(292, 212);
           this.Controls.Add(this.cancelButton);
           this.Controls.Add(this.OKButton);
           this.Controls.Add(this.optionGroupBox);
           this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
           this.MaximizeBox = false;
           this.MinimizeBox = false;
           this.Name = "CreateWallsUnderBeamsForm";
           this.ShowInTaskbar = false;
           this.Text = "Create Walls Under Beams";
           this.Load += new System.EventHandler(this.CreateWallsUnderBeamsForm_Load);
           this.optionGroupBox.ResumeLayout(false);
           this.optionGroupBox.PerformLayout();
           this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox optionGroupBox;
        private System.Windows.Forms.CheckBox structualCheckBox;
        private System.Windows.Forms.Label wallTypeLabel;
        private System.Windows.Forms.ComboBox wallTypeComboBox;
        private System.Windows.Forms.Button OKButton;
        private System.Windows.Forms.Button cancelButton;

    }
}