/// <summary> /// Helper method to control `SetAllowVaryBetweenGroups` /// option for instance binding param /// </summary> static void SetInstanceParamVaryBetweenGroupsBehaviour( Document doc, Guid guid, bool allowVaryBetweenGroups = true) { try // last resort { SharedParameterElement sp = SharedParameterElement.Lookup(doc, guid); // Should never happen as we will call // this only for *existing* shared param. if (null == sp) { return; } InternalDefinition def = sp.GetDefinition(); if (def.VariesAcrossGroups != allowVaryBetweenGroups) { // Must be within an outer transaction! def.SetAllowVaryBetweenGroups(doc, allowVaryBetweenGroups); } } catch { } // ideally, should report something to log... }
internal void CanVaryBtwGroups( string parameterName, bool allowVaryBtwGrps) { BindingMap bindingMap = m_doc.ParameterBindings; DefinitionBindingMapIterator itr = bindingMap.ForwardIterator(); while (itr.MoveNext()) { InternalDefinition internalDef = itr.Key as InternalDefinition; if (internalDef.Name == parameterName) { using (Transaction t = new Transaction(m_doc)) { t.Start("Allow varying b/w groups"); internalDef.SetAllowVaryBetweenGroups(m_doc, allowVaryBtwGrps); t.Commit(); } } } }
/// <summary> /// Проверяет налиxие общего параметра у элемента. Если параметр есть - возвращает его. Иначе добавляет параметр из файла общих параметров. /// </summary> /// <param name="elem"></param> /// <param name="app"></param> /// <param name="catset"></param> /// <param name="ParameterName"></param> /// <param name="paramGroup"></param> /// <param name="SetVaryByGroups"></param> /// <returns></returns> public static Parameter CheckAndAddSharedParameter(Element elem, Application app, CategorySet catset, string ParameterName, BuiltInParameterGroup paramGroup, bool SetVaryByGroups) { Document doc = elem.Document; Parameter param = elem.LookupParameter(ParameterName); if (param != null) { return(param); } string oldSharedParamsFile = app.SharedParametersFilename; app.SharedParametersFilename = @"\\picompany.ru\pikp\lib\_CadSettings\02_Revit\04. Shared Parameters\SP-ALL-PIC.txt"; ExternalDefinition exDef = null; string sharedFile = app.SharedParametersFilename; DefinitionFile sharedParamFile = app.OpenSharedParameterFile(); foreach (DefinitionGroup defgroup in sharedParamFile.Groups) { foreach (Definition def in defgroup.Definitions) { if (def.Name == ParameterName) { exDef = def as ExternalDefinition; } } } if (exDef == null) { throw new Exception("В файл общих параметров не найден общий параметр " + ParameterName); } bool checkContains = doc.ParameterBindings.Contains(exDef); if (checkContains) { var res = BindSharedParam(doc, elem, ParameterName, exDef); } InstanceBinding newIB = app.Create.NewInstanceBinding(catset); doc.ParameterBindings.Insert(exDef, newIB, paramGroup); if (SetVaryByGroups) { doc.Regenerate(); SharedParameterElement spe = SharedParameterElement.Lookup(doc, exDef.GUID); InternalDefinition intDef = spe.GetDefinition(); intDef.SetAllowVaryBetweenGroups(doc, true); } doc.Regenerate(); app.SharedParametersFilename = oldSharedParamsFile; param = elem.LookupParameter(ParameterName); if (param == null) { throw new Exception("Не удалось добавить обший параметр " + ParameterName); } return(param); }
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); }