如何获取Revit活动文档中协调模型的文件名与路径?
获取Revit协调模型的名称与路径解决方案
问题根源
你之前的代码存在两个关键问题:
- 混淆了
DirectShape(实例)和DirectShapeType(类型):协调模型的实例是DirectShape,DirectShapeType只是其共享类型定义,直接遍历DirectShapeType无法获取实例的文件路径。 - 代码变量错误:循环中误用了未定义的
fileRef,应该用当前遍历的element进行类型转换。
正确实现代码
要获取协调模型的名称和文件路径,正确做法是直接收集DirectShape元素,并通过其ShapeRepresentationManager获取导入源信息:
FilteredElementCollector collector = new FilteredElementCollector(doc); // 筛选出属于协调模型类别的DirectShape实例 var coordinationModels = collector.OfClass(typeof(DirectShape)).ToElements() .Where(e => e.Category?.Id.IntegerValue == (int)BuiltInCategory.OST_Coordination_Model); foreach (var element in coordinationModels) { DirectShape ds = element as DirectShape; if (ds == null) continue; // 获取协调模型名称 string modelName = ds.Name; // 获取原始文件路径 ShapeRepresentationManager repManager = ds.GetShapeRepresentationManager(); ImportSource importSource = repManager.GetImportSource(); string filePath = importSource.GetFilePath(); // 输出结果示例 Console.WriteLine($"名称:{modelName}"); Console.WriteLine("路径:{filePath}"); }
关键说明
- 类别过滤:通过
OST_Coordination_Model筛选,确保只处理协调模型类型的DirectShape,排除其他用途的DirectShape元素。 ImportSource:这是存储协调模型导入元数据的核心对象,调用GetFilePath()可直接获取原始文件的完整路径。- 版本兼容:该方法适用于Revit 2019及以上版本,更早版本需确认
ShapeRepresentationManagerAPI是否可用。
内容的提问来源于stack exchange,提问作者Leandro Arns Gonzales
相关产品推荐
相关产品推荐

