Modify family parameters from the code

Modify family parameters from the code

Anonymous
Not applicable
4,474 Views
8 Replies
Message 1 of 9

Modify family parameters from the code

Anonymous
Not applicable

Hi everyone. I've got a rfa file which contains a Revit Family. I'm trying to access its parameters from the code, so I can read them and modify them. I serached for a solution in the forum, but I couldn't find it. What should I do?

Here's a screenshot of the rfa opened with Revit.

family.jpg

 

Thank You

 

 

0 Likes
4,475 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

I understand that I need to use the EditFamily method. How do I use it?

0 Likes
Message 3 of 9

Anonymous
Not applicable
0 Likes
Message 4 of 9

Anonymous
Not applicable

Ok, I'll check it.

Thanks a lot

0 Likes
Message 5 of 9

Anonymous
Not applicable

When I try the code written in The Bulding Coder I get the following error: 'Util' name does not exists in the current context.

https://github.com/jeremytammik/the_building_coder_samples/blob/706115e03fdac2c586ba0537061cd5dd1ac4...

What I am missing?

0 Likes
Message 6 of 9

Anonymous
Not applicable

I have a new problem: I found the Util.cs class code in the web, but it's obsolete: it only works for the 2015 API.Here are the errors I got:utilcs.jpg

Here's the code I've tried:

https://searchcode.com/codesearch/view/90757950/

 

Thank you

 

0 Likes
Message 7 of 9

Anonymous
Not applicable

Another issue: I found an up to date Util.cs file. I fixed some bugs and it compiles fine. But here's the thing: the app just do nothing. I just want to edit the family parameters from the code. What should I do?

Here's the lastest version of Utils.cs: https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/U...

And here's the code I'm trying:

#region Header
//
// CmdFamilyParamValue.cs - list family parameter values
// defined on the types in a family document
//
// Copyright (C) 2009-2017 by Jeremy Tammik,
// Autodesk Inc. All rights reserved.
//
// Keywords: The Building Coder Revit API C# .NET add-in.
//
#endregion // Header

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

namespace BuildingCoder
{
  [Transaction( TransactionMode.ReadOnly )]
  class CmdFamilyParamValue : IExternalCommand
  {
    #region SetFamilyParameterValue
    /// <summary>
    /// Non-working sample code for
    /// http://forums.autodesk.com/t5/revit-api/family-types-amp-shared-parameter-values/m-p/6218767
    /// </summary>
    void SetFamilyParameterValueFails(
      Document doc,
      string paramNameToAmend )
    {
      FamilyManager mgr = doc.FamilyManager;
      FamilyTypeSet familyTypes = mgr.Types;
      FamilyTypeSetIterator familyTypeItor
        = familyTypes.ForwardIterator();
      familyTypeItor.Reset();
      while( familyTypeItor.MoveNext() )
      {
        FamilyParameter familyParam
          = mgr.get_Parameter( paramNameToAmend );

        if( familyParam != null )
        {
          FamilyType familyType = familyTypeItor.Current as FamilyType;
          Debug.Print( familyType.Name );
          mgr.Set( familyParam, 2 );
        }
      }
    }

    /// <summary>
    /// Working sample code for
    /// http://forums.autodesk.com/t5/revit-api/family-types-amp-shared-parameter-values/m-p/6218767
    /// </summary>
    void SetFamilyParameterValueWorks(
      Document doc,
      string paramNameToAmend )
    {
      FamilyManager mgr = doc.FamilyManager;
      FamilyParameter familyParam
        = mgr.get_Parameter( paramNameToAmend );

      if( familyParam != null )
      {
        foreach( FamilyType familyType in mgr.Types )
        {
          Debug.Print( familyType.Name );
          mgr.CurrentType = familyType;
          mgr.Set( familyParam, 2 );
        }
      }
    }
    #endregion // SetFamilyParameterValue

    static string FamilyParamValueString(
      FamilyType t,
      FamilyParameter fp,
      Document doc )
    {
      string value = t.AsValueString( fp );
      switch( fp.StorageType )
      {
        case StorageType.Double:
          value = Util.RealString(
            (double) t.AsDouble( fp ) )
            + " (double)";
          break;

        case StorageType.ElementId:
          ElementId id = t.AsElementId( fp );
          Element e = doc.GetElement( id );
          value = id.IntegerValue.ToString() + " ("
            + Util.ElementDescription( e ) + ")";
          break;

        case StorageType.Integer:
          value = t.AsInteger( fp ).ToString()
            + " (int)";
          break;

        case StorageType.String:
          value = "'" + t.AsString( fp )
            + "' (string)";
          break;
      }
      return value;
    }

    public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements )
    {
      UIApplication app = commandData.Application;
      Document doc = app.ActiveUIDocument.Document;
      if( !doc.IsFamilyDocument )
      {
        message =
          "Please run this command in a family document.";
      }
      else
      {
        FamilyManager mgr = doc.FamilyManager;

        int n = mgr.Parameters.Size;

        Debug.Print(
          "\nFamily {0} has {1} parameter{2}.",
          doc.Title, n, Util.PluralSuffix( n ) );

        Dictionary<string, FamilyParameter> fps
          = new Dictionary<string, FamilyParameter>( n );

        foreach( FamilyParameter fp in mgr.Parameters )
        {
          string name = fp.Definition.Name;
          fps.Add( name, fp );

          #region Look at associated parameters
#if LOOK_AT_ASSOCIATED_PARAMETERS
          ParameterSet ps = fp.AssociatedParameters;
          n = ps.Size;

          string values = string.Empty;
          foreach( Parameter p in ps )
          {
            if( 0 == values.Length )
            {
              values = " ";
            }
            else
            {
              values += ", ";
            }
            values += p.AsValueString();
          }

          Debug.Print(
            "Parameter {0} has {1} associated parameter{2}{3}{4}.",
            name,
            n,
            PluralSuffix( n ),
            ( 0 < n ? ":" : "" ),
            values );
#endif // LOOK_AT_ASSOCIATED_PARAMETERS
          #endregion // Look at associated parameters

        }
        List<string> keys = new List<string>( fps.Keys );
        keys.Sort();

        n = mgr.Types.Size;

        Debug.Print(
          "Family {0} has {1} type{2}{3}",
          doc.Title,
          n,
          Util.PluralSuffix( n ),
          Util.DotOrColon( n ) );

        foreach( FamilyType t in mgr.Types )
        {
          string name = t.Name;
          Debug.Print( "  {0}:", name );
          foreach( string key in keys )
          {
            FamilyParameter fp = fps[key];
            if( t.HasValue( fp ) )
            {
              string value
                = FamilyParamValueString( t, fp, doc );

              Debug.Print( "    {0} = {1}", key, value );
            }
          }
        }
      }

      #region Exercise ExtractPartAtomFromFamilyFile

      // by the way, here is a completely different way to
      // get at all the parameter values, and all the other
      // family information as well:

      bool exercise_this_method = false;

      if( doc.IsFamilyDocument && exercise_this_method )
      {
        string path = doc.PathName;
        if( 0 < path.Length )
        {
          app.Application.ExtractPartAtomFromFamilyFile(
            path, path + ".xml" );
        }
      }
      #endregion // Exercise ExtractPartAtomFromFamilyFile

      return Result.Failed;
    }
  }
}
0 Likes
Message 8 of 9

Anonymous
Not applicable

I've tried another way, by using the code as shown in a previous post. It didn't worked.

I send a link to my post in that thread, so everybody can see it.

https://forums.autodesk.com/t5/revit-api-forum/setting-familyparameter-via-api/m-p/6673567/highlight...

Thank you

0 Likes
Message 9 of 9

MarryTookMyCoffe
Collaborator
Collaborator

 

you have to start reading a SDK. This is a copy of your code:

    FamilyParameter familyParameter = doc.FamilyManager.AddParameter("Radius", BuiltInParameterGroup.PG_GEOMETRY, ParameterType.Length, false);
    ElementId elemId = familyParameter.Id;
doc.Regenerate();
doc.FamilyManager.Set( familyParameter, elemId);
doc.FamilyManager.SetFormula( familyParameter, "Height * 2");

1) you create a parameter type Lenght and try to set up value to him type Element ID

2) the element id you try to set is a element ID of parameter

3) this line:

document.FamilyManager.Set( familyParameter, diameterList[i]);

was for very good reason diameterList[i] just like it name suggest is type "double[]".

 

 

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes