next previous home

3-2 Load an entire family or a specific type

In this section, we load a new entire family or a single family instance into the current document. The document object provides straightforward methods to load either a whole RFA file or just a specific symbol from the corresponding TXT catalog file. Before implementing a new command, we add some new constants to LabConstants module to define which family and symbol to load. This ensures that all the hard-coded constant values have a single repository for easy access in case we need to alter them at a later date:

  // Lab 3_3
  public const string gsLibPath = @"C:\Documents and Settings\All Users\Application Data\Autodesk\RST 2009\Metric Library\";

  public const string gsWholeFamilyFileToLoad1 = gsLibPath + @"Structural\Framing\Steel\M_C-Channel.rfa"; // has TXT catalog file
  public const string gsWholeFamilyFileToLoad2 = gsLibPath + @"Structural\Framing\Steel\M_Plate.rfa"; // no TXT catalog file

  public const string gsFamilyFileToLoadSingleSymbol = gsLibPath + @"Structural\Framing\Steel\M_L-Angle.rfa";
  public const string gsSymbolName = "L152x102x12.7";
  ' Lab 3_3
  Public Const gsLibPath As String = "C:\Documents and Settings\All Users\Application Data\Autodesk\RST 2009\Metric Library\"

  Public Const gsWholeFamilyFileToLoad1 As String = gsLibPath + "Structural\Framing\Steel\M_C-Channel.rfa" ' has TXT catalog file
  Public Const gsWholeFamilyFileToLoad2 As String = gsLibPath + "Structural\Framing\Steel\M_Plate.rfa" ' no TXT catalog file

  Public Const gsFamilyFileToLoadSingleSymbol As String = gsLibPath + "Structural\Framing\Steel\M_L-Angle.rfa"
  Public Const gsSymbolName As String = "L152x102x12.7"

You may need to change the paths and family names to make sure the RFA files and the corresponding TXT catalog files exist on your machine.

Now we can add a new command to Labs3 calling the document method to load an entire family using the LoadFamily method:

  /// <summary>
  /// Load an entire family or a specific type from a family.
  /// </summary>
  public class Lab3_2_LoadStandardFamilies : IExternalCommand
  {
    public IExternalCommand.Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      Document doc = commandData.Application.ActiveDocument;
      //
      // Load a whole Family
      //
      // example for a family WITH TXT file
      if( doc.LoadFamily( LabConstants.gsWholeFamilyFileToLoad1 ) )
      {
        LabUtils.InfoMsg( "Successfully loaded family " + LabConstants.gsWholeFamilyFileToLoad1 + "." );
      }
      else
      {
        LabUtils.ErrorMsg( "ERROR loading family " + LabConstants.gsWholeFamilyFileToLoad1 + "." );
      }
      // example for a family WITHOUT TXT file
      if( doc.LoadFamily( LabConstants.gsWholeFamilyFileToLoad2 ) )
      {
        LabUtils.InfoMsg( "Successfully loaded family " + LabConstants.gsWholeFamilyFileToLoad2 + "." );
      }
      else
      {
        LabUtils.ErrorMsg( "ERROR loading family " + LabConstants.gsWholeFamilyFileToLoad2 + "." );
      }
      return IExternalCommand.Result.Succeeded;
    }
  }
Public Class Lab3_2_LoadStandardFamilies
    Implements IExternalCommand

    Public Function Execute(
        ByVal commandData As Autodesk.Revit.ExternalCommandData,
        ByRef message As String,
        ByVal elements As Autodesk.Revit.ElementSet)
        As Autodesk.Revit.IExternalCommand.Result Implements Autodesk.Revit.IExternalCommand.Execute

        Dim doc As Revit.Document = commandData.Application.ActiveDocument

        'Load a whole Family

	    ' example for a family WITH TXT file
        If Not CType(doc.LoadFamily(gsWholeFamilyFileToLoad1), Boolean) Then
            MsgBox("ERROR in loading Family " & gsWholeFamilyFileToLoad1 & "?")
        Else
            MsgBox("Successfully loaded Family " & gsWholeFamilyFileToLoad1 & "!")
        End If

        ' example for a family WITHOUT TXT file
        If Not CType(doc.LoadFamily(gsWholeFamilyFileToLoad2), Boolean) Then
            MsgBox("ERROR in loading Family " & gsWholeFamilyFileToLoad2 & "?")
        Else
            MsgBox("Successfully loaded Family " & gsWholeFamilyFileToLoad2 & "!")
        End If

        Return Revit.IExternalCommand.Result.Succeeded
    End Function
End Class

Build the project, adjust the ini file and test loading the whole family. In order to load a single symbol, you can add the following piece of code before return statement:

  //
  // Load only a specific Symbol (Type)
  // The symbol MUST exist in the corresponding catalog (TXT) file - same as in the UI
  if( doc.LoadFamilySymbol( LabConstants.gsFamilyFileToLoadSingleSymbol, LabConstants.gsSymbolName ) )
  {
    LabUtils.InfoMsg( "Successfully loaded family symbol " + LabConstants.gsFamilyFileToLoadSingleSymbol + " : " + LabConstants.gsSymbolName + "." );
  }
  else
  {
    LabUtils.ErrorMsg( "ERROR loading family symbol " + LabConstants.gsFamilyFileToLoadSingleSymbol + " : " + LabConstants.gsSymbolName + "." );
  }
  'Load only a specific Symbol (Type)
  ' The symbol MUST exist in the corresponding catalog (TXT) file - same as in the UI
  If Not CType(doc.LoadFamilySymbol(gsFamilyFileToLoadSingleSymbol, gsSymbolName), Boolean) Then
      MsgBox("ERROR in loading FamilySymbol " & gsFamilyFileToLoadSingleSymbol & " : " & gsSymbolName & "?")
  Else
      MsgBox("Successfully loaded FamilySymbol " & gsFamilyFileToLoadSingleSymbol & " : " & gsSymbolName & "!")
  End If

Compile and test again, and discuss with the course instructor and your peers.

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