Skip to content

Commit

Permalink
Merge pull request #1596 from sm6srw/DYN-488_Revit2017
Browse files Browse the repository at this point in the history
DYN-488 Revit 2017
  • Loading branch information
sm6srw authored Mar 3, 2017
2 parents 9399d17 + 341387b commit 6022fa5
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/Libraries/RevitNodes/Elements/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,55 @@ public void Tessellate(IRenderPackage package, TessellationParameters parameters
// or transactions and which must necessarily be threaded in a specific way.
}


/// <summary>
/// Get a parameter by name of an element
/// </summary>
/// <param name="parameterName">The name of the parameter.</param>
/// <returns></returns>
private Autodesk.Revit.DB.Parameter GetParameterByName(string parameterName)
{
//
// Parameter names are not unique on a given element. There are several valid cases where
// duplicated parameter names can be found in the Parameter Set.
//
// The most common ones are:
//
// 1. Multiple built-in parameters with the same name
// This is a common implementation pattern when a different parameter behavior is wanted
// for different scopes. For example, lets say that you want a parameter to be writable
// in the property palette but read-only in a schedule view. The easiest way to accomplish
// this would be to add two parameters. One that is read-write and one that is read-only.
// These parameters will both have the same name and they will share the same getter.
//
// 2. Built-in parameters and User parameters with the same name
// This happens when a loadable family defines a user parameter with the same name
// as a built-in parameter.
//
// Currently, we try to resolve this and get consistent results by
// 1. Get all parameters for the given name
// 2. Sort parameters by ElementId - This will give us built-in parameters first (ID's for built-ins are always < -1)
// 3. If it exist: Use the first writable parameter
// 4. Otherwise: Use the first read-only parameter
//
var allParams =
InternalElement.Parameters.Cast<Autodesk.Revit.DB.Parameter>()
.Where(x => string.CompareOrdinal(x.Definition.Name, parameterName) == 0)
.OrderBy(x => x.Id.IntegerValue);

var param = allParams.FirstOrDefault(x => x.IsReadOnly == false) ?? allParams.FirstOrDefault();

return param;
}

/// <summary>
/// Get the value of one of the element's parameters.
/// </summary>
/// <param name="parameterName">The name of the parameter whose value you want to obtain.</param>
/// <returns></returns>
public object GetParameterValueByName(string parameterName)
{

var param =
// We don't use Element.GetOrderedParameters(), it only returns ordered parameters
// as show in the UI
InternalElement.Parameters.Cast<Autodesk.Revit.DB.Parameter>()
// Element.Parameters returns a differently ordered list on every invocation.
// We must sort it to get sensible results.
.OrderBy(x => x.Id.IntegerValue)
.FirstOrDefault(x => x.Definition.Name == parameterName);
var param = GetParameterByName(parameterName);

if (param == null || !param.HasValue)
return string.Empty;
Expand Down Expand Up @@ -382,7 +415,7 @@ public Element OverrideInView(Revit.Filter.OverrideGraphicSettings overrides, bo
/// <param name="value">The value.</param>
public Element SetParameterByName(string parameterName, object value)
{
var param = InternalElement.Parameters.Cast<Autodesk.Revit.DB.Parameter>().FirstOrDefault(x => x.Definition.Name == parameterName);
var param = GetParameterByName(parameterName);

if (param == null)
throw new Exception(Properties.Resources.ParameterNotFound);
Expand Down

0 comments on commit 6022fa5

Please sign in to comment.