结构柱配筋

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)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Application application = uidoc.Application.Application;
            //获得当前视图
            View3D view3D = doc.ActiveView as View3D;
            //获得结构柱,此处忽略用户取消选择的处理
            Reference reference = uidoc.Selection.PickObject(ObjectType.Element, "选择结构柱");
            //获得结构柱的相关参数,并保存在结构柱中
            FamilyInstance column = doc.GetElement(reference) as FamilyInstance;
            //获得结构柱、b、h、高、transform、控制线、底层中心点等
            //通过柱原始几何包围框求b/h
            Options options = application.Create.NewGeometryOptions();
            GeometryElement geometry = column.GetOriginalGeometry(options);
            BoundingBoxXYZ boundingBoxXYZ = geometry.GetBoundingBox();
            ColumnInfo columnInfo = new ColumnInfo();
            columnInfo.ele = column;
            columnInfo.curve = column.GetAnalyticalModel().GetCurve();
            columnInfo.b = boundingBoxXYZ.Max.X - boundingBoxXYZ.Min.X;
            columnInfo.h = boundingBoxXYZ.Max.Y - boundingBoxXYZ.Min.Y;
            columnInfo.height = columnInfo.curve.Length;
            columnInfo.tf = column.GetTransform();
            columnInfo.protect = 25 / 304.8;
            if (columnInfo.curve.GetEndPoint(0).Z > columnInfo.curve.GetEndPoint(1).Z)
                columnInfo.origin = columnInfo.curve.GetEndPoint(1);
            else
                columnInfo.origin = columnInfo.curve.GetEndPoint(0);
            //外箍筋类型,案例不考虑内箍筋
            FilteredElementCollector rbTypeColGJ = new FilteredElementCollector(doc);
            rbTypeColGJ.OfClass(typeof(RebarBarType));
            IEnumerable<RebarBarType> rbTypesGJ = from ele in rbTypeColGJ
                                                  let barType = ele as RebarBarType
                                                  where barType.Name == "10 HRB400"
                                                  select barType;
            RebarBarType rbTypeGJ = rbTypesGJ.First();
            //外箍筋形状
            FilteredElementCollector rShapesColGJ = new FilteredElementCollector(doc);
            rShapesColGJ.OfClass(typeof(RebarShape));
            IEnumerable<RebarShape> rShapesGJ = from ele in rShapesColGJ
                                                let barshape = ele as RebarShape
                                                where barshape.Name == "33"
                                                select barshape;
            RebarShape rShapeGJ = rShapesGJ.First();

            //当此文档可能没有载入所需的钢筋形状或钢筋类型,应加以规避处理
            //以此为例
            if (rShapesGJ.Count() == 0)
            {
                TaskDialog.Show("提示", "请先加载33号钢筋形状");
                return Result.Succeeded;
            }
            RebarShape rebarShape = rShapesGJ.First();
            //角筋类型
            FilteredElementCollector rbtyprColJJ = new FilteredElementCollector(doc);
            rbtyprColJJ.OfClass(typeof(RebarBarType));
            IEnumerable<RebarBarType> rbTypesJJ = from ele in rbtyprColJJ
                                                  let barType = ele as RebarBarType
                                                  where barType.Name == "28 HRB400"
                                                  select barType;
            RebarBarType rTypeJJ = rbTypesJJ.First();
            //角筋、中部筋形状,案例不考虑钢筋弯钩
            FilteredElementCollector rShapeCol01 = new FilteredElementCollector(doc);
            rShapeCol01.OfClass(typeof(RebarShape));
            IEnumerable<RebarShape> rShapes01 = from ele in rShapeCol01
                                                let barshape = ele as RebarShape
                                                where barshape.Name == "01"
                                                select barshape;
            RebarShape rShape01 = rShapes01.First();

            //中部筋类型
            FilteredElementCollector rbTypeZ = new FilteredElementCollector(doc);
            rbTypeZ.OfClass(typeof(RebarBarType));
            IEnumerable<RebarBarType> rtypesZ = from ele in rbTypeZ
                                                let barshape = ele as RebarBarType
                                                where barshape.Name == "20 HRB400"
                                                select barshape;
            RebarBarType rtypeZ = rtypesZ.First();

            //创建并启动事务
            Transaction transaction = new Transaction(doc, "结构柱钢筋");
            transaction.Start();
            //关键步骤1:创建结构柱箍筋
            CreateGJ(doc, columnInfo, view3D, rbTypeGJ, rShapeGJ);
            //关键步骤2:创建角筋
            CreateJJ(doc, columnInfo, view3D, rTypeJJ, rShape01, rbTypeGJ);
            //关键步骤3:创建中部筋
            CreateZb(doc, columnInfo, view3D, rtypeZ, rShape01, rbTypeGJ);
            //提交事务
            transaction.Commit();
            return Result.Succeeded;
        }
        public struct ColumnInfo
        {
            public Element ele; //结构柱
            public Curve curve;//结构柱控制线
            public Transform tf;//结构柱坐标系
            public double protect;//保护层厚度
            public XYZ origin;//柱底中心坐标
            public double b;//柱b长
            public double h;//柱h长
            public double height;//柱高
        }
        /// <summary>
        /// 外箍筋创建
        /// </summary>
        /// <param name="doc">doc</param>
        /// <param name="cInfo">结构柱参数结构体</param>
        /// <param name="view3D">3D视图</param>
        /// <param name="rbtype">钢筋类型</param>
        /// <param name="rShape">钢筋形状</param>
        public void CreateGJ(Document doc, ColumnInfo cInfo, View3D view3D, RebarBarType rbtype, RebarShape rShape)
        {
            //箍筋排布间距,案例以200mm为例
            double space = 200 / 304.8;
            //外箍筋数量
            int num = (int)(cInfo.height / space);
            //箍筋中心离柱边距离,保护层以25mm为例,注意包含箍筋半径
            double ct = cInfo.protect + rbtype.BarDiameter / 2;
            Element ele = cInfo.ele;
            for (int i = 0; i < num; i++)
            {
                double depth = ct + i * space;//外箍筋位置
                //结构柱底部中心点
                XYZ origin = cInfo.tf.Inverse.OfPoint(cInfo.origin);
                //获得箍筋控制点
                XYZ delta1 = new XYZ(-cInfo.b / 2 + ct, -cInfo.h / 2 + ct, depth);
                XYZ delta2 = new XYZ(-cInfo.b / 2 + ct, cInfo.h / 2 - ct, depth);
                XYZ delta3 = new XYZ(cInfo.b / 2 - ct, cInfo.h / 2 - ct, depth);
                XYZ delta4 = new XYZ(cInfo.b / 2 - ct, -cInfo.h / 2 + ct, depth);
                XYZ p1 = cInfo.tf.OfPoint(origin + delta1);
                XYZ p2 = cInfo.tf.OfPoint(origin + delta2);
                XYZ p3 = cInfo.tf.OfPoint(origin + delta3);
                XYZ p4 = cInfo.tf.OfPoint(origin + delta4);
                //钢筋形态放置方向,以p1为放置点
                XYZ xVec = (p2 - p1);
                XYZ yVec = (p4 - p1);
                //创建钢筋
                Rebar rb = Rebar.CreateFromRebarShape(doc, rShape, rbtype, ele, p1, xVec, yVec);
                //设置钢筋在范围框内
                rb.GetShapeDrivenAccessor().ScaleToBox(p1, xVec, yVec);
                //作为实体查看,false时钢筋以线的形式表示
                rb.SetSolidInView(view3D, true);
                //清晰的视图设置
                rb.SetUnobscuredInView(view3D, true);
            }
        }
        /// <summary>
        /// 创建角筋
        /// </summary>
        /// <param name="doc">doc</param>
        /// <param name="cInfo">参数结构体</param>
        /// <param name="view3D">视图</param>
        /// <param name="rt">钢筋类型</param>
        /// <param name="rS">钢筋形状</param>
        /// <param name="rTG">钢筋类型</param>
        public void CreateJJ(Document doc, ColumnInfo cInfo, View3D view3D, RebarBarType rt, RebarShape rS, RebarBarType rTG)
        {
            Element ele = cInfo.ele;
            //角筋中心里柱边的距离
            double depth = rTG.BarDiameter + cInfo.protect + rt.BarDiameter / 2;
            //结构柱定位点通过ofpoint转成结构柱transform的坐标,否则钢筋不在主体内
            XYZ origin = cInfo.tf.Inverse.OfPoint(cInfo.origin);
            //获得四根角筋的控制点
            double deltaY = depth - cInfo.h / 2;
            XYZ p1 = origin + new XYZ(-cInfo.b / 2 + depth, deltaY, 0);
            XYZ p11 = origin + new XYZ(-cInfo.b / 2 + depth, deltaY, cInfo.height);
            XYZ p2 = origin + new XYZ(cInfo.b / 2 - depth, deltaY, 0);
            XYZ p22 = origin + new XYZ(cInfo.b / 2 - depth, deltaY, cInfo.height);
            XYZ p3 = origin + new XYZ(cInfo.b / 2 - depth, -deltaY, 0);
            XYZ p33 = origin + new XYZ(cInfo.b / 2 - depth, -deltaY, cInfo.height);
            XYZ p4 = origin + new XYZ(-cInfo.b / 2 + depth, -deltaY, 0);
            XYZ p44 = origin + new XYZ(-cInfo.b / 2 + depth, -deltaY, cInfo.height);
            //保存创建的钢筋,用于设置显式
            List<Rebar> rbs = new List<Rebar>();
            //角筋控制线通过ofpoint转成结构柱transformd的坐标,否则钢筋不在主体内
            IList<Curve> c1 = new List<Curve>()
            {
                Line.CreateBound(cInfo.tf.OfPoint(p1),cInfo.tf.OfPoint(p11))
            };
            IList<Curve> c2 = new List<Curve>()
            {
                Line.CreateBound(cInfo.tf.OfPoint(p2),cInfo.tf.OfPoint(p22))
            };
            IList<Curve> c3 = new List<Curve>()
            {
                Line.CreateBound(cInfo.tf.OfPoint(p3),cInfo.tf.OfPoint(p33))
            };
            IList<Curve> c4 = new List<Curve>()
            {
                Line.CreateBound(cInfo.tf.OfPoint(p4),cInfo.tf.OfPoint(p44))
            };
            //角筋弯钩方向和放置方向,没有弯钩时不影响结果
            RebarHookOrientation rho = RebarHookOrientation.Left;
            XYZ norm = cInfo.tf.BasisX;

            //创建角筋
            rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rS, rt, null, null, ele, norm, c1, rho, rho));
            rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rS, rt, null, null, ele, norm, c2, rho, rho));
            rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rS, rt, null, null, ele, norm, c3, rho, rho));
            rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rS, rt, null, null, ele, norm, c4, rho, rho));
            foreach (Rebar rb in rbs)
            {
                //作为实体查看,false时钢筋以线的形式表现
                rb.SetSolidInView(view3D, true);
                //清晰的视图设置
                rb.SetUnobscuredInView(view3D, true);
            }
        }
        public void CreateZb(Document doc, ColumnInfo cInfo, View3D view3D, RebarBarType rt, RebarShape rs, RebarBarType rTG)
        {
            //获得结构柱的b、h、高和transform
            Element ele = cInfo.ele;
            double b = cInfo.b;
            double h = cInfo.h;
            double heigh = cInfo.height;
            Transform tf = cInfo.tf;
            //结构柱定位点通过ofpoint转成结构柱transform的坐标,否则钢筋不在主体内
            XYZ origin = cInfo.tf.Inverse.OfPoint(cInfo.origin);
            XYZ norm = cInfo.tf.BasisX;
            //纵筋区域起始位置离柱边距离
            double depth = cInfo.protect + rTG.BarDiameter + rt.BarDiameter / 2;
            //获得b纵筋间距,案例以3条中部筋为例
            double spaceB = (cInfo.b - cInfo.protect * 2 - rt.BarDiameter - rTG.BarDiameter * 2) / 4;
            double spaceH = (cInfo.h - cInfo.protect * 2 - rt.BarDiameter - rTG.BarDiameter * 2) / 4;
            //B侧,案例以3条中部筋为例
            for (int i = 0; i < 3; i++)
            {
                //放置点位置
                double set = (i + 1) * spaceB + depth;
                //b上侧
                XYZ p1 = tf.OfPoint(origin + new XYZ(-b / 2 + set, h / 2 - depth, 0));
                XYZ p11 = tf.OfPoint(origin + new XYZ(-b / 2 + set, h / 2 - depth, heigh));
                //b下侧
                XYZ p2 = tf.OfPoint(origin + new XYZ(-b / 2 + set, -h / 2 +depth, 0));
                XYZ p22 = tf.OfPoint(origin + new XYZ(-b / 2 + set, -h / 2 + depth, heigh));
                //保存创建的钢筋用于显示
                List<Rebar> rbs = new List<Rebar>();
                //获得中部筋的控制线
                IList<Curve> c1 = new List<Curve>();
                IList<Curve> c2 = new List<Curve>();
                c1.Add(Line.CreateBound(p1, p11));
                c2.Add(Line.CreateBound(p2, p22));
                //角筋弯钩方向和放置方向,没有弯钩时不影响结果
                RebarHookOrientation rho = RebarHookOrientation.Left;
                //创建角筋
                rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rs, rt, null, null, ele, norm, c1, rho, rho));
                rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rs, rt, null, null, ele, norm, c2, rho, rho));
                foreach (Rebar rb in rbs)
                {
                    //作为实体查看,false时钢筋以线的形式表示
                    rb.SetSolidInView(view3D, true);
                    //清晰的视图设置
                    rb.SetUnobscuredInView(view3D, true);
                }
            }
            //H侧,案例以3条中部筋为例
            for (int i = 0; i < 3; i++)
            {
                double set = (i + 1) * spaceH + depth;
                //左侧
                XYZ p1 = tf.OfPoint(origin + new XYZ(-b / 2 + depth, h / 2 - set, 0));
                XYZ p11 = tf.OfPoint(origin + new XYZ(-b / 2 + depth, h / 2 - set, heigh));
                //右侧
                XYZ p2 = tf.OfPoint(origin + new XYZ(b / 2 - depth, h / 2 - set, 0));
                XYZ p22 = tf.OfPoint(origin + new XYZ(b / 2 - depth, h / 2 - set, heigh));
                //保存创建的钢筋用于显示
                List<Rebar> rbs = new List<Rebar>();
                //获得中部筋的控制线
                IList<Curve> c1 = new List<Curve>();
                IList<Curve> c2 = new List<Curve>();
                c1.Add(Line.CreateBound(p1, p11));
                c2.Add(Line.CreateBound(p2, p22));
                //角筋弯钩方向和放置方向,没有弯钩时不影响结果
                RebarHookOrientation rho = RebarHookOrientation.Left;
                //创建角筋
                rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rs, rt, null, null, ele, norm, c1, rho, rho));
                rbs.Add(Rebar.CreateFromCurvesAndShape(doc, rs, rt, null, null, ele, norm, c2, rho, rho));
                foreach (Rebar rb in rbs)
                {
                    //作为实体查看,false时钢筋以线的形式表示
                    rb.SetSolidInView(view3D, true);
                    //清晰的视图设置
                    rb.SetUnobscuredInView(view3D, true);
                }
            }
        }
    }
}


猜你喜欢

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