Revit二次开发 钢筋生成API(二)

2、自由钢筋生成API

创建一条无约束的自由形状钢筋。之后无法对该钢筋添加约束。

public static Rebar CreateFreeForm(
	Document doc,
	RebarBarType barType,
	Element host,
	IList<IList<Curve>> curves,
	out RebarFreeFormValidationResult error
)

这个合自由钢筋生成API(一)本质上没有区别,唯一的区别就是参数

IList<CurveLoop> curves 和IList<IList<Curve>> curves的不同。

这个就要看CurveLoop和IList<Curve>的区别。

  • CurveLoop:曲线循环的集合,通常用于定义建筑模型的几何边界或路径,默认会把首位相连
  • IList<Curve>:可以定义一个有多个线段组成的整线,比较适合各种形状的钢筋。

此函数的案例如下:

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;

namespace RebarExample.Cmds
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class CreateFreeForm3 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                Document doc = commandData.Application.ActiveUIDocument.Document;
                if (doc == null)
                    return Result.Failed;

                //查找到钢筋类型
                FilteredElementCollector fec = new FilteredElementCollector(doc).OfClass(typeof(RebarBarType));
                if (fec.GetElementCount() <= 0)
                    return Result.Failed;
                RebarBarType barType = fec.FirstElement() as RebarBarType;

                //启动事务
                using (Transaction tran = new Transaction(doc, "CreateFreeForm2"))
                {
                    Element host = null;
                    Selection sel = commandData.Application.ActiveUIDocument.Selection;
                    try
                    {
                        //选择主体对象
                        Reference hostRef = sel.PickObject(ObjectType.Element, "Select Host");
                        host = doc.GetElement(hostRef.ElementId);
                        if (host == null)
                            return Result.Failed;
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                        return Result.Failed;
                    }

                    tran.Start();

                    this.CreateFreeForm(doc, barType, host);


                    tran.Commit();
                }
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }


        /// <summary>
        /// 创建钢筋
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="barType"></param>
        /// <param name="host"></param>
        private void CreateFreeForm(Document doc, RebarBarType barType, Element host)
        {
            IList<IList<Curve>> curves = new List<IList<Curve>>();
            if (host is Wall wall)
            {
                //创建钢筋
                var wline = (wall.Location as LocationCurve).Curve;
                if (wline != null)
                {
              
                    for (var i = 0; i < 5; i++)
                    {
                        List<Curve> ps = new List<Curve>();
                        var start = wline.GetEndPoint(0);
                        var end = wline.GetEndPoint(1);
                        var v1 = new XYZ(start.X+0.5, start.Y, start.Z + i * 0.1);
                        var v2 = new XYZ(end.X-0.5, end.Y, end.Z + i * 0.1);
                        Curve l1 = Line.CreateBound(v1, v2);
                        var v3 = new XYZ(end.X-0.5, end.Y + 0.5, end.Z + i * 0.1);
                        Curve l2 = Line.CreateBound(v2, v3);
                        ps.Add(l1);
                        ps.Add(l2);
                        curves.Add(ps);
                    }
                }

            }

            RebarFreeFormValidationResult error;
            Rebar rebar = Rebar.CreateFreeForm(doc, barType, host, curves, out error);
        }
    }
}


生成结果展示如下:

image

 

posted @ 2025-09-17 17:04  Min.Xiaoshuang  阅读(12)  评论(0)    收藏  举报