If the action is simply to change the host of the pot to put the new walls. And it works great!!!
I don’t know if the code is correct because I don’t know Python.
import clr
clr.AddReference(“RevitServices”)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference(“RevitAPI”)
from Autodesk.Revit.DB import *
Récupérer le document actif
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
doc = uidoc.Document
Entrées Dynamo
doors_input = IN[0] # Liste des portes sélectionnées dans Dynamo
walls_input = IN[1] # Liste des murs sélectionnés dans Dynamo (ou None)
Déballer les éléments
doors = [UnwrapElement(door) for door in doors_input]
walls = [UnwrapElement(wall) for wall in walls_input] if walls_input else
FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()
Fonction pour trouver le mur le plus proche
def find_closest_wall(door, walls):
door_location = door.Location.Point
min_distance = float(“inf”)
closest_wall = None
for wall in walls:
wall_location = wall.Location.Curve
if wall_location is not None:
wall_point = wall_location.Evaluate(0.5, True) # Point central du mur
distance = door_location.DistanceTo(wall_point)
if distance < min_distance:
min_distance = distance
closest_wall = wall
return closest_wall
Fonction pour rehéberger la porte sur un nouveau mur
def rehost_door(door, new_wall):
try:
TransactionManager.Instance.EnsureInTransaction(doc)
# Récupérer la position et l'orientation
door_location = door.Location.Point
facing_orientation = door.FacingOrientation
hand_flipped = door.HandFlipped
facing_flipped = door.FacingFlipped
# Créer une nouvelle porte sur le nouveau mur
new_door = doc.Create.NewFamilyInstance(door_location, door.Symbol, new_wall, Structure.StructuralType.NonStructural)
# Appliquer les mêmes paramètres d'orientation
if new_door:
if hand_flipped:
new_door.flipHand()
if facing_flipped:
new_door.flipFacing()
# Supprimer l'ancienne porte
doc.Delete(door.Id)
TransactionManager.Instance.TransactionTaskDone()
return new_door # Retourner la nouvelle porte créée
except Exception as e:
return f"Erreur: {str(e)}"
Appliquer la modification à toutes les portes sélectionnées
new_doors =
errors =
for door in doors:
closest_wall = find_closest_wall(door, walls)
if closest_wall:
result = rehost_door(door, closest_wall)
if isinstance(result, str): # Si c’est une erreur, on l’ajoute à la liste d’erreurs
errors.append(result)
else:
new_doors.append(result)
else:
errors.append(f"Aucun mur trouvé pour la porte {door.Id.IntegerValue}.")
Sortie Dynamo
OUT = (new_doors, errors) # Retourne la liste des nouvelles portes + erreurs éventuelles