Revit .NET has provided the family API for several versions. We did not really address it in the past. In this series of posts, we are going to explore the Revit family .NET API and provide nice sample code and good analysis as usual.
Let’s create a Box solid into a family document. Here we go.
public static ElementId CreateBox(RvtDocument famDoc, XYZ bottomCenter, double width, double length, double height)
{
ElementId ret;
CurveArrArray curveArrArr = new CurveArrArray();
CurveArray curveArr1 = new CurveArray();
curveArrArr.Append(curveArr1);
Plane plane = new Plane(XYZ.BasisZ, bottomCenter);
Line line1 = famDoc.Application.Create.NewLineBound(
new XYZ(bottomCenter.X - length/2, bottomCenter.Y - width/2, bottomCenter.Z),
new XYZ(bottomCenter.X + length/2, bottomCenter.Y - width/2, bottomCenter.Z));
curveArr1.Append(line1);
Line line2 = famDoc.Application.Create.NewLineBound(
new XYZ(bottomCenter.X + length/2, bottomCenter.Y - width/2, bottomCenter.Z),
new XYZ(bottomCenter.X + length/2, bottomCenter.Y + width/2, bottomCenter.Z));
curveArr1.Append(line2);
Line line3 = famDoc.Application.Create.NewLineBound(
new XYZ(bottomCenter.X + length/2, bottomCenter.Y + width/2, bottomCenter.Z),
new XYZ(bottomCenter.X - length/2, bottomCenter.Y + width/2, bottomCenter.Z));
curveArr1.Append(line3);
Line line4 = famDoc.Application.Create.NewLineBound(
new XYZ(bottomCenter.X - length/2, bottomCenter.Y + width/2, bottomCenter.Z),
new XYZ(bottomCenter.X - length/2, bottomCenter.Y - width/2, bottomCenter.Z));
curveArr1.Append(line4);
Extrusion solid = famDoc.FamilyCreate.NewExtrusion(
true,
curveArrArr,
famDoc.FamilyCreate.NewSketchPlane(plane),
height);
ret = solid.Id;
return ret;
}
public static void TestCreateBox(UIApplication uiapp)
{
RvtDocument famDoc = GetOpenOrCreateFamilyTestDoc(uiapp);
using (Transaction tr = new Transaction(famDoc, "CreateBox"))
{
tr.Start();
CreateBox(famDoc, new XYZ(55, 0, 0), 2, 5, 10);
tr.Commit();
}
}
Here is how the newly created Box looks like in the Revit family document.
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.