Revit API 开发 (9): BRepBuilder 创建原生几何图形

前言

原生集合图形的好处不言而喻,如何创建一个复杂的原生图形,Revit API 也提供了对应的功能。

流程

如何使用 BRepBuilder 创建一个圆柱体,并且用DirectShape在Revit中创建。代码请参考Revit SDK。

BRepBuilder开始和结束

BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);
brepBuilder.Finish();

加上一个面的开始和结束

BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);

BRepBuilderGeometryId frontCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(cylSurf, null), false);
BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(frontCylFaceId);
brepBuilder.FinishFace(frontCylFaceId);

brepBuilder.Finish();

为这个面加上边

BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);

BRepBuilderGeometryId frontCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(cylSurf, null), false);
BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(frontCylFaceId);
brepBuilder.AddCoEdge(loopId_Front, linearEdgeBackId, false);
brepBuilder.AddCoEdge(loopId_Front, frontEdgeTopId, false);
brepBuilder.AddCoEdge(loopId_Front, linearEdgeFrontId, true);
brepBuilder.AddCoEdge(loopId_Front, frontEdgeBottomId, true);
brepBuilder.FinishLoop(loopId_Front);
brepBuilder.FinishFace(frontCylFaceId);

brepBuilder.Finish();

整体代码

private void CreateDirectShapeFromCylinder(Document doc)
{
    // Naming convention for faces and edges: we assume that x is to the left and pointing down, y is horizontal and pointing to the right, z is up
    BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);

    // The surfaces of the four faces.
    Frame basis = new Frame(new XYZ(50, -100, 0), new XYZ(0, 1, 0), new XYZ(-1, 0, 0), new XYZ(0, 0, 1));
    CylindricalSurface cylSurf = CylindricalSurface.Create(basis, 50);
    Plane top = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 100));  // normal points outside the cylinder
    Plane bottom = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 0)); // normal points inside the cylinder

    // Add the four faces
    BRepBuilderGeometryId frontCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(cylSurf, null), false);
    BRepBuilderGeometryId backCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(cylSurf, null), false);
    BRepBuilderGeometryId topFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(top, null), false);
    BRepBuilderGeometryId bottomFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(bottom, null), true);

    // Geometry for the four semi-circular edges and two vertical linear edges
    BRepBuilderEdgeGeometry frontEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -100, 0), new XYZ(100, -100, 0), new XYZ(50, -50, 0)));
    BRepBuilderEdgeGeometry backEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(100, -100, 0), new XYZ(0, -100, 0), new XYZ(50, -150, 0)));

    BRepBuilderEdgeGeometry frontEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -100, 100), new XYZ(100, -100, 100), new XYZ(50, -50, 100)));
    BRepBuilderEdgeGeometry backEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -100, 100), new XYZ(100, -100, 100), new XYZ(50, -150, 100)));

    BRepBuilderEdgeGeometry linearEdgeFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, -100, 0), new XYZ(100, -100, 100));
    BRepBuilderEdgeGeometry linearEdgeBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, -100, 0), new XYZ(0, -100, 100));

    // Add the six edges
    BRepBuilderGeometryId frontEdgeBottomId = brepBuilder.AddEdge(frontEdgeBottom);
    BRepBuilderGeometryId frontEdgeTopId = brepBuilder.AddEdge(frontEdgeTop);
    BRepBuilderGeometryId linearEdgeFrontId = brepBuilder.AddEdge(linearEdgeFront);
    BRepBuilderGeometryId linearEdgeBackId = brepBuilder.AddEdge(linearEdgeBack);
    BRepBuilderGeometryId backEdgeBottomId = brepBuilder.AddEdge(backEdgeBottom);
    BRepBuilderGeometryId backEdgeTopId = brepBuilder.AddEdge(backEdgeTop);

    // Loops of the four faces
    BRepBuilderGeometryId loopId_Top = brepBuilder.AddLoop(topFaceId);
    BRepBuilderGeometryId loopId_Bottom = brepBuilder.AddLoop(bottomFaceId);
    BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(frontCylFaceId);
    BRepBuilderGeometryId loopId_Back = brepBuilder.AddLoop(backCylFaceId);

    // Add coedges for the loop of the front face
    brepBuilder.AddCoEdge(loopId_Front, linearEdgeBackId, false);
    brepBuilder.AddCoEdge(loopId_Front, frontEdgeTopId, false);
    brepBuilder.AddCoEdge(loopId_Front, linearEdgeFrontId, true);
    brepBuilder.AddCoEdge(loopId_Front, frontEdgeBottomId, true);
    brepBuilder.FinishLoop(loopId_Front);
    brepBuilder.FinishFace(frontCylFaceId);

    // Add coedges for the loop of the back face
    brepBuilder.AddCoEdge(loopId_Back, linearEdgeBackId, true);
    brepBuilder.AddCoEdge(loopId_Back, backEdgeBottomId, true);
    brepBuilder.AddCoEdge(loopId_Back, linearEdgeFrontId, false);
    brepBuilder.AddCoEdge(loopId_Back, backEdgeTopId, true);
    brepBuilder.FinishLoop(loopId_Back);
    brepBuilder.FinishFace(backCylFaceId);

    // Add coedges for the loop of the top face
    brepBuilder.AddCoEdge(loopId_Top, backEdgeTopId, false);
    brepBuilder.AddCoEdge(loopId_Top, frontEdgeTopId, true);
    brepBuilder.FinishLoop(loopId_Top);
    brepBuilder.FinishFace(topFaceId);

    // Add coedges for the loop of the bottom face
    brepBuilder.AddCoEdge(loopId_Bottom, frontEdgeBottomId, false);
    brepBuilder.AddCoEdge(loopId_Bottom, backEdgeBottomId, false);
    brepBuilder.FinishLoop(loopId_Bottom);
    brepBuilder.FinishFace(bottomFaceId);

    brepBuilder.Finish();

    using (Transaction tr = new Transaction(doc, "Create a DirectShape"))
    {
        tr.Start();
        DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
        ds.SetShape(brepBuilder);
        tr.Commit();
    }
}

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值