Is it possible to create mass geometry from the API?

Is it possible to create mass geometry from the API?

jamesbvaughan
Explorer Explorer
395 Views
2 Replies
Message 1 of 3

Is it possible to create mass geometry from the API?

jamesbvaughan
Explorer
Explorer

I'm developing an addin for importing geometry into Revit, and I currently have it importing everything as Generic Models using DirectShape, but I'd like to import the geometry as masses instead of generic models.

I was hoping that this would be as simple as making this change:

Before:

 

var elementId = new ElementId(BuiltInCategory.OST_Mass);
var ds = DirectShape.CreateElement(doc, elementId);
ds.SetShape(brepBuilder); // where brepBuilder is an instance of BRepBuilder

 

After:

 

var elementId = new ElementId(BuiltInCategory.OST_Mass);
var ds = DirectShape.CreateElement(doc, elementId);
ds.SetShape(brepBuilder); // where brepBuilder is an instance of BRepBuilder

 

 

But I'm getting an exception with the message "An internal error occurred" on that SetShape call.

 

Is there something additional that I need to do for this to work with OST_Mass, or is there a different built-in category that would be more appropriate here?

 

Thank you!

0 Likes
Accepted solutions (1)
396 Views
2 Replies
Replies (2)
Message 2 of 3

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @jamesbvaughan 


     I think you have miss something in BrepBuilder. I copy the sample code from Revit Docs and Changed the element id to mass. It works for me. Kindly refer the below code for additional reference.

 

Reference Code From Revit Docs:

 

 

        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_Mass));
                ds.SetShape(brepBuilder);
                tr.Commit();
            }
        }

 

 

Reference Image:

 

Mohamed_Arshad_0-1696568933962.png

 

 

Note:  Make sure you enable the Mass Showing.

Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 3 of 3

jamesbvaughan
Explorer
Explorer

Hi Mohamed, thanks for the quick reply!

 

It turns out that the exception I mentioned was actually an unrelated issue, and my problem was that I hadn't enabled Mass visibility in the project. Thanks for suggesting that.

0 Likes