Retrieving the lines from FillPattern using Revit API

By Saikat Bhattacharya

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.


Comments

3 responses to “Retrieving the lines from FillPattern using Revit API”

  1. William Avatar
    William

    Hello! Thank you for writing about this page. I am a fairly new user of Revit API, so please bear with me.
    Would it be possible to show us an example of utilizing the offset value of each line? It’s not working out for me.

  2. How would one employ this method in Revit 2017? It seems there are a few references that don’t fly with the 2017 API…

  3. I had the same problem and found this useful answer to add to the information in this article: https://forums.autodesk.com/t5/revit-api-forum/dimension-on-hatch-pattern-slab/m-p/7078368#M22785
    This snippet worked for me for ceilings, I assume walls are similar
    foreach (ElementId id in selectedIds)
    {
    Element e = doc.GetElement(id);
    Ceiling c = e as Ceiling;
    CeilingType ct = doc.GetElement(c.GetTypeId()) as CeilingType;
    CompoundStructure comStruct = ct.GetCompoundStructure();
    foreach (CompoundStructureLayer structLayer in comStruct.GetLayers())
    {
    Autodesk.Revit.DB.Material layerMaterial = doc.GetElement(structLayer.MaterialId) as Material;
    FillPatternElement surfacePattern = layerMaterial.Document.GetElement(layerMaterial.SurfacePatternId) as FillPatternElement;
    if (null != surfacePattern)
    {
    info += “\n\nSurface Pattern: ” + surfacePattern.Name;
    FillPattern fpatt = surfacePattern.GetFillPattern();
    if (fpatt.IsSolidFill || fpatt.Target == FillPatternTarget.Drafting)
    {
    continue; //Skip solid fill or drafting pattern
    }
    IList fgList = fpatt.GetFillGrids();
    foreach (FillGrid fg in fgList)
    {
    //Do stuff with FillGrids
    }
    }
    }
    }

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading