Geometry Preview using DirectShape class

Geometry Preview using DirectShape class

At times, I am required to preview geometries for testing or presentation purposes. The patterns you see above, are actually a series of spherical geometries generated in Revit using the DirectShape class. DirectShape is a simple and quick way to churn out such geometries in Revit.

The code sample below creates a geometry element from a Solid object in Revit, using CreateElement & SetShape methods from the DirectShape Class.

Document doc = commandData.Application.ActiveUIDocument.Document;
 
Solid box = CreateBox( );
GeometryObject[] boxObj = new GeometryObject[] { box };
 
using (Transaction t = new Transaction(doc, "Transaction"))
{
    t.Start();
                
    DirectShape ds = DirectShape.CreateElement( doc , new ElementId( BuiltInCategory.OST_GenericModel ) );
    ds.SetShape( boxObj );
 
    t.Commit();
}

If you’re interested in how the Solid object, i.e. box, was derived in the demonstration above, the code sample below shows the implementation of the CreateBox( ) function.

public static Solid CreateBox( )
{
    XYZ btmLeft = new XYZ(-3 , -3 , 0 );
    XYZ topRight = new XYZ( 3 , 3 , 0 );
    XYZ btmRight = new XYZ(topRight.X, btmLeft.Y, 0);
    XYZ topLeft = new XYZ(btmLeft.X, topRight.Y, 0);
 
    Curve btm = Line.CreateBound(btmLeft, btmRight) as Curve;
    Curve right = Line.CreateBound(btmRight, topRight) as Curve;
    Curve top = Line.CreateBound(topRight, topLeft) as Curve;
    Curve left = Line.CreateBound(topLeft, btmLeft) as Curve;
 
    CurveLoop crvLoop = new CurveLoop();
 
    crvLoop.Append(btm);
    crvLoop.Append(right);
    crvLoop.Append(top);
    crvLoop.Append(left);
 
    IList<CurveLoop> cl = new List<CurveLoop>();
    cl.Add(crvLoop);
 
    Solid box = GeometryCreationUtilities.CreateExtrusionGeometry(cl, XYZ.BasisZ, 5 );
 
    return box;
 
}

You can find the source code for the full implementation HERE! Happy coding!

This Post Has One Comment

Leave a Reply