Some templates such as the 'Construction Template' do not have any BuildingPadType elements within them. Therefore you need to see if you find any and then use BuildingPadType.CreateDefault if you don't.
Note that as previously noted you'll need a topo surface under the pad or you'll get InvalidOperationException upon trying to create the BuildingPad instance.
public Result Obj_210814a(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
UIDocument UIDoc = commandData.Application.ActiveUIDocument;
if (UIDoc == null)
return Result.Cancelled;
Document IntDoc = UIDoc.Document;
FilteredElementCollector FEC = new FilteredElementCollector(IntDoc);
ElementCategoryFilter ECF = new ElementCategoryFilter(BuiltInCategory.OST_BuildingPad);
List<ElementId> Eids = FEC.WherePasses(ECF).WhereElementIsElementType().ToElementIds() as List<ElementId>;
BuildingPadType BPType = null;
if (Eids.Count == 0)
{
using (Transaction Tx = new Transaction(IntDoc, "Create default pad type"))
{
if (Tx.Start() == TransactionStatus.Started)
{
BPType = BuildingPadType.CreateDefault(IntDoc);
Tx.Commit();
}
}
}
else
{
BPType = IntDoc.GetElement(Eids[0]) as BuildingPadType;
}
FilteredElementCollector FEC_L = new FilteredElementCollector(IntDoc);
ElementClassFilter ECF_L = new ElementClassFilter(typeof(Level));
List<Level> Lvs = FEC_L.WherePasses(ECF_L).ToElements().Cast<Level>().ToList();
Level Lv = Lvs.Find(L => L.Elevation == Lvs.Min(L0 => L0.Elevation));
using (Transaction Tx = new Transaction(IntDoc, "Create pad imstance"))
{
if (Tx.Start() == TransactionStatus.Started)
{
double Val = 3000 / 304.8;
XYZ C0 = new XYZ(-Val / 2, Val / 2, 0);
XYZ C1 = new XYZ(-Val / 2, -Val / 2, 0);
XYZ C2 = new XYZ(Val / 2, -Val / 2, 0);
XYZ C3 = new XYZ(Val / 2, Val / 2, 0);
CurveLoop CL = new CurveLoop();
CL.Append(Line.CreateBound(C0, C1));
CL.Append(Line.CreateBound(C1, C2));
CL.Append(Line.CreateBound(C2, C3));
CL.Append(Line.CreateBound(C3, C0));
BuildingPad.Create(IntDoc, BPType.Id, Lv.Id, new CurveLoop[] { CL }.ToList());
Tx.Commit();
}
}
return Result.Succeeded;
}