应用程序:CurvedBeam

Revit 平台:所有

Revit 版本:2011.0

首次发布时间:2008.0

编程语言:C#

技能水平:初级

类别:结构

类型:外部命令

应用程序:CurvedBeam

主题:曲形梁创建。

摘要:本样例演示了如何创建曲形梁,该样例可以创建三种类型的曲形梁:圆弧、椭圆和 NURBS 样条线。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.Creation.Document

Autodesk.Revit.DB.Symbols.FamilySymbol

Autodesk.Revit.DB.FamilyInstance

Autodesk.Revit.DB.Level

Autodesk.Revit.DB.Geometry.Arc

Autodesk.Revit.DB.Geometry.Ellipse

Autodesk.Revit.DB.Geometry.NurbSpline

Autodesk.Revit.DB.Geometry.XYZ

项目文件:

CurvedBeam.cs

这个文件包含了类 Command,它继承了 IExternalCommand 接口并实现了 Execute 方法;此外,它还实现了从 Revit 项目中获取所有楼层和结构构架(梁和支撑)类型的功能。此文件还包含 SymbolMap 和 LevelMap 两个类,用于保留所有梁和楼层类型。

CurvedBeamForm.cs

这个文件包含了一个 Form 类,由两个 ComboBox 控件和三个按钮组成。ComboBox 控件将显示项目中所有结构构架(梁和支撑)类型和楼层;按钮将分别创建圆弧、椭圆、NURBS 样条线梁。

功能:

本示例主要使用了 FamilySymbol、FamilyInstance、Arc、Ellipse 和 NurbSpline 等类,以获取 Revit 项目中所有结构构架类型和楼层,然后使用它们来创建新的梁。

- 使用 ElementClassFilter 来迭代 Revit 中的结构构架类型和楼层。

- 使用 Family.Symbols 属性来获取一个族所有符号。

- 使用 FamilySymbol.Category 属性来获取家族符号的类别。

- 分别使用 Creation.Document 类的 NewArc、NewEllipse 和 NewNurbSpline 方法来创建圆弧、椭圆和 NURBS 样条线。

- 使用 Creation.Document 类的 NewFamilyInstance 方法来创建曲形梁实例,通过指定新梁的曲线。

实施:

1. 打开 Revit,确保项目中有一些结构构架类型和楼层。

2. 运行命令,将会弹出 "曲形梁" 窗体,所有可用的结构构架类型和楼层将列在两个 ComboBox 控件中。

3. 选择即将创建梁的类型和楼层。

4. 点击 "圆弧" 按钮将创建一个圆弧梁,点击 "部分椭圆" 按钮将创建一个部分椭圆梁,点击 "样条线" 按钮将创建一个 NURBS 样条线梁。

注:

1. 圆弧、椭圆和 NURBS 样条线实例的位置是硬编码的。

2. 请在最新版本(晚于 2006 年 12 月 14 日)中运行示例。

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

CurvedBeam.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.Collections;
using System.Windows.Forms;

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

namespace Revit.SDK.Samples.CurvedBeam.CS
{
    /// <summary>
    /// This class inherits from IExternalCommand interface, and implements the Execute method to create Arc, BSpline beams.
    /// </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.UsingCommandData)]
    public class Command : IExternalCommand
    {
        #region Class memeber variables
        Autodesk.Revit.UI.UIApplication m_revit = null;
        ArrayList m_beamMaps = new ArrayList();    // list of beams' type
        ArrayList m_levels = new ArrayList();    // list of levels
        #endregion


        #region Command class properties
        /// <summary>
        /// list of all type of beams
        /// </summary>
        public ArrayList BeamMaps
        {
            get
            {
                return m_beamMaps;
            }
        }

        /// <summary>
        /// list of all levels
        /// </summary>
        public ArrayList LevelMaps
        {
            get
            {
                return m_levels;
            }
        }
        #endregion


        #region IExternalCommand 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)
        {
            m_revit = commandData.Application;
            Transaction tran = new Transaction(m_revit.ActiveUIDocument.Document, "CurvedBeam");
            tran.Start();

            // if initialize failed return Result.Failed
            bool initializeOK = Initialize();
            if (!initializeOK)
            {
                return Autodesk.Revit.UI.Result.Failed;
            }

            // pop up new beam form
            CurvedBeamForm displayForm = new CurvedBeamForm(this);
            displayForm.ShowDialog();
            tran.Commit();

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


        /// <summary>
        /// iterate all the symbols of levels and beams
        /// </summary>
        /// <returns>A value that signifies if the initialization was successful for true or failed for false</returns>
        private bool Initialize()
        {
            try
            {
                ElementClassFilter levelFilter = new ElementClassFilter(typeof(Level));
                ElementClassFilter famFilter = new ElementClassFilter(typeof(Family));
                LogicalOrFilter orFilter = new LogicalOrFilter(levelFilter, famFilter);
                FilteredElementCollector collector = new FilteredElementCollector(m_revit.ActiveUIDocument.Document);
                FilteredElementIterator i = collector.WherePasses(orFilter).GetElementIterator();
                i.Reset();
                bool moreElement = i.MoveNext();
                while (moreElement)
                {
                    object o = i.Current;

                    // add level to list
                    Level level = o as Level;
                    if (null != level)
                    {
                        m_levels.Add(new LevelMap(level));
                        goto nextLoop;
                    }

                    // get
                    Family f = o as Family;
                    if (null == f)
                    {
                        goto nextLoop;
                    }

                    foreach (ElementId elementId in f.GetFamilySymbolIds())
                    {
                       object symbol = m_revit.ActiveUIDocument.Document.GetElement(elementId);
                        FamilySymbol familyType = symbol as FamilySymbol;
                        if (null == familyType)
                        {
                            goto nextLoop;
                        }
                        if (null == familyType.Category)
                        {
                            goto nextLoop;
                        }

                        // add symbols of beams and braces to lists 
                        string categoryName = familyType.Category.Name;
                        if ("Structural Framing" == categoryName)
                        {
                            m_beamMaps.Add(new SymbolMap(familyType));
                        }
                    }
                nextLoop:
                    moreElement = i.MoveNext();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return true;
        }

        /// <summary>
        /// create an horizontal arc instance with specified z coordinate value
        /// </summary>
        public Arc CreateArc(double z)
        {
            Autodesk.Revit.DB.XYZ center = new Autodesk.Revit.DB.XYZ(0, 0, z);
            double radius = 20.0;
            double startAngle = 0.0;
            double endAngle = 5.0;
            Autodesk.Revit.DB.XYZ xAxis = new Autodesk.Revit.DB.XYZ(1, 0, 0);
            Autodesk.Revit.DB.XYZ yAxis = new Autodesk.Revit.DB.XYZ(0, 1, 0);
            return Arc.Create(center, radius, startAngle, endAngle, xAxis, yAxis);
        }


        /// <summary>
        /// create a horizontal partial ellipse instance with specified z coordinate value
        /// </summary>
        public Curve CreateEllipse(double z)
        {
            Autodesk.Revit.DB.XYZ center = new Autodesk.Revit.DB.XYZ(0, 0, z);
            double radX = 30;
            double radY = 50;
            Autodesk.Revit.DB.XYZ xVec = new Autodesk.Revit.DB.XYZ(1, 0, 0);
            Autodesk.Revit.DB.XYZ yVec = new Autodesk.Revit.DB.XYZ(0, 1, 0);
            double param0 = 0.0;
            double param1 = 3.1415;
            Curve ellpise = Ellipse.CreateCurve(center, radX, radY, xVec, yVec, param0, param1);
            m_revit.ActiveUIDocument.Document.Regenerate();
            return ellpise;
        }


        /// <summary>
        /// create a horizontal nurbspline instance with specified z coordinate value
        /// </summary>
        public Curve CreateNurbSpline(double z)
        {
            // create control points with same z value
            List<XYZ> ctrPoints = new List<XYZ>();
            Autodesk.Revit.DB.XYZ xyz1 = new Autodesk.Revit.DB.XYZ(-41.887503610431267, -9.0290629129782189, z);
            Autodesk.Revit.DB.XYZ xyz2 = new Autodesk.Revit.DB.XYZ(-9.27600019217055, 0.32213521486563046, z);
            Autodesk.Revit.DB.XYZ xyz3 = new Autodesk.Revit.DB.XYZ(9.27600019217055, 0.32213521486563046, z);
            Autodesk.Revit.DB.XYZ xyz4 = new Autodesk.Revit.DB.XYZ(41.887503610431267, 9.0290629129782189, z);

            ctrPoints.Add(xyz1); ctrPoints.Add(xyz2); ctrPoints.Add(xyz3);
            ctrPoints.Add(xyz4);

            IList<double> weights = new List<double>();
            double w1 = 1, w2 = 1, w3 = 1, w4 = 1;
            weights.Add(w1); weights.Add(w2); weights.Add(w3);
            weights.Add(w4);

            IList<double> knots = new List<double>();
            double k0 = 0, k1 = 0, k2 = 0, k3 = 0, k4 = 34.425128, k5 = 34.425128, k6 = 34.425128, k7 = 34.425128;

            knots.Add(k0); knots.Add(k1); knots.Add(k2); knots.Add(k3);
            knots.Add(k4); knots.Add(k5); knots.Add(k6);
            knots.Add(k7);

            Curve detailNurbSpline = NurbSpline.CreateCurve(3, knots,ctrPoints, weights);
            m_revit.ActiveUIDocument.Document.Regenerate();

            return detailNurbSpline;
        }


        /// <summary>
        /// create a curved beam
        /// </summary>
        /// <param name="fsBeam">beam type</param>
        /// <param name="curve">Curve of this beam.</param>
        /// <param name="level">beam's reference level</param>
        /// <returns></returns>
        public bool CreateCurvedBeam(FamilySymbol fsBeam, Curve curve, Level level)
        {
            FamilyInstance beam;
            try
            {
               if (!fsBeam.IsActive)
                  fsBeam.Activate();
                beam = m_revit.ActiveUIDocument.Document.Create.NewFamilyInstance(curve, fsBeam, level, StructuralType.Beam);
                if (null == beam)
                {
                    return false;
                }

                // get beam location curve
                LocationCurve beamCurve = beam.Location as LocationCurve;
                if (null == beamCurve)
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Revit", ex.ToString());
                return false;
            }

            // regenerate document
            m_revit.ActiveUIDocument.Document.Regenerate();
            return true;
        }
    }


    /// <summary>
    /// assistant class contains symbol and it's name
    /// </summary>
    public class SymbolMap
    {
        #region SymbolMap class member variables
        string m_symbolName = "";
        FamilySymbol m_symbol = null;
        #endregion


        /// <summary>
        /// constructor without parameter is forbidden
        /// </summary>
        private SymbolMap()
        {
            // no operation 
        }


        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="symbol">family symbol</param>
        public SymbolMap(FamilySymbol symbol)
        {
            m_symbol = symbol;
            string familyName = "";
            if (null != symbol.Family)
            {
                familyName = symbol.Family.Name;
            }
            m_symbolName = familyName + " : " + symbol.Name;
        }


        /// <summary>
        /// SymbolName property
        /// </summary>
        public string SymbolName
        {
            get
            {
                return m_symbolName;
            }
        }


        /// <summary>
        /// ElementType property
        /// </summary>
        public FamilySymbol ElementType
        {
            get
            {
                return m_symbol;
            }
        }
    }


    /// <summary>
    /// assistant class contains level and it's name
    /// </summary>
    public class LevelMap
    {
        #region LevelMap class member variable
        string m_levelName = "";
        Level m_level = null;
        #endregion


        #region LevelMap Constructors
        /// <summary>
        /// constructor without parameter is forbidden
        /// </summary>
        private LevelMap()
        {
            // no operation
        }


        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="level">level</param>
        public LevelMap(Level level)
        {
            m_level = level;
            m_levelName = level.Name;
        }
        #endregion


        #region LevelMap properties
        /// <summary>
        /// LevelName property
        /// </summary>
        public string LevelName
        {
            get
            {
                return m_levelName;
            }
        }

        /// <summary>
        /// Level property
        /// </summary>
        public Level Level
        {
            get
            {
                return m_level;
            }
        }
        #endregion
    }
}

CurvedBeamForm.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.CurvedBeam.CS
{
    /// <summary>
    /// new beam form
    /// </summary>
    partial class CurvedBeamForm
    {
        /// <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);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.BeamTypeCB = new System.Windows.Forms.ComboBox();
            this.LevelCB = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.newArcButton = new System.Windows.Forms.Button();
            this.newEllipseButton = new System.Windows.Forms.Button();
            this.newNurbSplineButton = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // BeamTypeCB
            // 
            this.BeamTypeCB.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.BeamTypeCB.FormattingEnabled = true;
            this.BeamTypeCB.Location = new System.Drawing.Point(95, 6);
            this.BeamTypeCB.Name = "BeamTypeCB";
            this.BeamTypeCB.Size = new System.Drawing.Size(274, 21);
            this.BeamTypeCB.TabIndex = 0;
            // 
            // LevelCB
            // 
            this.LevelCB.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.LevelCB.FormattingEnabled = true;
            this.LevelCB.Location = new System.Drawing.Point(95, 43);
            this.LevelCB.Name = "LevelCB";
            this.LevelCB.Size = new System.Drawing.Size(274, 21);
            this.LevelCB.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(73, 13);
            this.label1.TabIndex = 8;
            this.label1.Text = "Type of Beam";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(52, 46);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(33, 13);
            this.label2.TabIndex = 9;
            this.label2.Text = "Level";
            // 
            // newArcButton
            // 
            this.newArcButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.newArcButton.Image = global::Revit.SDK.Samples.CurvedBeam.CS.Properties.Resources.arc3;
            this.newArcButton.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.newArcButton.Location = new System.Drawing.Point(33, 83);
            this.newArcButton.Name = "newArcButton";
            this.newArcButton.Size = new System.Drawing.Size(54, 41);
            this.newArcButton.TabIndex = 4;
            this.newArcButton.Text = "&Arc";
            this.newArcButton.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.newArcButton.UseVisualStyleBackColor = true;
            this.newArcButton.Click += new System.EventHandler(this.newArcButton_Click);
            // 
            // newEllipseButton
            // 
            this.newEllipseButton.Image = global::Revit.SDK.Samples.CurvedBeam.CS.Properties.Resources.partialellipse;
            this.newEllipseButton.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.newEllipseButton.Location = new System.Drawing.Point(162, 83);
            this.newEllipseButton.Name = "newEllipseButton";
            this.newEllipseButton.Size = new System.Drawing.Size(54, 41);
            this.newEllipseButton.TabIndex = 5;
            this.newEllipseButton.Text = "Partial &Ellipse";
            this.newEllipseButton.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.newEllipseButton.UseVisualStyleBackColor = true;
            this.newEllipseButton.Click += new System.EventHandler(this.newEllipseButton_Click);
            // 
            // newNurbSplineButton
            // 
            this.newNurbSplineButton.Image = global::Revit.SDK.Samples.CurvedBeam.CS.Properties.Resources.spline;
            this.newNurbSplineButton.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.newNurbSplineButton.Location = new System.Drawing.Point(291, 83);
            this.newNurbSplineButton.Name = "newNurbSplineButton";
            this.newNurbSplineButton.Size = new System.Drawing.Size(54, 41);
            this.newNurbSplineButton.TabIndex = 6;
            this.newNurbSplineButton.Text = "&Spline";
            this.newNurbSplineButton.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.newNurbSplineButton.UseVisualStyleBackColor = true;
            this.newNurbSplineButton.Click += new System.EventHandler(this.newNurbSplineButton_Click);
            // 
            // label4
            // 
            this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label4.Location = new System.Drawing.Point(14, 73);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(358, 2);
            this.label4.TabIndex = 12;
            // 
            // CurvedBeamForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(380, 132);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.newEllipseButton);
            this.Controls.Add(this.newNurbSplineButton);
            this.Controls.Add(this.newArcButton);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.LevelCB);
            this.Controls.Add(this.BeamTypeCB);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "CurvedBeamForm";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Curved Beam";
            this.Load += new System.EventHandler(this.CreateCurvedBeamForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        /// <summary>
        /// method called when form is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateCurvedBeamForm_Load(object sender, System.EventArgs e)
        {
            //this.TextBoxRefresh();

            this.BeamTypeCB.DataSource = m_dataBuffer.BeamMaps;
            this.BeamTypeCB.DisplayMember = "SymbolName";
            this.BeamTypeCB.ValueMember = "ElementType";

            this.LevelCB.DataSource = m_dataBuffer.LevelMaps;
            this.LevelCB.DisplayMember = "LevelName";
            this.LevelCB.ValueMember = "Level";
        }

        private Command m_dataBuffer;
        private System.Windows.Forms.ComboBox BeamTypeCB;
        private System.Windows.Forms.ComboBox LevelCB;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button newArcButton;
        private System.Windows.Forms.Button newEllipseButton;
        private System.Windows.Forms.Button newNurbSplineButton;
        private System.Windows.Forms.Label label4;
    }
}