Revit二次开发之创建房间,根据房间边界创建楼板等

以前就已经对创建房间有过了解,只是点到为止没有进一步的深究下去,今天有点时间就整理一下思路,留着以后备用。其实创建完房间后,可以算房间面积,空间体积什么的,也可以获得房间的边界线去做一些工作,比如生成楼板,墙踢脚线,墙饰条等等,其实原理都一样就是利用房间的闭合曲线!

 

首先来一个批量创建房间的代码,这里就只写步骤了,不拷贝整个工程了

 

  UIApplication uiApp = commandData.Application;

  UIDocument uidoc = uiApp.ActiveUIDocument;

  Document doc = uidoc.Document;

  ViewPlan view = uidoc.ActiveView as ViewPlan;

 Transaction ts = new Transaction(doc, "BIM");

            ts.Start();

         

                Level level = uidoc.ActiveView.GenLevel;

                PlanTopology pt = doc.get_PlanTopology(level);

                foreach (PlanCircuit pc in pt.Circuits)

                {

                    if (!pc.IsRoomLocated)

                    {

                        Room newRoom = doc.Create.NewRoom(null, pc);

                        LinkElementId elemid = new LinkElementId(newRoom.Id);

                        Location location = newRoom.Location;

                        LocationPoint locationPoint = location as LocationPoint;

                        XYZ point3d = locationPoint.Point;

                        UV point2d = new UV(point3d.X, point3d.Y);

                        RoomTag roomTag = doc.Create.NewRoomTag(elemid, point2d, view.Id);

                        if (family != null)

                        {

                            try

                            {

//这里的symbol其实是自定义的一个房间标记族,可以自行导入,这里就不再介绍了

                                FamilySymbol symbol = family.Document.GetElement(family.GetFamilySymbolIds().First()) as FamilySymbol;

                                if (symbol != null)

                                {

                                    roomTag.ChangeTypeId(symbol.Id);

                                }

                            }

                            catch { }

                        }

                    }

                }

 

                ts.Commit();

         

以上代码就是批量生成房间的代码段,接下来就是获得房间边界生成楼板,不过在此之前我们先看看每个房间边界的样子,我们可以根据Room.GetBoundarySegments()方法来获得 IList<>集合,根据这个集合去遍历所有的房间边界,在此之前我们还需要一个SpatialElementBoundaryOptions选项,它给我们提供了SpatialElementBoundaryLocation枚举类型,公有四个,分别是,Center,CoreBoundary,CoreCenter,finish,那么分别是什么意思呢,查阅一下RevitAPI帮助文档后得知:

1.Center,这里可以理解为墙的中心线

2.CoreBoundary,这里可以理解为墙的核心层边界线

3.CoreCenter,这里可以理解为墙的核心层中心线

4,.Finish.这里可以理解为墙的面层

上述翻译有可能不太准确,希望读者自行实验判断吧,接下来我们实测一下这四个选项,我的思路是根据房间的边界线生成详图线用以观察四个选项的区别,为了明显起见我想自定义一个线样式,宽度为1个单位,颜色为红色,主要代码如下:

 

 生成线样式

                using (Transaction ts = new Transaction(doc, "LineStyle"))

                {

                    ts.Start();

                    Category lineCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);

                    Category newCategory = doc.Settings.Categories.NewSubcategory(lineCategory, "房间边界线");

                    Color newColor = new Color(255, 0, 0);

                    newCategory.LineColor = newColor;

                    newCategory.SetLineWeight(1, GraphicsStyleType.Projection);

                    ts.Commit();

                }

 

生成之后的系统线样式下:

Revit二次开发之创建房间,根据房间边界创建楼板等
好了 那么接下来我们就可以过滤房间边界来看一看它们的样子:核心代码,记得开启事务

 

           //这里拾取一个房间,为了简便没有加过滤器限制

            Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "");

            Room room = doc.GetElement(refer) as Room;

            SpatialElementBoundaryOptions opt = new SpatialElementBoundaryOptions();

            opt.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Center;

           //声明的Curve集合是为创建楼板而服务的

            CurveArray array = new CurveArray();

          IList(IList(BoundarySegment)) loops = room.GetBoundarySegments(opt);

            foreach (IList(BoundarySegment) loop in loops)

            {

                foreach (BoundarySegment seg in loop)

                {

                    Curve curve = seg.GetCurve();

                    //创建详图线,设置它的线样式类型为我们刚才创建的

                    DetailCurve dc = doc.Create.NewDetailCurve(uidoc.ActiveView, curve);

                    if (BackLineStyle(doc) != null)

                    {

                        SetLineStyle(BackLineStyle(doc), dc);

                    }

                    array.Append(dc.GeometryCurve);

                }

            }

                 

  //两个相关的方法

 //搜索目标线样式

        private Category BackLineStyle(Document doc)

        {

            Category lineCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);

            CategoryNameMap map = lineCategory.SubCategories;

            foreach (Category g in map)

            {

                if (g.Name == "房间边界线")

                {

                    return g;

                }

            }

            return null;

        }

  //设置线样式

        private void SetLineStyle(Category cate, DetailCurve line)

        {

            ElementId Id = new ElementId(cate.Id.IntegerValue + 1);

 

            foreach (Parameter p in line.Parameters)

            {

                if (p.Definition.Name == "线样式")

                {

                    p.Set(Id);

                    break;

                }

            }

        }

上述代买我们用的是Center选项,让我们来看看效果:

Revit二次开发之创建房间,根据房间边界创建楼板等
红色是房价你的边界线的路径,可以看得出与墙中心线重合,我使用的墙结构信息如下:

Revit二次开发之创建房间,根据房间边界创建楼板等
改变Center选项为Finish后,我们再看看效果:

Revit二次开发之创建房间,根据房间边界创建楼板等
其他两个选项,读者自行分析吧,这里就不再介绍了:

接上述代码获得房间边界,我们可以创建楼板,以下为核心代码:

 //创建楼板,类型默认 标高默认

        private void CreateFloor(Document doc,CurveArray array)

        {

            FloorType floorType= new FilteredElementCollector(doc).OfClass(typeof(FloorType)).FirstOrDefault() as FloorType;

            Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).OrderBy(o => (o as Level).ProjectElevation).First() as Level;

            Floor floor = doc.Create.NewFloor(array,floorType,level,false,XYZ.BasisZ);

        }

生成楼板(其实根据闭合曲线能创建很多东西,也可以用其做族的放样,生成散水,踢脚线等,有时间我会写一个创建族的基本流程)之后的局部三维视图如下:

Revit二次开发之创建房间,根据房间边界创建楼板等/本项目工程全部代码:

 

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.DB.Mechanical;

using System.Xml;

using Autodesk.Revit.UI.Events;

using System.Windows.Forms;

using Autodesk.Revit.DB.Events;

using Autodesk.Revit.DB.Architecture;

 

namespace HelloWorld

{

    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]

    public class Test : IExternalCommand

    {

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

        {

            UIApplication uiApp = commandData.Application;

            UIDocument uidoc = uiApp.ActiveUIDocument;

            Document doc = uidoc.Document;

 

            if (IsExistLineStyle(doc, "房间边界线"))

            {

 

            }

            else

            {

                生成线样式

                using (Transaction ts = new Transaction(doc, "LineStyle"))

                {

                    ts.Start();

                    Category lineCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);

                    Category newCategory = doc.Settings.Categories.NewSubcategory(lineCategory, "房间边界线");

                    Color newColor = new Color(255, 0, 0);

                    newCategory.LineColor = newColor;

                    newCategory.SetLineWeight(1, GraphicsStyleType.Projection);

                    ts.Commit();

                }

            }

 

            Transaction ts2 = new Transaction(doc, "BIM");

            ts2.Start();

            Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "");

            Room room = doc.GetElement(refer) as Room;

 

            SpatialElementBoundaryOptions opt = new SpatialElementBoundaryOptions();

            opt.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;

            CurveArray array = new CurveArray();

            IList(IList(BoundarySegment)) loops = room.GetBoundarySegments(opt);

            foreach (IList(BoundarySegment) loop in loops)

            {

                foreach (BoundarySegment seg in loop)

                {

                    Curve curve = seg.GetCurve();

                    DetailCurve dc = doc.Create.NewDetailCurve(uidoc.ActiveView, curve);

                    if (BackLineStyle(doc) != null)

                    {

                        SetLineStyle(BackLineStyle(doc), dc);

                    }

                    array.Append(dc.GeometryCurve);

                }

                break;

            }

            CreateFloor(doc, array);

            ts2.Commit();

            return Result.Succeeded;

        }

        //设置线样式

        private void SetLineStyle(Category cate, DetailCurve line)

        {

            ElementId Id = new ElementId(cate.Id.IntegerValue + 1);

 

            foreach (Parameter p in line.Parameters)

            {

                if (p.Definition.Name == "线样式")

                {

                    p.Set(Id);

                    break;

                }

            }

        }

        //判断线样式是否存在

        private bool IsExistLineStyle(Document doc, string Name)

        {

 

            Category IsCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);

            CategoryNameMap map = IsCategory.SubCategories;

            foreach (Category g in map)

            {

                if (g.Name == Name)

                {

                    return true;

                }

            }

            return false;

        }

        //搜索目标线样式

        private Category BackLineStyle(Document doc)

        {

            Category lineCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);

            CategoryNameMap map = lineCategory.SubCategories;

            foreach (Category g in map)

            {

                if (g.Name == "房间边界线")

                {

                    return g;

                }

            }

            return null;

        }

        //创建楼板,类型默认 标高默认

        private void CreateFloor(Document doc,CurveArray array)

        {

            FloorType floorType= new FilteredElementCollector(doc).OfClass(typeof(FloorType)).FirstOrDefault() as FloorType;

            Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).OrderBy(o => (o as Level).ProjectElevation).First() as Level;

            Floor floor = doc.Create.NewFloor(array,floorType,level,false,XYZ.BasisZ);

        }

    }

}

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 我们可以使用 Revit API 来创建楼板。下面是一个示例代码:// 创建一个新的楼板 Floor floor = Floor.Create(doc, lineArray); // 设置楼板的高度 floor.get_Parameter(BuiltInParameter.FLOOR_HEIGH_PARAM).Set(10); // 设置楼板的类型 floor.get_Parameter(BuiltInParameter.FLOOR_ATTR_THICKNESS_PARAM).Set(floorType.Id); ### 回答2: Revit二次开发是指在Revit软件的基础上,使用API(Application Programming Interface)进行编程,以创建自定义功能、定制工具和插件。以下是展示Revit二次开发创建楼板的示例代码: ```csharp using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.Attributes; [Transaction(TransactionMode.Manual)] public class CreateFloorCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // 获取Revit文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 获取楼层板类型 ElementId floorTypeId = null; FilteredElementCollector floorTypeCollector = new FilteredElementCollector(doc) .OfClass(typeof(FloorType)); foreach (FloorType floorType in floorTypeCollector) { if (floorType.Name == "楼板类型名称") { floorTypeId = floorType.Id; break; } } if (floorTypeId != null) { // 获取楼层高度 double floorHeight = 3000; // 楼层高度为3000毫米 // 获取边界线 Line floorBoundaryLine = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(5000, 0, 0)); CurveArray floorBoundary = new CurveArray(); floorBoundary.Append(floorBoundaryLine); // 创建楼板 using(Transaction trans = new Transaction(doc, "创建楼板")) { trans.Start(); Floor floor = doc.Create.NewFloor(floorBoundary, false); floor.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM) .Set(floorHeight); floor.FloorType = doc.GetElement(floorTypeId) as FloorType; trans.Commit(); } return Result.Succeeded; } else { message = "找不到指定的楼板类型"; return Result.Failed; } } } ``` 以上代码使用了Revit API中的相关类和方法,通过获取Revit文档,楼板类型,楼层高度和边界线等信息,创建了一个自定义的楼板。需要替换代码中的`楼板类型名称`为实际楼板类型的名称,在使用该代码之前,请确保已经正确安装并配置了Revit API开发环境。 ### 回答3: Revit二次开发创建楼板的代码如下: 首先,我们需要导入Revit API的命名空间,包括`Autodesk.Revit.DB`和`Autodesk.Revit.UI`。 然后,创建一个继承自`IExternalCommand`接口的类,以便我们可以在Revit中运行这个命令。我们将命令命名为"CreateFloor"。 在类中,我们需要实现`Execute`方法,该方法将在运行命令时被调用。在这个方法中,我们将创建楼板并将其添加到当前项目中。 以下是示例代码: ```csharp using Autodesk.Revit.DB; using Autodesk.Revit.UI; public class CreateFloor : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // 获取当前文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 创建标高 Level level = new FilteredElementCollector(doc) .OfClass(typeof(Level)) .FirstOrDefault(e => e.Name.Equals("Level 1")) as Level; // 定义楼板的几何形状 CurveArray curveArray = new CurveArray(); XYZ p1 = new XYZ(0, 0, 0); XYZ p2 = new XYZ(10, 0, 0); XYZ p3 = new XYZ(10, 10, 0); XYZ p4 = new XYZ(0, 10, 0); Line line1 = Line.CreateBound(p1, p2); Line line2 = Line.CreateBound(p2, p3); Line line3 = Line.CreateBound(p3, p4); Line line4 = Line.CreateBound(p4, p1); curveArray.Append(line1); curveArray.Append(line2); curveArray.Append(line3); curveArray.Append(line4); // 创建楼板 using (Transaction transaction = new Transaction(doc, "Create Floor")) { transaction.Start(); Floor floor = doc.Create.NewFloor(curveArray, false); floor.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM).Set(0); // 设置楼板高度 transaction.Commit(); } return Result.Succeeded; } } ``` 以上示例代码演示了如何使用Revit API创建楼板。我们首先获取当前文档,然后创建一个标高。接下来,我们定义了楼板的几何形状,最后创建楼板并将其添加到项目中。在事务中运行代码,以确保操作的完整性和一致性。 以上是简单的创建楼板代码示例,该代码仅供参考。在实际开发过程中,可能需要根据具体需求进行更多的参数设置和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值