Revit API 斜楼板

写点干货,其实也不是很干,不过在TheBuildingCoder,Forum还有叶老师的博客里边都找不到对应的资料。问题其实很简单,如何用Revit API创建斜楼板。

Revit API中的NewFloor()一般用来创建楼板,不过方法中只接受平面的轮廓线,若轮廓线不在平面上则报错,提示轮廓线无效。叶老师的博客中介绍斜楼板无法使用NewFloor(),需要使用NewSlab()方法。TheBuildingCoder中的介绍也类似,不过为使得模型前后更一致切符合用户习惯,本文记录了我在Revit中使用NewFloor() API来成功创建斜楼板的过程。

现有资料

叶老师的博客:如何用API来创建斜板

TheBuildingCoder:(爬墙失败,链接后面再补)

UI上的操作

UI上也是无法直接创建斜楼板的,需要先创建一块平楼板,然后在修改菜单中的形状编辑栏使用修改子图元工具来编辑楼板上的点,通过设置每个点的偏移来创建斜楼板。

API操作

在API Document中搜索,可以发现Floor类中有一个和UI上形状编辑对应的属性,类型为SlabShapeEditor。这个类恰好也有一个方法对应UI的修改子图元功能:ModifySubElement(SlabShapeVertex, Double)。于是我使用了下面的代码来创建一块斜楼板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void CreateSlopedFloor(FloorType ft, Level level)
{

Document doc = ft.Document;
XYZ pt1 = new XYZ(0, 0, 0);
XYZ pt2 = new XYZ(100, 0, 0);
XYZ pt3 = new XYZ(100, 100, 0);
XYZ pt4 = new XYZ(0, 100, 0);
CurveArray profile = new CurveArray();
profile.Append(Line.CreateBound(pt1, pt2));
profile.Append(Line.CreateBound(pt2, pt3));
profile.Append(Line.CreateBound(pt3, pt4));
profile.Append(Line.CreateBound(pt4, pt1));

Floor floor = doc.Create.NewFloor(profile, ft, level, true);
floor.Document.Regenerate();
double offset = 30;
// 获取SlabShapeEditor
SlabShapeEditor slabEditor = floor.SlabShapeEditor;
slabEditor.DrawPoint(pt1);
slabEditor.DrawPoint(pt2);
slabEditor.DrawPoint(pt3);
slabEditor.DrawPoint(pt4);
var vertices = slabEditor.SlabShapeVertices;
// 修改第三点和第四点的偏移值
foreach (SlabShapeVertex vertex in vertices)
{
if (vertex.Position.IsAlmostEqualTo(pt3))
{
slabEditor.ModifySubElement(vertex, offset);
}
else if (vertex.Position.IsAlmostEqualTo(pt4))
{
slabEditor.ModifySubElement(vertex, offset);
}
}
}

似乎并没有错误,编译和执行也都没有问题,但是创建出来的结果却出乎意料,楼板还是平的。。。此时只能够Debug走一遍代码,经过一番搜寻发现了问题所在:slabEditor.SlabShapeVertices数量为零。此时对第三点和第四点的遍历修改操作不会被执行,因此偏移值没有成功设定。
回过头再仔细看UI操作,又有了新的发现:正常的楼板和斜楼板被选中后是不同的状态,斜楼板被选中后,四周会有角点和折线(分别对应SlabShapeVertices和SlabShapeCrease),而平楼板则什么都没有,因此角点和折线显然不是一开始就存在于SlabShapEditor中的,而是后来添加上的。而使用SlabShapeEditor.DrawPoint()方法则可添加角点。
我把代码修改为以下代码后,此时成功的创建了一块斜楼板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void CreateSlopedFloor(FloorType ft, Level level)
{

Document doc = ft.Document;
XYZ pt1 = new XYZ(0, 0, 0);
XYZ pt2 = new XYZ(100, 0, 0);
XYZ pt3 = new XYZ(100, 100, 0);
XYZ pt4 = new XYZ(0, 100, 0);
CurveArray profile = new CurveArray();
profile.Append(Line.CreateBound(pt1, pt2));
profile.Append(Line.CreateBound(pt2, pt3));
profile.Append(Line.CreateBound(pt3, pt4));
profile.Append(Line.CreateBound(pt4, pt1));

Floor floor = doc.Create.NewFloor(profile, ft, level, true);
floor.Document.Regenerate();
double offset = 30;
// 获取SlabShapeEditor
SlabShapeEditor slabEditor = floor.SlabShapeEditor;
// 绘制角点,这里只需要修改角点偏移,因此不需要添加折痕线
slabEditor.DrawPoint(pt1);
slabEditor.DrawPoint(pt2);
slabEditor.DrawPoint(pt3);
slabEditor.DrawPoint(pt4);
var vertices = slabEditor.SlabShapeVertices;
// 修改第三点和第四点的偏移值
foreach (SlabShapeVertex vertex in vertices)
{
if (vertex.Position.IsAlmostEqualTo(pt3))
{
slabEditor.ModifySubElement(vertex, offset);
}
else if (vertex.Position.IsAlmostEqualTo(pt4))
{
slabEditor.ModifySubElement(vertex, offset);
}
}
}

文章目录
  1. 1. 现有资料
  2. 2. UI上的操作
  3. 3. API操作