Hello, I am trying to use my function code to create a stirrup in the column, but it gives me an error in the RebarBarType, it says that it does not recognize it, and my input is a string, which can recognize the number, and with this try to select the diameter of the bar
this is Code
import clr
import sys
import System
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS
clr.AddReference(“RevitServices”)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference(“RevitAPI”)
clr.AddReference(“RevitAPIUI”) # Agregando referencia adicional
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
clr.AddReference(‘System’)
from System.Collections.Generic import List
clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
Entradas desde Dynamo (con los datos esperados)
curves = UnwrapElement(IN[0]) # Lista de curvas para la barra de refuerzo
rebar_style_input = IN[1] # Estilo de la barra (Standard, StirrupTie, etc.)
rebar_type_input = IN[2] # Tipo de barra (nombre en string o RebarBarType)
start_hook_type = UnwrapElement(IN[3]) # Tipo de gancho inicial (RebarHookType)
end_hook_type = UnwrapElement(IN[4]) # Tipo de gancho final (RebarHookType)
start_hook_orientation = IN[5] # Orientación del gancho inicial
end_hook_orientation = IN[6] # Orientación del gancho final
host_element = UnwrapElement(IN[7]) # Elemento anfitrión (ej. una viga o columna)
vector = IN[8] # Dirección del refuerzo (XYZ)
Convertir el estilo de barra a un tipo válido
type_map = {
“Standard”: RebarStyle.Standard,
“StirrupTie”: RebarStyle.StirrupTie
}
rebar_style = type_map.get(rebar_style_input, RebarStyle.Standard) # Valor por defecto si no coincide
Función para mostrar todos los RebarBarType disponibles
def get_available_rebar_types():
collector = FilteredElementCollector(doc).OfClass(RebarBarType)
return [rbt.Name for rbt in collector]
Validación de la entrada ‘rebar_type_input’
def validate_rebar_type(rebar_type_input):
try:
if isinstance(rebar_type_input, str):
# Verificar si rebar_type_input es un string y filtrar los RebarBarType por nombre
collector = FilteredElementCollector(doc).OfClass(RebarBarType)
available_types = get_available_rebar_types()
# Filtrar por el número contenido en el nombre
import re
search_number = re.search(r'\d+', rebar_type_input)
if search_number:
search_number = search_number.group()
matching_types = [rbt for rbt in collector if search_number in rbt.Name]
else:
matching_types = []
if matching_types:
rebar_type = matching_types[0] # Tomar el primer tipo encontrado
return rebar_type, None
else:
return None, "Error: No se encontró un RebarBarType que contenga el número '{}' en su nombre. Tipos disponibles: {}".format(rebar_type_input, ', '.join(available_types))
else:
return None, "Error: El parámetro 'rebar_type_input' no es un string. Se recibió: {}. Se esperaba un string con el nombre del tipo de barra.".format(type(rebar_type_input))
except Exception as e:
return None, "Error al validar 'rebar_type_input': {}".format(str(e))
Validar ‘rebar_type_input’
rebar_type, error_message = validate_rebar_type(rebar_type_input)
Si hay un error en la validación, devolvemos el mensaje de error
if error_message:
OUT = error_message
else:
# Aquí puedes agregar el código para crear el Rebar con el tipo válido (cuando ‘rebar_type’ esté definido)
try:
# Iniciar transacción
TransactionManager.Instance.EnsureInTransaction(doc)
# Crear la barra de refuerzo
new_rebar = None
try:
new_rebar = Rebar.CreateFromCurves(
doc,
rebar_style,
rebar_type,
start_hook_type,
end_hook_type,
host_element,
vector,
curves,
start_hook_orientation,
end_hook_orientation,
True, # Usar forma existente si es posible
True # Crear nueva forma si no existe
)
except Exception as create_error:
import traceback
error_line = traceback.format_exc()
raise Exception("Error en Rebar.CreateFromCurves en la línea: {}".format(error_line))
# Confirmar la transacción
TransactionManager.Instance.TransactionTaskDone()
OUT = new_rebar
except Exception as e:
TransactionManager.Instance.ForceCloseTransaction()
import traceback
error_line = traceback.format_exc()
OUT = "Error en la ejecución del script en la línea: {}".format(error_line)