应用程序:CreateComplexAreaRein

Revit 平台:Structure

Revit 版本:2011.0

首次发布版本:9.0

编程语言:C#

技能水平:中等

类别:Structure

类型:ExternalCommand

主题:创建区域钢筋。

摘要:本示例演示了如何通过Revit API创建区域钢筋,并更改其参数。

相关类:

Autodesk.Revit.DB.Structural.AnalyticalModel

Autodesk.Revit.DB.CurveArray

Autodesk.Revit.DB.Line

Autodesk.Revit.DB.Structural.AreaReinforcementType

Autodesk.Revit.DB.ElementIterator

Autodesk.Revit.DB.Solid

项目文件:

CreateComplexAreaRein.cs

实现了IExternalCommand接口,并向用户展示了如何创建区域加固和更改其参数。

AreaReinData.cs

区域钢筋的数据和数据管理器。在当前项目中存储各种区域钢筋类型,并包含用于更改参数值的方法。

GeomUtil.cs

此文件定义了一个名为GeomUtil的类,提供一些常见的几何估计和计算方法。

ParameterUtil.cs

此文件定义了一个名为ParameterUtil的类,包含用于管理参数的方法。例如,通过名称查找参数和更改参数值。

功能:

- 本示例展示了如何通过 Revit API 创建区域加固并设置参数。

- 使用所有四层,并使用两个嵌套的矩形曲线循环来创建楼板加固,以形成一个矩形的开口。

- 在内部的 4 条曲线上,设置覆盖标志,并将顶部 2 层的钩子翻转到“向上”。

- 如果用户在项目中加载了区域加固类型,则可以在项目中找到用于创建区域加固的类型。

- 楼板/墙的几何信息可以作为 Floor/Wall 对象的 Geometry 属性来获取。

- 可以通过以下通用参数从区域加固中找到“布局规则”属性:REBAR_SYSTEM_LAYOUT_RULE。

- 可以通过以下通用参数获取 AreaReinforcement Curve 的属性:

名称 内置参数

覆盖区域加固设置  REBAR_SYSTEM_OVERRIDE

顶部主钩方向  REBAR_SYSTEM_HOOK_ORIENT_TOP_DIR_1

顶部次钩方向  REBAR_SYSTEM_HOOK_ORIENT_TOP_DIR_2

实施:

1. 绘制一个矩形楼板(或打开CreateComplexAreaRein.rvt),并选中它。

2. 运行此命令。

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

CreateComplexAreaRein.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 Autodesk.Revit.UI;

namespace Revit.SDK.Samples.CreateComplexAreaRein.CS
{
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;
    using System.Windows.Forms;

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

    using DocCreator = Autodesk.Revit.Creation.Document;


    [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
    {
        private UIDocument m_currentDoc;
        private static ExternalCommandData m_revit;
        private AreaReinData m_data;

        ///<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 revit,
            ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            Transaction trans = new Transaction(revit.Application.ActiveUIDocument.Document, "Revit.SDK.Samples.CreateComplexAreaRein");
            trans.Start();
            //initialize members
            m_revit = revit;
            m_currentDoc = revit.Application.ActiveUIDocument;
            m_data = new AreaReinData(revit.Application.ActiveUIDocument.Document);

            try
            {
                //check precondition and prepare necessary data to create AreaReinforcement.
                Reference refer = null;
                IList<Curve> curves = new List<Curve>();
                Floor floor = InitFloor(ref refer, ref curves);

                //ask for user's input
                AreaReinData dataOnFloor = new AreaReinData(revit.Application.ActiveUIDocument.Document);
                CreateComplexAreaReinForm createForm = new
                    CreateComplexAreaReinForm(dataOnFloor);
                if (createForm.ShowDialog() == DialogResult.OK)
                {
                    //define the Major Direction of AreaReinforcement,
                    //we get direction of first Line on the Floor as the Major Direction
                    Line firstLine = (Line)(curves[0]);
                    Autodesk.Revit.DB.XYZ majorDirection = new Autodesk.Revit.DB.XYZ(
                        firstLine.GetEndPoint(1).X - firstLine.GetEndPoint(0).X,
                        firstLine.GetEndPoint(1).Y - firstLine.GetEndPoint(0).Y,
                        firstLine.GetEndPoint(1).Z - firstLine.GetEndPoint(0).Z);

                    //create AreaReinforcement by AreaReinforcement.Create() function
                    DocCreator creator = m_revit.Application.ActiveUIDocument.Document.Create;
                    ElementId areaReinforcementTypeId = AreaReinforcementType.CreateDefaultAreaReinforcementType(revit.Application.ActiveUIDocument.Document);
                    ElementId rebarBarTypeId = RebarBarType.CreateDefaultRebarBarType(revit.Application.ActiveUIDocument.Document);
                    ElementId rebarHookTypeId = RebarHookType.CreateDefaultRebarHookType(revit.Application.ActiveUIDocument.Document);
                    AreaReinforcement areaRein = AreaReinforcement.Create(revit.Application.ActiveUIDocument.Document, floor, curves, majorDirection, areaReinforcementTypeId, rebarBarTypeId, rebarHookTypeId);

                    //set AreaReinforcement and it's AreaReinforcementCurves parameters
                    dataOnFloor.FillIn(areaRein);
                    trans.Commit();
                    return Autodesk.Revit.UI.Result.Succeeded;
                }
            }
            catch (ApplicationException appEx)
            {
                message = appEx.Message;
                trans.RollBack();
                return Autodesk.Revit.UI.Result.Failed;
            }
            catch
            {
                message = "Unknow Errors.";
                trans.RollBack();
                return Autodesk.Revit.UI.Result.Failed;
            }
            trans.RollBack();
            return Autodesk.Revit.UI.Result.Cancelled;
        }

        /// <summary>
        /// ExternalCommandData
        /// </summary>
        public static ExternalCommandData CommandData
        {
            get
            {
                return m_revit;
            }
        }

        /// <summary>
        /// initialize member data, judge simple precondition
        /// </summary>
        private Floor InitFloor(ref Reference refer, ref IList<Curve> curves)
        {
           ElementSet elems = new ElementSet();
            foreach (ElementId elementId in m_currentDoc.Selection.GetElementIds())
            {
               elems.Insert(m_currentDoc.Document.GetElement(elementId));
            }
            //selected 0 or more than 1 element
            if (elems.Size != 1)
            {
                string msg = "Please select exactly one slab.";
                ApplicationException appEx = new ApplicationException(msg);
                throw appEx;
            }
            Floor floor = null;
            foreach (object o in elems)
            {
                //selected one floor
                floor = o as Floor;
                if (null == floor)
                {
                    string msg = "Please select exactly one slab.";
                    ApplicationException appEx = new ApplicationException(msg);
                    throw appEx;
                }
            }
            //check the shape is rectangular and get its edges
            GeomHelper helper = new GeomHelper();
            if (!helper.GetFloorGeom(floor, ref refer, ref curves))
            {
                ApplicationException appEx = new
                    ApplicationException(
                    "Your selection is not a structural rectangular horizontal slab.");
                throw appEx;
            }

            return floor;
        }
    }
}

AreaReinData.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.CreateComplexAreaRein.CS
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;

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

    using GeoElement = Autodesk.Revit.DB.GeometryElement;
    using Element = Autodesk.Revit.DB.Element;


    /// <summary>
    /// data and data manager of the AreaReinforcement
    /// </summary>
    public class AreaReinData
    {
        private Document m_doc;

        /// <summary>
        /// constructor
        /// </summary>
        public AreaReinData(Document doc)
        {
            m_doc = doc;
        }

        private LayoutRules m_layoutRule = LayoutRules.Maximum_Spacing;

        /// <summary>
        /// Parameter LayoutRule of AreaReinforcement
        /// </summary>
        public LayoutRules LayoutRule
        {
            get
            {
                return m_layoutRule;
            }
            set
            {
                m_layoutRule = value;
            }
        }

        /// <summary>
        /// set the parameters to given AreaReinforcement
        /// </summary>
        /// <param name="areaRein"></param>
        public virtual void FillIn(AreaReinforcement areaRein)
        {
            int temp = (int)m_layoutRule;
            bool flag = ParameterUtil.SetParaInt(areaRein,
                BuiltInParameter.REBAR_SYSTEM_LAYOUT_RULE, temp);
            //if BuiltInParameter doesn't work
            if (!flag)
            {
                Parameter paraLayout = ParameterUtil.FindParaByName(
                    areaRein.Parameters, "Layout Rule");
                if (null != paraLayout)
                {
                    paraLayout.Set(temp);
                }
            }
            ChangeAreaReinCurves(areaRein);
        }

        /// <summary>
        /// On the interior 4 curves, set the override flag 
        /// and flip the hooks on the top 2 layers to "up"
        /// </summary>
        /// <param name="areaRein"></param>
        private void ChangeAreaReinCurves(AreaReinforcement areaRein)
        {
            //interior 4 curves are listed in the back of the curves, 
            //this order is decided when we create it
            IList<ElementId> curveIds = areaRein.GetBoundaryCurveIds();
            for (int i = 4; i < 8; i++)
            {
                AreaReinforcementCurve areaReinCurve =
                    m_doc.GetElement(curveIds[i]) as AreaReinforcementCurve;
                //remove hooks, set the hook the top 2 layers to 'up'
                ParameterUtil.SetParaInt(areaReinCurve,
                    BuiltInParameter.REBAR_SYSTEM_OVERRIDE, -1);
                ParameterUtil.SetParaInt(areaReinCurve,
                    BuiltInParameter.REBAR_SYSTEM_HOOK_ORIENT_TOP_DIR_1,
                    (int)HookOrientation.Up);
                ParameterUtil.SetParaInt(areaReinCurve,
                    BuiltInParameter.REBAR_SYSTEM_HOOK_ORIENT_TOP_DIR_2,
                    (int)HookOrientation.Up);
            }
        }
    }
}