/// <summary> /// Executes on type to instance change /// </summary> /// <param name="uiapp"></param> /// <param name="text"></param> /// <param name="values"></param> private void ExecuteParameterChange(UIApplication uiapp, String text, List <string> values, string type) { UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; if (!doc.IsFamilyDocument) { Command.global_message = "Please run this command in a family document."; TaskDialog.Show("Message", Command.global_message); } if ((uidoc != null)) { using (TransactionGroup tg = new TransactionGroup(doc, "Parameter Type To Instance")) { tg.Start(); using (Transaction trans = new Transaction(uidoc.Document)) { FailureHandlingOptions failureHandlingOptions = trans.GetFailureHandlingOptions(); FailureHandler failureHandler = new FailureHandler(); failureHandlingOptions.SetFailuresPreprocessor(failureHandler); failureHandlingOptions.SetClearAfterRollback(true); trans.SetFailureHandlingOptions(failureHandlingOptions); // Since we'll modify the document, we need a transaction // It's best if a transaction is scoped by a 'using' block // The name of the transaction was given as an argument if (trans.Start(text) == TransactionStatus.Started) { FamilyManager mgr = doc.FamilyManager; foreach (var value in values) { FamilyParameter fp = mgr.get_Parameter(value); if (fp.IsInstance) { mgr.MakeType(fp); } else { mgr.MakeInstance(fp); }; } } doc.Regenerate(); trans.Commit(); uidoc.RefreshActiveView(); if (failureHandler.ErrorMessage != "") { if (EncounteredError != null) { EncounteredError(this, null); } } } tg.Assimilate(); } } }
/// <summary> /// Ask the user to open a revit family template and then add FamilyTypes and FamilyParameters /// to it. Say the user opens a Door template, he can then save the family as a new door family and load /// it into a new project for use. /// </summary> public void AddFamilyParameterAndType() { Document doc; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "Select family document"; openFileDialog.Filter = "RFT Files (*.rft)|*.rft"; if (openFileDialog.ShowDialog() == DialogResult.OK) { doc = m_revitApp.Application.NewFamilyDocument(openFileDialog.FileName); } else { return; } using (Transaction transaction = new Transaction(m_revitApp.ActiveUIDocument.Document)) { transaction.Start("AddFamilyParameterAndType"); if (doc.IsFamilyDocument) { // has to be a family document to be able to use the Family Manager. FamilyManager famMgr = doc.FamilyManager; //Add a family param. FamilyParameter famParam = famMgr.AddParameter("RevitLookup_Param", BuiltInParameterGroup.PG_TITLE, ParameterType.Text, false); famMgr.Set(famParam, "Default text."); //Create a couple of new family types. Note that we can set different values for the param //in different family types. FamilyType newFamType = famMgr.NewType("RevitLookup_Type1"); famMgr.CurrentType = newFamType; if (newFamType.HasValue(famParam)) { famMgr.Set(famParam, "Text1."); } FamilyType newFamType1 = famMgr.NewType("RevitLookup_Type2"); famMgr.CurrentType = newFamType; if (newFamType.HasValue(famParam)) { famMgr.Set(famParam, "Text2."); } famMgr.MakeType(famParam); if ((famParam != null) && (newFamType != null)) { MessageBox.Show("New family types/params added successfully.", "RevitLookup", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Family types/params addition failed.", "RevitLookup", MessageBoxButtons.OK, MessageBoxIcon.Error); } } transaction.Commit(); } }