Global |
Exception | Condition |
---|---|
ArgumentException | The given value argument is not a valid instance of ParameterValue! -or- The given parameter value arguments is not of the storage type the global parameter expects. |
ArgumentNullException | A non-optional argument was null |
InvalidOperationException | This is a formula-driven parameter. As such it does not allow the current operation. -or- This is a dimension-driven parameter. As such it does not allow the current operation. |
Note that a value can only be set for parameters that are neither formula-driven nor dimension-driven, as those have their values determined by evaluating the formula or by the dimension, respectively.
The argument this method accepts is of the same type of ParameterValue that is returned by GetValue. However, the type can also be easily deduced: Text parameters accept only StringParameterValue. Integer and YesNo parameters accept only IntegerParameterValue. All other types of parameters accept only DoubleParameterValue. Curiously, the actual value of a YesNo parameter can be only either 0 or 1.
/// <summary> /// Modifies values of certain types of global parameters so that /// - integer values will be incremented by one /// - double values will be multiplied by two /// - boolean values will be negated /// </summary> /// <param name="document">Revit project document in which we create global parameters.</param> /// <param name="gpset">A set of ElementIds of global parameter elements</param> /// <returns>Number of actually modified values</returns> public int ModifyGlobalParametersValues(Document document, ISet<ElementId> gpset) { int nChangedValues = 0; // number of values changed // we need a transaction to modify the model using (Transaction trans = new Transaction(document, "Change global parameters values")) { trans.Start(); foreach(ElementId gpid in gpset) { // filter out element Ids that are not of a global parameters // (in production code, this would be an assert-able situation) if (!GlobalParametersManager.IsValidGlobalParameter(document, gpid)) continue; // get the current value of the global parameter GlobalParameter gp = document.GetElement(gpid) as GlobalParameter; ParameterValue gpvalue = gp.GetValue(); // test whether it is a Double or Integer if (gp.GetType() == typeof(DoubleParameterValue)) { DoubleParameterValue dvalue = gpvalue as DoubleParameterValue; dvalue.Value *= 2.0; gp.SetValue(dvalue); nChangedValues += 1; } else if (gp.GetType() == typeof(IntegerParameterValue)) { // Integer values may represent Boolean parameters too IntegerParameterValue ivalue = gpvalue as IntegerParameterValue; if (gp.GetDefinition().GetDataType() == SpecTypeId.Boolean.YesNo) { ivalue.Value = (ivalue.Value == 0) ? 1 : 0; } else // common integers { ivalue.Value += 1; } gp.SetValue(ivalue); nChangedValues += 1; } } trans.Commit(); } return nChangedValues; }