NewFamilySymbolProfile Sample Add-In

I've been answering rather a lot of queries in the Revit API discussion forum lately, leading me to once again become top solution author; here is one of the latest:

How to use NewFamilySymbolProfile

Questions on how to use NewFamilySymbolProfile have come up a couple of times, e.g., in the following threads:

In the second, Revitalizer provides a succinct answer, saying:

The next step after retrieving the family symbol from the profile family is to call the Autodesk.Revit.Creation.Application NewFamilySymbolProfile method:

  FamilySymbolProfile
    Application.NewFamilySymbolProfile(
      FamilySymbol familySymbol )

However, the first two thread answers were insufficiently complete and clear, hence the third follow-up thread.

Question: I want to pass a profile from an existing profile family to FamilyCreate.NewSweep.

I looked at the thread on loading a profile for the NewSweep command.

Jeremy's comments from this post indicate that the profile family cannot be passed directly, but that the profile geometry needs to be recreated. Am I reading this correctly?

You can certainly retrieve a profile for a sweep operation from a family file.

However, the family file does not provide any provision for storing a profile per se, so you will have to make use of the other geometrical options available in that context to define some kind of geometry specifying the sweep profile.

You can then parse that geometry, extract the profile information, and put together the required curve array in your add-in.

As mentioned in some other Sweep posts, there are many samples that create a new profile, but none that accept a FamilySymbolProfile directly.

Can this be done?

Answer: This question was actually answered in the thread on sweep using profile family.

One first needs to create a FamilySymbolProfile object from the FamilySymbol generated by loading the profile Family, then use the FamilyFactory.NewSweep method passing in the FamilySymbolProfile as SweepProfile.

I implemented a new sample add-in NewFamilySymbolProfile for you to demonstrate the steps using the Profiles_L-Angles.rfa profile family.

UK Profiles Framing Steel Profiles_L-Angles.rfa

Here is the code:

#region Namespaces
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion

namespace NewFamilySymbolProfile
{
  [TransactionTransactionMode.Manual )]
  public class Command : IExternalCommand
  {
    const string _filepath = "C:/Users/All Users/Autodesk"
      + "/RVT 2018/Libraries/UK/Profiles/Framing/Steel"
      + "/Profiles_L-Angles.rfa";

    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication uiapp = commandData.Application;
      UIDocument uidoc = uiapp.ActiveUIDocument;
      Application app = uiapp.Application;
      Document doc = uidoc.Document;

      Document profile_doc = app.OpenDocumentFile( 
        _filepath );

      // The profile document contains no family sybols:

      FilteredElementCollector symbols
        = new FilteredElementCollector( profile_doc )
          .OfClass( typeofFamilySymbol ) );

      Debug.Assert( 0 == symbols.GetElementCount(), 
        "expected no family symbol" );

      // Load the profile family to generate them:

      FamilySymbol profile_symbol = null;

      usingTransaction tx = new Transaction( doc ) )
      {
        tx.Start( "Load Profile Family" );

        Family family;

        doc.LoadFamily( _filepath, out family );

        tx.Commit();

        ISet<ElementId> ids = family.GetFamilySymbolIds();

        foreachElementId id in ids )
        {
          profile_symbol = doc.GetElement( id ) 
            as FamilySymbol;

          Debug.Print( profile_symbol.Name );
        }
      }

      // Generate the family symbol profile:

      FamilySymbolProfile fsp = null;

      ifnull != profile_symbol )
      {
        usingTransaction tx = new Transaction( doc ) )
        {
          tx.Start( "Create FamilySymbolProfile" );
          fsp = app.Create.NewFamilySymbolProfile( 
            profile_symbol );
          tx.Commit();
        }
      }

      return Result.Succeeded;
    }
  }
}

The comments explain all the important steps.

Top Solution Author

As said, I have been answering rather a lot of queries lately, leading to a new top solution author record score of 37, afaik:

Top solution author