使用Revit API获取墙层材料厚度时数值不符的问题
问题原因与解决方案
你遇到的问题并非API获取了材料默认厚度,而是Revit API返回的长度值采用内部单位(英尺),而你在Revit中设置的是毫米单位,两者未做单位转换导致数值不符。
关键说明
Revit所有长度类API(包括GetLayerWidth)默认返回内部单位英尺,和项目中显示的毫米/厘米等单位不直接对应,必须通过单位转换才能得到与Revit界面一致的数值。
修改后的代码
以下是添加单位转换逻辑后的完整代码,将内部单位英尺转换为毫米:
# 导入必要的命名空间 from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, Wall, UnitUtils, UnitTypeId # Create the dict for JSON components = { "wall": [], } # Select all walls walls = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).ToElements() # Get the outer wall outer_wall = None for item in walls: if isinstance(item, Wall): el = doc.GetElement(item.Id) if el.Name == "Aussenwand": outer_wall = doc.GetElement(el.Id) # Get the material and width of each layer of outer wall and add to components if outer_wall is not None: wall_type = doc.GetElement(outer_wall.GetTypeId()) compound_structure = wall_type.GetCompoundStructure() for layer_index in range(compound_structure.LayerCount): # 获取内部单位(英尺)的厚度 internal_width = compound_structure.GetLayerWidth(layer_index) # 转换为毫米单位 layer_width = UnitUtils.ConvertFromInternalUnits(internal_width, UnitTypeId.Millimeters) # 保留两位小数优化显示 layer_width = round(layer_width, 2) material_id = compound_structure.GetMaterialId(layer_index) if material_id.IntegerValue != -1: material = doc.GetElement(material_id) components["wall"].append({material.MaterialCategory: layer_width})
结果验证
转换后你会得到与Revit界面一致的数值:
- 原返回的
0.049212598425196846英尺,转换为毫米后约等于15mm,和你设置的厚度匹配。 - 其他数值也会对应转换为你设置的毫米数值。
注意事项
- 若使用Revit 2020及更早版本,
UnitTypeId需替换为DisplayUnitType.DUT_MILLIMETERS,代码调整为:layer_width = UnitUtils.ConvertFromInternalUnits(internal_width, DisplayUnitType.DUT_MILLIMETERS) - 可根据需求调整小数保留位数,让结果更贴合Revit界面显示。
内容的提问来源于stack exchange,提问作者Behdad Hajipour
相关产品推荐
相关产品推荐

