Grids

The Grid class represents a single grid line within Autodesk Revit.

Grids are represented by the Grid class which is derived from the DatumPlane class, which is derived from the Element class. It contains all grid properties and methods. The inherited Name property is used to retrieve the content of the grid line's bubble.

Curve

The Grid class Curve property gets the object that represents the grid line geometry.

  • If the IsCurved property returns true, the Curve property will be an Arc class object.
  • If the IsCurved property returns false, the Curve property will be a Line class object.

For more information, refer to Geometry.

The following code is a simple example using the Grid class. The result appears in a message box after invoking the command.

Code Region 15-3: Using the Grid class

public void GetInfo_Grid(Grid grid)
{
    string message = "Grid : ";

    // Show IsCurved property
    message += "\nIf grid is Arc : " + grid.IsCurved;

    // Show Curve information
    Autodesk.Revit.DB.Curve curve = grid.Curve;
    if (grid.IsCurved)
    {
        // if the curve is an arc, give center and radius information
        Autodesk.Revit.DB.Arc arc = curve as Autodesk.Revit.DB.Arc;
        message += "\nArc's radius: " + arc.Radius;
        message += "\nArc's center:  (" + XYZToString(arc.Center);
    }
    else
    {
        // if the curve is a line, give length information
        Autodesk.Revit.DB.Line line = curve as Autodesk.Revit.DB.Line;
        message += "\nLine's Length: " + line.Length;
    }
    // Get curve start point
    message += "\nStart point: " + XYZToString(curve.GetEndPoint(0));
    // Get curve end point
    message += "; End point: " + XYZToString(curve.GetEndPoint(0));

    TaskDialog.Show("Revit",message);
}

// output the point's three coordinates
string XYZToString(XYZ point)
{
    return "(" + point.X + ", " + point.Y + ", " + point.Z + ")";
}

Creating a Grid

Two overloaded Create() methods are available in the Grid class to create a new grid in the Revit Platform API. Using the following method with different parameters, you can create a curved or straight grid:

Code Region 15-4: Grid.Create()

public Grid Create( Document document, Arc arc );
public Grid Create( Document document, Line line );
Note: The arc or the line used to create a grid must be in a horizontal plane.

The following code sample illustrates how to create a new grid with a line or an arc.

Code Region 15-5: Creating a grid with a line or an arc

void CreateGrid(Autodesk.Revit.DB.Document document)
{
    // Create the geometry line which the grid locates
    XYZ start = new XYZ(0, 0, 0);
    XYZ end = new XYZ(30, 30, 0);
    Line geomLine = Line.CreateBound(start, end);

    // Create a grid using the geometry line
    Grid lineGrid = Grid.Create(document, geomLine);
           
    if (null == lineGrid)
    {
        throw new Exception("Create a new straight grid failed.");
    }

    // Modify the name of the created grid
    lineGrid.Name = "New Name1";
    
    // Create the geometry arc which the grid locates
    XYZ end0 = new XYZ(0, 0, 0);
    XYZ end1 = new XYZ(10, 40, 0);
    XYZ pointOnCurve = new XYZ(5, 7, 0);
    Arc geomArc = Arc.Create(end0, end1, pointOnCurve);

           
    // Create a grid using the geometry arc
    Grid arcGrid = Grid.Create(document, geomArc);
           
    if (null == arcGrid)
    {
        throw new Exception("Create a new curved grid failed.");
    }

    // Modify the name of the created grid
    arcGrid.Name = "New Name2";
}
Note: In Revit, the grids are named automatically in a numerical or alphabetical sequence when they are created.