Many times we may find that some family types are not available directly in the family document, so we have to create them. It is not easy or even possible to create family types on the fly, but fortunately Revit .NET API provides the ElementType.Duplicate method that we can use to make a copy of an existing family type and adjust its parameters and properties to meet the need.
In this post, let’s create some new types for the Metal Clad Column and use them to create different sizes of Metal Clad Columns.
Here are the method and some sample caller code.
public static FamilySymbol GetFirstSymbol(Family family, string name)
{
return (from FamilySymbol fs in family.Symbols where fs.Name == name select fs).FirstOrDefault();
}
public static FamilySymbol GetCreateMetalCladColumnType(Document doc, double diameter)
{
Family family = ColumnCreation.FindColumnFamilies(doc).FirstOrDefault(e => e.Name == "Metal Clad Column");
if (family == null)
family = LoadColumnFamily(doc, "Metal Clad Column");
double adjustedDiameter = ((int)(diameter * 12.0)) / 12.0;
string name = string.Format("{0}' {1}\"", (int)adjustedDiameter, (int)((adjustedDiameter - (int)adjustedDiameter) * 12.0));
FamilySymbol symbol = ColumnCreation.GetFirstSymbol(family, name);
if (symbol != null)
return symbol;
symbol = ColumnCreation.GetFirstSymbol(family).Duplicate(name) as FamilySymbol;
symbol.get_Parameter("Diameter").Set(adjustedDiameter);
return symbol;
}
public static FamilyInstance InsertMetalCladColumn(Document doc, XYZ location, Level topLevel, double topOffset, Level baseLevel, double baseOffset, double diameter)
{
FamilySymbol symbol = GetCreateMetalCladColumnType(doc, diameter);
return InsertColumn(doc, symbol, location, topLevel, topOffset, baseLevel, baseOffset);
}
…
IOrderedEnumerable<Level> levels = FindAndSortLevels(CachedDoc);
Level baseLevel = levels.First();
Level topLevel = levels.ElementAt(1);
ColumnCreation.InsertMetalCladColumn(CachedDoc, new XYZ(10, 0, 0), topLevel, -4.0, baseLevel, 0, 1.025);
ColumnCreation.InsertMetalCladColumn(CachedDoc, new XYZ(20, 0, 0), topLevel, -4.0, baseLevel, 0, 2.515);
ColumnCreation.InsertMetalCladColumn(CachedDoc, new XYZ(30, 0, 0), topLevel, -4.0, baseLevel, 0, 3.015);
ColumnCreation.InsertMetalCladColumn(CachedDoc, new XYZ(40, 0, 0), topLevel, -4.0, baseLevel, 0, 4.515);
ColumnCreation.InsertMetalCladColumn(CachedDoc, new XYZ(50, 0, 0), topLevel, -4.0, baseLevel, 0, 6.015);
…
The Metal Clad Columns will be successfully created into the current document as can be seen from the project model dialog.
Revit Addin Wizard (RevitAddinWizard) provides various wizards, coders and widgets to help program Revit addins. It can be downloaded from the Download link at the bottom of the blog index page.