Code Examples Sept 9th, 2018

Here is a C# macro that opens a selected family in the family editor, adds a new type parameter to the family, sets the value of the parameter, and loads the family back into the project.

I have tested this on Generic Annotation families, althought I have not managed to set the value of type parameters for families that have multiple types. I assume another line of code may be needed to set the current family type.

Note the difference between the Document doc used for the original project, and the Document docfamily used in the family editor, and the locations they are used in the transactions and in the commands.

Also, don't let the iFamilyLoadOptions overwhelm you. For a macro, just paste that section of code in right after the macro's #endregion line , and just before your first macro (basically just as shown below.) You can obviously change the overwriteParameterValues to enable or disable automatically overwriting existing families.

public class CustomFamilyLoadOption : IFamilyLoadOptions { public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues) { overwriteParameterValues = true; return true; }
public bool OnSharedFamilyFound(Family sharedFamily,bool familyInUse,out FamilySource source, out bool overwriteParameterValues) { source = FamilySource.Family; overwriteParameterValues = true; return true; } }

public void AddParameterExample() { UIDocument uidoc = this.ActiveUIDocument; Document doc = this.ActiveUIDocument.Document; Selection selection = uidoc.Selection; ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds(); foreach (ElementId id in selectedIds) //for each selected element { try { Element e = doc.GetElement(id); FamilyInstance fyInst = e as FamilyInstance; Family fam = fyInst.Symbol.Family; Document docfamily = doc.EditFamily(fam); // docfamily.SaveAs(@"C:/Temp/NewFamily225.rfa"); FamilyManager famMan = docfamily.FamilyManager; using (Transaction trans = new Transaction(docfamily, "Add Parameter")) { trans.Start(); try //removing parameter //remove the parameter if it already exists {FamilyParameter famParam = famMan.get_Parameter("LegendText"); famMan.RemoveParameter (famParam); } catch{} try // adding parameter { //add the new parameter - last parameter is true for an instance parameter, false for a type parameter famMan.AddParameter ("LegendText",BuiltInParameterGroup.PG_TEXT,ParameterType.Text , false); FamilyParameter famParam = famMan.get_Parameter("LegendText"); ElementId etypeid = e.GetTypeId(); Element etype = doc.GetElement(etypeid); famMan.Set(famParam, "My Example Text."); } catch { } trans.Commit(); }//end transaction //load the family back in to the project using (Transaction trans = new Transaction(doc, "Load Family")) docfamily.LoadFamily(doc, new CustomFamilyLoadOption()); }//end try catch (Exception e) { TaskDialog.Show("Revit",e.Message.ToString()); }// end catch }//end for each }// end public void AddInstParameter

Text examples: Here are a couple of sample lines of code that show how to get the text and location of a textnote, and how to place a textnote. A textnote should be selected before running this code.

public void TextNoteExample() { UIDocument uidoc = this.ActiveUIDocument; Document doc = this.ActiveUIDocument.Document; // Get the element selection of current document Selection selection = uidoc.Selection;
//store element id's ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds(); foreach (ElementId id in selectedIds) //for each selected element
{ Element e = doc.GetElement(id); //get the element from the id //Get the text parameter and the text contained in the parameter Parameter MyTextParam = e.get_Parameter(BuiltInParameter.TEXT_TEXT); string MyText = MyTextParam.AsString();
//cast the element as a TextNote; TextNote T = e as TextNote; XYZ TEXT_LOCATION = T.Coord as XYZ; TaskDialog.Show("Text Note Location:", TEXT_LOCATION.X.ToString () + " , " + TEXT_LOCATION.Y.ToString () + " , " + TEXT_LOCATION.Z.ToString ()); } //create a new text note:
using (Transaction tran = new Transaction(doc, "Creating a Text note")) { XYZ origin = new XYZ(0, 0, 0); ElementId defaultTypeId = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType); tran.Start(); TextNote note = TextNote.Create(doc, doc.ActiveView.Id, origin, "My New Text", defaultTypeId); tran.Commit(); } }