public static bool CreateSharedProjectParameters(Document doc, string param, CategorySet catSet) { DefinitionFile sharedParamsFile = GetSharedParamsFile(doc); if (null == sharedParamsFile) { throw new Exception("Shared Paremeter file does not exist.\nSet or Create the project Shared Parameter file."); } DefinitionGroup sharedParamsGroup = GetCreateSharedParamsGroup(sharedParamsFile, "Group6Face"); Definition definition = GetCreateSharedParamsDefinition(sharedParamsGroup, ParameterType.Text, param, true); BindingMap bindMap = doc.ParameterBindings; Autodesk.Revit.DB.Binding binding = bindMap.get_Item(definition); bindMap.ReInsert(definition, binding); InstanceBinding instanceBinding = doc.Application.Create.NewInstanceBinding(catSet); try { if (!doc.ParameterBindings.Insert(definition, instanceBinding)) { doc.ParameterBindings.ReInsert(definition, instanceBinding); } return(true); } catch (Exception ex) { throw new Exception("Uneable to create parameter bindings.\n" + ex.Message); } }
private void TransactionCommit(Document doc, ExternalDefinition def, Autodesk.Revit.DB.Binding binding, BuiltInParameterGroup group) { using (Transaction transNew = new Transaction(doc, "Create project parameter")) { try { transNew.Start(); BindingMap map = doc.ParameterBindings; if (SelectParameters.OverwriteParam == true) { map.ReInsert(def, binding, group); } map.Insert(def, binding, group); } catch (Exception e) { transNew.RollBack(); MessageBox.Show(e.ToString()); } transNew.Commit(); } }
private void AddSharedParameters(Application app, Document doc) { //Save the previous shared param file path string previousSharedParam = app.SharedParametersFilename; //Extract shared param to a txt file string tempPath = System.IO.Path.GetTempPath(); string SPPath = Path.Combine(tempPath, "bimsyncSharedParameter.txt"); if (!File.Exists(SPPath)) { //extract the familly List <string> files = new List <string>(); files.Add("bimsyncSharedParameter.txt"); Services.ExtractEmbeddedResource(tempPath, "bimsync.Resources", files); } //set the shared param file app.SharedParametersFilename = SPPath; //Define a category set containing the project properties CategorySet myCategorySet = app.Create.NewCategorySet(); Categories categories = doc.Settings.Categories; Category projectCategory = categories.get_Item(BuiltInCategory.OST_ProjectInformation); myCategorySet.Insert(projectCategory); //Retrive shared parameters DefinitionFile myDefinitionFile = app.OpenSharedParameterFile(); DefinitionGroup definitionGroup = myDefinitionFile.Groups.get_Item("bimsync"); foreach (Definition paramDef in definitionGroup.Definitions) { // Get the BingdingMap of current document. BindingMap bindingMap = doc.ParameterBindings; //the parameter does not exist if (!bindingMap.Contains(paramDef)) { //Create an instance of InstanceBinding InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategorySet); bindingMap.Insert(paramDef, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); } //the parameter is not added to the correct categories else if (bindingMap.Contains(paramDef)) { InstanceBinding currentBinding = bindingMap.get_Item(paramDef) as InstanceBinding; currentBinding.Categories = myCategorySet; bindingMap.ReInsert(paramDef, currentBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); } } //Reset to the previous shared parameters text file app.SharedParametersFilename = previousSharedParam; File.Delete(SPPath); }
private bool AddParameter(Element element) { bool instanceBindOK = false; try { DefinitionFile definitionFile = m_app.Application.OpenSharedParameterFile(); DefinitionGroup defGroup = definitionFile.Groups.get_Item("HOK BCF"); if (null == defGroup) { defGroup = definitionFile.Groups.Create("HOK BCF"); } foreach (string defName in bcfParameters) { #if RELEASE2015 Parameter parameter = element.LookupParameter(defName); #else Parameter parameter = element.get_Parameter(defName); #endif if (null != parameter) { continue; } Definition definition = defGroup.Definitions.get_Item(defName); if (null == definition) { #if RELEASE2015 ExternalDefinitonCreationOptions option = new ExternalDefinitonCreationOptions(defName, ParameterType.Text); definition = defGroup.Definitions.Create(option); #else definition = defGroup.Definitions.Create(defName, ParameterType.Text); #endif } BindingMap bindingMap = m_app.ActiveUIDocument.Document.ParameterBindings; InstanceBinding instanceBinding = bindingMap.get_Item(definition) as InstanceBinding; if (null != instanceBinding) { instanceBinding.Categories.Insert(element.Category); instanceBindOK = bindingMap.ReInsert(definition, instanceBinding); } else { CategorySet categories = m_app.Application.Create.NewCategorySet(); categories.Insert(element.Category); instanceBinding = m_app.Application.Create.NewInstanceBinding(categories); instanceBindOK = bindingMap.Insert(definition, instanceBinding, BuiltInParameterGroup.PG_TEXT); } if (!instanceBindOK) { break; } } } catch (Exception ex) { MessageBox.Show("Failed to add project parameters for element " + element.Name + "\n" + ex.Message, "CommandForm:AddParameter", MessageBoxButtons.OK, MessageBoxIcon.Warning); instanceBindOK = false; } return(instanceBindOK); }
public static Parameter AddParameterBase(Document doc, Element element, Category category, string parameterName, int parameterSetId, ForgeTypeId specId) { bool isElementType = (element is ElementType); Definitions definitions = isElementType ? Importer.TheCache.TypeGroupDefinitions : Importer.TheCache.InstanceGroupDefinitions; bool newlyCreated = false; Definition definition = definitions.get_Item(parameterName); if (definition == null) { ExternalDefinitionCreationOptions option = new ExternalDefinitionCreationOptions(parameterName, specId); definition = definitions.Create(option); if (definition == null) { Importer.TheLog.LogError(parameterSetId, "Couldn't create parameter: " + parameterName, false); return(null); } newlyCreated = true; } Guid guid = (definition as ExternalDefinition).GUID; Parameter parameter = null; ElementBinding binding = null; bool reinsert = false; if (!newlyCreated) { BindingMap bindingMap = Importer.TheCache.GetParameterBinding(doc); binding = bindingMap.get_Item(definition) as ElementBinding; reinsert = (binding != null); } if (binding == null) { if (isElementType) { binding = new TypeBinding(); } else { binding = new InstanceBinding(); } } // The binding can fail if we haven't identified a "bad" category above. Use try/catch as a safety net. try { if (!reinsert || !binding.Categories.Contains(category)) { binding.Categories.Insert(category); BindingMap bindingMap = Importer.TheCache.GetParameterBinding(doc); if (reinsert) { bindingMap.ReInsert(definition, binding, BuiltInParameterGroup.PG_IFC); } else { bindingMap.Insert(definition, binding, BuiltInParameterGroup.PG_IFC); } } parameter = element.get_Parameter(guid); } catch { } if (parameter == null) { Importer.TheLog.LogError(parameterSetId, "Couldn't create parameter: " + parameterName, false); } return(parameter); }
private void AddSharedParameters(Application app, Document doc, CategorySet myCategorySet) { //Save the previous shared param file path string previousSharedParam = app.SharedParametersFilename; //Extract shared param to a txt file string tempPath = System.IO.Path.GetTempPath(); string SPPath = Path.Combine(tempPath, "FileProperties.txt"); if (!File.Exists(SPPath)) { //extract the familly List <string> files = new List <string>(); files.Add("FileProperties.txt"); Tools.ExtractEmbeddedResource(tempPath, "TimeStamp.Resources", files); } //set the shared param file app.SharedParametersFilename = SPPath; //Retrive shared parameters DefinitionFile myDefinitionFile = app.OpenSharedParameterFile(); DefinitionGroup definitionGroup = myDefinitionFile.Groups.get_Item("FileProperties"); foreach (Definition paramDef in definitionGroup.Definitions) { // Get the BingdingMap of current document. BindingMap bindingMap = doc.ParameterBindings; //the parameter does not exist if (!bindingMap.Contains(paramDef)) { //Create an instance of InstanceBinding InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategorySet); bindingMap.Insert(paramDef, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); } //the parameter is not added to the correct categories else if (bindingMap.Contains(paramDef)) { InstanceBinding currentBinding = bindingMap.get_Item(paramDef) as InstanceBinding; currentBinding.Categories = myCategorySet; bindingMap.ReInsert(paramDef, currentBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); } } //SetAllowVaryBetweenGroups for these parameters string[] parameterNames = { "BIM42_Date", "BIM42_Version", "BIM42_File", "BIM42_Discipline" }; DefinitionBindingMapIterator definitionBindingMapIterator = doc.ParameterBindings.ForwardIterator(); definitionBindingMapIterator.Reset(); while (definitionBindingMapIterator.MoveNext()) { InternalDefinition paramDef = definitionBindingMapIterator.Key as InternalDefinition; if (paramDef != null) { if (parameterNames.Contains(paramDef.Name)) { paramDef.SetAllowVaryBetweenGroups(doc, true); } } } //Reset to the previous shared parameters text file app.SharedParametersFilename = previousSharedParam; File.Delete(SPPath); }
/// <summary> /// 将外部共享参数创建为项目参数,并将其绑定到指定的类别 /// </summary> /// <param name="transDoc"> bindingMap.Insert 与 bindingMap.ReInsert 方法必须在事务内才能执行</param> /// <param name="doc"></param> /// <param name="facenCategoryId"> 面层对象所属的类别 </param> public void BindParametersToCategory(Transaction transDoc, Document doc, ElementId facenCategoryId) { // 打开共享文件 Autodesk.Revit.ApplicationServices.Application app = doc.Application; string OriginalSharedFileName = app.SharedParametersFilename; // Revit程序中,原来的共享文件路径 app.SharedParametersFilename = ProjectPath.SharedParameters; // 设置Revit的共享参数文件 DefinitionFile myDefinitionFile = app.OpenSharedParameterFile(); // 如果没有找到对应的文件,则打开时不会报错,而是直接返回Nothing // app.SharedParametersFilename = OriginalSharedFileName; // 将Revit程序中的共享文件路径还原,以隐藏插件程序中的共享参数文件。 // create a new group in the shared parameters file DefinitionGroup myGroup = myDefinitionFile.Groups.get_Item(FaceWallParameters.sp_Group_Face); // 提取此共享参数组中的每一个参数,并赋值给指定的类别 ExternalDefinition exdef_FaceIdTag = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_FaceIdTag); ExternalDefinition exdef_FaceType = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_FaceType); ExternalDefinition exdef_Volumn = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_Volumn); ExternalDefinition exdef_Area = (ExternalDefinition)myGroup.Definitions.get_Item(FaceWallParameters.sp_Area); // BindingMap bindingMap = doc.ParameterBindings; Category myCategory = Category.GetCategory(doc, facenCategoryId); // --------------------------------------------------------------------------------------------------------- // 判断指定的类别中是否绑定有此 "面层标识 FaceIdTag" 参数 bool faceParaHasInsertedIntoThatCategory = false; bool parameterHasBeenAddedToProject = false; if (bindingMap.Contains(exdef_FaceIdTag)) { Autodesk.Revit.DB.Binding parameterBinding = doc.ParameterBindings.get_Item(exdef_FaceIdTag); parameterHasBeenAddedToProject = true; if (parameterBinding is InstanceBinding) { // 外部共享参数 exdef_FaceIdTag 在此文档中 绑定到了哪些类别 CategorySet bindedCategories = ((InstanceBinding)parameterBinding).Categories; if (bindedCategories.Contains(myCategory)) { faceParaHasInsertedIntoThatCategory = true; } } } // --------------------------------------------------------------------------------------------------------- // 如果此参数"面层标识 FaceIdTag"还没有被添加到项目参数中,则先将用 Insert 其进行添加 if (!parameterHasBeenAddedToProject) { // 在“项目参数”中添加此参数,并为其指定一个绑定的类别。 CategorySet myCategories = app.Create.NewCategorySet(); myCategories.Insert(myCategory); //Create an instance of InstanceBinding InstanceBinding instanceBinding = app.Create.NewInstanceBinding(myCategories); // Get the BingdingMap of current document. // Bind the definitions to the document bindingMap.Insert(exdef_FaceIdTag, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); // 此 Insert 操作必须在事务开启后才能执行 bindingMap.Insert(exdef_FaceType, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); bindingMap.Insert(exdef_Volumn, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); bindingMap.Insert(exdef_Area, instanceBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); } // --------------------------------------------------------------------------------------------------------- // 如果"面层标识 FaceIdTag" 参数已经绑定到了指定的类别上,就认为其他三个参数也绑定上来了,此时就不需要再绑定了; // 如果没有绑定,则要将这四个参数一次性全部绑定到此指定的类别上 if (parameterHasBeenAddedToProject && !faceParaHasInsertedIntoThatCategory) { Autodesk.Revit.DB.Binding parameterBinding = doc.ParameterBindings.get_Item(exdef_FaceIdTag); ((InstanceBinding)parameterBinding).Categories.Insert(myCategory); // 将新的类别添加到 binding 的类别集合中 // 重新进行绑定 bindingMap.ReInsert(exdef_FaceIdTag, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); bindingMap.ReInsert(exdef_FaceType, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); bindingMap.ReInsert(exdef_Volumn, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); bindingMap.ReInsert(exdef_Area, parameterBinding, BuiltInParameterGroup.PG_IDENTITY_DATA); } }
/// <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); }