Automatic pipe insulation

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)
"

But i keep having a mistake:

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.

Hello,

The error you’re getting is caused by 2 things:

  1. There is no need to write DB in this line:
insulation_types = DB.FilteredElementCollector(doc).OfClass(DB.PipeInsulationType).ToElements()

because you already imported everything from DB using *, in this line in your imports:

from Autodesk.Revit.DB import *

Corrected version:

insulation_types = FilteredElementCollector(doc).OfClass(PipeInsulationType).ToElements()

Also, because you already imported Autodesk.Revit.DB namespace, there is no need to import it again from pyrevit in this line:

from pyrevit import DB
  1. This line in your imports is incorrect:
from Autodesk.Revit.DB.Plumbing import PipeInsulation

Instead, try this:

from Autodesk.Revit.DB.Plumbing import PipeInsulationType

PipeInsulationType and PipeInsulation are two different classes:
PipeInsulationType - PipeInsulationType Class
PipeInsulation - PipeInsulation Class

In this line you are using PipeInsulationType:

insulation_types = DB.FilteredElementCollector(doc).OfClass(DB.PipeInsulationType).ToElements()

so it must be the same in the imports.

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.

I hope this helped and let me know if you have any questions!

Bad imports = code generated by AI

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

1 Like

Not always true, but ChatGPT free is good at messing up !

Thank you for the tips, it was really helpfull. As i said in the topic am only just learning now and there are many things i can’t make sense of yet.

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.

You may want to try Cursor as a code editor, it is better than github copilot in vscode.

Setup your autocomplete properly and take the time to read and understand each issue.

I suggest you take @ErikFrits courses https://learnrevitapi.com/ That will get you started much faster than on your own.