pythonrevit-apirevitpythonshellpyrevit

Getting wrong width for thickness of element in Revit using Revit API


I created a plugin with PyRevit using Revit API to get the materials in a layer used in the model with their thickness, but the result coming from API is not the same result that I gave to materials as thickness.

For instance I tried this code:

# 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):
        layer_width = compound_structure.GetLayerWidth(layer_index)
        material_id = compound_structure.GetMaterialId(layer_index)
        if material_id.IntegerValue != -1:
            material = doc.GetElement(material_id)
            components["wall"].append({material.MaterialCategory: layer_width})

so here is for example the result of this code:

{
'wall':
 [{'Gipsputz': 0.049212598425196846},
 {'Mauerwerk': 0.5741469816272966},
 {'Isolierung': 0.45931758530183725},
 {'Gipsputz': 0.049212598425196846}]
}

But the thickness that I gave to this material in Revit is something else, for example the first element should have the thcikness of 15 mm This is the layers of wall

I thick that there is maybe some default thickness for the materials and Revit is getting them. Is there anyone who has any idea how to solve it?


Solution

  • UPDATE:

    After hours of conflict with this code, I realized there is a mistake from my side. The Revit API is giving me units in imperial system and I need to change them to metric.

    There are 2 solutions for that, I chose to do it using Revit API with UnitTypeId and UnitUtils:

    # 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):
            layer_width_feet = compound_structure.GetLayerWidth(layer_index)
            layer_width_mm = UnitUtils.ConvertFromInternalUnits(layer_width_feet, UnitTypeId.Millimeters)
            material_id = compound_structure.GetMaterialId(layer_index)
            material = doc.GetElement(material_id)
            components["wall"].append({material.MaterialCategory: round(layer_width_mm)})
    
    

    The other solution is to change it simply by code and not using Revit API. Fore example each 1 foot is 304,8 mm.