public static Definition GetDefinition(string name, Model.DefinitionGroupType defGroupType, ParameterType paramType) { Definition def = null; DefinitionFile defFile = revitData.Application.OpenSharedParameterFile(); if (defFile == null) { CreateSharedParameterFile(Constant.ConstantValue.SharedParameterFileName); defFile = revitData.Application.OpenSharedParameterFile(); } DefinitionGroups defGroups = defFile.Groups; var defGroup = defGroups.get_Item(defGroupType.ToString()); if (defGroup == null) { defGroup = defGroups.Create(defGroupType.ToString()); } var defs = defGroup.Definitions; def = defs.get_Item(name); if (def == null) { def = defGroup.Definitions.Create(new ExternalDefinitionCreationOptions(name, paramType) { UserModifiable = true }); } return(def); }
/// <summary> ///This method is provided by the official website, here is the link ///https://www.revitapidocs.com/2019/f5066ef5-fa12-4cd2-ad0c-ca72ab21e479.htm /// </summary> /// <param name="app"></param> /// <param name="myDefinitionFile"></param> /// <returns></returns> public bool SetNewParameterToTypeWall(UIApplication app, DefinitionFile myDefinitionFile) { // Create a new group in the shared parameters file DefinitionGroups myGroups = myDefinitionFile.Groups; DefinitionGroup myGroup = myGroups.Create("Revit API Course"); // Create a type definition ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions("Note", ParameterType.Text); Definition myDefinition_CompanyName = myGroup.Definitions.Create(option); // Create a category set and insert category of wall to it CategorySet myCategories = app.Application.Create.NewCategorySet(); // Use BuiltInCategory to get category of wall Category myCategory = Category.GetCategory(app.ActiveUIDocument.Document, BuiltInCategory.OST_Walls); myCategories.Insert(myCategory);//add wall into the group. Of course, you can add multiple categories //Create an object of InstanceBinding according to the Categories, //The next line: I also provide the TypeBinding example here, but hide the code by comments //TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories); InstanceBinding instanceBinding = app.Application.Create.NewInstanceBinding(myCategories); // Get the BingdingMap of current document. BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings; //Bind the definitions to the document bool typeBindOK = bindingMap.Insert(myDefinition_CompanyName, instanceBinding, BuiltInParameterGroup.PG_TEXT); //The next two lines: I also provide the TypeBinding example here, but hide the code by comments //bool typeBindOK=bindingMap.Insert(myDefinition_CompanyName, typeBinding, //BuiltInParameterGroup.PG_TEXT); return(typeBindOK); }
} //end fun //這個方法也是被公開在官網上的,我只是小小修改並且翻譯了註解,參考以下網址 //http://www.revitapidocs.com/2017/f5066ef5-fa12-4cd2-ad0c-ca72ab21e479.htm private void AddNewParametersToCategories(UIApplication app, DefinitionFile myDefinitionFile, BuiltInCategory category, List <string> newParameters) { //在共用參數檔案當中先建立一個新的參數群組 DefinitionGroups myGroups = myDefinitionFile.Groups; DefinitionGroup myGroup = myGroups.Create("使用者定義參數群組"); // 創立參數定義,含參數名稱及參數型別 foreach (string paraName in newParameters) { ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions(paraName, ParameterType.Text); Definition myDefinition = myGroup.Definitions.Create(option); // 創建一個類別群組,將來在這個類別群組裡的所有類別都會被新增指定的參數 CategorySet myCategories = app.Application.Create.NewCategorySet(); // 創建一個類別.以牆為例 Category myCategory = Category.GetCategory(app.ActiveUIDocument.Document, category); myCategories.Insert(myCategory);//把牆類別插入類別群組,當然你可以插入不只一個類別 //以下兩者是亮點,「類別綁定」與「實體綁定」的結果不同,以本例而言我們需要的其實是實體綁定 //TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories); InstanceBinding instanceBinding = app.Application.Create.NewInstanceBinding(myCategories); //取得一個叫作BingdingMap的物件,以進行後續新增參數 BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings; // 最後將新增參數加到群組上頭,並且指定了instanceBiding的方法(也可替換為typeBinding) bool typeBindOK = bindingMap.Insert(myDefinition, instanceBinding, BuiltInParameterGroup.PG_TEXT); } }
// 定義參數與綁定類型的Function private bool setNewParameterToTypeWall(UIApplication app, DefinitionFile myDefinitionFile) { // 共用參數文檔中包含了Group,代表參數所屬的群組, // 而Group內又包含了Definition,也就是你想增加的參數, // 欲新增Definition首先必須新增一個Group DefinitionGroups myGroups = myDefinitionFile.Groups; DefinitionGroup myGroup = myGroups.Create("參數群組"); // 創建Definition的定義以及其儲存類型 ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions("聯絡人", ParameterType.Text); Definition myDefinition = myGroup.Definitions.Create(options); // 創建品類集並插入牆品類到裡面 CategorySet myCategories = app.Application.Create.NewCategorySet(); // 使用BuiltInCategory來取得牆品類 Category myCategory = app.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Walls); myCategories.Insert(myCategory); // 根據品類創建類型綁定的物件 TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories); // 取得當前文件中所有的類型綁定 BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings; // 將客製化的參數定義綁定到文件中 bool typeBindOK = bindingMap.Insert(myDefinition, typeBinding, BuiltInParameterGroup.PG_TEXT); return(typeBindOK); }
//這個方法也是被公開在官網上的,我只是小小修改並且翻譯了註解,參考以下網址 //http://www.revitapidocs.com/2017/f5066ef5-fa12-4cd2-ad0c-ca72ab21e479.htm public bool SetNewParameterToTypeWall(UIApplication app, DefinitionFile myDefinitionFile) { //在共用參數檔案當中先建立一個新的參數群組 DefinitionGroups myGroups = myDefinitionFile.Groups; DefinitionGroup myGroup = myGroups.Create("新參數群組"); // 創立參數定義,含參數名稱及參數型別 ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions("牆上塗鴉", ParameterType.Text); Definition myDefinition_CompanyName = myGroup.Definitions.Create(option); // 創建一個類別群組,將來在這個類別群組裡的所有類別都會被新增指定的參數 CategorySet myCategories = app.Application.Create.NewCategorySet(); // 創建一個類別.以牆為例 Category myCategory = Category.GetCategory(app.ActiveUIDocument.Document, BuiltInCategory.OST_Walls); myCategories.Insert(myCategory);//把牆類別插入類別群組,當然你可以插入不只一個類別 //以下兩者是亮點,「類別綁定」與「實體綁定」的結果不同,以本例而言我們需要的其實是實體綁定 //TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories); InstanceBinding instanceBinding = app.Application.Create.NewInstanceBinding(myCategories); //取得一個叫作BingdingMap的物件,以進行後續新增參數 BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings; // 最後將新增參數加到群組上頭,並且指定了instanceBiding的方法(也可替換為typeBinding) bool typeBindOK = bindingMap.Insert(myDefinition_CompanyName, instanceBinding, BuiltInParameterGroup.PG_TEXT); //bool typeBindOK=bindingMap.Insert(myDefinition_CompanyName, typeBinding, //BuiltInParameterGroup.PG_TEXT); return(typeBindOK); }
private void bttnCreate_Click(object sender, EventArgs e) { Definition definition = null; using (Transaction trans = new Transaction(m_doc)) { trans.Start("Create Shared Parameter"); try { defDictionary.Clear(); DefinitionGroups dGroups = definitionFile.Groups; DefinitionGroup dGroup = dGroups.get_Item("HOK Mass Parameters"); if (null == dGroup) { dGroup = dGroups.Create("HOK Mass Parameters"); } Definitions definitions = dGroup.Definitions; foreach (ListViewItem item in listViewMassParameter.Items) { definition = definitions.get_Item("Mass_" + item.Text); Parameter parameter = item.Tag as Parameter; if (null == definition && null != parameter) { ParameterType paramType = parameter.Definition.ParameterType; #if RELEASE2013 || RELEASE2014 definition = definitions.Create("Mass_" + parameter.Definition.Name, paramType); #elif RELEASE2015 ExternalDefinitonCreationOptions options = new ExternalDefinitonCreationOptions("Mass_" + parameter.Definition.Name, paramType); definition = definitions.Create(options); #elif RELEASE2016 ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions("Mass_" + parameter.Definition.Name, paramType); definition = definitions.Create(options); #endif } if (!defDictionary.ContainsKey(parameter.Definition.Name)) { defDictionary.Add(parameter.Definition.Name, definition); } } trans.Commit(); this.DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show("Failed to set shared parameters.\n" + ex.Message, "Form_Parameters:CreateButtonClick", MessageBoxButtons.OK, MessageBoxIcon.Warning); trans.RollBack(); } } }
/// <summary> /// Add a new parameter, "Unique ID", to the beams and slabs /// The following process should be followed: /// Open the shared parameters file, via the Document.OpenSharedParameterFile method. /// Access an existing group or create a new group, via the DefinitionFile.Groups property. /// Access an existing or create a new external parameter definition, /// via the DefinitionGroup.Definitions property. /// Create a new Binding with the categories to which the parameter will be bound /// using an InstanceBinding or a TypeBinding. /// Finally add the binding and definition to the document /// using the Document.ParameterBindings object. /// </summary> /// <returns>bool type, a value that signifies if add parameter was successful</returns> public bool SetNewParameterToBeamsAndSlabs() { //Open the shared parameters file // via the private method AccessOrCreateExternalSharedParameterFile DefinitionFile informationFile = AccessOrCreateExternalSharedParameterFile(); if (null == informationFile) { return(false); } // Access an existing or create a new group in the shared parameters file DefinitionGroups informationCollections = informationFile.Groups; DefinitionGroup informationCollection = null; informationCollection = informationCollections.get_Item("MyParameters"); if (null == informationCollection) { informationCollections.Create("MyParameters"); informationCollection = informationCollections.get_Item("MyParameters"); } // Access an existing or create a new external parameter definition // belongs to a specific group Definition information = informationCollection.Definitions.get_Item("Unique ID"); if (null == information) { ExternalDefinitionCreationOptions ExternalDefinitionCreationOptions = new ExternalDefinitionCreationOptions("Unique ID", Autodesk.Revit.DB.SpecTypeId.String.Text); informationCollection.Definitions.Create(ExternalDefinitionCreationOptions); information = informationCollection.Definitions.get_Item("Unique ID"); } // Create a new Binding object with the categories to which the parameter will be bound CategorySet categories = m_revit.Application.Create.NewCategorySet(); Category structuralFramingCategorie = null; Category floorsClassification = null; // use category in instead of the string name to get category structuralFramingCategorie = m_revit.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_StructuralFraming); floorsClassification = m_revit.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Floors); categories.Insert(structuralFramingCategorie); categories.Insert(floorsClassification); InstanceBinding caseTying = m_revit.Application.Create.NewInstanceBinding(categories); // Add the binding and definition to the document bool boundResult = m_revit.ActiveUIDocument.Document.ParameterBindings.Insert(information, caseTying); return(boundResult); }
/// <summary> /// Gets definition groups by sharing parameter file path. /// </summary> /// <param name="groups"></param> /// <param name="groupName"></param> /// <returns></returns> public static DefinitionGroup GetGroup(this DefinitionGroups groups, string groupName) { if (groups == null) { throw new ArgumentNullException(nameof(groups)); } if (groupName == null) { throw new ArgumentNullException(nameof(groupName)); } return(groups.get_Item(groupName) ?? groups.Create(groupName)); }
public static ExternalDefinition Create_SP(string sp_name , ParameterType type, string group_name) { DefinitionFile deFile = CachedApp.OpenSharedParameterFile(); // create new group in the shared paramters files DefinitionGroups groups = deFile.Groups; bool founded = false; Definition myDefinition = groups.First().Definitions.First(); foreach (DefinitionGroup dg in groups) { if (dg.Name == group_name) { ExternalDefinition exDef = dg.Definitions.get_Item(sp_name) as ExternalDefinition; if (exDef != null) { //exDef.Description return(exDef); } //ExternalDefinition exDef = dg.Definitions.get_Item("CompanyName") as ExternalDefinition; ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions(sp_name, type); myDefinition = dg.Definitions.Create(option); founded = true; break; } } if (!founded) { DefinitionGroup myGroup = groups.Create(group_name); // Create a type definition ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions(sp_name, type); myDefinition = myGroup.Definitions.Create(option); } ExternalDefinition eDef = myDefinition as ExternalDefinition; return(eDef); // }
private DefinitionGroup GetSharedGroup() { string sharedParametersFile = this.GetSharedParametersFile(); this.m_revitApp.SharedParametersFilename = sharedParametersFile; DefinitionFile definitionFile = this.m_revitApp.OpenSharedParameterFile(); if (definitionFile == null) { return(null); } DefinitionGroups groups = definitionFile.Groups; DefinitionGroup definitionGroup = groups.get_Item("RDBParameters"); if (definitionGroup == null) { groups.Create("RDBParameters"); definitionGroup = groups.get_Item("RDBParameters"); } return(definitionGroup); }
public bool SetNewParameterToInstanceWall(UIApplication app, DefinitionFile myDefinitionFile) { if (myDefinitionFile == null) { throw new Exception("No SharedParameter File!"); } // create a new group in the shared parameters file DefinitionGroups myGroups = myDefinitionFile.Groups; DefinitionGroup myGroup = myGroups.Create("MyParameters1"); // create an instance definition in definition group MyParameters ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions("Instance_ProductDate", ParameterType.Text); // Don't let the user modify the value, only the API option.UserModifiable = false; // Set tooltip option.Description = "Wall product date"; Definition myDefinition_ProductDate = myGroup.Definitions.Create(option); // create a category set and insert category of wall to it CategorySet myCategories = app.Application.Create.NewCategorySet(); // use BuiltInCategory to get category of wall Category myCategory = Category.GetCategory(app.ActiveUIDocument.Document, BuiltInCategory.OST_Walls); myCategories.Insert(myCategory); //Create an instance of InstanceBinding InstanceBinding instanceBinding = app.Application.Create.NewInstanceBinding(myCategories); // Get the BingdingMap of current document. BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings; // Bind the definitions to the document bool instanceBindOK = bindingMap.Insert(myDefinition_ProductDate, instanceBinding, BuiltInParameterGroup.PG_TEXT); return(instanceBindOK); }
/// <summary> /// Gets definition groups by sharing parameter file path. /// </summary> /// <param name="groups"></param> /// <param name="groupName"></param> /// <returns></returns> public static DefinitionGroup GetGroup(this DefinitionGroups groups, string groupName) { return(groups.get_Item(groupName) ?? groups.Create(groupName)); }
/// <summary> /// Add shared parameters needed in this sample. /// parameter 1: one string parameter named as "BasalOpening" which is used for customization of door opening for each country. /// parameter 2: one string parameter named as "InstanceOpening" to indicate the door's opening value. /// parameter 3: one YESNO parameter named as "Internal Door" to flag the door is internal door or not. /// </summary> /// <param name="app">Revit application.</param> public static void AddSharedParameters(UIApplication app) { // Create a new Binding object with the categories to which the parameter will be bound. CategorySet categories = app.Application.Create.NewCategorySet(); // get door category and insert into the CategorySet. Category doorCategory = app.ActiveUIDocument.Document.Settings.Categories. get_Item(BuiltInCategory.OST_Doors); categories.Insert(doorCategory); // create one instance binding for "Internal Door" and "InstanceOpening" parameters; // and one type binding for "BasalOpening" parameters InstanceBinding instanceBinding = app.Application.Create.NewInstanceBinding(categories); TypeBinding typeBinding = app.Application.Create.NewTypeBinding(categories); BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings; // Open the shared parameters file // via the private method AccessOrCreateSharedParameterFile DefinitionFile defFile = AccessOrCreateSharedParameterFile(app.Application); if (null == defFile) { return; } // Access an existing or create a new group in the shared parameters file DefinitionGroups defGroups = defFile.Groups; DefinitionGroup defGroup = defGroups.get_Item("DoorProjectSharedParameters"); if (null == defGroup) { defGroup = defGroups.Create("DoorProjectSharedParameters"); } // Access an existing or create a new external parameter definition belongs to a specific group. // for "BasalOpening" if (!AlreadyAddedSharedParameter(app.ActiveUIDocument.Document, "BasalOpening", BuiltInCategory.OST_Doors)) { Definition basalOpening = defGroup.Definitions.get_Item("BasalOpening"); if (null == basalOpening) { basalOpening = defGroup.Definitions.Create("BasalOpening", ParameterType.Text, false); } // Add the binding and definition to the document. bindingMap.Insert(basalOpening, typeBinding, BuiltInParameterGroup.PG_GEOMETRY); } // for "InstanceOpening" if (!AlreadyAddedSharedParameter(app.ActiveUIDocument.Document, "InstanceOpening", BuiltInCategory.OST_Doors)) { Definition instanceOpening = defGroup.Definitions.get_Item("InstanceOpening"); if (null == instanceOpening) { instanceOpening = defGroup.Definitions.Create("InstanceOpening", ParameterType.Text, true); } // Add the binding and definition to the document. bindingMap.Insert(instanceOpening, instanceBinding, BuiltInParameterGroup.PG_GEOMETRY); } // for "Internal Door" if (!AlreadyAddedSharedParameter(app.ActiveUIDocument.Document, "Internal Door", BuiltInCategory.OST_Doors)) { Definition internalDoorFlag = defGroup.Definitions.get_Item("Internal Door"); if (null == internalDoorFlag) { internalDoorFlag = defGroup.Definitions.Create("Internal Door", ParameterType.YesNo, true); } // Add the binding and definition to the document. bindingMap.Insert(internalDoorFlag, instanceBinding); } }
private static string CreateProjectParameters(Application app, Document doc, List <Category> RevitCategories, List <string> Parameters, string GroupName) { int newParameters = 0; int newCategories = 0; CategorySet tempCategorySet = app.Create.NewCategorySet(); foreach (Category item in RevitCategories) { IList <Element> fec = new FilteredElementCollector(doc).OfCategoryId(item.Id).WhereElementIsNotElementType().ToElements(); if (fec.Count > 0) { tempCategorySet.Insert(item); } } //get the parameter file string sharedParameterFile = doc.Application.SharedParametersFilename; DefinitionGroups existingGroups = app.OpenSharedParameterFile().Groups; DefinitionGroup groupName = existingGroups.get_Item(GroupName); if (existingGroups.get_Item(GroupName) == null) { groupName = existingGroups.Create(GroupName); //store under Data } try { foreach (string name in Parameters) { if (!tempCategorySet.IsEmpty) { //shared parameter does not exist if (groupName.Definitions.get_Item(name) == null) { InstanceBinding newInstanceBinding = app.Create.NewInstanceBinding(tempCategorySet); //cats ExternalDefinition externalDefinition = groupName.Definitions.Create(new ExternalDefinitionCreationOptions(name, ParameterType.Text)) as ExternalDefinition; doc.ParameterBindings.Insert(externalDefinition, newInstanceBinding, BuiltInParameterGroup.PG_DATA); newParameters++; } //shared parameter exists else { ExternalDefinition existingDefinition = groupName.Definitions.get_Item(name) as ExternalDefinition; ElementBinding binding = doc.ParameterBindings.get_Item(existingDefinition) as ElementBinding; //additional categories added to an existing shared parameter (re-insert) if (binding != null) { foreach (Category item in binding.Categories) { tempCategorySet.Insert(item); } InstanceBinding newInstanceBinding = app.Create.NewInstanceBinding(tempCategorySet); //cats doc.ParameterBindings.ReInsert(existingDefinition, newInstanceBinding, BuiltInParameterGroup.PG_DATA); newCategories++; } //shared parameter exists but has never been inserted (if the user undo the operation) else { InstanceBinding newInstanceBinding = app.Create.NewInstanceBinding(tempCategorySet); //cats doc.ParameterBindings.Insert(existingDefinition, newInstanceBinding, BuiltInParameterGroup.PG_DATA); newCategories++; } } } } } catch (Exception ex) { TaskDialog.Show("Error", ex.Message); } finally { app.SharedParametersFilename = sharedParameterFile; } //File.Delete(tempSharedParameterFile); return($"Parameters created: {newParameters}\nParameters categories updated: {newCategories}"); }
private void StoreSharedParams() { using (Transaction trans = new Transaction(doc)) { trans.Start("Store Shared Parameters"); try { string currentAssembly = System.Reflection.Assembly.GetAssembly(this.GetType()).Location; string definitionPath = Path.GetDirectoryName(currentAssembly) + "/Resources/Mass Shared Parameters.txt"; m_app.SharedParametersFilename = definitionPath; definitionFile = m_app.OpenSharedParameterFile(); FilteredElementCollector collector = new FilteredElementCollector(doc); Element element = null; List <string> activeSharedParams = new List <string>(); switch (massCategory) { case MassCategory.Rooms: element = collector.OfCategory(BuiltInCategory.OST_Rooms).WhereElementIsNotElementType().ToElements().First(); activeSharedParams = roomSharedParameters; break; case MassCategory.Areas: element = collector.OfCategory(BuiltInCategory.OST_Areas).WhereElementIsNotElementType().ToElements().First(); activeSharedParams = areaSharedParameters; break; case MassCategory.Floors: element = collector.OfCategory(BuiltInCategory.OST_Floors).WhereElementIsNotElementType().ToElements().First(); activeSharedParams = floorSharedParameters; break; } if (null != definitionFile) { DefinitionGroups dGroups = definitionFile.Groups; DefinitionGroup dGroup = dGroups.get_Item("HOK Mass Parameters"); if (null == dGroup) { dGroup = dGroups.Create("HOK Mass Parameters"); } Definitions definitions = dGroup.Definitions; Definition definition; foreach (string paramName in activeSharedParams) { definition = definitions.get_Item("Mass_" + paramName); if (null == definition) { #if RELEASE2013 || RELEASE2014 Parameter param = element.get_Parameter(paramName); definition = definitions.Create("Mass_" + param.Definition.Name, param.Definition.ParameterType); #elif RELEASE2015 Parameter param = element.LookupParameter(paramName); ExternalDefinitonCreationOptions options = new ExternalDefinitonCreationOptions("Mass_" + param.Definition.Name, param.Definition.ParameterType); definition = definitions.Create(options); #elif RELEASE2016 Parameter param = element.LookupParameter(paramName); ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions("Mass_" + param.Definition.Name, param.Definition.ParameterType); definition = definitions.Create(options); #endif } if (null != definition && !defDictionary.ContainsKey(paramName)) { defDictionary.Add(paramName, definition); } } } trans.Commit(); } catch (Exception ex) { MessageBox.Show("Failed to store shared parameters.\n" + ex.Message, "Form_RoomMass:StoreSharedParams", MessageBoxButtons.OK, MessageBoxIcon.Warning); trans.RollBack(); } } }
/// <summary> /// Creates new shared parameter if it does not exist and bind it to the type or instance of the objects /// </summary> /// <param name="paramType">Type of the parameter</param> /// <param name="categoriesForParam">Category of elements to bind the parameter to</param> /// <param name="defaultGroupName">Group name of the parameters</param> /// <param name="parameterName">Name of the parameter</param> /// <returns>TRUE if shared parameter is created, FALSE otherwise</returns> public static bool SetNewSharedParameter(this Element element, ParameterType paramType, CategorySet categoriesForParam, string parameterName, BuiltInParameterGroup group) { string defaultGroupName = "4P_imported_parameters"; //this is a hack to avoid multiple parameters with the same name. if (categoriesForParam == null) { return(false); } if (categoriesForParam.Size == 0) { return(false); } foreach (Category cat in categoriesForParam) { if (cat == null) { return(false); } if (!cat.AllowsBoundParameters) { return(false); } } Application application = element.Document.Application; Document document = element.Document; if (_definitionFile == null) { //ask for the location of the shared parameters file var dialog = new Microsoft.Win32.OpenFileDialog(); dialog.CheckFileExists = false; dialog.Title = "Set shared pID file..."; dialog.ShowDialog(); string shrFilePath = dialog.FileName; if (shrFilePath == null) { _definitionFile = application.OpenSharedParameterFile(); if (_definitionFile == null) { SetSharedParamFileInUserProgramFolder(document); } } else { SetDefinitionFile(element, shrFilePath); } } if (_definitionFile == null) { throw new Exception("Definition file must be set before creation of the new parameters."); } DefinitionFile myDefinitionFile = _definitionFile; // Get parameter or create new one DefinitionGroups myGroups = myDefinitionFile.Groups; Definition myDefinition = null; bool found = false; foreach (DefinitionGroup gr in myGroups) { foreach (Definition def in gr.Definitions) { if (def.Name == parameterName) { myDefinition = def; found = true; break; } } if (found) { break; } } //if there is not such a parameter new one is created in default group if (myDefinition == null) { DefinitionGroup myGroup = myGroups.get_Item(defaultGroupName); if (myGroup == null) { myGroup = myGroups.Create(defaultGroupName); } // Create a type definition myDefinition = myGroup.Definitions.get_Item(parameterName); if (myDefinition == null) { myDefinition = myGroup.Definitions.Create(parameterName, paramType); } } //Create an object of TypeBinding or InstanceBinding according to the Categories and "typeBinding" variable Binding binding = null; // Get the BingdingMap of current document. BindingMap bindingMap = document.ParameterBindings; binding = bindingMap.get_Item(myDefinition); bool bindOK = false; if (!element.CanHaveTypeAssigned() && !(element is Material) && !(element is ProjectInfo)) { if (binding != null) { TypeBinding typeBinding = binding as TypeBinding; if (typeBinding == null) { throw new Exception("Parameter with this definition already exists and is bound to instances. It cannot be bound to the type at the same time"); } foreach (Category cat in categoriesForParam) { typeBinding.Categories.Insert(cat); } bindOK = bindingMap.ReInsert(myDefinition, binding, group); return(bindOK); } else { binding = application.Create.NewTypeBinding(categoriesForParam); } } else { if (binding != null) { InstanceBinding instBinding = binding as InstanceBinding; if (instBinding == null) { throw new Exception("Parameter with this definition already exists and is bound to types. It cannot be bound to the instance at the same time"); } foreach (Category cat in categoriesForParam) { instBinding.Categories.Insert(cat); } bindOK = bindingMap.ReInsert(myDefinition, binding, group); return(bindOK); } else { binding = application.Create.NewInstanceBinding(categoriesForParam); } } // Bind the definitions to the document bindOK = bindingMap.Insert(myDefinition, binding, group); return(bindOK); }