I can't add the PDFs in each plan created with the script

I am creating a script that allows me to add a blueprint for each PDF in the specified path. However, I am not able to add the image of the PDF in the blueprint. Any problem with the ImageType instance and its arguments?

import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

# Importar DocumentManager y TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Obtener el documento actual
doc = DocumentManager.Instance.CurrentDBDocument

rutas = IN[0]
caracteres = IN[1]
nGuiones = IN[2]

TransactionManager.Instance.EnsureInTransaction(doc)

def create_plano(doc, nombre, r, i):
    # Obtener la ubicación del centro de la hoja
    center = XYZ(0, 0, 0)

    # Crear una nueva hoja de vista
    plano = ViewSheet.Create(doc, ElementId.InvalidElementId)
    plano.SheetNumber = nombre

    # Crear una nueva instancia de ImageTypeOptions para importar la imagen
    image_type_options = ImageTypeOptions(r, True, ImageTypeSource.Import)

    # Crear el ImageType en el documento activo
    image_type = ImageType.Create(doc, image_type_options)

    # Mover la imagen a la ubicación del centro de la hoja
    imageInstance = ImageInstance.Create(doc, plano.Id, center, image_type.Id)

for r in rutas:
    nombre = (r.rsplit("\\", 1))[1]
    nombre = nombre[:len(nombre) - (caracteres + 4)]

    if nombre.count("-") != nGuiones:
        nombre = nombre[:len(nombre) - 3]

    i=1
    
    for i in range(20):
        i +=1
        try:
            create_plano(doc, nombre, r, i)
        except:
            break

TransactionManager.Instance.TransactionTaskDone()

Hello @oneiber_v and welcome !

try this version


# Import necessary libraries
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Get the current document
doc = DocumentManager.Instance.CurrentDBDocument

# Input variables
rutas = IN[0]  # List of file paths (PDFs)
caracteres = IN[1]  # Number of characters to remove from the file name
nGuiones = IN[2]  # Number of hyphens to consider in the file name

# Start a transaction to make changes to the Revit document
TransactionManager.Instance.EnsureInTransaction(doc)

# Function to create a sheet based on a PDF
def create_sheet_by_pdf(doc, nombre, file_path):
    # Define a center point (XYZ)
    center = XYZ(0, 0, 0)
    
    # Create a new sheet in the document
    plano = ViewSheet.Create(doc, ElementId.InvalidElementId)
    plano.SheetNumber = nombre  # Set the sheet number
    
    # Define options for importing an image from a file
    image_type_options = ImageTypeOptions(file_path, True, ImageTypeSource.Import)
    
    # Create an image type using the specified options
    image_type = ImageType.Create(doc, image_type_options)
    
    # Define options for placing the image on the sheet
    imgPlacementOpt = ImagePlacementOptions(center, BoxPlacement.Center)
    
    # Create an image instance on the sheet using the image type and placement options
    imageInstance = ImageInstance.Create(doc, plano, image_type.Id, imgPlacementOpt)

# Loop through each PDF file path in the list
for pdf_path in rutas:
    # Extract the file name from the path
    nombre = (pdf_path.rsplit("\\", 1))[1]
    
    # Remove a specified number of characters and the file extension (".pdf")
    nombre = nombre[:len(nombre) - (caracteres + 4)]
    
    # Check if the file name contains the expected number of hyphens
    if nombre.count("-") != nGuiones:
        nombre = nombre[:len(nombre) - 3]  # Remove the last 3 characters
    
    # Call the create_sheet_by_pdf function to create a sheet for the PDF
    create_sheet_by_pdf(doc, nombre, pdf_path)

# Complete the transaction
TransactionManager.Instance.TransactionTaskDone()

Note,
Errors should never pass silently, so this syntax should be avoided

        try:
            create_plano(doc, nombre, r, i)
        except:
            break

Hello, thank you for replying. I have tried but I got the following error.

Advertencia
PythonEvaluator.Error en la operación
Evaluate.
Traceback (most recent call last):
File line 60, in
File “”, line 39, in
create_sheet_by_pdf
Exception: options.Path: unexpected
relative path or bad request to relativize a
path.
Parameter name: options

some problem with the path, it has only generated a plan for a single pdf of all specified in the path, also it does not add the image/pdf to the created plan. For some reason I always fail to insert the image/pdf to the plan.

Hi,
I’m not unable to reproduce this error without more information

try to use this method to check if the path is correct (before to call create_plano method)