前言
这个例子是通过 Revit API 来直接创建几何图形。和这篇博客内容相似:Revit API 开发 (9): BRepBuilder 创建原生几何图形
内容
最终效果:
创建立方体
完整代码详见:Revit 2021 SDK\Samples\GeometryAPI\BRepBuilderExample\CS\CreateCube.cs
步骤:
- 创建 6 个平面的几何 Plane;
- 用步骤 1 的 Plane 创建 6 个平面的 BRep 拓扑;
- 创建12条棱的几何数据,并用这些几何数据创建 Edge,即 BRep 拓扑;
- 以步骤 2 的平面 BRep 拓扑为基础创建闭合线圈 Loop 的 BRep 拓扑;
- 将步骤 3 的Brep 拓扑 Edge,添加到步骤 4 的 Brep 拓扑 Loop 中;
- 通知 BRep 构造器完成工作。
创建 6 个平面的几何 Plane
Plane bottom = Plane.CreateByOriginAndBasis(new XYZ(50, 50, 0), new XYZ(1, 0, 0), new XYZ(0, 1, 0)); // bottom. XY plane, Z = 0, normal pointing inside the cube.
Plane top = Plane.CreateByOriginAndBasis(new XYZ(50, 50, 100), new XYZ(1, 0, 0), new XYZ(0, 1, 0)); // top. XY plane, Z = 100, normal pointing outside the cube.
Plane front = Plane.CreateByOriginAndBasis(new XYZ(100, 50, 50), new XYZ(0, 0, 1), new XYZ(0, 1, 0)); // front side. ZY plane, X = 0, normal pointing inside the cube.
Plane back = Plane.CreateByOriginAndBasis(new XYZ(0, 50, 50), new XYZ(0, 0, 1), new XYZ(0, 1, 0)); // back side. ZY plane, X = 0, normal pointing outside the cube.
Plane left = Plane.CreateByOriginAndBasis(new XYZ(50, 0, 50), new XYZ(0, 0, 1), new XYZ(1, 0, 0)); // left side. ZX plane, Y = 0, normal pointing inside the cube
Plane right = Plane.CreateByOriginAndBasis(new XYZ(50, 100, 50), new XYZ(0, 0, 1), new XYZ(1, 0, 0)); // right side. ZX plane, Y = 100, normal pointing outside the cube
用步骤 1 的 Plane 创建 6 个平面的 BRep 拓扑
BRepBuilderGeometryId faceId_Bottom = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(bottom, null), true);
BRepBuilderGeometryId faceId_Top = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(top, null), false);
BRepBuilderGeometryId faceId_Front = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(front, null), true);
BRepBuilderGeometryId faceId_Back = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(back, null), false);
BRepBuilderGeometryId faceId_Left = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(left, null), true);
BRepBuilderGeometryId faceId_Right = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(right, null), false);
创建12条棱的几何数据,并用这些几何数据创建 Edge,即 BRep 拓扑
BRepBuilderEdgeGeometry edgeBottomFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, 0, 0), new XYZ(100, 100, 0));
BRepBuilderEdgeGeometry edgeBottomRight = BRepBuilderEdgeGeometry.Create(new XYZ(100, 100, 0), new XYZ(0, 100, 0));
BRepBuilderEdgeGeometry edgeBottomBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, 100, 0), new XYZ(0, 0, 0));
BRepBuilderEdgeGeometry edgeBottomLeft = BRepBuilderEdgeGeometry.Create(new XYZ(0, 0, 0), new XYZ(100, 0, 0));
// now walk around top face
BRepBuilderEdgeGeometry edgeTopFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, 0, 100), new XYZ(100, 100, 100));
BRepBuilderEdgeGeometry edgeTopRight = BRepBuilderEdgeGeometry.Create(new XYZ(100, 100, 100), new XYZ(0, 100, 100));
BRepBuilderEdgeGeometry edgeTopBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, 100, 100), new XYZ(0, 0, 100));
BRepBuilderEdgeGeometry edgeTopLeft = BRepBuilderEdgeGeometry.Create(new XYZ(0, 0, 100), new XYZ(100, 0, 100));
// sides
BRepBuilderEdgeGeometry edgeFrontRight = BRepBuilderEdgeGeometry.Create(new XYZ(100, 100, 0), new XYZ(100, 100, 100));
BRepBuilderEdgeGeometry edgeRightBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, 100, 0), new XYZ(0, 100, 100));
BRepBuilderEdgeGeometry edgeBackLeft = BRepBuilderEdgeGeometry.Create(new XYZ(0, 0, 0), new XYZ(0, 0, 100));
BRepBuilderEdgeGeometry edgeLeftFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, 0, 0), new XYZ(100, 0, 100));
// 3.b (define the edges themselves)
BRepBuilderGeometryId edgeId_BottomFront = brepBuilder.AddEdge(edgeBottomFront);
BRepBuilderGeometryId edgeId_BottomRight = brepBuilder.AddEdge(edgeBottomRight);
BRepBuilderGeometryId edgeId_BottomBack = brepBuilder.AddEdge(edgeBottomBack);
BRepBuilderGeometryId edgeId_BottomLeft = brepBuilder.AddEdge(edgeBottomLeft);
BRepBuilderGeometryId edgeId_TopFront = brepBuilder.AddEdge(edgeTopFront);
BRepBuilderGeometryId edgeId_TopRight = brepBuilder.AddEdge(edgeTopRight);
BRepBuilderGeometryId edgeId_TopBack = brepBuilder.AddEdge(edgeTopBack);
BRepBuilderGeometryId edgeId_TopLeft = brepBuilder.AddEdge(edgeTopLeft);
BRepBuilderGeometryId edgeId_FrontRight = brepBuilder.AddEdge(edgeFrontRight);
BRepBuilderGeometryId edgeId_RightBack = brepBuilder.AddEdge(edgeRightBack);
BRepBuilderGeometryId edgeId_BackLeft = brepBuilder.AddEdge(edgeBackLeft);
BRepBuilderGeometryId edgeId_LeftFront = brepBuilder.AddEdge(edgeLeftFront);
以步骤 2 的平面 BRep 拓扑为基础创建闭合线圈 Loop 的 BRep 拓扑
BRepBuilderGeometryId loopId_Bottom = brepBuilder.AddLoop(faceId_Bottom);
BRepBuilderGeometryId loopId_Top = brepBuilder.AddLoop(faceId_Top);
BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(faceId_Front);
BRepBuilderGeometryId loopId_Back = brepBuilder.AddLoop(faceId_Back);
BRepBuilderGeometryId loopId_Right = brepBuilder.AddLoop(faceId_Right);
BRepBuilderGeometryId loopId_Left = brepBuilder.AddLoop(faceId_Left);
将步骤 3 的Brep 拓扑 Edge,添加到步骤 4 的 Brep 拓扑 Loop 中
// Bottom face. All edges reversed
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomFront, true); // other direction in front loop
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomLeft, true); // other direction in left loop
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomBack, true); // other direction in back loop
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomRight, true); // other direction in right loop
brepBuilder.FinishLoop(loopId_Bottom);
brepBuilder.FinishFace(faceId_Bottom);
// 其它5个面类似,省略。
通知 BRep 构造器完成工作
brepBuilder.Finish();
创建曲面
创建曲面和创建立方体也是类似的,代码详见:
Revit 2021 SDK\Samples\GeometryAPI\BRepBuilderExample\CS\CreateNURBS.cs
总体原则,先创建几何图形,再创建拓扑结构作为几何图形的容器,而拓扑结构是可以有嵌套的,比如 loop 下面是 edge,即 loop 不需要直接对应到几何图形,它对应到 edge,而 edge 再去对应几何图形。
步骤:
- 创建曲面:
BRepBuilderSurfaceGeometry.CreateNURBSSurface
; - 创建作为边界的曲线,
NurbSpline.CreateCurve
,并为其创建拓扑 ,BRepBuilderEdgeGeometry.Create
; - 将步骤1创建的曲面加入拓扑:
BRepBuilderGeometryId nurbSplineFaceId = brepBuilder.AddFace(nurbsSurface, false)
- 在步骤3得到曲面拓扑上加个 loop,作为后续 edge 的容器:
BRepBuilderGeometryId loopId = brepBuilder.AddLoop(nurbSplineFaceId)
- 将步骤2得到曲线得拓扑加入 brep 拓扑:
brepBuilder.AddEdge
- 将步骤 5 得到得 edge 加入到 loop 中;
- 完成。
创建圆柱和圆台
与 Revit API 开发 (9): BRepBuilder 创建原生几何图形 类似。
其它
创建出来得几何图形都是通过 DirectShape
来展示,可以参考:
Revit API 开发 (8): DirectShape 创建Revit可以识别的几何图形