public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; CategorySet categorySet = new CategorySet(); categorySet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_StructuralFraming)); categorySet.Insert(Category.GetCategory(doc, BuiltInCategory.OST_Floors)); CustomParameter customParameter = new CustomParameter("commentAA", " comments", BindingType.Instance, ParameterType.Text, BuiltInParameterGroup.PG_IDENTITY_DATA, categorySet, true, true); using (Transaction t = new Transaction(doc, "set share parameter")) { t.Start(); DefinitionFile definitionFile = app.OpenSharedParameterFile(); customParameter.CreateParameter(definitionFile, uiapp, "commentCC"); t.Commit(); } return(Result.Succeeded); }
/// <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> ///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); }
// 定義參數與綁定類型的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); }
public CategorySet GetRebarCategory() { CategorySet catSet = app.Create.NewCategorySet(); catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_Rebar)); return(catSet); }
//這個方法也是被公開在官網上的,我只是小小修改並且翻譯了註解,參考以下網址 //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); }
public void Add_SP_to_Cat() { //UIApplication uiapp = this.Application; UIDocument uidoc = this.ActiveUIDocument; Document doc = uidoc.Document; //Application app = this. Category cat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls); CategorySet cs = Application.Create.NewCategorySet(); cs.Insert(cat); DefinitionFile deFile = Application.OpenSharedParameterFile(); foreach (DefinitionGroup dg in deFile.Groups) { if (dg.Name == "New len") { ExternalDefinition exDef = dg.Definitions.get_Item("CompanyName") as ExternalDefinition; using (Transaction t = new Transaction(doc)) { t.Start("Add Shared Parameters"); InstanceBinding newIB = Application.Create.NewInstanceBinding(cs); doc.ParameterBindings.Insert(exDef, newIB, BuiltInParameterGroup.PG_TEXT); t.Commit(); } } } }
private void addParameterToAreas() { try { CategorySet set = new CategorySet(); Category area = CurrentDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Areas); set.Insert(area); var binding = CurrentDoc.Application.Create.NewInstanceBinding(set); if (String.IsNullOrEmpty(CurrentDoc.Application.SharedParametersFilename) || (System.IO.File.Exists(CurrentDoc.Application.SharedParametersFilename) == false)) { // make our own file. string file = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "RevitSharedParameters.txt"); CurrentDoc.Application.SharedParametersFilename = file; System.IO.File.WriteAllText(file, ""); } DefinitionFile defFile = CurrentDoc.Application.OpenSharedParameterFile(); var group = defFile.Groups.get_Item("AreaRoomTypes"); if (group == null) { group = defFile.Groups.Create("AreaRoomTypes"); } ExternalDefinitionCreationOptions opts = new ExternalDefinitionCreationOptions(TypeParam, ParameterType.Text); Definition def = group.Definitions.Create(opts); CurrentDoc.ParameterBindings.Insert(def, binding); } catch (Exception ex) { log("Tried to add the " + TypeParam + " parameter to Areas, but failed! " + ex.GetType().Name + ": " + ex.Message); } }
/// <summary> /// 删除某个类别中的共享参数 /// </summary> /// <param name="doc"></param> /// <param name="groupName">参数组</param> /// <param name="parameterName">参数名</param> /// <param name="moveCategory">类别</param> /// <param name="isInstance">false:类别, true: 实例</param> /// <returns></returns> public bool DeleteShareParameter(Document doc, string groupName, string parameterName, Category moveCategory, bool isInstance) { bool isSuccess = false; if (string.IsNullOrWhiteSpace(path)) { throw new Exception("无效路径"); } doc.Application.SharedParametersFilename = this.path; DefinitionFile file = doc.Application.OpenSharedParameterFile(); if (file == null) { throw new Exception("共享参数文件不存在"); } DefinitionGroups groups = file.Groups; DefinitionGroup group = file.Groups.get_Item(groupName); if (group == null) { throw new Exception("参数组不存在"); } Definition definition = group.Definitions.get_Item(parameterName); if (definition == null) { throw new Exception("共享参数不存在"); } ElementBinding elementBinding = doc.ParameterBindings.get_Item(definition) as ElementBinding; CategorySet set = elementBinding.Categories; CategorySet newSet = new CategorySet(); foreach (Category category in set) { if (category.Name != moveCategory.Name) { newSet.Insert(category); } } if (isInstance) { elementBinding = doc.Application.Create.NewInstanceBinding(newSet); } else { elementBinding = doc.Application.Create.NewTypeBinding(newSet); } doc.ParameterBindings.Remove(definition); if (elementBinding.Categories.Size > 0) { return(doc.ParameterBindings.Insert(definition, elementBinding, BuiltInParameterGroup.PG_TEXT)); } else { return(false); } }
private static void AddDocumentParameters(List <RevitParameter> dataList, DefinitionFile sharedParameterFile, AddFamilyParametersResult results) { CategorySet categorySet = revitDocument.Application.Create.NewCategorySet(); foreach (var item in dataList) { DefinitionGroup dg = ParamsHelper.GetOrCreateSharedParamsGroup(sharedParameterFile, item.GroupName); ExternalDefinition externalDefinition = ParamsHelper.GetOrCreateSharedParamDefinition(dg, item.ParamType, item.ParamName, item.IsVisible); Category category = revitDocument.Settings.Categories.get_Item(item.Category); categorySet.Insert(category); Binding newIb; if (item.IsInstance) { newIb = revitDocument.Application.Create.NewInstanceBinding(categorySet); } else { newIb = revitDocument.Application.Create.NewTypeBinding(categorySet); } revitDocument.ParameterBindings.Insert(externalDefinition, newIb, item.ParamGroup); if (revitDocument.ParameterBindings.Contains(externalDefinition)) { revitDocument.ParameterBindings.ReInsert(externalDefinition, newIb); } results.AddFamilyParameterNote(item); } }
//private void CreateExternalSharedParamFile(string sharedParameterFile) //{ // System.IO.FileStream fileStream = System.IO.File.Create(sharedParameterFile); // fileStream.Close(); //} //private void ShowDefinitionFileInfo(DefinitionFile myDefinitionFile) //{ // StringBuilder fileInformation = new StringBuilder(500); // // get the file name // fileInformation.AppendLine("File Name: " + myDefinitionFile.Filename); // // iterate the Definition groups of this file // foreach (DefinitionGroup myGroup in myDefinitionFile.Groups) // { // // get the group name // fileInformation.AppendLine("Group Name: " + myGroup.Name); // // iterate the difinitions // foreach (Definition definition in myGroup.Definitions) // { // // get definition name // fileInformation.AppendLine("Definition Name: " + definition.Name); // } // } // TaskDialog.Show("Revit", fileInformation.ToString()); //} //private DefinitionFile SetAndOpenExternalSharedParamFile(Autodesk.Revit.ApplicationServices.Application application, string sharedParameterFile) //{ // // set the path of shared parameter file to current Revit // application.SharedParametersFilename = sharedParameterFile; // // open the file // return application.OpenSharedParameterFile(); //} //private void ReadEditExternalParam(DefinitionFile file) //{ // // get ExternalDefinition from shared parameter file // DefinitionGroups myGroups = file.Groups; // DefinitionGroup myGroup = myGroups.get_Item("MyGroup"); // if (myGroup != null) // { // ExternalDefinition myExtDef = myGroup.Definitions.get_Item("MyParam") as ExternalDefinition; // if (myExtDef != null) // { // DefinitionGroup newGroup = myGroups.get_Item("AnotherGroup"); // if (newGroup != null) // { // // change the OwnerGroup of the ExternalDefinition // myExtDef.OwnerGroup = newGroup; // } // } // } //} private void createCategory() { try { SharedParameterElement sp = SharedParameterElement.Lookup(doc, new Guid("2FB57A90-CF7B-4F49-8127-B0C7F7276FD7")); if (sp == null) { using (Transaction myTrans = new Transaction(doc)) { myTrans.Start("Attach Parameters"); try { sharedParameterYarat(revitApp, "Parametre1", "REVITeRA", ParameterType.Text, true, new Guid("2FB57A90-CF7B-4F49-8127-B0C7F7276FD7")); sharedParameterYarat(revitApp, "Parametre2", "REVITeRA", ParameterType.Text, true, new Guid("641644D9-B65F-4EF7-96A5-D9B243B8477D")); sharedParameterYarat(revitApp, "Parametre3", "REVITeRA", ParameterType.Text, true, new Guid("208BB516-A3CB-47AC-B339-3EFDDF5DE71A")); CategorySet cats1 = new CategorySet(); cats1 = revitApp.Create.NewCategorySet(); cats1.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls)); sharedParameterEkle(revitApp, "Parametre1", cats1, BuiltInParameterGroup.INVALID, true); sharedParameterEkle(revitApp, "Parametre2", cats1, BuiltInParameterGroup.INVALID, true); sharedParameterEkle(revitApp, "Parametre3", cats1, BuiltInParameterGroup.INVALID, true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } myTrans.Commit(); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public List <bool> SetNewParametersToInstances() { List <bool> instanceBindingOK = new List <bool>(); if (SharedParameterFile != null && ActiveDefinitions != null) { CategorySet myCategories = App.Create.NewCategorySet(); Category myCategory = UIApp.ActiveUIDocument.Document.Settings.Categories.get_Item(ActiveBuiltInCategory); myCategories.Insert(myCategory); InstanceBinding instanceBinding = App.Create.NewInstanceBinding(myCategories); BindingMap bindingMap = UIApp.ActiveUIDocument.Document.ParameterBindings; foreach (Definition definition in ActiveDefinitions) { bool IsOk = bindingMap.Insert(definition, instanceBinding, BuiltInParameterGroup.PG_TEXT); instanceBindingOK.Add(IsOk); } UIApp.ActiveUIDocument.SaveAs(); } else { instanceBindingOK.Add(false); } return(instanceBindingOK); }
} //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); } }
public bool AddShadeParameter(Document doc, string groupName, string parameterName, Category category, bool isInstance) { if (this.path == "") { throw new Exception("路径无效"); } doc.Application.SharedParametersFilename = this.path; DefinitionFile definitionFile = doc.Application.OpenSharedParameterFile(); DefinitionGroups groups = definitionFile.Groups; DefinitionGroup group = groups.get_Item(groupName); if (group == null) { throw new Exception("没有找到对应的参数组,组名:" + groupName); } Definition definition = group.Definitions.get_Item(parameterName); if (definition == null) { throw new Exception("没有找到对应的参数,参数名:" + parameterName); } ElementBinding binding = null; ElementBinding bind = doc.ParameterBindings.get_Item(definition) as ElementBinding; CategorySet set = new CategorySet();; if (bind != null) { foreach (Category c in bind.Categories) { set.Insert(c); } } var b = set.Insert(category); if (isInstance) { binding = doc.Application.Create.NewInstanceBinding(set); } else { binding = doc.Application.Create.NewTypeBinding(set); } doc.ParameterBindings.Remove(definition); return(doc.ParameterBindings.Insert(definition, binding, BuiltInParameterGroup.PG_TEXT)); }
void ProblemAddingParameterBindingForCategory( Document doc) { Application app = doc.Application; if (null == _spellingErrorCorrector) { _spellingErrorCorrector = new Util.SpellingErrorCorrector(app); } DefinitionFile sharedParametersFile = app.OpenSharedParameterFile(); DefinitionGroup group = sharedParametersFile .Groups.Create("Reinforcement"); //Definition def = group.Definitions.Create( // 2014 // "ReinforcementParameter", ParameterType.Text ); ExternalDefinitionCreationOptions opt = new ExternalDefinitionCreationOptions( "ReinforcementParameter", ParameterType.Text); Definition def = group.Definitions.Create(opt); // 2015 // To handle both ExternalDefinitonCreationOptions // and ExternalDefinitionCreationOptions: def = _spellingErrorCorrector.NewDefinition( group.Definitions, "ReinforcementParameter", ParameterType.Text); List <BuiltInCategory> bics = new List <BuiltInCategory>(); //bics.Add(BuiltInCategory.OST_AreaRein); //bics.Add(BuiltInCategory.OST_FabricAreas); //bics.Add(BuiltInCategory.OST_FabricReinforcement); //bics.Add(BuiltInCategory.OST_PathRein); //bics.Add(BuiltInCategory.OST_Rebar); bics.Add(BuiltInCategory .OST_IOSRebarSystemSpanSymbolCtrl); CategorySet catset = new CategorySet(); foreach (BuiltInCategory bic in bics) { catset.Insert( doc.Settings.Categories.get_Item(bic)); } InstanceBinding binding = app.Create.NewInstanceBinding(catset); doc.ParameterBindings.Insert(def, binding, BuiltInParameterGroup.PG_CONSTRUCTION); }
/// <summary> /// Create shared parameter for Rooms category /// </summary> /// <returns>True, shared parameter exists; false, doesn't exist</returns> private bool CreateMyRoomSharedParameter() { // Create Room Shared Parameter Routine: --> // 1: Check whether the Room shared parameter("External Room ID") has been defined. // 2: Share parameter file locates under sample directory of this .dll module. // 3: Add a group named "SDKSampleRoomScheduleGroup". // 4: Add a shared parameter named "External Room ID" to "Rooms" category, which is visible. // The "External Room ID" parameter will be used to map to spreadsheet based room ID(which is unique) try { // check whether shared parameter exists if (ShareParameterExists(RoomsData.SharedParam)) { return(true); } // create shared parameter file String modulePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String paramFile = modulePath + "\\RoomScheduleSharedParameters.txt"; if (File.Exists(paramFile)) { File.Delete(paramFile); } FileStream fs = File.Create(paramFile); fs.Close(); // cache application handle Autodesk.Revit.ApplicationServices.Application revitApp = m_commandData.Application.Application; // prepare shared parameter file m_commandData.Application.Application.SharedParametersFilename = paramFile; // open shared parameter file DefinitionFile parafile = revitApp.OpenSharedParameterFile(); // create a group DefinitionGroup apiGroup = parafile.Groups.Create("SDKSampleRoomScheduleGroup"); // create a visible "External Room ID" of text type. ExternalDefinitionCreationOptions ExternalDefinitionCreationOptions = new ExternalDefinitionCreationOptions(RoomsData.SharedParam, ParameterType.Text); Definition roomSharedParamDef = apiGroup.Definitions.Create(ExternalDefinitionCreationOptions); // get Rooms category Category roomCat = m_commandData.Application.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Rooms); CategorySet categories = revitApp.Create.NewCategorySet(); categories.Insert(roomCat); // insert the new parameter InstanceBinding binding = revitApp.Create.NewInstanceBinding(categories); m_commandData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(roomSharedParamDef, binding); return(false); } catch (Exception ex) { throw new Exception("Failed to create shared parameter: " + ex.Message); } }
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); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; //FilteredElementCollector col // = new FilteredElementCollector(doc) // .WhereElementIsNotElementType() // .OfCategory(BuiltInCategory.INVALID) // .OfClass(typeof(Wall)); // Modify document within a transaction using (Transaction tx = new Transaction(doc)) { tx.Start("Transaction Name"); //SharedParameterBindingManager manager = new SharedParameterBindingManager(); //DefinitionGroup dGroup = dFile.Groups.Create("Demo group"); //manager.Definition = dGroup.Definitions.Create(manager.GetCreationOptions()); //manager.AddCategory(BuiltInCategory.OST_ProjectInformation); //manager.AddBindings(doc); app.SharedParametersFilename = @"D:\Files\Data\Autodesk Revit\00_Templeate\00_Main Project\SP_ForTesting1.txt"; DefinitionFile dFile = app.OpenSharedParameterFile(); Category wall = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls); CategorySet cats1 = doc.Application.Create.NewCategorySet(); cats1.Insert(wall); SharedParameterManager spManager = new SharedParameterManager(); spManager.AplicationRvt = app; spManager.FilePathToSharedFile = @"D:\Files\Data\Autodesk Revit\00_Templeate\00_Main Project\"; spManager.SharedFileName = "SP_ForTesting2.txt"; spManager.AddExistingSharedParameter("MyGroup1", "MyParam", cats1, BuiltInParameterGroup.PG_IDENTITY_DATA, true); TaskDialog.Show("sdfsd", "Done"); tx.Commit(); } return(Result.Succeeded); }
public string myMethod_BindParameters(string path, bool IsTypeParameter) { UIDocument uidoc = myWindow1.commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; string myStringSharedParameterFileName = ""; if (uidoc.Application.Application.SharedParametersFilename != null) { myStringSharedParameterFileName = uidoc.Application.Application.SharedParametersFilename; //Q:\Revit Revit Revit\Template 2018\PRL_Parameters.txt } uidoc.Application.Application.SharedParametersFilename = path; DefinitionFile myDefinitionFile = uidoc.Application.Application.OpenSharedParameterFile(); if (myStringSharedParameterFileName != "") { uidoc.Application.Application.SharedParametersFilename = myStringSharedParameterFileName; } if (myDefinitionFile == null) { DatabaseMethods.writeDebug(path + Environment.NewLine + Environment.NewLine + "File does not exist OR cannot be opened.", true); return(""); } CategorySet catSet = uidoc.Application.Application.Create.NewCategorySet(); foreach (Category myCatttt in doc.Settings.Categories) { if (myCatttt.AllowsBoundParameters) { catSet.Insert(myCatttt); } } string myStringCollectup = ""; DefinitionGroup group = myDefinitionFile.Groups.get_Item("Default"); foreach (Definition myDefinition in group.Definitions) { myStringCollectup = myStringCollectup + " • " + myDefinition.Name; Binding binding = IsTypeParameter ? uidoc.Application.Application.Create.NewTypeBinding(catSet) as Binding : uidoc.Application.Application.Create.NewInstanceBinding(catSet) as Binding; using (Transaction tx = new Transaction(doc)) { tx.Start("EE07_Part1_Binding"); doc.ParameterBindings.Insert(myDefinition, binding, BuiltInParameterGroup.PG_IDENTITY_DATA); tx.Commit(); } } myStringCollectup = (IsTypeParameter ? "Type" : "Instance") + " parameters added to all categories:" + Environment.NewLine + myStringCollectup; return(myStringCollectup); }
public bool RemoveOrAddFromRebarCategory(Document doc, Element elem, bool addOrDeleteCat) { Application app = doc.Application; ElementBinding elemBind = this.GetBindingByParamName(Name, doc); //получаю список категорий CategorySet newCatSet = app.Create.NewCategorySet(); int rebarcatid = new ElementId(BuiltInCategory.OST_Rebar).IntegerValue; foreach (Category cat in elemBind.Categories) { int catId = cat.Id.IntegerValue; if (catId != rebarcatid) { newCatSet.Insert(cat); } } if (addOrDeleteCat) { Category cat = elem.Category; newCatSet.Insert(cat); } TypeBinding newBind = app.Create.NewTypeBinding(newCatSet); if (doc.ParameterBindings.Insert(def, newBind, paramGroup)) { return(true); } else { if (doc.ParameterBindings.ReInsert(def, newBind, paramGroup)) { return(true); } else { return(false); } } }
private CategorySet Cat(Document doc, List <BuiltInCategory> listBuiltInCategory, CategorySet categories) { foreach (BuiltInCategory builtInCategory in listBuiltInCategory) { Category category = Category.GetCategory(doc, builtInCategory); categories.Insert(category); } return(categories); }
public static bool AddSharedParameterByDefaulGroupName(Document doc, string sharedParameterFile, string groupName, string parameterName, Category newCategory, bool isInstance, BuiltInParameterGroup parameterGroup) { doc.Application.SharedParametersFilename = sharedParameterFile; DefinitionFile definitionFile = doc.Application.OpenSharedParameterFile(); DefinitionGroups groups = definitionFile.Groups; DefinitionGroup group = groups.get_Item(groupName); if (group == null) { throw new Exception("没有找到对应的参数组"); } Definition definition = group.Definitions.get_Item(parameterName); if (definition == null) { throw new Exception("没有找到对应的参数"); } ElementBinding binding = null; ElementBinding orientBinding = doc.ParameterBindings.get_Item(definition) as ElementBinding; CategorySet categories = new CategorySet();; if (orientBinding != null) { foreach (Category c in orientBinding.Categories) { categories.Insert(c); } } categories.Insert(newCategory); if (isInstance) { binding = doc.Application.Create.NewInstanceBinding(categories); } else { binding = doc.Application.Create.NewTypeBinding(categories); } doc.ParameterBindings.Remove(definition); var result = doc.ParameterBindings.Insert(definition, binding, parameterGroup); return(result); }
private void BindParameterDefinition(ExternalDefinition def, Category cat, BuiltInParameterGroup group, bool inst) { var catSet = new CategorySet(); catSet.Insert(cat); var bind = (inst) ? this.app.Create.NewInstanceBinding(catSet) as Binding : this.app.Create.NewTypeBinding(catSet) as Binding; this.doc.ParameterBindings.Insert(def, bind, group); }
private bool AddSharedTestParameter(ExternalCommandData commandData, string paramName, ParameterType paramType, bool userModifiable) { try { // check whether shared parameter exists if (ShareParameterExists(commandData.Application.ActiveUIDocument.Document, paramName)) { return(true); } // create shared parameter file String modulePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); String paramFile = modulePath + "\\RebarTestParameters.txt"; if (File.Exists(paramFile)) { File.Delete(paramFile); } FileStream fs = File.Create(paramFile); fs.Close(); // cache application handle Autodesk.Revit.ApplicationServices.Application revitApp = commandData.Application.Application; // prepare shared parameter file commandData.Application.Application.SharedParametersFilename = paramFile; // open shared parameter file DefinitionFile parafile = revitApp.OpenSharedParameterFile(); // create a group DefinitionGroup apiGroup = parafile.Groups.Create("RebarTestParamGroup"); // create a visible param ExternalDefinitionCreationOptions ExtDefinitionCreationOptions = new ExternalDefinitionCreationOptions(paramName, paramType); ExtDefinitionCreationOptions.HideWhenNoValue = true; //used this to show the parameter only in some rebar instances that will use it ExtDefinitionCreationOptions.UserModifiable = userModifiable; // set if users need to modify this Definition rebarSharedParamDef = apiGroup.Definitions.Create(ExtDefinitionCreationOptions); // get rebar category Category rebarCat = commandData.Application.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Rebar); CategorySet categories = revitApp.Create.NewCategorySet(); categories.Insert(rebarCat); // insert the new parameter InstanceBinding binding = revitApp.Create.NewInstanceBinding(categories); commandData.Application.ActiveUIDocument.Document.ParameterBindings.Insert(rebarSharedParamDef, binding); return(true); } catch (Exception ex) { throw new Exception("Failed to create shared parameter: " + ex.Message); } }
public void CreateStreetParameters(Document doc) { // Categories CategorySet catSet = new CategorySet(); Category cat = Category.GetCategory(doc, BuiltInCategory.OST_Roads); catSet.Insert(cat); Binding binding = new InstanceBinding(catSet); var app = doc.Application; app.SharedParametersFilename = ParamFile; DefinitionFile defFile = app.OpenSharedParameterFile(); DefinitionGroup defGroup = defFile.Groups.FirstOrDefault(d => d.Name == defGroupName); if (defGroup == null) { defGroup = defFile.Groups.Create(defGroupName); } var options = new ExternalDefinitionCreationOptions(nameParamName, ParameterType.Text); options.GUID = new Guid(NameParamGUID); options.Description = nameParamDescription; CreateParam(doc, binding, defGroup, options); options = new ExternalDefinitionCreationOptions(streetUsageParamName, ParameterType.Text); options.GUID = new Guid(StreetUsageParamGUID); options.Description = streetUsageDescription; CreateParam(doc, binding, defGroup, options); options = new ExternalDefinitionCreationOptions(streetTypeParamName, ParameterType.Text); options.GUID = new Guid(StreetTypeParamGUID); options.Description = streetTypeDescription; CreateParam(doc, binding, defGroup, options); options = new ExternalDefinitionCreationOptions(streetLanesParam, ParameterType.Integer); options.GUID = new Guid(StreetLanesParamGUID); options.Description = streetLanesDescription; CreateParam(doc, binding, defGroup, options); options = new ExternalDefinitionCreationOptions(streetMaxSpeedParam, ParameterType.Integer); options.GUID = new Guid(StreetMaxSpeedParamGUID); options.Description = streetMaxSpeedDescription; CreateParam(doc, binding, defGroup, options); options = new ExternalDefinitionCreationOptions(streetSurfaceParam, ParameterType.Text); options.GUID = new Guid(StreetSurfaceParamGUID); options.Description = streetSurfaceParamDescription; CreateParam(doc, binding, defGroup, options); }
private CategorySet GetCategories(Document doc) { Categories categories = doc.Settings.Categories; CategorySet categorySet = new CategorySet(); foreach (BuiltInCategory bic in m_categories) { categorySet.Insert(categories.get_Item(bic)); } return(categorySet); }
/***************************************************/ /**** Public methods ****/ /***************************************************/ public static CategorySet CategorySet(Document document, IEnumerable <Category> categories) { CategorySet result = document.Application.Create.NewCategorySet(); foreach (Category category in categories) { if (category.AllowsBoundParameters) { result.Insert(category); } } return(result); }
/***************************************************/ /**** Public methods ****/ /***************************************************/ public static CategorySet CategoriesWithBoundParameters(this Document document) { CategorySet result = document.Application.Create.NewCategorySet(); foreach (Category cat in document.Settings.Categories) { if (cat.AllowsBoundParameters) { result.Insert(cat); } } return(result); }
/// <summary> /// Add Shared GID Parameter to active Document /// </summary> /// <param name="document"></param> /// <returns>returns if operation succeeded</returns> public static bool GrevitAddSharedParameter(this Document document) { try { string parameterName = "GID"; ParameterType parameterType = ParameterType.Text; 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 Category Set to apply the parameter to CategorySet categories = document.Application.Create.NewCategorySet(); // Walk thru all categories and add them if they allow bound parameters foreach (Category cat in document.Settings.Categories) { if (cat.AllowsBoundParameters) { categories.Insert(cat); } } // Create and External Definition in a Group called Grevit and a Parameter called GID #if (!Revit2015) ExternalDefinition def = document.Application.OpenSharedParameterFile().Groups.Create("Grevit").Definitions.Create(new ExternalDefinitionCreationOptions(parameterName, parameterType)) as ExternalDefinition; #else ExternalDefinition def = document.Application.OpenSharedParameterFile().Groups.Create("Grevit").Definitions.Create(new ExternalDefinitonCreationOptions(parameterName, parameterType)) as ExternalDefinition; #endif document.Application.SharedParametersFilename = sharedParameterFile; System.IO.File.Delete(tempSharedParameterFile); // Apply the Binding to almost all Categories Autodesk.Revit.DB.Binding bin = document.Application.Create.NewInstanceBinding(categories); BindingMap map = (new UIApplication(document.Application)).ActiveUIDocument.Document.ParameterBindings; map.Insert(def, bin, BuiltInParameterGroup.PG_DATA); return(true); } catch (Exception e) { return(false); } }
AddSharedParamsToFile() { // open the file DefinitionFile sharedParametersFile = null; try { sharedParametersFile = OpenSharedParamFile(); } catch (Exception ex) { MessageBox.Show(string.Format("Can't open Shared Parameters file. (Exception: {0})", ex.Message)); return; } // get or create our group DefinitionGroup sharedParameterGroup = sharedParametersFile.Groups.get_Item(m_paramGroupName); if (sharedParameterGroup == null) { sharedParameterGroup = sharedParametersFile.Groups.Create(m_paramGroupName); } // get or create the parameter Definition sharedParameterDefinition = sharedParameterGroup.Definitions.get_Item(m_paramName); if (sharedParameterDefinition == null) { //sharedParameterDefinition = sharedParameterGroup.Definitions.Create( m_paramName, ParameterType.Text, true ); // 2015, jeremy: 'Autodesk.Revit.DB.Definitions.Create(string, Autodesk.Revit.DB.ParameterType, bool)' is obsolete: 'This method is deprecated in Revit 2015. Use Create(Autodesk.Revit.DB.ExternalDefinitonCreationOptions) instead' ExternalDefinitionCreationOptions opt = new ExternalDefinitionCreationOptions(m_paramName, ParameterType.Text); opt.UserModifiable = true; opt.Visible = true; sharedParameterDefinition = sharedParameterGroup.Definitions.Create(opt); // 2016, jeremy } // create a category set with the wall category in it CategorySet categories = m_app.Application.Create.NewCategorySet(); Category wallCategory = m_app.ActiveUIDocument.Document.Settings.Categories.get_Item("Walls"); categories.Insert(wallCategory); // create a new instance binding for the wall categories InstanceBinding instanceBinding = m_app.Application.Create.NewInstanceBinding(categories); // add the binding m_app.ActiveUIDocument.Document.ParameterBindings.Insert(sharedParameterDefinition, instanceBinding); MessageBox.Show(string.Format("Added a shared parameter \"{0}\" that applies to Category \"Walls\".", m_paramName)); }
private static void AddCategories(Document doc, CategorySet myCategories, BuiltInCategory cat) { Category myCategory = doc.Settings.Categories.get_Item(cat); myCategories.Insert(myCategory); }
void f( Document doc ) { Application app = doc.Application; DefinitionFile sharedParametersFile = app.OpenSharedParameterFile(); DefinitionGroup group = sharedParametersFile .Groups.Create( "Reinforcement" ); Definition def = group.Definitions.Create( "ReinforcementParameter", ParameterType.Text ); List<BuiltInCategory> bics = new List<BuiltInCategory>(); //bics.Add(BuiltInCategory.OST_AreaRein); //bics.Add(BuiltInCategory.OST_FabricAreas); //bics.Add(BuiltInCategory.OST_FabricReinforcement); //bics.Add(BuiltInCategory.OST_PathRein); //bics.Add(BuiltInCategory.OST_Rebar); bics.Add( BuiltInCategory .OST_IOSRebarSystemSpanSymbolCtrl ); CategorySet catset = new CategorySet(); foreach( BuiltInCategory bic in bics ) { catset.Insert( doc.Settings.Categories.get_Item( bic ) ); } InstanceBinding binding = app.Create.NewInstanceBinding( catset ); doc.ParameterBindings.Insert( def, binding, BuiltInParameterGroup.PG_CONSTRUCTION ); }
void ProblemAddingParameterBindingForCategory( Document doc) { Application app = doc.Application; if( null == _spellingErrorCorrector ) { _spellingErrorCorrector = new Util.SpellingErrorCorrector( app ); } DefinitionFile sharedParametersFile = app.OpenSharedParameterFile(); DefinitionGroup group = sharedParametersFile .Groups.Create( "Reinforcement" ); //Definition def = group.Definitions.Create( // 2014 // "ReinforcementParameter", ParameterType.Text ); ExternalDefinitionCreationOptions opt = new ExternalDefinitionCreationOptions( "ReinforcementParameter", ParameterType.Text ); Definition def = group.Definitions.Create( opt ); // 2015 // To handle both ExternalDefinitonCreationOptions // and ExternalDefinitionCreationOptions: def = _spellingErrorCorrector.NewDefinition( group.Definitions, "ReinforcementParameter", ParameterType.Text ); List<BuiltInCategory> bics = new List<BuiltInCategory>(); //bics.Add(BuiltInCategory.OST_AreaRein); //bics.Add(BuiltInCategory.OST_FabricAreas); //bics.Add(BuiltInCategory.OST_FabricReinforcement); //bics.Add(BuiltInCategory.OST_PathRein); //bics.Add(BuiltInCategory.OST_Rebar); bics.Add( BuiltInCategory .OST_IOSRebarSystemSpanSymbolCtrl ); CategorySet catset = new CategorySet(); foreach( BuiltInCategory bic in bics ) { catset.Insert( doc.Settings.Categories.get_Item( bic ) ); } InstanceBinding binding = app.Create.NewInstanceBinding( catset ); doc.ParameterBindings.Insert( def, binding, BuiltInParameterGroup.PG_CONSTRUCTION ); }