M

GlobalParameter.Create

Description:
Creates a new Global Parameter in the given document.
Remarks:
Global parameters may be created only in Project documents, not in families.

Each global parameter must have a valid name that is unique within the document. To test whether a name is unique, use the IsUniqueName(Document, String) method.

While global parameters can be created with almost any type of data, there is a few types that are not currently supported, such as the ElementId type. Programmers can test whether a particular data type is appropriate for a global parameter by using the IsValidDataType(ParameterType) method.

Parameters are created as non-reporting initially, but programmers are free to modify the IsReporting property once a global parameter is created and happens to be of a type eligible for reporting.

public static GlobalParameter Create(
	Document document,
	string name,
	ParameterType datatype
)
Return Value GlobalParameter An instance of the new global parameter
/// <summary>
/// Creates a new Global Parameter of type Length, assigns it an initial value,
/// and uses it to label a set of input dimension elements.
/// </summary>
/// <param name="document">Revit project document in which to create the parameter.</param>
/// <param name="name">Name of the global parameter to create.</param>
/// <param name="value">A value the new global parameter is to have.</param>
/// <param name="dimensionsToLabel">A set of dimension to labe by the new global parameter.</param>
/// <returns>ElementId of the new GlobalParameter</returns>
public ElementId CreateNewGlobalParameter(Document document, String name, double value, ISet<ElementId> dimensionsToLabel)
{
    if (!GlobalParametersManager.AreGlobalParametersAllowed(document))
        throw new System.InvalidOperationException("Global parameters are not permitted in the given document");

    if (!GlobalParametersManager.IsUniqueName(document, name))
        throw new System.ArgumentException("Global parameter with such name already exists in the document", "name");

    ElementId gpid = ElementId.InvalidElementId;

    // creation of any element must be in a transaction
    using (Transaction trans = new Transaction(document, "Create Global Parameter"))
    {
        trans.Start();

        // create a GP with the given name and type Length
        GlobalParameter gp = GlobalParameter.Create(document, name, ParameterType.Length);
        if (gp != null)
        {
            // if created successfully, assign it a value
            // note: parameters of type Length accept Double values
            gp.SetValue(new DoubleParameterValue(value));

            // if a collection of dimensions was given, label them with this new parameter
            foreach (ElementId elemid in dimensionsToLabel)
            {
                // not just any dimension is allowed to be labeled
                // check first to avoid exceptions
                if (gp.CanLabelDimension(elemid))
                {
                    gp.LabelDimension(elemid);
                }
            }

            gpid = gp.Id;
        }
        trans.Commit();
    }

    return gpid;
}