/***************************************************/ /**** Public methods ****/ /***************************************************/ public static Definition SharedParameter(Document document, string parameterName, ParameterType parameterType, string parameterGroup, bool instance, IEnumerable <Category> categories) { // Inspired by https://github.com/DynamoDS/DynamoRevit/blob/master/src/Libraries/RevitNodes/Elements/Parameter.cs if (categories != null && !categories.Any()) { BH.Engine.Reflection.Compute.RecordError($"Parameter {parameterName} of type {LabelUtils.GetLabelFor(parameterType)} could not be created because no category bindings were provided."); return(null); } // get current shared parameter file string sharedParameterFile = document.Application.SharedParametersFilename; // if the file does not exist, throw an error if (string.IsNullOrWhiteSpace(sharedParameterFile) || !System.IO.File.Exists(sharedParameterFile)) { BH.Engine.Reflection.Compute.RecordError("The shared parameters file specified in the document does not exist."); return(null); } // Create new parameter group if it does not exist yet DefinitionGroup groupDef = document.Application.OpenSharedParameterFile().Groups.get_Item(parameterGroup); if (groupDef == null) { try { groupDef = document.Application.OpenSharedParameterFile().Groups.Create(parameterGroup); } catch { BH.Engine.Reflection.Compute.RecordError("New group could not be created in the active document's shared parameter file. Please try using an existing group or unlocking the shared parameter file."); return(null); } } // If the parameter definition does exist return it bool bindings = false; Definition def = groupDef.Definitions.get_Item(parameterName); if (def != null) { if (document.ParameterBindings.Contains(def)) { BH.Engine.Reflection.Compute.RecordWarning($"Parameter {parameterName} already exists in group {parameterGroup}. It already has category bindings, they were not updated - please make sure they are correct."); bindings = true; } else { BH.Engine.Reflection.Compute.RecordWarning($"Parameter {parameterName} already exists in group {parameterGroup}. It did not have any category bindings, so input bindings were applied."); } } else { def = groupDef.Definitions.Create(new ExternalDefinitionCreationOptions(parameterName, parameterType)) as ExternalDefinition; } if (!bindings) { // Apply instance or type binding CategorySet paramCategories = (categories == null) ? document.CategoriesWithBoundParameters() : Create.CategorySet(document, categories); Binding bin = (instance) ? (Binding)document.Application.Create.NewInstanceBinding(paramCategories) : (Binding)document.Application.Create.NewTypeBinding(paramCategories); document.ParameterBindings.Insert(def, bin); } return(def); }
/***************************************************/ /**** Public methods ****/ /***************************************************/ public static Definition ProjectParameter(Document document, string parameterName, ParameterType parameterType, BuiltInParameterGroup parameterGroup, bool instance, IEnumerable <Category> categories) { // Inspired by https://github.com/DynamoDS/DynamoRevit/blob/master/src/Libraries/RevitNodes/Elements/Parameter.cs if (categories != null && !categories.Any()) { BH.Engine.Reflection.Compute.RecordError($"Parameter {parameterName} of type {LabelUtils.GetLabelFor(parameterType)} could not be created because no category bindings were provided."); return(null); } // Look for existing parameter definition bool bindings = false; IEnumerable <Definition> existingDefs = new FilteredElementCollector(document).OfClass(typeof(SharedParameterElement)).Cast <SharedParameterElement>().Select(x => x.GetDefinition()); Definition def = existingDefs.FirstOrDefault(x => x.Name == parameterName && x.ParameterGroup == parameterGroup); if (def != null) { if (document.ParameterBindings.Contains(def)) { BH.Engine.Reflection.Compute.RecordWarning($"Parameter {parameterName} already exists in group {LabelUtils.GetLabelFor(parameterGroup)}. It already has category bindings, they were not updated - please make sure they are correct."); bindings = true; } else { BH.Engine.Reflection.Compute.RecordWarning($"Parameter {parameterName} already exists in group {LabelUtils.GetLabelFor(parameterGroup)}. It did not have any category bindings, so input bindings were applied."); } } else { // buffer the current shared parameter file name and apply a new empty parameter file instead string sharedParameterFile = document.Application.SharedParametersFilename; string tempSharedParameterFile = System.IO.Path.GetTempFileName() + ".txt"; using (System.IO.File.Create(tempSharedParameterFile)) { } document.Application.SharedParametersFilename = tempSharedParameterFile; // Create a new shared parameter, since the file is empty everything has to be created from scratch def = document.Application.OpenSharedParameterFile() .Groups.Create("TempParameterGroup").Definitions.Create( new ExternalDefinitionCreationOptions(parameterName, parameterType)) as ExternalDefinition; // apply old shared parameter file document.Application.SharedParametersFilename = sharedParameterFile; // delete temp shared parameter file System.IO.File.Delete(tempSharedParameterFile); } if (!bindings) { // Apply instance or type binding CategorySet paramCategories = (categories == null) ? document.CategoriesWithBoundParameters() : Create.CategorySet(document, categories); Binding bin = (instance) ? (Binding)document.Application.Create.NewInstanceBinding(paramCategories) : (Binding)document.Application.Create.NewTypeBinding(paramCategories); document.ParameterBindings.Insert(def, bin, parameterGroup); } return(def); }