梁配置箍筋(不考虑加密区)

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 创建梁箍筋
{
    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Application application = commandData.Application.Application;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //获得当前视图,此处忽略当前视图不是3D视图的处理
            View3D view3D = doc.ActiveView as View3D;

            //选择梁
            Reference reference = uidoc.Selection.PickObject(ObjectType.Element, "选择梁");
            Element element = doc.GetElement(reference);
            FamilyInstance beam = element as FamilyInstance;
            //获取梁控制线
            Curve beamCurve = (beam.Location as LocationCurve).Curve;
            //获取梁原始集合包围框
            Options options = application.Create.NewGeometryOptions();
            GeometryElement geoEle = beam.GetOriginalGeometry(options);
            BoundingBoxXYZ boundingBox = geoEle.GetBoundingBox();
            //获得梁的长、宽、高,由于梁是可载入族,用b、h等参数不可靠
            //建议通过包围框获得梁的宽和高
            double beamLength = beamCurve.Length;
            double width = boundingBox.Max.Y - boundingBox.Min.Y;
            double heigth = boundingBox.Max.Z - boundingBox.Min.Z;
            //箍筋排布间距,案例以200mm为例
            double space = 200 / 304.8;
            //获得梁的坐标系
            Transform tf = beam.GetTransform();
            //获得钢筋的类型。以10HRB400为例
            FilteredElementCollector rbTypeCol = new FilteredElementCollector(doc);
            rbTypeCol.OfClass(typeof(RebarBarType));
            IEnumerable<RebarBarType> rTypes = from elem in rbTypeCol
                                               let r = elem as RebarBarType
                                               where r.Name == "10 HRB400"
                                               select r;
            RebarBarType type = rTypes.First();
            //钢筋弯钩,以标准-135°为例
            FilteredElementCollector rhTypeCol = new FilteredElementCollector(doc);
            rhTypeCol.OfClass(typeof(RebarHookType));
            IEnumerable<RebarHookType> rhTypes;
            rhTypes = from ele in rhTypeCol
                      let r = ele as RebarHookType
                      where r.Name== "标准 - 135 度"
                      select r;
            RebarHookType rhType = rhTypes.First();
            //平面方向
            XYZ normal = tf.BasisX;
            //创建并启动事务
            Transaction transaction = new Transaction(doc, "钢筋");
            transaction.Start();
            //箍筋中心里梁边距离,保护层以25mm为例,注意包含箍筋的半径
            double depth = 25 / 304.8 + type.BarDiameter / 2;
            //本案例忽略钢筋加密区
            int num = (int)Math.Ceiling(beamLength / space);
            for (int i = 1; i < num; i++)
            {
                //每个箍筋放置位置
                double setSpace = -beamLength / 2 + (i - 1) * space;
                //获得箍筋控制点
                XYZ p1, p2, p3, p4;
                p1 = tf.OfPoint(new XYZ(setSpace, width / 2 - depth, heigth / 2 - depth));
                p2 = tf.OfPoint(new XYZ(setSpace, width / 2 - depth, -heigth / 2 + depth));
                p3 = tf.OfPoint(new XYZ(setSpace, -width / 2 + depth, -heigth / 2 + depth));
                p4 = tf.OfPoint(new XYZ(setSpace, -width / 2 + depth, heigth / 2 - depth));
                //按顺序收尾连接曲线
                IList<Curve> curves = new List<Curve>();
                curves.Add(Line.CreateBound(p1, p2));
                curves.Add(Line.CreateBound(p2, p3));
                curves.Add(Line.CreateBound(p3, p4));
                curves.Add(Line.CreateBound(p4, p1));
                //创建钢筋,该方法会创建很多钢筋形状
                Rebar rb = Rebar.CreateFromCurves(doc, RebarStyle.Standard, type, rhType, rhType, beam, normal, curves, RebarHookOrientation.Right, RebarHookOrientation.Right, true, true);
                //作为实体查看,false时钢筋以线的形式表示
                rb.SetSolidInView(view3D, true);
            }
            transaction.Commit();
            return Result.Succeeded;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/waiting233/article/details/116604102
今日推荐