using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.ApplicationServices; namespace AddParameter { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)] public class Command : IExternalCommand { Document _doc; UIDocument _uidoc; public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; _doc = doc; _uidoc = uidoc; var refe = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Please select element"); Element selectedElement = doc.GetElement(refe) as Element; string shapeFile = @"E:【学习】MyRevitSDKSampleAddParameterParameter" + "\RevitParameters.txt"; this._doc.Application.SharedParametersFilename = shapeFile; Transaction trans = new Transaction(doc, "CreatSharedParams"); trans.Start(); CreatShareParam("C自定义参数_1", ParameterType.Text, false, BuiltInParameterGroup.PG_TEXT, selectedElement); SetParamValue("C自定义参数_1", "我开发了很多Revit插件和功能", selectedElement); trans.Commit(); return Result.Succeeded; } public bool CreatShareParam(string parameterName, ParameterType parameterType, bool canModify, BuiltInParameterGroup parameterGroup, Element element) { if (element.Category == null) { return false; } try { Application app = this._doc.Application; DefinitionFile dfile = app.OpenSharedParameterFile(); DefinitionGroup dg; if (!element.Category.AllowsBoundParameters) { return false; } Category category = element.Category;//选择的元素添加 Categories cats = _doc.Settings.Categories;//测试绑定所有元素 dg = dfile.Groups.get_Item("测试共享参数组"); if(dg==null) dg = dfile.Groups.Create("测试共享参数组"); if (dg != null) { ExternalDefinitionCreationOptions edco = new ExternalDefinitionCreationOptions(parameterName, parameterType); edco.UserModifiable = canModify; Definition df; try { df = dg.Definitions.Create(edco); } catch (Exception) { df = dg.Definitions.get_Item(edco.Name); } var instanceBinding = _doc.ParameterBindings.get_Item(df) as InstanceBinding; foreach (Category cat in cats) { if (!cat.AllowsBoundParameters)//判断类别是否允许绑定参数 continue; if (instanceBinding != null) { if (!instanceBinding.Categories.Contains(cat)) { instanceBinding.Categories.Insert(cat); } _doc.ParameterBindings.ReInsert(df, instanceBinding); } else { Autodesk.Revit.DB.CategorySet categorySet = app.Create.NewCategorySet(); categorySet.Insert(cat); instanceBinding = app.Create.NewInstanceBinding(categorySet); _doc.ParameterBindings.Insert(df, instanceBinding); } } //if (instanceBinding != null) //{ // if (!instanceBinding.Categories.Contains(category)) // instanceBinding.Categories.Insert(category); // _doc.ParameterBindings.ReInsert(df, instanceBinding); //} //else //{ // Autodesk.Revit.DB.CategorySet categorySet = app.Create.NewCategorySet(); // categorySet.Insert(category); // instanceBinding = app.Create.NewInstanceBinding(categorySet); // _doc.ParameterBindings.Insert(df, instanceBinding); //} } return true; } catch (Exception ex) { TaskDialog.Show("error in creating parameters:", "name:" + parameterName + " " + "id:" + element.Id.ToString()); throw; } } public void SetParamValue(string parameterName, string parameterValue, Element element) { try { if (element.LookupParameter(parameterName) != null && element.LookupParameter(parameterName).IsReadOnly == false) { element.LookupParameter(parameterName).Set(parameterValue); } } catch (Exception ex) { TaskDialog.Show("Set Parameter Value Error", ex.ToString()); return; } } } }