Change Tile Pattern
next previous home

In this lab, we shall change the tiling pattern on the divided surface created using Lab 7-2 using the built-in enumeration available with the API. The output should be similar to the image included below.

Tile Pattern

Similar to the previous lab, we shall create a new filter ? a type filter, to extract all the elements from the Revit model which are of Form type. This can be done using the Filter.NewTypeFilter.

  # region Lab7_3_ChangeTilePattern
  /// <summary>
  /// Change the tiling pattern of the divided surface using the built-in TilePattern enumeration.
  /// </summary>
  public class Lab7_3_ChangeTilePattern : IExternalCommand
  {
    public IExternalCommand.Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
    {
      Application app = commandData.Application;
      Document doc = app.ActiveDocument;
      try
      {
        // find forms in the model by filter:
        Filter filterForm = app.Create.Filter.NewTypeFilter(typeof(Form));
        List<Autodesk.Revit.Element> forms = new List<Autodesk.Revit.Element>();
        doc.get_Elements(filterForm, forms);
        foreach (Form form in forms)
        {
          . . .
        }
      }
      catch
      {
        return IExternalCommand.Result.Failed;
      }
      return IExternalCommand.Result.Succeeded;
    }
  }
  #endregion
    #Region "Lab7_3_ChangeTilePattern"
    Public Class Lab7_3_ChangeTilePattern
        Implements IExternalCommand
        Public Function Execute( _
            ByVal commandData As ExternalCommandData, _
            ByRef message As String, _
            ByVal elements As ElementSet) _
        As IExternalCommand.Result _
        Implements IExternalCommand.Execute
            Dim app As Application = commandData.Application
            Dim doc As Document = app.ActiveDocument
            Try
                ' find forms in the model by filter:
                Dim filterForm As Filter = app.Create.Filter.NewTypeFilter(GetType(Form))
                Dim forms As New List(Of Autodesk.Revit.Element)()
                Dim iForms As Integer = doc.Elements(filterForm, forms)
                For Each form As Form In forms
                    . . .
                Next
            Catch
                Return IExternalCommand.Result.Failed
            End Try
            Return IExternalCommand.Result.Succeeded
        End Function
    End Class
    #End Region

Now with each form, we shall get access to the DividedSurfaceData using the form.GetDividedSurfaceData method. This divided surface data contains a list of references which can be extracted using the GetReferencesWithDividedSurfaces() method on the divided surface data. Using the built-in enumeration of tile patterns stored in document.Settings, we shall use the ObjectType property on the divided surface element to set it to one of the tile patterns available in the built-in enumeration. Say, we set the third pattern in the enumeration to the divided surface element.

  // Get access to the divided surface data from the form
  DividedSurfaceData dsData = form.GetDividedSurfaceData();
  if (null != dsData)
  {
    // get the references associated with the divided surfaces
    foreach (Reference reference in dsData.GetReferencesWithDividedSurfaces())
    {
      DividedSurface divSurface = dsData.GetDividedSurfaceForReference(reference);

      int count = 0;
      TilePatterns tilepatterns = doc.Settings.TilePatterns;
      foreach (TilePatternsBuiltIn TilePatternEnum in Enum.GetValues(typeof(TilePatternsBuiltIn)))
      {
        if (count.Equals(3))
        {
          divSurface.ObjectType = tilepatterns.GetTilePattern(TilePatternEnum);
          break;
        }
        count = count + 1;
      }
    }
  }
    ' Get access to the divided surface data from the form
    Dim dsData As DividedSurfaceData = form.GetDividedSurfaceData()
    If dsData IsNot Nothing Then
        ' get the references associated with the divided surfaces
        For Each reference As Reference In dsData.GetReferencesWithDividedSurfaces()
            Dim divSurface As DividedSurface = dsData.GetDividedSurfaceForReference(reference)

            Dim count As Integer = 0
            Dim tilepatterns As TilePatterns = doc.Settings.TilePatterns
            For Each TilePatternEnum As TilePatternsBuiltIn In [Enum].GetValues(GetType(TilePatternsBuiltIn))
                If count.Equals(3) Then
                    divSurface.ObjectType = tilepatterns.GetTilePattern(TilePatternEnum)
                    Exit For
                End If
                count = count + 1
            Next
        Next
    End If

Compile the code, update Revit.ini and test the command. Examine the outcome.

next previous home copyright © 2007-2009 jeremy tammik, autodesk inc. all rights reserved.