应用程序:CreateAirHandler

Revit平台:MEP

Revit版本:2011.0

首次发布用于:2010.0

编程语言:C#

技能等级:中等

类别:家族

类型:外部命令

主题:创建一个空气处理机械设备族并添加连接器。

摘要:此示例创建一个空气处理程序并添加接头(管道、风管)。

相关类:

Autodesk.Revit.UI.IExternalCommand Autodesk.Rovit.UI.UID文档Autodesk.Rivit.Creation.FamilyItemFactory Autodesk.Revit.DB.Extraction Autodesk.REVt.DB.Mechanical.DuctionConnector Autodesk.Revt.DB.Pumping.Pipe Connector Autodesk.Devit.DB.CombinableElement

项目文件:

命令.cs

该文件包含从IExternalCommand继承的类Command,并实现Execute方法来创建一些拉伸并在其上添加连接器。

功能:

此示例首先使用OwnerFamily.FamilyCategory属性来检查模板。

然后,此示例使用Autodesk.Revit.Creation.FamilyItemFactory类中的NewExtrusion、NewPipeConnector、NewDuctConnector来创建处理程序和连接器,并使用xxxDuct属性来设置连接器的正确参数。

最后,此示例使用Document::CombineElements方法将挤出件连接到一个空气处理器中。

实施:

此外部命令可以在Revit族上运行。

1.通过以下行更新您的Revit.ini:

[ExternalCommands]ECCount=1 ECName1=创建空气处理器ECClassName1=Revit.SDK.Samples.CreateAirHandler.CS.Command ECAssembly1=<your-path>\CreateAirHandler.dll ECDescription1=创建一个空气处理器并添加连接器。

2.使用“Mechanical Equipment”模板文件创建Revit机械设备族。

3.运行此外部命令,空气处理族即被创建。

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

Command.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 Autodesk.Revit;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;

namespace Revit.SDK.Samples.CreateAirHandler.CS
{
   /// <summary>
   /// Create one air handler and add connectors.
   /// </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
   {
      /// <summary>
      /// The revit application
      /// </summary>
      private static Autodesk.Revit.ApplicationServices.Application m_application;

      /// <summary>
      /// The current document of the application
      /// </summary>
      private static Autodesk.Revit.DB.Document m_document;

      /// <summary>
      /// the factory to creaate extrusions and connectors
      /// </summary>
      private FamilyItemFactory f;

      /// <summary>
      /// the extrusion array
      /// </summary>
      private Extrusion[] extrusions;

      /// <summary>
      /// The list of all the elements to be combined in the air handler system
      /// </summary>
      private CombinableElementArray m_combineElements;

      #region Data used to create extrusions and connectors
      /// <summary>
      /// Data to create extrusions and connectors
      /// </summary>
      private Autodesk.Revit.DB.XYZ[,] profileData = new Autodesk.Revit.DB.XYZ[5, 4]
        {
            // In Array 0 to 2, the data is the points that defines the edges of the profile
            {
                new Autodesk.Revit.DB.XYZ (-17.28, -0.53, 0.9),
                new Autodesk.Revit.DB.XYZ (-17.28, 11, 0.9),
                new Autodesk.Revit.DB.XYZ (-0.57, 11, 0.9),
                new Autodesk.Revit.DB.XYZ (-0.57, -0.53, 0.9)
            },
            {
                new Autodesk.Revit.DB.XYZ (-0.57, 7, 6.58),
                new Autodesk.Revit.DB.XYZ (-0.57, 7, 3),
                new Autodesk.Revit.DB.XYZ (-0.57, 3.6, 3),
                new Autodesk.Revit.DB.XYZ (-0.57, 3.6, 6.58)
            },
            {
                new Autodesk.Revit.DB.XYZ (-17.28, -0.073, 7.17),
                new Autodesk.Revit.DB.XYZ (-17.28, 10.76, 7.17),
                new Autodesk.Revit.DB.XYZ (-17.28, 10.76, 3.58),
                new Autodesk.Revit.DB.XYZ (-17.28, -0.073, 3.58)
            },
            // In Array 3 and 4, the data is the normal and origin of the plane of the arc profile
            {
                new Autodesk.Revit.DB.XYZ (0, -1, 0),
                new Autodesk.Revit.DB.XYZ (-9, 0.53, 7.17),
                null,
                null
            },
            {
                new Autodesk.Revit.DB.XYZ (0, -1, 0),
                new Autodesk.Revit.DB.XYZ (-8.24, 0.53, 0.67),
                null,
                null
            }
        };

      /// <summary>
      /// the normal and origin of the sketch plane
      /// </summary>
      private Autodesk.Revit.DB.XYZ[,] sketchPlaneData = new Autodesk.Revit.DB.XYZ[5, 2]
        {
            {new Autodesk.Revit.DB.XYZ (0, 0, 1), new Autodesk.Revit.DB.XYZ (0, 0, 0.9)},
            {new Autodesk.Revit.DB.XYZ (1, 0, 0), new Autodesk.Revit.DB.XYZ (-0.57, 0, 0)},
            {new Autodesk.Revit.DB.XYZ (-1, 0, 0), new Autodesk.Revit.DB.XYZ (-17.28, 0, 0)},
            {new Autodesk.Revit.DB.XYZ (0, -1, 0), new Autodesk.Revit.DB.XYZ (0, 0.53, 0)},
            {new Autodesk.Revit.DB.XYZ (0, -1, 0), new Autodesk.Revit.DB.XYZ (0, 0.53, 0)}
        };

      /// <summary>
      /// the start and end offsets of the extrusion
      /// </summary>
      private double[,] extrusionOffsets = new double[5, 2]
        {
            {-0.9, 6.77},
            {0, -0.18},
            {0, -0.08},
            {1, 1.15},
            {1, 1.15}
        };

      /// <summary>
      /// whether the extrusion is solid
      /// </summary>
      private bool[] isSolid = new bool[5] { true, false, false, true, true };

      /// <summary>
      /// the radius of the arc profile
      /// </summary>
      private double arcRadius = 0.17;

      /// <summary>
      /// the height and width of the connector
      /// </summary>
      private double[,] connectorDimensions = new double[2, 2]
        {
            {3.58, 3.4},
            {3.59, 10.833}
        };

      /// <summary>
      /// the flow of the connector
      /// </summary>
      private double flow = 547;

      /// <summary>
      /// Transaction of ExternalCommand
      /// </summary>
      private Transaction m_transaction;
      #endregion

      #region IExternalCommand Members
      /// <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,
          ElementSet elements)
      {
         // set out default result to failure.
         Autodesk.Revit.UI.Result retRes = Autodesk.Revit.UI.Result.Failed;
         m_application = commandData.Application.Application;
         m_document = commandData.Application.ActiveUIDocument.Document;
         f = m_document.FamilyCreate;
         extrusions = new Extrusion[5];
         m_combineElements = new CombinableElementArray();

         m_transaction = new Transaction(m_document, "External Tool");
         m_transaction.Start();

         if (m_document.OwnerFamily.FamilyCategory.Name !=
             m_document.Settings.Categories.get_Item(BuiltInCategory.OST_MechanicalEquipment).Name) // FamilyCategory.Name is not "Mechanical Equipment".
         {
            message = "Please make sure you opened a template of Mechanical Equipment.";
            return retRes;
         }

         try
         {
            CreateExtrusions();
            m_document.Regenerate();
            CreateConnectors();
            m_document.Regenerate();
            m_document.CombineElements(m_combineElements);
            m_document.Regenerate();
         }
         catch (Exception x)
         {
            m_transaction.RollBack();
            message = x.Message;
            return retRes;
         }

         m_transaction.Commit();

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

      /// <summary>
      /// get all planar faces of an extrusion
      /// </summary>
      /// <param name="extrusion">the extrusion to read</param>
      /// <returns>a list of all planar faces of the extrusion</returns>
      public List<PlanarFace> GetPlanarFaces(Extrusion extrusion)
      {
         // the option to get geometry elements
         Options m_geoOptions = m_application.Create.NewGeometryOptions();
         m_geoOptions.View = m_document.ActiveView;
         m_geoOptions.ComputeReferences = true;

         // get the planar faces
         List<PlanarFace> m_planarFaces = new List<PlanarFace>();
         Autodesk.Revit.DB.GeometryElement geoElement = extrusion.get_Geometry(m_geoOptions);
         //foreach (GeometryObject geoObject in geoElement.Objects)
         IEnumerator<GeometryObject> Objects = geoElement.GetEnumerator();
         while (Objects.MoveNext())
         {
            GeometryObject geoObject = Objects.Current;

            Solid geoSolid = geoObject as Solid;
            if (null == geoSolid)
            {
               continue;
            }
            foreach (Face geoFace in geoSolid.Faces)
            {
               if (geoFace is PlanarFace)
               {
                  m_planarFaces.Add(geoFace as PlanarFace);
               }
            }
         }
         return m_planarFaces;
      }

      /// <summary>
      /// create the extrusions of the air handler system
      /// </summary>
      private void CreateExtrusions()
      {
         Autodesk.Revit.Creation.Application app = m_application.Create;
         CurveArray curves = null;
         CurveArrArray profile = null;
         Plane plane = null;
         SketchPlane sketchPlane = null;

         #region Create the cuboid extrusions

         for (int i = 0; i <= 2; ++i)
         {
            // create the profile
            curves = app.NewCurveArray();
            curves.Append(Line.CreateBound(profileData[i, 0], profileData[i, 1]));
            curves.Append(Line.CreateBound(profileData[i, 1], profileData[i, 2]));
            curves.Append(Line.CreateBound(profileData[i, 2], profileData[i, 3]));
            curves.Append(Line.CreateBound(profileData[i, 3], profileData[i, 0]));
            profile = app.NewCurveArrArray();
            profile.Append(curves);

            // create the sketch plane
            plane = Plane.CreateByNormalAndOrigin(sketchPlaneData[i, 0], sketchPlaneData[i, 1]);
            sketchPlane = SketchPlane.Create(m_document, plane);

            // create the extrusion
            extrusions[i] = f.NewExtrusion(isSolid[i], profile, sketchPlane,
                extrusionOffsets[i, 1]);
            extrusions[i].StartOffset = extrusionOffsets[i, 0];
            m_combineElements.Append(extrusions[i]);
         }

         #endregion

         #region Create the round extrusions

         for (int i = 3; i <= 4; ++i)
         {
            // create the profile
            profile = app.NewCurveArrArray();

            curves = app.NewCurveArray();
            plane = Plane.CreateByNormalAndOrigin(profileData[i, 0], profileData[i, 1]);
            curves.Append(Arc.Create(plane, arcRadius, 0, Math.PI * 2));
            profile.Append(curves);

                // create the sketch plane
            plane = Plane.CreateByNormalAndOrigin(sketchPlaneData[i, 0], sketchPlaneData[i, 1]);
            sketchPlane = SketchPlane.Create(m_document, plane);

            // create the extrusion
            extrusions[i] = f.NewExtrusion(isSolid[i], profile, sketchPlane,
                extrusionOffsets[i, 1]);
            extrusions[i].StartOffset = extrusionOffsets[i, 0];
            m_combineElements.Append(extrusions[i]);
         }

         #endregion
      }

      /// <summary>
      /// create the connectors on the extrusions
      /// </summary>
      private void CreateConnectors()
      {
         List<PlanarFace> m_planarFaces = null;
         Parameter param = null;

         #region Create the Supply Air duct connector

         // get the planar faces of extrusion1
         m_planarFaces = GetPlanarFaces(extrusions[1]);

         // create the Supply Air duct connector
         //DuctConnector connSupplyAir = f.NewDuctConnector(m_planarFaces[0].Reference,
         //    DuctSystemType.SupplyAir);
         ConnectorElement connSupplyAir = ConnectorElement.CreateDuctConnector(m_document, DuctSystemType.SupplyAir, ConnectorProfileType.Rectangular, m_planarFaces[0].Reference);
         param = connSupplyAir.get_Parameter(BuiltInParameter.CONNECTOR_HEIGHT);
         param.Set(connectorDimensions[0, 0]);
         param = connSupplyAir.get_Parameter(BuiltInParameter.CONNECTOR_WIDTH);
         param.Set(connectorDimensions[0, 1]);
         param = connSupplyAir.get_Parameter(BuiltInParameter.RBS_DUCT_FLOW_DIRECTION_PARAM);
         param.Set(2);
         param = connSupplyAir.get_Parameter(BuiltInParameter.RBS_DUCT_FLOW_CONFIGURATION_PARAM);
         param.Set(1);
         param = connSupplyAir.get_Parameter(BuiltInParameter.RBS_DUCT_FLOW_PARAM);
         param.Set(flow);

         #endregion

         #region Create the Return Air duct connector

         // get the planar faces of extrusion2
         m_planarFaces = GetPlanarFaces(extrusions[2]);

         // create the Return Air duct connector
         //DuctConnector connReturnAir = f.NewDuctConnector(m_planarFaces[0].Reference,
         //    DuctSystemType.ReturnAir);
         ConnectorElement connReturnAir = ConnectorElement.CreateDuctConnector(m_document, DuctSystemType.ReturnAir, ConnectorProfileType.Rectangular, m_planarFaces[0].Reference);
         param = connReturnAir.get_Parameter(BuiltInParameter.CONNECTOR_HEIGHT);
         param.Set(connectorDimensions[1, 0]);
         param = connReturnAir.get_Parameter(BuiltInParameter.CONNECTOR_WIDTH);
         param.Set(connectorDimensions[1, 1]);
         param = connReturnAir.get_Parameter(BuiltInParameter.RBS_DUCT_FLOW_DIRECTION_PARAM);
         param.Set(1);
         param =
             connReturnAir.get_Parameter(BuiltInParameter.RBS_DUCT_FLOW_CONFIGURATION_PARAM);
         param.Set(1);
         param = connReturnAir.get_Parameter(BuiltInParameter.RBS_DUCT_FLOW_PARAM);
         param.Set(flow);

         #endregion

         #region Create the Supply Hydronic pipe connector

         // get the planar faces of extrusion3
         m_planarFaces = GetPlanarFaces(extrusions[3]);

         // create the Hydronic Supply pipe connector
         //PipeConnector connSupplyHydronic = f.NewPipeConnector(m_planarFaces[0].Reference,
         //    PipeSystemType.SupplyHydronic);
         ConnectorElement connSupplyHydronic = ConnectorElement.CreatePipeConnector(m_document, PipeSystemType.SupplyHydronic, m_planarFaces[0].Reference);
         param = connSupplyHydronic.get_Parameter(BuiltInParameter.CONNECTOR_RADIUS);
         param.Set(arcRadius);
         param =
             connSupplyHydronic.get_Parameter(BuiltInParameter.RBS_PIPE_FLOW_DIRECTION_PARAM);
         param.Set(2);

         #endregion

         #region Create the Return Hydronic pipe connector

         // get the planar faces of extrusion4
         m_planarFaces = GetPlanarFaces(extrusions[4]);

         // create the Hydronic Return pipe connector
         //PipeConnector connReturnHydronic = f.NewPipeConnector(m_planarFaces[0].Reference,
         //    PipeSystemType.ReturnHydronic);
         ConnectorElement connReturnHydronic = ConnectorElement.CreatePipeConnector(m_document, PipeSystemType.ReturnHydronic, m_planarFaces[0].Reference);
         param = connReturnHydronic.get_Parameter(BuiltInParameter.CONNECTOR_RADIUS);
         param.Set(arcRadius);
         param =
             connReturnHydronic.get_Parameter(BuiltInParameter.RBS_PIPE_FLOW_DIRECTION_PARAM);
         param.Set(1);

         #endregion

      }
   }
}