public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; if (doc.IsFamilyDocument) { message = "This command can only be used in a project, not in a family file."; return(Result.Failed); } using (Transaction tx = new Transaction(doc)) { tx.Start("Create per doc Parameters"); // get the current shared params definition file DefinitionFile sharedParamsFile = LabUtils.GetSharedParamsFile(app.Application); if (null == sharedParamsFile) { message = "Error getting the shared params file."; return(Result.Failed); } // get or create the shared params group DefinitionGroup sharedParamsGroup = LabUtils.GetOrCreateSharedParamsGroup( sharedParamsFile, LabConstants.ParamGroupName); if (null == sharedParamsGroup) { message = "Error getting the shared params group."; return(Result.Failed); } // visible param Definition docParamDefVisible = LabUtils.GetOrCreateSharedParamsDefinition( sharedParamsGroup, ParameterType.Integer, LabConstants.ParamNameVisible, true); if (null == docParamDefVisible) { message = "Error creating visible per-doc parameter."; return(Result.Failed); } // invisible param Definition docParamDefInvisible = LabUtils.GetOrCreateSharedParamsDefinition( sharedParamsGroup, ParameterType.Integer, LabConstants.ParamNameInvisible, false); if (null == docParamDefInvisible) { message = "Error creating invisible per-doc parameter."; return(Result.Failed); } // bind the param try { CategorySet catSet = app.Application.Create.NewCategorySet(); catSet.Insert(doc.Settings.Categories.get_Item( BuiltInCategory.OST_ProjectInformation)); Binding binding = app.Application.Create.NewInstanceBinding(catSet); doc.ParameterBindings.Insert(docParamDefVisible, binding); doc.ParameterBindings.Insert(docParamDefInvisible, binding); } catch (Exception e) { message = "Error binding shared parameter: " + e.Message; return(Result.Failed); } // set the initial values // get the singleton project info element Element projInfoElem = LabUtils.GetProjectInfoElem(doc); if (null == projInfoElem) { message = "No project info element found. Aborting command..."; return(Result.Failed); } // For simplicity, access params by name rather than by GUID // and simply the first best one found under that name: projInfoElem.LookupParameter(LabConstants.ParamNameVisible).Set(55); projInfoElem.LookupParameter(LabConstants.ParamNameInvisible).Set(0); tx.Commit(); } return(Result.Succeeded); }
//static public BuiltInCategory Target = BuiltInCategory.OST_Walls; //static public string Target = "Drawing1.dwg"; //static public BuiltInCategory Target = BuiltInCategory.OST_IOSModelGroups; // doc.Settings.Categories.get_Item returns null //static public string Target = "Model Groups"; // doc.Settings.Categories.get_Item throws an exception SystemInvalidOperationException "Operation is not valid due to the current state of the object." //static public BuiltInCategory Target = BuiltInCategory.OST_Lines; // model lines //static public BuiltInCategory Target = BuiltInCategory.OST_SWallRectOpening; // Rectangular Straight Wall Openings, case 1260656 [Add Parameters Wall Opening] //static public BuiltInCategory Target = BuiltInCategory.OST_Materials; // can I attach a shared parameter to a material element? Yes, absolutely, no problem at all. public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; Application app = uiapp.Application; Document doc = uiapp.ActiveUIDocument.Document; Category cat = null; #region Determine model group category #if DETERMINE_MODEL_GROUP_CATEGORY List <Element> modelGroups = new List <Element>(); //Filter fType = app.Create.Filter.NewTypeFilter( typeof( Group ) ); // "Binding the parameter to the category Model Groups is not allowed" Filter fType = app.Create.Filter.NewTypeFilter(typeof(GroupType)); // same result "Binding the parameter to the category Model Groups is not allowed" Filter fCategory = app.Create.Filter.NewCategoryFilter(BuiltInCategory.OST_IOSModelGroups); Filter f = app.Create.Filter.NewLogicAndFilter(fType, fCategory); if (0 < doc.get_Elements(f, modelGroups)) { cat = modelGroups[0].Category; } #endif // DETERMINE_MODEL_GROUP_CATEGORY #endregion // Determine model group category if (null == cat) { // The category we are defining the parameter for try { cat = doc.Settings.Categories.get_Item(Target); } catch (Exception ex) { message = "Error obtaining the shared param document category: " + ex.Message; return(Result.Failed); } if (null == cat) { message = "Unable to obtain the shared param document category."; return(Result.Failed); } } // Get the current shared params definition file DefinitionFile sharedParamsFile = LabUtils.GetSharedParamsFile(app); if (null == sharedParamsFile) { message = "Error getting the shared params file."; return(Result.Failed); } // Get or create the shared params group DefinitionGroup sharedParamsGroup = LabUtils.GetOrCreateSharedParamsGroup( sharedParamsFile, LabConstants.SharedParamsGroupAPI); if (null == sharedParamsGroup) { message = "Error getting the shared params group."; return(Result.Failed); } // Visibility of the new parameter: the // Category.AllowsBoundParameters property // determines whether a category is allowed to // have user-visible shared or project parameters. // If it is false, it may not be bound to visible // shared parameters using the BindingMap. Note // that non-user-visible parameters can still be // bound to these categories. bool visible = cat.AllowsBoundParameters; // Get or create the shared params definition Definition fireRatingParamDef = LabUtils.GetOrCreateSharedParamsDefinition( sharedParamsGroup, ParameterType.Number, LabConstants.SharedParamsDefFireRating, visible); if (null == fireRatingParamDef) { message = "Error in creating shared parameter."; return(Result.Failed); } // Create the category set for binding and add the category // we are interested in, doors or walls or whatever: CategorySet catSet = app.Create.NewCategorySet(); try { catSet.Insert(cat); } catch (Exception) { message = string.Format( "Error adding '{0}' category to parameters binding set.", cat.Name); return(Result.Failed); } // Bind the parameter. try { using (Transaction t = new Transaction(doc)) { t.Start("Bind Fire Rating Shared Parameter"); Binding binding = app.Create.NewInstanceBinding(catSet); // We could check if already bound, but looks // like Insert will just ignore it in that case. doc.ParameterBindings.Insert(fireRatingParamDef, binding); // You can also specify the parameter group here: //doc.ParameterBindings.Insert( fireRatingParamDef, binding, BuiltInParameterGroup.PG_GEOMETRY ); t.Commit(); } } catch (Exception ex) { message = ex.Message; return(Result.Failed); } return(Result.Succeeded); }