Hello,
am trying to create a tool to automatically add insulation on objects. the goal is to add insulation depending on the value of the systeme type.
so far i’ve written this:
"# coding: utf-8
import clr
from rpw import revit
from Autodesk.Revit.UI.Selection import ObjectType
from Autodesk.Revit.DB.Plumbing import PipeInsulation
from Autodesk.Revit.DB import *
from pyrevit import forms
from pyrevit import DB
doc = revit.doc
uidoc = revit.uidoc
Demander à l’utilisateur de sélectionner des éléments
try:
with forms.WarningBar(title=“Sélectionnez les éléments à isoler”):
references = uidoc.Selection.PickObjects(ObjectType.Element)
# Choisir dynamiquement le type d'isolation
insulation_types = DB.FilteredElementCollector(doc).OfClass(DB.PipeInsulationType).ToElements()
type_choices = {i.Name: i.Id for i in insulation_types if i is not None}
selected_type = forms.SelectFromList.show(
sorted(type_choices.keys()),
title="Choisissez un type d'isolant",
multiselect=False
)
if not selected_type:
raise Exception("Aucun type d'isolant sélectionné.")
insulation_type_id = type_choices[selected_type]
# Demander l'épaisseur
thickness_input = forms.ask_for_string(
default="0.03", # ≈ 9 mm
prompt="Entrez l'épaisseur (en pieds, ex : 0.03 pour 9 mm)",
title="Épaisseur de l'isolant"
)
if not thickness_input:
raise Exception("Épaisseur non définie.")
thickness = float(thickness_input)
# Démarrer la transaction
t = Transaction(doc, "Ajouter isolant aux éléments sélectionnés")
t.Start()
count = 0
for ref in references:
element = doc.GetElement(ref.ElementId)
if not element:
continue
# Vérifie s'il n'a pas déjà d'isolation
existing = PipeInsulation.GetInsulationIds(element)
if existing and len(existing) > 0:
continue
try:
PipeInsulation.Create(doc, element.Id, insulation_type_id, thickness)
count += 1
except Exception as e:
print("Erreur pour l'élément {} : {}".format(element.Id, e))
t.Commit()
forms.alert("{} isolants ajoutés avec succès.".format(count), title="Résultat", ok=True)
except Exception as e:
forms.alert(“Erreur : {}”.format(e), title=“Erreur”, ok=True)
"
i’ve loooked throught the documentation of Revit API but haven’t been able to find a way.
Am not yet very goodd at programing so im asking here if someone could give me pointers on why i can’t access the insulation in the Modele.
DB is a so called namespace. FilteredElementCollector comes directly from it.
PipeInsulationType isn’t included directly in the DB namespace, but another one, called Autodesk.Revit.DB.Plumbing, that’s why you have to import it explicitly.
That’s also what I thought… However, I also used much more AI at the beginning and gradually started understanding more and more on my own, to the point of being able to write correct code without it. I think there is value in using AI while learning, the key is to only use it as one of the learning resources and not rely too much on it
am trying to learn by understanding why the script doesn’t work. but i seem to be stopped at every function. As Jean Marc said chatGPT is not very good at it. even the paid version is good at messing up.