Grids are represented by the Grid 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.
The Grid class Curve property gets the object that represents the grid line geometry.
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 + ")"; } |
Two overloaded Document methods are available 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: NewGrid() |
public Grid NewGrid( Arc arc ); public Grid NewGrid( Line line ); |
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 = document.Create.NewGrid(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 = document.Create.NewGrid(geomArc); if (null == arcGrid) { throw new Exception("Create a new curved grid failed."); } // Modify the name of the created grid arcGrid.Name = "New Name2"; } |
You can also create several grids at once using the Document.NewGrids() method, which takes a CurveArray parameter.