|
我想实现在Revit族中创建一个立方体,基于立方体的一个面创建风管连接件。插件代码有两个操作,第一个创建实例,第二个创建风管连接件。
族中有一个嵌套公制机械设备族,族名称“族1”,族类型名称“族1”,族类型的ID是3165。这个嵌套族中仅有一个拉伸立方体。
族文件截图(视图中的立方体,是为了看嵌套族里的内容,手工创建的,插件运行时没有这个立方体):
族截图
代码如下:
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
FamilySymbol symbol = (FamilySymbol)(doc.GetElement(new ElementId(3165)));
Transaction transaction = new Transaction(doc, "abc");
transaction.Start();
FamilyInstance instance = doc.FamilyCreate.NewFamilyInstance(new XYZ(), symbol, StructuralType.NonStructural);
transaction.Commit();
var planar = GetPlanars(instance);
if (planar != null)
{
transaction.Start();
ConnectorElement.CreateDuctConnector(doc, DuctSystemType.Global, ConnectorProfileType.Rectangular, planar.Reference);
transaction.Commit();
MessageBox.Show("Succeeded");
}
return Result.Succeeded;
}
private PlanarFace GetPlanars(FamilyInstance instance)
{
Options options = new Options
{
ComputeReferences = false,
DetailLevel = ViewDetailLevel.Fine
};
GeometryElement geometryobj = instance.get_Geometry(options);
if (geometryobj == null || geometryobj.Count() == 0)
{
MessageBox.Show("获取面失败");
return null;
}
IEnumerator<GeometryObject> enumerator = geometryobj.GetEnumerator();
while (enumerator.MoveNext())
{
Solid solid = enumerator.Current as Solid;
if (solid == null || solid.SurfaceArea == 0)
{
GeometryInstance geoElem = enumerator.Current as GeometryInstance;
if (geoElem != null)
{
GeometryElement enumerator2 = geoElem.GetInstanceGeometry();
IEnumerator<GeometryObject> enumerator3 = enumerator2.GetEnumerator();
while (enumerator3.MoveNext())
{
solid = enumerator3.Current as Solid;
if (solid != null && solid.SurfaceArea > 0)
{
foreach (Face face in solid.Faces)
{
if (face is PlanarFace)
{
return (PlanarFace)face;
}
}
}
}
}
}
}
return null;
}
}
运行时,创建实例没有问题,获取实例几何的面也没有问题。调试截图如下:
获取几何面
创建风管连接件时出错,提示平面引用有问题
调试平面引用出错
Revit错误提示截面
向各位专家请教,我该如何创建这个风管连接件?
|
|