1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingAutodesk.Revit.UI;7 usingAutodesk.Revit.DB;8 usingAutodesk.Revit.Attributes;9 usingAutodesk.Revit.ApplicationServices;10
11
12 namespaceCreateFamily13 {14 [Transaction(TransactionMode.Manual)]15 public classClass1:IExternalCommand16 {17 public Result Execute(ExternalCommandData commandData, ref stringmessage, ElementSet elements)18 {19 string rftPath = @"C:\ProgramData\Autodesk\RVT 2016\Family Templates\Chinese\公制柱.rft";20 UIApplication uiapp =commandData.Application;21 Application app =uiapp.Application;22 UIDocument uidoc =commandData.Application.ActiveUIDocument;23 Document doc =uidoc.Document;24
25
26 //创建族文件
27 Document faDoc =app.NewFamilyDocument(rftPath);28
29 Transaction trans = new Transaction(faDoc, "Create Family");30 trans.Start();31 FamilyManager manager =faDoc.FamilyManager;32 //添加材质参数
33 FamilyParameter mfp = manager.AddParameter("材质", BuiltInParameterGroup.PG_MATERIALS, ParameterType.Material, false);34
35 //创建拉伸
36 CurveArrArray arry =GetCurves();37 SketchPlane skplane =GetSketchPlane(faDoc);38 Extrusion extrusion = faDoc.FamilyCreate.NewExtrusion(true, arry, skplane, 4000 / 304.8);39 faDoc.Regenerate();40
41 //创建约束
42 Reference topFaceRef = null;43 Options opt = newOptions();44 opt.ComputeReferences = true;45 opt.DetailLevel =ViewDetailLevel.Fine;46 GeometryElement gelm =extrusion.get_Geometry(opt);47 foreach (GeometryObject gobj ingelm)48 {49 if (gobj isSolid)50 {51 Solid s = gobj asSolid;52 foreach (Face face ins.Faces)53 {54 if (face.ComputeNormal(new UV()).IsAlmostEqualTo(new XYZ(0, 0, 1)))55 {56 topFaceRef =face.Reference;57 }58 }59 }60 }61 View v =GetView(faDoc);62 Reference r =GetTopLevel(faDoc);63 Dimension d =faDoc.FamilyCreate.NewAlignment(v, r, topFaceRef);64 d.IsLocked = true;65 faDoc.Regenerate();66
67 //关联材质参数
68 Parameter p =extrusion.get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM);69 manager.AssociateElementParameterToFamilyParameter(p, mfp);70
71
72 trans.Commit();73
74 Family fa =faDoc.LoadFamily(doc);75 faDoc.Close(false);76 trans = new Transaction(doc, "CreateColumn");77 trans.Start();78 fa.Name = "我的柱";79 trans.Commit();80 returnResult.Succeeded;81 }82
83 privateCurveArrArray GetCurves()84 {85 double len = 300 / 304.8;86
87 XYZ p1 = new XYZ(-len, -len, 0);88 XYZ p2 = new XYZ(len, -len, 0);89 XYZ p3 = new XYZ(len, len, 0);90 XYZ p4 = new XYZ(-len, len, 0);91
92 Line l1 =Line.CreateBound(p1, p2);93 Line l2 =Line.CreateBound(p2, p3);94 Line l3 =Line.CreateBound(p3, p4);95 Line l4 =Line.CreateBound(p4, p1);96 CurveArrArray ary = newCurveArrArray();97 CurveArray arry = newCurveArray();98 arry.Append(l1);99 arry.Append(l2);100 arry.Append(l3);101 arry.Append(l4);102 ary.Append(arry);103 returnary;104 }105
106 privateSketchPlane GetSketchPlane(Document doc)107 {108 FilteredElementCollector temc = newFilteredElementCollector(doc);109 temc.OfClass(typeof(SketchPlane));110 SketchPlane sketchPlane = temc.First(m => m.Name == "低于参照标高") asSketchPlane;111 returnsketchPlane;112 }113
114 privateView GetView(Document doc)115 {116 FilteredElementCollector viewFilter = newFilteredElementCollector(doc);117 viewFilter.OfClass(typeof(View));118 View v = viewFilter.First(m => m.Name == "前") asView;119 returnv;120 }121
122 privateReference GetTopLevel(Document doc)123 {124 FilteredElementCollector temc = newFilteredElementCollector(doc);125 temc.OfClass(typeof(Level));126 Level lvl = temc.First(m => m.Name == "高于参照标高") asLevel;127 return newReference(lvl);128 }129 }130 }