This blog post covers a specific query on how we can retrieve the lines that form a FillPattern using the Revit API.
Using the FillPattern API (that was first introduced in Revit 2012), we can access the FillGrid representing a set of lines which form the part of a FillPattern on an element - for example on a wall. Using the following lines of code, we can access the FillGrid from a FillPattern.
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication app = commandData.Application;
foreach (Element ele in app.ActiveUIDocument.Selection.Elements)
{
Wall selectedWall = ele as Wall;
if (null != selectedWall)
{
foreach (Material material in selectedWall.Materials)
{
if (null != material.SurfacePattern)
{
FillPattern fpatt = material.SurfacePattern.GetFillPattern();
IList<FillGrid> fgList = fpatt.GetFillGrids();
foreach (FillGrid fg in fgList)
{
// With each FillGrid object, we can now directly access
// offset, rotation, etc of the pattern lines
}
}
}
}
}
return Result.Succeeded;
}
}
}
The FillGrid object returned from pattern using the API provides us with values like offset, angle, origin, etc. For a given example of a wall with a horizontal lines which the surface pattern for the material of the wall, say, we get offset value of 0.5. This implies that the pattern consists of solid horizontal lines with a spacing of 0.5 feet between each other. This is what is represented in the Offset value on the FillGrid object. Using GetFillGrids() we can find out what the line consists of. It will either be a single value indicating a solid line or multiple values indicating a pattern of dashes (on, off, on, off, etc).
If an API user is interested in accessing the coordinates of each of the horizontal lines which form the fill pattern, we can use the offset value (or the distance between) each horizontal solid line and use the start and end point of these lines will actually be the the start and end points of the wall. This information can be used to get the coordinates of each of the horizontal solid lines which create this pattern. For angular ones, it might get little more complex but the FillGrid will return the angle in case the lines are at an angle. API users might have to do some additional investigation by cross checking the values from the UI and the API to understand how you can calculate the coordinates of the pattern lines in more complex fill patterns.