This is the code which I am using, but it show the exception"NullReferenceException".
using (Transaction t = new Transaction(doc, "DrawBeam"))
{
t.Start();
FilteredElementCollector column = new FilteredElementCollector(doc);
column.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_StructuralColumns);
foreach (FamilyInstance c in column)
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralFraming);
FamilySymbol symbol = collector.FirstElement() as FamilySymbol;
// The only way to get a Face to use with this NewFamilyInstance overload
// is from Element.Geometry with ComputeReferences turned on
Face face = null;
Options geomOptions = new Options();
geomOptions.ComputeReferences = true;
GeometryElement wallGeom = c.get_Geometry(geomOptions);
foreach (GeometryObject geomObj in wallGeom)
{
Solid geomSolid = geomObj as Solid;
if (null != geomSolid)
{
foreach (Face geomFace in geomSolid.Faces)
{
face = geomFace;
break;
}
break;
}
}
// Get the center of the wall
//BoundingBoxUV bboxUV = face.GetBoundingBox();
BoundingBoxUV bboxUV=face.GetBoundingBox();
UV center = (bboxUV.Max + bboxUV.Min) / 2.0;
XYZ location = face.Evaluate(center);
XYZ normal = face.ComputeNormal(center);
XYZ refDir = normal.CrossProduct(XYZ.BasisZ);
FamilyInstance instance = doc.Create.NewFamilyInstance(face, location, refDir, symbol);
}
t.Commit();