应用程序:AreaReinCurve

Revit平台:Structure

Revit版本:2011.0

首次发布用于:9.0

编程语言:C#

技能等级:起始

类别:结构

类型:外部命令

主题:AreaReinCurve的参数。

摘要:此示例向用户展示如何获取AreaReinforcementCurve并更改其参数。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.ElementSet

Autodesk.Revit.DB.Structure.AreaReinforcementCurve

Autodesk.Revit.DB.BuiltInParameter

项目文件:

AreaReinCurve.cs实现IExternalCommand界面,并向用户展示如何获取AreaReinforcementCurve以及如何通过BuildInPatameter获取参数。

GeomUtil.cs这个文件定义了一个类GeomUtil提供了一些常见的几何估计和计算方法。

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

功能:

此示例向用户显示如何获取面积钢筋和面积钢筋曲线,并向用户展示如何通过Element.get_parameter(Autodesk.Revit.Parameters.BuiltInParameter parameterId)方法获取参数并通过parameter.Set(…)方法更改其值。

实施:

1.绘制AreaReinforcement(或打开AreaReinCurve.rvt)并选择它。

2.运行此命令

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

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

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

    [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
    {
        AreaReinforcement m_areaRein;
        List<AreaReinforcementCurve> m_areaReinCurves;
        Document m_doc;

        /// <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.AreaReinCurve");
            trans.Start();
            ElementSet selected = new ElementSet();
            foreach (ElementId elementId in revit.Application.ActiveUIDocument.Selection.GetElementIds())
            {
               selected.Insert(revit.Application.ActiveUIDocument.Document.GetElement(elementId));
            }

            try
            {
                m_doc = revit.Application.ActiveUIDocument.Document;

                //selected is not one rectangular AreaReinforcement
                if (!PreData(selected))
                {
                    message = "Please select only one rectangular AreaReinforcement.";
                    trans.RollBack();
                    return Autodesk.Revit.UI.Result.Failed;
                }

                //fail to turn off layers
                if (!TurnOffLayers())
                {
                    message = "Can't turn off layers as expected or can't find these layers.";
                    trans.RollBack();
                    return Autodesk.Revit.UI.Result.Failed;
                }

                //fail to remove hooks
                if (!ChangeHookType())
                {
                    message = "Can't remove HookTypes as expected.";
                    trans.RollBack();
                    return Autodesk.Revit.UI.Result.Failed;
                }
            }
            catch (ApplicationException appEx)
            {
                message = appEx.ToString();
                trans.RollBack();
                return Autodesk.Revit.UI.Result.Failed;
            }
            catch
            {
                message = "Unexpected error happens.";
                trans.RollBack();
                return Autodesk.Revit.UI.Result.Failed;
            }

            //command is successful
            string msg = "All layers but Major Direction Layer or Exterior Direction Layer ";
            msg += "have been turn off; ";
            msg += "Removed the Hooks from one boundary curve of the Major Direction Layer ";
            msg += "or Exterior Direction Layer.";
            TaskDialog.Show("Revit", msg);
            trans.Commit();
            return Autodesk.Revit.UI.Result.Succeeded;
        }

        /// <summary>
        /// check whether the selected is expected, prepare necessary data
        /// </summary>
        /// <param name="selected">selected elements</param>
        /// <returns>whether the selected AreaReinforcement is expected</returns>
        private bool PreData(ElementSet selected)
        {
            //selected is not only one AreaReinforcement
            if (selected.Size != 1)
            {
                return false;
            }
            foreach (Object o in selected)
            {
                m_areaRein = o as AreaReinforcement;
                if (null == m_areaRein)
                {
                    return false;
                }
            }

            //whether the selected AreaReinforcement is rectangular
            CurveArray curves = new CurveArray();
            m_areaReinCurves = new List<AreaReinforcementCurve>();
            IList<ElementId> curveIds = m_areaRein.GetBoundaryCurveIds();
            foreach (ElementId o in curveIds)
            {
                AreaReinforcementCurve areaCurve = m_doc.GetElement(o) as AreaReinforcementCurve;
                if (null == areaCurve)
                {
                    ApplicationException appEx = new ApplicationException
                        ("There is unexpected error with selected AreaReinforcement.");
                    throw appEx;
                }
                m_areaReinCurves.Add(areaCurve);
                curves.Append(areaCurve.Curve);
            }
            bool flag = GeomUtil.IsRectangular(curves);

            return flag;
        }

        /// <summary>
        /// turn off all layers but the Major Direction Layer or Exterior Direction Layer
        /// </summary>
        /// <returns>whether the command is successful</returns>
        private bool TurnOffLayers()
        {
            //AreaReinforcement is on the floor or slab
            bool flag = true;
            flag = ParameterUtil.SetParaInt(m_areaRein,
                BuiltInParameter.REBAR_SYSTEM_ACTIVE_BOTTOM_DIR_1, 0);
            flag &= ParameterUtil.SetParaInt(m_areaRein,
                BuiltInParameter.REBAR_SYSTEM_ACTIVE_BOTTOM_DIR_2, 0);
            flag &= ParameterUtil.SetParaInt(m_areaRein,
                BuiltInParameter.REBAR_SYSTEM_ACTIVE_TOP_DIR_2, 0);

            //AreaReinforcement is on the wall
            if (!flag)
            {
                flag = true;
                flag &= ParameterUtil.SetParaInt(m_areaRein, "Interior Major Direction", 0);
                flag &= ParameterUtil.SetParaInt(m_areaRein, "Exterior Minor Direction", 0);
                flag &= ParameterUtil.SetParaInt(m_areaRein, "Interior Minor Direction", 0);
            }

            return flag;
        }

        /// <summary>
        /// remove the hooks from one boundary curve of the Major Direction Layer 
        /// or Exterior Direction Layer
        /// </summary>
        /// <returns>whether the command is successful</returns>
        private bool ChangeHookType()
        {
            //find two vertical AreaReinforcementCurve
            Line line0 = m_areaReinCurves[0].Curve as Line;
            Line line1 = m_areaReinCurves[1].Curve as Line;
            Line line2 = m_areaReinCurves[2].Curve as Line;
            AreaReinforcementCurve temp = null;
            if (GeomUtil.IsVertical(line0, line1))
            {
                temp = m_areaReinCurves[1];
            }
            else
            {
                temp = m_areaReinCurves[2];
            }

            //remove hooks
            ParameterUtil.SetParaInt(m_areaReinCurves[0],
                BuiltInParameter.REBAR_SYSTEM_OVERRIDE, -1);
            Parameter para = m_areaReinCurves[0].get_Parameter(
                BuiltInParameter.REBAR_SYSTEM_HOOK_TYPE_TOP_DIR_1);
            bool flag = ParameterUtil.SetParaNullId(para);

            ParameterUtil.SetParaInt(temp, BuiltInParameter.REBAR_SYSTEM_OVERRIDE, -1);
            para = temp.get_Parameter(
                BuiltInParameter.REBAR_SYSTEM_HOOK_TYPE_TOP_DIR_1);
            flag &= ParameterUtil.SetParaNullId(para);

            return flag;
        }
    }
}

GeomUtil.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.AreaReinCurve.CS
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    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>
    /// provide some common geometry judgement and calculate method
    /// </summary>
    class GeomUtil
    {
        const double PRECISION = 0.00001;    //precision when judge whether two doubles are equal

        /// <summary>
        /// judge whether given 4 lines can form a rectangular
        /// </summary>
        /// <param name="lines"></param>
        /// <returns>is rectangular</returns>
        /// <summary>
        /// judge whether given 4 lines can form a rectangular
        /// </summary>
        /// <param name="lines"></param>
        /// <returns>is rectangular</returns>
        public static bool IsRectangular(CurveArray curves)
        {
            if (curves.Size != 4)
            {
                return false;
            }

            Line[] lines = new Line[4];
            for (int i = 0; i < 4; i++)
            {
                lines[i] = curves.get_Item(i) as Line;
                if (null == lines[i])
                {
                    return false;
                }
            }

            Line iniLine = lines[0];
            Line[] verticalLines = new Line[2];
            Line paraLine = null;
            int index = 0;
            for (int i = 1; i < 4; i++)
            {
                if (IsVertical(lines[0], lines[i]))
                {
                    verticalLines[index] = lines[i];
                    index++;
                }
                else
                {
                    paraLine = lines[i];
                }
            }
            if (index != 2)
            {
                return false;
            }
            bool flag = IsVertical(paraLine, verticalLines[0]);
            return flag;
        }

        /// <summary>
        /// judge whether two lines are vertical
        /// </summary>
        /// <param name="line1"></param>
        /// <param name="line2"></param>
        /// <returns></returns>
        public static bool IsVertical(Line line1, Line line2)
        {
            Autodesk.Revit.DB.XYZ  vector1 = SubXYZ(line1.GetEndPoint(0), line1.GetEndPoint(1));
            Autodesk.Revit.DB.XYZ  vector2 = SubXYZ(line2.GetEndPoint(0), line2.GetEndPoint(1));

            double result = DotMatrix(vector1, vector2);

            if (Math.Abs(result) < PRECISION)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// subtraction of two Autodesk.Revit.DB.XYZ as Matrix
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static Autodesk.Revit.DB.XYZ  SubXYZ (Autodesk.Revit.DB.XYZ  p1, Autodesk.Revit.DB.XYZ  p2)
        {
            double x = p1.X - p2.X;
            double y = p1.Y - p2.Y;
            double z = p1.Z - p2.Z;

            Autodesk.Revit.DB.XYZ  result = new Autodesk.Revit.DB.XYZ (x, y, z);
            return result;
        }

        /// <summary>
        /// dot product of two Autodesk.Revit.DB.XYZ as Matrix
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static double DotMatrix(Autodesk.Revit.DB.XYZ  p1, Autodesk.Revit.DB.XYZ  p2)
        {
            double v1 = p1.X;
            double v2 = p1.Y;
            double v3 = p1.Z;

            double u1 = p2.X;
            double u2 = p2.Y;
            double u3 = p2.Z;

            double result = v1 * u1 + v2 * u2 + v3 * u3;

            return result;
        }

        /// <summary>
        /// judge whether the subtraction of two doubles is less than the internal decided precision
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        private static bool IsEqual(double d1, double d2)
        {
            double diff = Math.Abs(d1 - d2);
            if (diff < PRECISION)
            {
                return true;
            }
            return false;
        }
    }
}

ParameterUtil.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.AreaReinCurve.CS
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

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

    /// <summary>
    /// contain utility methods find or set certain parameter
    /// </summary>
    public class ParameterUtil
    {
        /// <summary>
        /// find certain parameter in a set
        /// </summary>
        /// <param name="paras"></param>
        /// <param name="name">find by name</param>
        /// <returns>found parameter</returns>
        public static bool SetParaInt(Element elem, string paraName, int value)
        {
            ParameterSet paras = elem.Parameters;
            Parameter findPara = FindParaByName(paras, paraName);

            if (null == findPara)
            {
                return false;
            }

            if (!findPara.IsReadOnly)
            {
                findPara.Set(value);
                return true;
            }

            return false;
        }

        /// <summary>
        /// find certain parameter in a set
        /// </summary>
        /// <param name="paras"></param>
        /// <param name="name">find by name</param>
        /// <returns>found parameter</returns>
        public static Parameter FindParaByName(ParameterSet paras, string name)
        {
            Parameter findPara = null;

            foreach (Parameter para in paras)
            {
                if (para.Definition.Name == name)
                {
                    findPara = para;
                }
            }

            return findPara;
        }

        /// <summary>
        /// set certain parameter of given element to int value
        /// </summary>
        /// <param name="elem">given element</param>
        /// <param name="paraIndex">BuiltInParameter</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetParaInt(Element elem, BuiltInParameter paraIndex, int value)
        {
            Parameter para = elem.get_Parameter(paraIndex);
            if (null == para)
            {
                return false;
            }

            if (!para.IsReadOnly)
            {
                para.Set(value);
                return true;
            }

            return false;
        }

        /// <summary>
        /// set certain parameter of given element to int value
        /// </summary>
        /// <param name="elem">given element</param>
        /// <param name="paraIndex">BuiltInParameter</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetParaNullId(Parameter para)
        {
            Autodesk.Revit.DB.ElementId id = new ElementId(-1);

            if (!para.IsReadOnly)
            {
                para.Set(id);
                return true;
            }
            return false;
        }
    }
}