ParameterElements store information about a particular user-defined parameter in the document
User-defined parameters are stored in the document and represented by the ParameterElement class. The subclass SharedParameterElement represents a shared parameter loaded into the document.
Once a shared parameter has been loaded into a document, information about it can be retrieved from the SharedParameterElement class. SharedParameterElement inherits the GetDefinition() method from the parent ParameterElement class. GetDefinition() returns the InternalDefinition that represents the definition for the parameter in the document, as opposed to the ExternalDefinition for a shared parameter that is stored in the shared parameter file. SharedParameterElement also provides access to the Guid that identifies the shared parameter via the GuidValue property.
The static Create() method on this class can create a new SharedParameterElement in the document from an ExternalDefinition.
The static Lookup() method can retrieve a SharedParameterElement from a given Guid.
In the following example, a schedule contains a field representing the value of a shared parameter. The definition for the shared parameter is retrieved from the SharedParameterElement.
Code Region: Getting the definition of a shared parameter |
// Check if a given shared parameter in a schedule can vary across groups public bool CanParamVaryAcrossGroups(ViewSchedule schedule, string sharedParamName) { bool variesAcrossGroups = false; int numFields = schedule.Definition.GetFieldCount(); // Find the field with the given name for (int i = 0; i < numFields; i++) { ScheduleField field = schedule.Definition.GetField(i); if (field.GetName().Contains(sharedParamName)) { // Get the SharedParameterElement from the field's parameter id SharedParameterElement spe = schedule.Document.GetElement(field.ParameterId) as SharedParameterElement; if (spe != null) { InternalDefinition definition = spe.GetDefinition(); variesAcrossGroups = definition.VariesAcrossGroups; } } } return variesAcrossGroups; } |
SharedParameterElements are especially useful when working with RebarContainers. A shared parameter can be added as an override to the parameters manager for a RebarContainer. The shared parameter does not need to be bound to any categories to be added as an override. The following example adds a given shared parameter as an override to a RebarContainer.
Code Region: Using a SharedParameterElement as an override for a RebarContainer |
// Find the named shared parameter and add it as an override to the parameter manger for the given RebarContainer void AddSharedParameterOverride(RebarContainer container, string sharedParamName) { // find the shared parameter guid FilteredElementCollector collector = new FilteredElementCollector(container.Document); collector.OfClass(typeof(SharedParameterElement)); IEnumerable<SharedParameterElement> paramCollector = collector.Cast<SharedParameterElement>(); foreach (SharedParameterElement spe in paramCollector) { if (spe.Name.CompareTo(sharedParamName) == 0) { RebarContainerParameterManager paramManager = container.GetParametersManager(); paramManager.AddSharedParameterAsOverride(spe.Id); break; } } } |