Set Family Parameter Requires Type

I was running down to town to have tea with my friend Otto on Saturday morning, and happened to see this autumn leaf lying on the ground.
Turned back, picked it up, and made a picture of it in the sunshine on Otto’s veranda.
It is completely unretouched, I promise!

Autumn leaf

Back to the Revit API, here is a short note on another issue run into by Ishwar Nagwani, Technical Consultant in our Bangalore organisation, who recently contributed the

pick points to create a new a floor
sample, and answered by Harry Mattison of the Revit development team.

Question: I am trying to update an existing family parameter and set its value, but it is always throwing an InvalidOperationException saying “There is no current type”.
What is wrong?

Here is some minimal sample code to reproduce the issue.
Only the family file name is hardcoded.


public class Command : IExternalCommand
{
  public Result Execute(
    ExternalCommandData commandData,
    ref string message,
    ElementSet elements )
  {
    UIApplication uiapp = commandData.Application;
 
    uiapp.OpenAndActivateDocument(
      "C:\Projects\FY12\GE Revit\SWBD-AV2.rfa" );
 
    UIDocument uidoc = uiapp.ActiveUIDocument;
    Application app = uiapp.Application;
    Document doc = uidoc.Document;
 
    FamilyManager familyMgr = doc.FamilyManager;
 
    FamilyParameter param = familyMgr.get_Parameter(
      "Width" );
 
    if( null == param )
    {
      param = doc.FamilyManager.AddParameter(
        "Width", BuiltInParameterGroup.PG_GEOMETRY,
        ParameterType.Length, true );
    }
 
    familyMgr.Set( param, 0.2 ); // set the value
 
    return Result.Succeeded;
  }
}

Answer: The exception message actually says it all, albeit rather succinctly: “There is no current type”.

FamilyManager.Set is used to set the value of a parameter for the current family type.
The family you are working with had no types defined, therefore Set could not work.
You can remedy this by adding a family type manually or with the following two lines:


  if( doc.FamilyManager.Types.Size == 0 )
    doc.FamilyManager.NewType( "Type 1" );
  familyMgr.Set( param, 0.2 );

Thanks to Ishwar and Harry for sharing this!


Comments

8 responses to “Set Family Parameter Requires Type”

  1. Consider insetead of creating a type called “Type 1” you instead create a type with the same name as the short family file name, which can be retrieved using the API from the current document.
    A family loaded into a project that has no types in it will get a default type assigned to it in the project editor that’s the same as the short family file name.
    By naming your type that way, it will behave the same way in the project editor as if no types were defined.
    The only way it would behave differently is if the family file name itself changes in the future. Then the old family file name will show up as the only type available in the project browser.

  2. Wow that leaf is amazing, my little girl would love to find a leaf like that, it is really unusual, what sort of tree is this leaf off.

  3. Love the picture of the leaf, it is very rare that you find something so beautiful which is natural and untouched.

  4. Thanks, there’s no way I would have found this without reading your comment here.
    Should be documented, or Revit should be changed to produce this type automatically. It’s a detail I should not need to concern myself with as a plug-in developer.

  5. Mayur Avatar
    Mayur

    Hi Jeremy ,
    I am creating new family type and adding several parameters to it..Is there any way to add parameter without adding family type or can we remove it after adding parameter..application is getting hanged because having too many family type..
    please suggest

  6. Dear Mayur,
    You must have at least one type defined to add parameters, and that’s it.
    There is no reason why the number should grow beyond that.
    Cheers, Jeremy.

  7. sadot.arefin@gmail.com Avatar
    sadot.arefin@gmail.com

    Hi Jeremy,
    Its kind a late for asking but let me ask you…
    I have only one Type in my family say “RoundPipe” and when I place my instance of this RoundPipe type I must change its value before sending it to “PromptForFamilyInstancePlacement”
    What I want to say is that my instances will have different parameter value but all will be derived from same family type (FamilySymbol) without creating a new type or new symbol
    Thanks
    Shourov

  8. Dear Shourov,
    I assume that you mean ‘change a parameter value’ when you say ‘change its value’.
    Unfortunately, you cannot do this before placing it, so the call to PromptForFamilyInstancePlacement must come first, and the initial value will be whatever default is defined for your family symbol.
    You can change the value immediately after placement, however, e.g. using the dynamic model updater framework DMU.
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading