Here is the code form AI below
-- coding: utf-8 --
“”"
Interactive Apply Lining to Selected Fabrication Ducts in Revit
Author: Your Name
Description:
Prompts the user to select a lining material and thickness,
then applies them to selected fabrication duct elements.
“”"
from Autodesk.Revit.DB import (
FilteredElementCollector,
Transaction,
BuiltInCategory
)
from Autodesk.Revit.DB.Fabrication import FabricationPart
from Autodesk.Revit.UI import TaskDialog
from Autodesk.Revit.Exceptions import InvalidOperationException
from pyrevit import revit, forms
doc = revit.doc
---------------- HELPER FUNCTIONS ----------------
def get_selected_fabrication_ducts():
“”“Return selected fabrication ducts, or prompt user if none selected.”“”
selection_ids = revit.get_selection_ids()
if not selection_ids:
forms.alert(“Please select one or more fabrication ducts.”, exitscript=True)
ducts =
for el_id in selection_ids:
el = doc.GetElement(el_id)
if isinstance(el, FabricationPart) and el.Category.Id.IntegerValue == int(BuiltInCategory.OST_FabricationDuctwork):
ducts.append(el)
if not ducts:
forms.alert(“No fabrication ducts found in selection.”, exitscript=True)
return ducts
def get_all_material_names():
“”“Return a sorted list of all material names in the project.”“”
mats = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Materials).ToElements()
return sorted([m.Name for m in mats])
def get_material_by_name(name):
“”“Find a material by name in the project.”“”
mats = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Materials).ToElements()
for mat in mats:
if mat.Name.lower() == name.lower():
return mat
return None
def apply_lining(ducts, material, thickness_mm):
“”“Apply lining material and thickness to ducts.”“”
with Transaction(doc, “Apply Lining to Fabrication Ducts”) as t:
t.Start()
for duct in ducts:
try:
mat_param = duct.LookupParameter(“Lining Material”)
thick_param = duct.LookupParameter(“Lining Thickness”)
if mat_param and material:
mat_param.Set(material.Id)
if thick_param:
Convert mm to feet (Revit internal units)
thickness_ft = thickness_mm / 304.8
thick_param.Set(thickness_ft)
except Exception as e:
print(“Error applying lining to {}: {}”.format(duct.Id, e))
t.Commit()
---------------- MAIN SCRIPT ----------------
try:
ducts = get_selected_fabrication_ducts()
# Prompt user for material
material_names = get_all_material_names()
chosen_material_name = forms.select_from_list(
material_names,
title="Select Lining Material",
multiselect=False
)
if not chosen_material_name:
forms.alert("No material selected. Script cancelled.", exitscript=True)
material = get_material_by_name(chosen_material_name)
if not material:
forms.alert("Material '{}' not found in project.".format(chosen_material_name), exitscript=True)
# Prompt user for thickness
thickness_mm = forms.ask_for_number(
prompt="Enter lining thickness (mm):",
default=25,
min=0
)
if thickness_mm is None:
forms.alert("No thickness entered. Script cancelled.", exitscript=True)
# Apply lining
apply_lining(ducts, material, thickness_mm)
TaskDialog.Show("pyRevit", "Lining applied successfully to {} ducts.".format(len(ducts)))
except InvalidOperationException:
forms.alert(“This command must be run in a valid Revit project view.”)