完整项目代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.ApplicationServices; //Application的引用
namespace FamilyAPI
{
[TransactionAttribute(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
string rftPath = @"C:\ProgramData\Autodesk\RVT 2018\Family Templates\Chinese\公制柱.rft";
UIApplication uiapp = commandData.Application;
Application app = uiapp.Application;
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Document faDoc = app.NewFamilyDocument(rftPath);//创建族文件
Transaction trans = new Transaction(faDoc, "创建族");//创建事务
trans.Start();
FamilyManager manager = faDoc.FamilyManager;
FamilyParameter mfp = manager.AddParameter("材质", BuiltInParameterGroup.PG_MATERIALS, ParameterType.Material, false);//创建材质
CurveArrArray arry = GetCurve();
SketchPlane skplane = GetSketchPlane(faDoc);
Extrusion extruction = faDoc.FamilyCreate.NewExtrusion(true, arry, skplane, 4000 / 304.8);
faDoc.Regenerate();
Reference topFaceRef = null;
Options opt = new Options();
opt.ComputeReferences = true;
opt.DetailLevel = ViewDetailLevel.Fine;
GeometryElement gelm = extruction.get_Geometry(opt);
foreach(GeometryObject gobj in gelm)
{
if(gobj is Solid)
{
Solid s = gobj as Solid;
foreach(Face face in s.Faces)
{
if (face.ComputeNormal(new UV()).IsAlmostEqualTo(new XYZ(0, 0, 1)))
{
topFaceRef = face.Reference;
}
}
}
}
View v = GetView(faDoc);
Reference r = GetTopLevel(faDoc);
Dimension d = faDoc.FamilyCreate.NewAlignment(v, r, topFaceRef);
d.IsLocked = true;
faDoc.Regenerate();
Parameter p = extruction.get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM);
manager.AssociateElementParameterToFamilyParameter(p, mfp);
trans.Commit();
Family fa = faDoc.LoadFamily(doc);
faDoc.Close(false);
trans = new Transaction(doc, "创建柱");
trans.Start();
fa.Name = "我的柱子";
trans.Commit();
return Result.Succeeded;
}
private Reference GetTopLevel(Document doc)
{
FilteredElementCollector temc = new FilteredElementCollector(doc);
temc.OfClass(typeof(Level));
Level lvl = temc.First(m => m.Name== "高于参照标高") as Level;
return new Reference(lvl);
}
private View GetView(Document doc)
{
FilteredElementCollector viewFilter = new FilteredElementCollector(doc);
viewFilter.OfClass(typeof(View));
View v = viewFilter.First(m => m.Name == "前") as View;
return v;
}
private SketchPlane GetSketchPlane(Document doc)
{
FilteredElementCollector temc = new FilteredElementCollector(doc);
temc.OfClass(typeof(SketchPlane));
SketchPlane sketchPlane = temc.First(m => m.Name == "低于参照标高") as SketchPlane;
return sketchPlane;
}
private CurveArrArray GetCurve()
{
double len = 300 / 304.8;//对英制单位进行转换
//创建4个点
XYZ p1 = new XYZ(-len, -len, 0);
XYZ p2 = new XYZ(len, -len, 0);
XYZ p3 = new XYZ(len, len, 0);
XYZ p4 = new XYZ(-len, len, 0);
//创建闭合曲线
Line l1 = Line.CreateBound(p1, p2);
Line l2 = Line.CreateBound(p2, p3);
Line l3 = Line.CreateBound(p3, p4);
Line l4 = Line.CreateBound(p4, p1);
CurveArrArray ary = new CurveArrArray();
CurveArray arry = new CurveArray();
arry.Append(l1);
arry.Append(l2);
arry.Append(l3);
arry.Append(l4);
ary.Append(arry);
return ary;
}
}
}
参考文献:
周婧祎《Autodesk Revit 2016二次开发入门教程》