Revit 将分割表面应用到项目墙体的代码执行异常问题咨询
问题核心原因
你的代码无执行效果是3个核心错误导致的:
- 类别过滤规则错误:
BuiltInCategory.OST_CurtainGridsWall对应幕墙网格类别,不是墙体类别,无法匹配到项目中的墙体元素 - 遍历类型不匹配:你将过滤结果强转为
Form类,Form是概念体量编辑环境下的专用形体类,项目中的墙体对应的类是Autodesk.Revit.DB.Wall - 操作接口调用错误:
doc.FamilyCreate仅在族编辑模式下可用,项目环境创建分割表面需要调用DividedSurface.Create方法
修正后可运行代码
public class Command : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; Autodesk.Revit.Creation.Application creApp = app.Application.Create; try { // 修正1:过滤类别改为墙体类别 FilteredElementCollector wallCollector = new FilteredElementCollector(doc); wallCollector.OfCategory(BuiltInCategory.OST_Walls).OfClass(typeof(Wall)); using (Transaction tx = new Transaction(doc)) { tx.Start("Create Divided Surface"); // 修正2:遍历对象改为Wall类 foreach (Wall wall in wallCollector) { Options options = creApp.NewGeometryOptions(); options.ComputeReferences = true; options.View = doc.ActiveView; GeometryElement element = wall.get_Geometry(options); foreach (GeometryObject geoObject in element) { Solid solid = geoObject as Solid; if (solid == null || solid.Faces.Size == 0) continue; foreach (Face face in solid.Faces) { if (face != null && face.Reference != null) { // 修正3:项目环境下调用分割表面创建方法 DividedSurface ds = DividedSurface.Create(doc, face.Reference); // 可自定义分割规则,示例设置U/V方向网格数 ds.USpacingRule.Number = 5; ds.VSpacingRule.Number = 5; } } } } tx.Commit(); } return Result.Succeeded; } catch (Exception ex) { message = ex.Message; return Result.Failed; } } }
额外注意事项
- 如果不需要对所有墙体面生成分割表面,可以加面的法向量判断,只处理外墙面/内墙面
- 代码默认适配Revit 2016+版本,如果你使用2013及更早版本,只需要保留原代码的几何遍历逻辑即可
内容的提问来源于stack exchange,提问作者All In
相关产品推荐
相关产品推荐

