前言
Revit 有多种可以导入几何图形的方法,但是DirectShape绝对是其中特殊的一种。用它生成的几何图形,就像是Revit原生的一样。当然,为了达到“原生”的目的,在编程上你不可避免地也需要按照它给你定义的规则来进行编写。
下面,我会讲解几个例子。
用DirectShap创建一个长方体
创建流程:
- 创建底面的正方形
- 4个顶点,然后通过顶点创建4条线
- 拉伸出来一个长方体
- GeometryCreationUtilities.CreateExtrusionGeometry
注意事项:
- GeometryCreationUtilities.CreateExtrusionGeometry
- 顶点顺序要注意,要创建一个curveloop
- DirectShape 是一个类,可以设定不同的Category
- Transaction 一定要用,任何改变文档的操作都需要使用 Transaction
另外如果想创建更加复杂的图形,并且希望自己去控制各个细节,那这个例子肯定是不够的。需要使用 BRepBuilder
。
public void CreateCubicDirectShape(Document doc)
{
List<Curve> profile = new List<Curve>();
double edgeLength = 2.0;
XYZ p1 = new XYZ(edgeLength, 0, 0);
XYZ p2 = new XYZ(edgeLength, edgeLength, 0);
XYZ p3 = new XYZ(0, edgeLength, 0);
XYZ p4 = new XYZ(0, 0, 0);
profile.Add(Line.CreateBound(p1, p2));
profile.Add(Line.CreateBound(p2, p3));
profile.Add(Line.CreateBound(p3, p4));
profile.Add(Line.CreateBound(p4, p1));
CurveLoop curveLoop = CurveLoop.Create(profile);
SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId);
Solid cubic = GeometryCreationUtilities.CreateExtrusionGeometry(new CurveLoop[] { curveLoop }, XYZ.BasisZ, 10);
using (Transaction t = new Transaction(doc, "Create sphere direct shape"))
{
t.Start();
// create direct shape and assign the sphere shape
DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
ds.ApplicationId = "Application id";
ds.ApplicationDataId = "Geometry object id";
ds.SetShape(new GeometryObject[] { cubic });
t.Commit();
}
}
效果图: