1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Autodesk.Revit.UI;
7 using Autodesk.Revit.DB;
8 using Autodesk.Revit.Attributes;
9 using Autodesk.Revit.ApplicationServices;
10
11
12 namespace CreateFamily
13 {
14 [Transaction(TransactionMode.Manual)]
15 public class Class1:IExternalCommand
16 {
17 public Result Execute(ExternalCommandData commandData, ref string message, 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 = new Options();
44 opt.ComputeReferences = true;
45 opt.DetailLevel = ViewDetailLevel.Fine;
46 GeometryElement gelm = extrusion.get_Geometry(opt);
47 foreach (GeometryObject gobj in gelm)
48 {
49 if (gobj is Solid)
50 {
51 Solid s = gobj as Solid;
52 foreach (Face face in s.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 return Result.Succeeded;
81 }
82
83 private CurveArrArray 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 = new CurveArrArray();
97 CurveArray arry = new CurveArray();
98 arry.Append(l1);
99 arry.Append(l2);
100 arry.Append(l3);
101 arry.Append(l4);
102 ary.Append(arry);
103 return ary;
104 }
105
106 private SketchPlane GetSketchPlane(Document doc)
107 {
108 FilteredElementCollector temc = new FilteredElementCollector(doc);
109 temc.OfClass(typeof(SketchPlane));
110 SketchPlane sketchPlane = temc.First(m => m.Name == "低于参照标高") as SketchPlane;
111 return sketchPlane;
112 }
113
114 private View GetView(Document doc)
115 {
116 FilteredElementCollector viewFilter = new FilteredElementCollector(doc);
117 viewFilter.OfClass(typeof(View));
118 View v = viewFilter.First(m => m.Name == "前") as View;
119 return v;
120 }
121
122 private Reference GetTopLevel(Document doc)
123 {
124 FilteredElementCollector temc = new FilteredElementCollector(doc);
125 temc.OfClass(typeof(Level));
126 Level lvl = temc.First(m => m.Name == "高于参照标高") as Level;
127 return new Reference(lvl);
128 }
129 }
130 }