天天看点

在revit柱中添加钢筋

在柱中添加钢筋 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.ApplicationServices;

using Autodesk.Revit.DB.Structure;

namespace 在柱中添加钢筋

{

    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]

    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]

    public class Class1 : IExternalCommand

    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

        {

            Document revitDoc = commandData.Application.ActiveUIDocument.Document;  //取得文档

            Application revitApp = commandData.Application.Application;             //取得应用程序

            UIDocument uiDoc = commandData.Application.ActiveUIDocument;            //取得当前活动文档

            Selection sel = uiDoc.Selection;

            Reference ref1 = sel.PickObject(ObjectType.Element, "选择一根柱子");

            FamilyInstance column = revitDoc.GetElement(ref1) as FamilyInstance;

            if (column==null)

            {

                TaskDialog.Show("错误1", "没有选择柱子");

            }

            RebarBarType barType = revitDoc.GetElement(new ElementId(195080)) as RebarBarType;

            RebarHookType hookType = revitDoc.GetElement(new ElementId(195034)) as RebarHookType;

            using (Transaction transaction = new Transaction(revitDoc))

            {

                transaction.Start("在柱中创建一根钢筋");

                //在下面添加主要代码内容

                Rebar newRebar = CreateRebar(revitDoc, column, barType, hookType);

                //限制钢筋的范围可以用newRebar.ScaleToBox()来表示

                transaction.Commit();

            }

            return Result.Succeeded;

        }

        Rebar CreateRebar(Autodesk.Revit.DB.Document document, FamilyInstance column, RebarBarType barType, RebarHookType hookType)

        {

            // Define the rebar geometry information - Line rebar

            LocationPoint location = column.Location as LocationPoint;

            XYZ origin = location.Point;

            XYZ normal = new XYZ(1, 0, 0);

            // create rebar 9' long

            XYZ rebarLineEnd = new XYZ(origin.X, origin.Y, origin.Z + 9);

            Line rebarLine = Line.CreateBound(origin, rebarLineEnd);

            // Create the line rebar

            IList<Curve> curves = new List<Curve>();

            curves.Add(rebarLine);

            Rebar rebar = Rebar.CreateFromCurves(document, Autodesk.Revit.DB.Structure.RebarStyle.Standard, barType, hookType, hookType,

                                column, origin, curves, RebarHookOrientation.Right, RebarHookOrientation.Left, true, true);

            if (null != rebar)

            {

                // set specific layout for new rebar as fixed number, with 10 bars, distribution path length of 1.5'

                // with bars of the bar set on the same side of the rebar plane as indicated by normal

                // and both first and last bar in the set are shown

                rebar.SetLayoutAsFixedNumber(10, 1.5, true, true, true);

            }

            return rebar;

        }

    }

}

继续阅读