/// <summary> /// Gets or Creates Element's shared param. /// </summary> /// <param name="elem">Revit Element to get param for</param> /// <param name="paramName">Parameter Name</param> /// <param name="grpName">Param Group Name (relevant only when Creation takes place)</param> /// <param name="paramType">Param Type (relevant only when Creation takes place)</param> /// <param name="visible">Param UI Visibility (relevant only when Creation takes place)</param> /// <param name="instanceBinding">Param Binding: Instance or Type (relevant only when Creation takes place)</param> /// <returns></returns> public static Parameter GetOrCreateElemSharedParam(Element elem, string paramName, string grpName, ParameterType paramType, bool visible, bool instanceBinding) { try { // Check if existing Parameter param = GetElemParam(elem, paramName); if (null != param) { return(param); } // If here, need to create it... BindSharedParamResult res = BindSharedParam(elem.Document, elem.Category, paramName, grpName, paramType, visible, instanceBinding); if (res != BindSharedParamResult.eSuccessfullyBound && res != BindSharedParamResult.eAlreadyBound) { return(null); } // If here, binding is OK and param seems to be IMMEDIATELY available from the very same command return(GetElemParam(elem, paramName)); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(string.Format("Error in getting or creating Element Param: {0}", ex.Message)); return(null); } }
// Assumes outer transaction public static Parameter GetOrCreateElemSharedParam( Element elem, string paramName, string grpName, ParameterType paramType, bool visible, bool instanceBinding, bool userModifiable, Guid guid, bool useTempSharedParamFile, string tooltip = "", BuiltInParameterGroup uiGrp = BuiltInParameterGroup.INVALID, bool allowVaryBetweenGroups = true) { try { // Check if existing Parameter param = elem.LookupParameter(paramName); if (null != param) { // NOTE: If you don't want forcefully setting // the "old" instance params to // allowVaryBetweenGroups =true, // just comment the next 3 lines. if (instanceBinding && allowVaryBetweenGroups) { SetInstanceParamVaryBetweenGroupsBehaviour( elem.Document, guid, allowVaryBetweenGroups); } return(param); } // If here, need to create it (my custom // implementation and classes…) BindSharedParamResult res = BindSharedParam( elem.Document, elem.Category, paramName, grpName, paramType, visible, instanceBinding, userModifiable, guid, useTempSharedParamFile, tooltip, uiGrp); if (res != BindSharedParamResult.eSuccessfullyBound && res != BindSharedParamResult.eAlreadyBound) { return(null); } // Set AllowVaryBetweenGroups for NEW Instance // Binding Shared Param if (instanceBinding) { SetInstanceParamVaryBetweenGroupsBehaviour( elem.Document, guid, allowVaryBetweenGroups); } // If here, binding is OK and param seems to be // IMMEDIATELY available from the very same command return(elem.LookupParameter(paramName)); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show( string.Format( "Error in getting or creating Element Param: {0}", ex.Message)); return(null); } }